2018-01-25 05:55:12 +08:00
|
|
|
<!DOCTYPE HTML>
|
2020-09-23 16:23:38 +08:00
|
|
|
<html lang="en">
|
2018-01-25 05:55:12 +08:00
|
|
|
<head>
|
2020-09-23 16:23:38 +08:00
|
|
|
<meta charset="utf-8">
|
2018-01-25 05:55:12 +08:00
|
|
|
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
body {
|
|
|
|
font: 10.5pt arial;
|
|
|
|
color: #4d4d4d;
|
|
|
|
line-height: 150%;
|
|
|
|
width: 500px;
|
|
|
|
padding-left: 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
code {
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
|
|
|
|
code.multiline {
|
|
|
|
display: block;
|
|
|
|
white-space: pre-wrap
|
|
|
|
}
|
|
|
|
|
|
|
|
#jsoneditor {
|
|
|
|
width: 500px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<p>
|
2018-04-02 06:40:10 +08:00
|
|
|
Selection indication was done using the <code>on[Text]SelectionChange</code> listeners.<br/>
|
2018-01-25 05:55:12 +08:00
|
|
|
you can try the following calls in the console of your browser:<br/>
|
|
|
|
<code class="multiline">
|
|
|
|
// text and code modes:
|
|
|
|
editor.getTextSelection()
|
2018-04-02 06:40:10 +08:00
|
|
|
editor.setTextSelection(startPos, endPos)
|
2018-01-25 05:55:12 +08:00
|
|
|
// tree mode:
|
2018-03-02 04:49:08 +08:00
|
|
|
editor.getSelection()
|
2018-04-02 06:40:10 +08:00
|
|
|
editor.setSelection(startNode, endNode)
|
2018-01-25 05:55:12 +08:00
|
|
|
</code>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<form>
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
<div id="textModeSelection" style="display:none;">
|
|
|
|
<b>Selection:</b><div id="textRange"></div>
|
|
|
|
<b>Text:</b><div id="selectedText"></div>
|
|
|
|
</div>
|
|
|
|
<div id="treeModeSelection">
|
2018-04-02 06:40:10 +08:00
|
|
|
<b>Selection:</b>
|
2018-01-25 05:55:12 +08:00
|
|
|
<div id="selectedNodes"></div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<script>
|
2019-08-29 21:45:32 +08:00
|
|
|
const container = document.getElementById('jsoneditor')
|
2018-01-25 05:55:12 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
const options = {
|
2018-01-25 05:55:12 +08:00
|
|
|
mode: 'tree',
|
2019-07-09 02:00:34 +08:00
|
|
|
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
|
2018-01-25 05:55:12 +08:00
|
|
|
onError: function (err) {
|
2019-08-29 21:45:32 +08:00
|
|
|
alert(err.toString())
|
2018-01-25 05:55:12 +08:00
|
|
|
},
|
|
|
|
onChange: function () {
|
2019-08-29 21:45:32 +08:00
|
|
|
console.log('change')
|
2018-01-25 05:55:12 +08:00
|
|
|
},
|
|
|
|
onModeChange: function (mode) {
|
2019-08-29 21:45:32 +08:00
|
|
|
const treeMode = document.getElementById('treeModeSelection')
|
|
|
|
const textMode = document.getElementById('textModeSelection')
|
2018-01-25 05:55:12 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
treeMode.style.display = textMode.style.display = 'none'
|
2018-01-25 05:55:12 +08:00
|
|
|
|
|
|
|
if (mode === 'code' || mode === 'text') {
|
2019-08-29 21:45:32 +08:00
|
|
|
textMode.style.display = 'inline'
|
2018-01-25 05:55:12 +08:00
|
|
|
} else {
|
2019-08-29 21:45:32 +08:00
|
|
|
treeMode.style.display = 'inline'
|
2018-01-25 05:55:12 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
indentation: 4,
|
|
|
|
escapeUnicode: true,
|
2018-04-27 03:19:35 +08:00
|
|
|
onTextSelectionChange: function(start, end, text) {
|
2019-08-29 21:45:32 +08:00
|
|
|
const rangeEl = document.getElementById('textRange')
|
|
|
|
rangeEl.innerHTML = 'start: ' + JSON.stringify(start) + ', end: ' + JSON.stringify(end)
|
|
|
|
const textEl = document.getElementById('selectedText')
|
|
|
|
textEl.innerHTML = text
|
2018-01-25 05:55:12 +08:00
|
|
|
},
|
2018-04-02 06:40:10 +08:00
|
|
|
onSelectionChange: function(start, end) {
|
2019-08-29 21:45:32 +08:00
|
|
|
const nodesEl = document.getElementById('selectedNodes')
|
|
|
|
nodesEl.innerHTML = ''
|
2018-04-02 06:40:10 +08:00
|
|
|
if (start) {
|
2019-08-29 21:45:32 +08:00
|
|
|
nodesEl.innerHTML = ('start: ' + JSON.stringify(start))
|
2018-04-02 06:40:10 +08:00
|
|
|
if (end) {
|
2019-08-29 21:45:32 +08:00
|
|
|
nodesEl.innerHTML += ('<br/>end: ' + JSON.stringify(end))
|
2018-04-02 06:40:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 05:55:12 +08:00
|
|
|
}
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
2018-01-25 05:55:12 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
const json = {
|
2018-01-25 05:55:12 +08:00
|
|
|
"array": [1, 2, [3,4,5]],
|
|
|
|
"boolean": true,
|
|
|
|
"htmlcode": '"',
|
|
|
|
"escaped_unicode": '\\u20b9',
|
|
|
|
"unicode": '\u20b9,\uD83D\uDCA9',
|
|
|
|
"return": '\n',
|
|
|
|
"null": null,
|
|
|
|
"number": 123,
|
|
|
|
"object": {"a": "b", "c": "d"},
|
|
|
|
"string": "Hello World",
|
|
|
|
"url": "http://jsoneditoronline.org"
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
2018-01-25 05:55:12 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
window.editor = new JSONEditor(container, options, json)
|
2018-01-25 05:55:12 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
console.log('json', json)
|
|
|
|
console.log('string', JSON.stringify(json))
|
2018-01-25 05:55:12 +08:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|