-
Notifications
You must be signed in to change notification settings - Fork 7
/
demo.js
381 lines (339 loc) · 9.7 KB
/
demo.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* eslint no-console: 0 */
import {mountApp, unmountApp} from '../src/ed'
import fixture from './fixture'
import Gremlins from 'gremlins.js/src/main'
import gremlinProsemirror from './gremlin-prosemirror'
let ed
const fixtureContent = fixture.content
let apiJSON = document.querySelector('#debug-api')
// ProseMirror setup
function setup (options) {
const container = document.querySelector('#app')
if (ed) {
unmountApp(container)
ed = null
}
if (options.initialContent) {
apiJSON.value = JSON.stringify(options.initialContent, null, 2)
}
const props =
{ initialContent: (options.initialContent || []),
onChange: () => { console.log('onChange') },
onMount: function (mounted) {
ed = mounted
console.log(ed)
window.ed = ed
},
onShareFile: onShareFileDemo,
onShareUrl: onShareUrlDemo,
onRequestCoverUpload: onRequestCoverUploadDemo,
onPlaceholderCancel: onPlaceholderCancelDemo,
onCommandsChanged: function (commands) {
// console.log(commands)
},
onDropFiles: onDropFilesDemo,
onDropFileOnBlock: onDropFileOnBlockDemo,
// onRequestLink: function (value) {
// console.log('onRequestLink', value)
// },
imgfloConfig: null,
widgetPath: './node_modules/',
coverPrefs: { filter: false },
menuBar: true,
featureFlags: {
edCta: false,
edEmbed: false,
},
}
mountApp(container, props)
// Only for fixture demo
initializePlaceholderMetadata(props.initialContent)
}
const initialContent = (window.location.hash === '#fixture' ? fixtureContent : [])
setup({initialContent})
// onShareFile upload demo
let input
function onShareFileDemo (index) {
console.log('onShareFile: app triggers native picker', index)
// Remove old input from DOM
if (input && input.parentNode) {
input.parentNode.removeChild(input)
}
input = document.createElement('input')
input.type = 'file'
input.multiple = true
input.accept = 'image/*'
input.onchange = makeInputOnChange(index)
input.style.display = 'none'
document.body.appendChild(input)
input.click()
}
function makeInputOnChange (index) {
return function (event) {
event.stopPropagation()
const input = event.target
const files = input.files
if (!files || !files.length) return
filesUploadSim(index, files)
}
}
function filesUploadSim (index, files) {
// Make placeholder blocks
let names = []
for (let i = 0, len = files.length; i < len; i++) {
const file = files[i]
const name = file.name.substr(0, file.name.indexOf('.'))
names.push(name)
}
// Insert placeholder blocks into content
console.log(`app calls ed.insertPlaceholders(${index}, ${files.length}) and gets array of ids`)
const ids = ed.insertPlaceholders(index, files.length)
for (let i = 0, len = files.length; i < len; i++) {
const file = files[i]
const url = URL.createObjectURL(file)
const id = ids[i]
ed.setCoverPreview(id, url)
}
console.log('app uploads files now and calls `ed.updateProgress(id, meta)` with updates')
simulateProgress(
function (progress) {
ids.forEach(function (id, index) {
let status = `Uploading ${names[index]}`
ed.updateProgress(id, {status, progress})
})
},
function () {
const updatedBlocks = ids.map(function (id, index) {
ed.updateProgress(id, {progress: null})
return (
{ id,
type: 'image',
metadata: {title: names[index]},
}
)
})
ed.setContent(updatedBlocks)
}
)
}
// File picker debug
document.getElementById('upload').onclick = function () {
ed.execCommand('ed_upload_image')
}
// onShareUrl demo
function onShareUrlDemo (share) {
const {block, url} = share
console.log('onShareUrl: app shares url now and calls ed.updateProgress() with updates', share)
simulateProgress(
function (progress) {
const status = `Sharing ${url}`
ed.updateProgress(block, {status, progress})
},
function () {
console.log('Share: mount block')
ed.setContent([
{ id: block,
type: 'article',
metadata:
{ title: 'Shared article title',
description: `Simulated share from ${url}`,
},
},
])
window.setTimeout(function () {
console.log('Share: mount block + cover')
ed.setContent([
{ id: block,
type: 'article',
metadata:
{ title: 'Shared article title + cover',
description: `Simulated share from ${url}`,
},
cover:
{ src: 'http://meemoo.org/images/meemoo-illo-by-jyri-pieniniemi-400.png',
width: 400,
height: 474,
},
},
])
}, 1000)
}
)
}
function simulateProgress (progress, complete) {
let percent = 0
let animate = function () {
percent += 0.5
if (percent < 100) {
// Loop animation
requestAnimationFrame(animate)
// Update placeholder status
progress(percent)
} else {
// Change placeholder to article block
complete()
}
}
animate()
}
// Debug buttons
// Toggle debug
let showDebug = false
let debug = document.getElementById('debug')
let toggleDebug = document.getElementById('debug-toggle')
toggleDebug.onclick = () => {
if (showDebug) {
debug.style.display = 'none'
} else {
debug.style.display = 'block'
}
showDebug = !showDebug
}
// Hydrate
function APIToEditor () {
let json
try {
json = JSON.parse(apiJSON.value)
} catch (e) {
return console.warn('bad json')
}
ed.setContent(json)
}
document.querySelector('#hydrate').onclick = APIToEditor
// Dehydrate
function EditorToAPI () {
apiJSON.value = JSON.stringify(ed.getContent(), null, 2)
}
document.querySelector('#dehydrate').onclick = EditorToAPI
// Simulate changes from API
const bangOnContent = document.querySelector('#sim')
let timeout
let simulateUpdates = function () {
// Loop
timeout = setTimeout(simulateUpdates, 1000)
let content = ed.getContent()
// console.log(content[6].url)
ed.setContent(content)
}
let toggleUpdates = function () {
if (timeout) {
clearTimeout(timeout)
timeout = null
bangOnContent.textContent = 'Sim changes from API ▶'
} else {
timeout = setTimeout(simulateUpdates, 500)
bangOnContent.textContent = 'Sim changes from API ◼︎'
}
}
bangOnContent.onclick = toggleUpdates
bangOnContent.click()
// Load full post
function loadFixture () {
window.location.hash = '#fixture'
setup({initialContent: fixtureContent})
}
document.querySelector('#fixture').onclick = loadFixture
function initializePlaceholderMetadata (content) {
for (let i = 0, len = content.length; i < len; i++) {
const block = content[i]
if (!block || !block.id || !block.metadata) {
continue
}
const {progress, status, failed} = block.metadata
if (progress === undefined && status === undefined && failed === undefined) {
continue
}
ed.updateProgress(block.id, {progress, status, failed})
const {cover} = block
if (cover && cover.src) {
ed.setCoverPreview(block.id, cover.src)
}
}
}
function onPlaceholderCancelDemo (id) {
console.log(`App would cancel the share or upload with id: ${id}`)
}
// Cover change
function onRequestCoverUploadDemo (id) {
console.log('onRequestCoverUpload: app triggers native picker', id)
// Remove old input from DOM
if (input && input.parentNode) {
input.parentNode.removeChild(input)
}
input = document.createElement('input')
input.type = 'file'
input.multiple = false
input.accept = 'image/*'
input.onchange = makeRequestCoverUploadInputOnChange(id)
input.style.display = 'none'
document.body.appendChild(input)
input.click()
}
function makeRequestCoverUploadInputOnChange (id) {
return function (event) {
event.stopPropagation()
const file = input.files[0]
const src = URL.createObjectURL(file)
ed.setCoverPreview(id, src)
ed.updateProgress(id, {failed: false})
console.log('app uploads files now and calls ed.updateProgress with updates')
simulateProgress(
function (progress) {
let status = 'Uploading...'
ed.updateProgress(id, {status, progress})
},
function () {
ed.updateProgress(id, {progress: null})
// Apps should have dimensions from API
// and should not need to load the image client-side
const img = new Image()
img.onload = function () {
const {width, height} = img
ed.setCover(id, {src, width, height})
}
img.src = src
}
)
}
}
function onDropFilesDemo (index, files) {
console.log('onDropFiles: files dropped')
filesUploadSim(index, files)
}
function onDropFileOnBlockDemo (id, file) {
console.log('onDropFileOnBlock: file dropped')
const src = URL.createObjectURL(file)
ed.setCoverPreview(id, src)
console.log('app uploads files now and calls ed.updateProgress with updates')
simulateProgress(
function (progress) {
let status = 'Uploading...'
ed.updateProgress(id, {status, progress})
},
function () {
// Apps should have dimensions from API
// and should not need to load the image client-side
const img = new Image()
img.onload = function () {
const {width, height} = img
ed.setCover(id, {src, width, height})
}
img.src = src
}
)
}
/* Fuzz / Monkey / Gremlin.js testing */
const fuzz = document.getElementById('fuzz')
let fuzzer = null
fuzz.addEventListener('click', function () {
if (fuzzer) {
fuzzer.stop()
fuzzer = null
} else {
fuzzer = Gremlins
.createHorde()
// .allGremlins()
.gremlin(gremlinProsemirror)
fuzzer.unleash()
}
})