-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
161 lines (145 loc) · 4.81 KB
/
index.html
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!doctype html>
<head>
<meta charset="utf-8" />
<title>JSON Schema Builder</title>
<link rel="stylesheet" href="style.css" />
<!--link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/styles/gruvbox-dark.min.css"-->
<!--link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/styles/gruvbox-light.min.css"-->
<!--link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/solarized-light.min.css"-->
<!--script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js"></script-->
<style>
.deletable .delete-me {
visibility: hidden;
}
.deletable:hover .delete-me {
visibility: visible;
cursor: pointer;
}
</style>
</head>
<body>
<script src="elm.js"></script>
<script>
let initValue = null;
try {
initValue = localStorage.editMe ? JSON.parse(localStorage.editMe) : {}
} catch(e) {
initValue = {};
}
const elm = Elm.Main.fullscreen(initValue);
elm.ports.select.subscribe(id => {
setTimeout(() => {
// console.log('attempt to select', id);
const x = document.getElementById(id);
// console.log(x);
if (x) {
x.select();
}
}, 100);
});
elm.ports.download.subscribe(x => {
const a = document.createElement('a');
const blob = new Blob([JSON.stringify(x, null, ' ')], { type: 'application/json' });
const url = URL.createObjectURL(blob);
a.setAttribute('href', url);
a.download = 'JSON Schema';
a.click();
});
// this is hack to update url and highlight menu
// TODO: discover the right way of doing this
setTimeout(() => {
document.body.addEventListener('dragstart', () => {
console.log('drag started');
});
document.body.addEventListener('dragover', e => {
const b = e.dataTransfer.effectAllowed;
e.dataTransfer.dropEffect = ('move' === b || 'linkMove' === b ||
'copyMove' === b) ? 'move' : 'copy';
// console.log('drag over');
return cancel(e);
});
document.body.addEventListener('dragenter', e => {
elm.ports.dragOver.send(true);
return cancel(e);
});
document.body.addEventListener('dragleave', e => {
elm.ports.dragOver.send(false);
return cancel(e);
});
document.body.addEventListener('drop', drop);
document.body.addEventListener('dragdrop', drop);
function drop(e) {
cancel(e);
if (e.dataTransfer.types.includes('Files')) {
const files = e.dataTransfer.files;
elm.ports.dragOver.send(false);
[].forEach.call(files, file => readContents(file));
} else if (e.dataTransfer.types.includes('text/uri-list')) {
elm.ports.dragOver.send(false);
fetch(e.dataTransfer.getData('text/uri-list'))
.then(x => x.text())
.then(load);
}
}
function readContents(file) {
const reader = new FileReader();
reader.readAsText(file);
reader.addEventListener('loadend', () => {
try {
load(reader.result);
} catch(e) {
console.error(e);
}
});
}
function load(text) {
const data = JSON.parse(text);
localStorage.editMe = text;
elm.ports.editSchema.send(data);
}
const content = document.querySelector('#content');
if (content) {
const headers = content.querySelectorAll('div[tabindex] > h1');
// TODO: recalc on window resize
const checkpoints = Array.from(headers).map(el => ({
hash: '#' + el.parentNode.id,
offsetTop: offsetTop(el, content) - 100,
})).reverse();
let timeout = null;
let hash = location.hash;
if (hash && hash.length > 1) {
const el = document.querySelector('#' + CSS.escape(hash.substr(1)));
if (el) {
el.focus();
}
}
content.addEventListener('scroll', ({ target }) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
const found = checkpoints.filter(x => x.offsetTop < target.scrollTop )[0];
if (found) {
if (hash !== found.hash) {
hash = found.hash
// elm.ports.activeSection.send(found.hash);
// history.replaceState({}, "", found.hash);
}
}
}, 200);
});
}
function offsetTop(el, limit) {
if (el === limit) {
return 0;
} else {
return el.offsetTop + offsetTop(el.parentNode, limit);
}
}
}, 100);
function cancel(e) {
if (e.preventDefault) {
e.preventDefault();
}
return false;
}
</script>
</body>