-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
72 lines (66 loc) · 1.67 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { JSONEditor } from '/lib/index.js'
let content = {
text: undefined,
json: {
array: [1, 2, 3],
boolean: true,
color: '#82b92c',
null: null,
number: 123,
object: { a: 'b', c: 'd' },
time: 1575599819000,
string: 'Foo Bar',
json: "{\n a: 1,\n b: 2,\n c: 3\n}"
},
}
const editorLeft = new JSONEditor({
target: document.getElementById('left'),
props: {
content,
mode: 'text',
askToFormat: false,
onChange: (updatedContent) => {
editorRight.set(updatedContent)
},
},
})
const editorRight = new JSONEditor({
target: document.getElementById('right'),
props: {
content,
mode: 'tree',
navigationBar: false,
onChange: (updatedContent) => {
editorLeft.set(updatedContent)
},
},
})
// Avoid flicker
document.getElementById('switch').style.display = 'inline-block'
// Get toggle element and add click event
const $mode = document.getElementById('mode')
$mode.onclick = (event) => {
const mode = event.target.checked ? 'light' : 'dark'
toggleMode(mode)
localStorage.setItem('mode', mode);
};
// Handle local storage
const currentMode = localStorage.getItem('mode') || 'dark'
if (currentMode === 'dark') {
if ($mode.checked) {
$mode.checked = false
toggleMode(mode)
}
} else {
$mode.checked = true
toggleMode(mode)
}
function toggleMode(mode) {
if (mode === 'dark') {
if (!document.className) {
document.body.classList.add('jse-theme-dark')
}
} else {
document.body.classList.remove('jse-theme-dark')
}
}