UNPKG

jsoneditor

Version:

A web-based tool to view, edit, format, and validate JSON

122 lines (107 loc) 3.11 kB
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"> <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/> 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) // tree mode: editor.getSelection() editor.setSelection(startNode, endNode) </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> <div id="selectedNodes"></div> </div> </form> <script> const container = document.getElementById('jsoneditor') const options = { mode: 'tree', modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes onChange: function () { console.log('change') }, onModeChange: function (mode) { const treeMode = document.getElementById('treeModeSelection') const textMode = document.getElementById('textModeSelection') treeMode.style.display = textMode.style.display = 'none' if (mode === 'code' || mode === 'text') { textMode.style.display = 'inline' } else { treeMode.style.display = 'inline' } }, 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 }, 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)) } } } } const json = { "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" } window.editor = new JSONEditor(container, options, json) console.log('json', json) console.log('string', JSON.stringify(json)) </script> </body> </html>