jsoneditor/examples/15_selection_api.html

125 lines
3.1 KiB
HTML
Raw Permalink Normal View History

2018-01-25 05:55:12 +08:00
<!DOCTYPE HTML>
<html lang="en">
2018-01-25 05:55:12 +08:00
<head>
<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>
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()
editor.setTextSelection(startPos, endPos)
2018-01-25 05:55:12 +08:00
// tree mode:
editor.getSelection()
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">
<b>Selection:</b>
2018-01-25 05:55:12 +08:00
<div id="selectedNodes"></div>
</div>
</form>
<script>
const container = document.getElementById('jsoneditor')
2018-01-25 05:55:12 +08:00
const options = {
2018-01-25 05:55:12 +08:00
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
2018-01-25 05:55:12 +08:00
onError: function (err) {
alert(err.toString())
2018-01-25 05:55:12 +08:00
},
onChange: function () {
console.log('change')
2018-01-25 05:55:12 +08:00
},
onModeChange: function (mode) {
const treeMode = document.getElementById('treeModeSelection')
const textMode = document.getElementById('textModeSelection')
2018-01-25 05:55:12 +08:00
treeMode.style.display = textMode.style.display = 'none'
2018-01-25 05:55:12 +08:00
if (mode === 'code' || mode === 'text') {
textMode.style.display = 'inline'
2018-01-25 05:55:12 +08:00
} else {
treeMode.style.display = 'inline'
2018-01-25 05:55:12 +08:00
}
},
indentation: 4,
escapeUnicode: true,
onTextSelectionChange: function(start, end, text) {
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
},
onSelectionChange: function(start, end) {
const nodesEl = document.getElementById('selectedNodes')
nodesEl.innerHTML = ''
if (start) {
nodesEl.innerHTML = ('start: ' + JSON.stringify(start))
if (end) {
nodesEl.innerHTML += ('<br/>end: ' + JSON.stringify(end))
}
}
2018-01-25 05:55:12 +08:00
}
}
2018-01-25 05:55:12 +08:00
const json = {
2018-01-25 05:55:12 +08:00
"array": [1, 2, [3,4,5]],
"boolean": true,
"htmlcode": '&quot;',
"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"
}
2018-01-25 05:55:12 +08:00
window.editor = new JSONEditor(container, options, json)
2018-01-25 05:55:12 +08:00
console.log('json', json)
console.log('string', JSON.stringify(json))
2018-01-25 05:55:12 +08:00
</script>
</body>
</html>