jsoneditor/public/index.html

191 lines
4.6 KiB
HTML
Raw Normal View History

2020-04-26 04:32:20 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<title>JSON Editor (Svelte)</title>
2020-05-07 02:11:59 +08:00
<link rel='icon' type='image/png' href='favicon.png'>
2020-04-26 04:32:20 +08:00
2020-05-07 02:11:59 +08:00
<style>
html, body {
position: relative;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
color: #333;
margin: 0;
padding: 8px;
box-sizing: border-box;
}
h1 {
color: purple;
}
#testEditorContainer {
2020-05-07 02:11:59 +08:00
width: 800px;
height: 500px;
2020-05-22 20:13:51 +08:00
max-width: 100%;
2020-05-07 02:11:59 +08:00
}
</style>
2020-04-26 04:32:20 +08:00
</head>
<body>
<div id="testEditorContainer"></div>
2020-05-07 02:11:59 +08:00
<p>
<button id="loadLargeJson">load large json</button>
<button id="clearJson">clear json</button>
2020-05-22 16:53:15 +08:00
<button id="patchJson">patch json</button>
2020-05-07 02:11:59 +08:00
<input id="loadFile" type="file">
</p>
2020-06-03 03:53:36 +08:00
<p>
<button id="expandAll">expand all</button>
<button id="expand2">expand 2 levels</button>
2020-06-03 03:53:36 +08:00
<button id="collapseAll">collapse all</button>
</p>
2020-05-07 02:11:59 +08:00
<script type="module">
2020-08-16 17:44:28 +08:00
import jsoneditor, { TreeMode } from './dist/es/jsoneditor.js'
2020-05-07 02:11:59 +08:00
2020-06-03 00:02:48 +08:00
const doc = {
2020-05-07 02:11:59 +08:00
'array': [1, 2, 3, {
name: 'Item ' + 2,
id: String(2),
index: 2,
time: new Date().toISOString(),
location: {
latitude: 1.23,
longitude: 23.44,
coordinates: [23.44, 1.23]
}
}],
2020-07-08 16:04:09 +08:00
'emptyArray': [],
2020-05-07 02:11:59 +08:00
'boolean': true,
'color': '#82b92c',
'null': null,
'number': 123,
'object': {
'a': 'b', 'c': 'd', nested: {
name: 'Item ' + 2,
id: String(2),
index: 2,
time: new Date().toISOString(),
location: {
latitude: 1.23,
longitude: 23.44,
coordinates: [23.44, 1.23]
}
}
},
2020-07-08 16:04:09 +08:00
'emptyObject': {},
2020-05-07 02:11:59 +08:00
'': '',
'string': 'Hello World',
'url': 'https://jsoneditoronline.org',
'Lorem Ipsum': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
}
const testEditor = jsoneditor({
target: document.getElementById('testEditorContainer'),
2020-08-16 17:44:28 +08:00
mode: TreeMode,
doc,
onChangeJson: doc => console.log('onChangeJson', doc),
validate: doc => {
if (
doc && typeof doc === 'object' &&
doc.object && typeof doc.object === 'object' &&
doc.object.a === 'b') {
return [
{
path: ['object', 'a'],
message: '"a" should not be "b" ;)'
}
]
}
return []
2020-05-07 02:11:59 +08:00
}
})
window.testEditor = testEditor // expose to window for debugging
2020-05-07 02:11:59 +08:00
document.getElementById('loadLargeJson').onclick = function handleLoadLargeJson() {
const count = 500
console.log('create large json', {count})
console.time('create large json')
const largeJson = {}
largeJson.numbers = []
largeJson.array = []
for (let i = 0; i < count; i++) {
const longitude = 4 + i / count
const latitude = 51 + i / count
largeJson.numbers.push(i)
largeJson.array.push({
name: 'Item ' + i,
id: String(i),
index: i,
time: new Date().toISOString(),
location: {
latitude,
longitude,
coordinates: [longitude, latitude]
},
random: Math.random()
})
}
console.timeEnd('create large json')
// const stringifiedSize = JSON.stringify(largeJson).length
// console.log(`large json stringified size: ${filesize(stringifiedSize)}`)
testEditor.set(largeJson)
}
document.getElementById('clearJson').onclick = function handleClearJson() {
testEditor.set({})
}
2020-05-22 16:53:15 +08:00
document.getElementById('patchJson').onclick = function handleClearJson() {
const operations = [{
op: 'replace',
path: '/object/c',
value: 'd2'
}]
testEditor.patch(operations)
}
2020-05-07 02:11:59 +08:00
document.getElementById('loadFile').onchange = function loadFile(event) {
console.log('loadFile', event.target.files)
const reader = new FileReader()
const file = event.target.files[0]
reader.onload = function (event) {
const text = event.target.result
const json = JSON.parse(text)
testEditor.set(json)
}
reader.readAsText(file)
}
2020-06-03 03:53:36 +08:00
document.getElementById('expandAll').onclick = function expandAll () {
testEditor.expand(() => true)
}
document.getElementById('expand2').onclick = function expandAll () {
testEditor.expand(path => path.length < 2)
2020-06-03 03:53:36 +08:00
}
document.getElementById('collapseAll').onclick = function collapseAll () {
testEditor.collapse(() => false)
2020-06-03 03:53:36 +08:00
}
2020-05-07 02:11:59 +08:00
</script>
2020-04-26 04:32:20 +08:00
</body>
</html>