-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
335 lines (309 loc) · 9.18 KB
/
editor.html
File metadata and controls
335 lines (309 loc) · 9.18 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<html>
<head>
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
/>
<style>
* {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;
font-size: 14px;
}
main {
display: flex;
width: 100%;
height: 100vh;
}
main > * {
width: 50%;
min-height: 100%;
}
textarea {
padding: 0.5em;
font-size: 14px;
background: #272822;
color: #fff;
}
iframe {
position: relative;
min-height: auto;
box-shadow: 0px 0px 10px 5px #000;
}
output {
position: fixed;
max-height: 25vh;
overflow: auto;
z-index: 9;
bottom: 0;
left: 0;
right: 0;
background-color: #fff;
color: #f00;
padding: 0 1em;
}
output:not(:empty) {
padding-top: 0.25em;
padding-bottom: 0.25em;
}
@media screen and (max-width: 768px) {
* {
font-size: 12.5px;
letter-spacing: -0.5px;
}
}
@media screen and (orientation: portrait) {
main {
flex-direction: column;
height: auto;
}
main > * {
width: 100%;
min-height: 70vh;
}
iframe {
height: 100vh;
}
}
@media screen and (pointer: fine) and (max-resolution: 1dppx) {
::-webkit-scrollbar {
max-width: 8px;
max-height: 8px;
}
::-webkit-scrollbar-corner {
background: transparent;
}
::-webkit-scrollbar-thumb {
border: 1px solid transparent;
border-radius: 9px;
background: rgba(128, 128, 128, 0.5);
background-clip: content-box;
}
}
</style>
<script>
$ = function () {
return document.querySelector(...arguments)
}
</script>
</head>
<body>
<main>
<textarea></textarea>
<iframe></iframe>
<template id="srcdoc">
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
/>
<style>
@media screen and (pointer: fine) and (max-resolution: 1dppx) {
::-webkit-scrollbar {
max-width: 8px;
max-height: 8px;
}
::-webkit-scrollbar-corner {
background: transparent;
}
::-webkit-scrollbar-thumb {
border: 1px solid transparent;
border-radius: 9px;
background: rgba(128, 128, 128, 0.5);
background-clip: content-box;
}
}
</style>
</head>
<body>
loading...
<script>
addEventListener(
'error',
(e) => {
parent.postMessage(['error', e.message], '*')
},
true
)
</script>
<script type="module">
import loader from './src/loader.js'
window.loader = loader
</script>
<script>
let app
function createApp(code) {
if (!window.loader) {
setTimeout((_) => createApp(code), 10)
return
}
console.clear()
parent.postMessage(['error', ''], '*')
app?.destory()
const App = loader(code)
const _app = new App()
_app.mount(document.body, 'wrap')
app = _app
console.log('app:', app)
}
onmessage = function (e) {
const { type, value, id } = e.data || {}
parent.postMessage({ type, value, id, re: true }, '*')
switch (type) {
case 'code':
createApp(value)
break
default:
break
}
}
</script>
</body>
</html>
</template>
</main>
<output id="output">
<script>
addEventListener('message', (e) => {
if (e.data[0] === 'error') {
$('output').innerText = e.data[1]
}
})
</script>
</output>
<!-- -->
<link
rel="stylesheet"
href="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/codemirror.css"
/>
<link
rel="stylesheet"
href="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/theme/monokai.css"
/>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/mode/htmlmixed/htmlmixed.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/mode/xml/xml.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/mode/css/css.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/hint/javascript-hint.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/hint/xml-hint.js"></script>
<link
rel="stylesheet"
href="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/fold/foldgutter.css"
/>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/fold/foldcode.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/fold/foldgutter.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/codemirror/5.65.2/addon/fold/xml-fold.js"></script>
<script>
const iframe = document.querySelector('iframe')
const textarea = document.querySelector('textarea')
const url = new URL(location)
// srcdoc
$('iframe').srcdoc = $('#srcdoc').innerHTML
// editor
const editor = CodeMirror.fromTextArea(textarea, {
autofocus: true,
mode: 'text/html',
theme: 'monokai',
lineNumbers: true,
lineWrapping: !true,
foldGutter: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
})
// default code
let defaultCode = `
<button onclick="alert(value)">hello \${value}</button>
<script>
let value = 'world'
<\/script>
`
let lineSeparator = '\n'
// init code
const codeParam = url.searchParams.get('code')
const urlParam = url.searchParams.get('url')
if (codeParam) {
setTimeout(() => {
defaultCode = codeParam
editor.setValue(codeParam)
editor.clearHistory()
})
} else if (urlParam) {
fetch(urlParam)
.then((res) => res.text())
.then((text) => {
defaultCode = text
lineSeparator = text.match(/\r?\n/)?.[0]
editor.setValue(text)
editor.clearHistory()
})
} else {
setTimeout(() => {
editor.setValue(defaultCode)
editor.clearHistory()
})
}
// send
function send(type, value) {
return new Promise((rs, rj) => {
let success = false
let timeout = false
let id = Math.random()
addEventListener('message', function f(e) {
const {
type: _type,
value: _value,
id: _id,
re: _re,
} = e.data || {}
if (_re && _id === id) {
success = true
rs(e.data)
removeEventListener('message', f)
}
})
_send(type, value)
function _send(type, value) {
iframe.contentWindow.postMessage({ type, value, id }, '*')
setTimeout(() => {
if (!success && !timeout) {
_send(type, value)
}
}, 100)
}
setTimeout(() => {
timeout = true
rj('timeout')
}, 3000)
})
}
// input code
editor.on('change', () => {
const code = editor.getValue(lineSeparator)
send('code', code)
// store
// localStorage.code = value
if (code !== defaultCode) {
// url.searchParams.delete('url')
url.searchParams.set('code', code)
history.replaceState(null, null, url.toString().replace(/%2F/g, '/'))
}
})
// ! ctrl + s
addEventListener('keydown', (e) => {
if (e.key === 's' && (e.ctrlKey || e.metaKey)) {
e.preventDefault()
}
})
// check browser
send('check').catch(() => {
alert('当前浏览器不支持\n请更换浏览器\nIOS微信请用外部浏览器打开')
})
</script>
</body>
</html>