/
dom.ts
370 lines (330 loc) · 12.8 KB
/
dom.ts
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
import {
RecordData,
MouseRecordData,
FormElementWatcherData,
RecordType,
MouseEventType,
CharacterDataUpdateData,
AttributesUpdateData,
FormElementEvent,
WindowWatcherData,
UpdateNodeData,
RemoveUpdateData,
DOMUpdateDataType,
ScrollWatcherData,
VNode,
VSNode,
LocationRecordData,
CanvasRecordData,
UnionToIntersection,
SnapshotRecord
} from '@timecat/share'
import FIXED_CSS from './fixed.scss'
import { PlayerComponent } from './player'
import { nodeStore, isElementNode, isExistingNode, delay, isVNode, revertStrByPatches } from '@timecat/utils'
import { setAttribute, createNode, createSpecialNode, convertVNode } from '@timecat/virtual-dom'
/**
* if return true then revert
* @param data
* @param orderSet
* @returns true | undefined
*/
function insertOrMoveNode(data: UpdateNodeData, orderSet: Set<number>) {
const { parentId, nextId, node } = data
const parentNode = nodeStore.getNode(parentId!)
if (parentNode && isElementNode(parentNode)) {
let nextNode: Node | null = null
if (nextId) {
// Must wait for the relation node inserted first
if (orderSet.has(nextId)) {
return true
}
nextNode = findNextNode(nextId)
if (!nextNode) {
return true
}
}
const n = node as VNode | VSNode
let insertNode: Node | null
if (typeof node === 'number') {
insertNode = nodeStore.getNode(node)!
if (orderSet.has(node)) {
orderSet.delete(node)
}
} else if (isVNode(n)) {
insertNode = convertVNode(n as VNode)
} else {
insertNode = createSpecialNode(n as VSNode)
}
if (insertNode) {
parentNode.insertBefore(insertNode, nextNode)
}
} else {
return true
}
}
function findNextNode(nextId: number | null): Node | null {
return nextId ? nodeStore.getNode(nextId) : null
}
export async function updateDom(this: PlayerComponent, Record: RecordData) {
const { type, data } = Record
switch (type) {
case RecordType.SNAPSHOT: {
const snapshotData = data as SnapshotRecord['data']
const { frameId } = snapshotData
if (frameId) {
const iframeNode = nodeStore.getNode(frameId) as HTMLIFrameElement
if (iframeNode) {
const contentDocument = iframeNode.contentDocument!
createIframeDOM(contentDocument, snapshotData)
injectIframeContent(contentDocument, snapshotData)
}
}
break
}
case RecordType.SCROLL: {
const { top, left, id } = data as ScrollWatcherData
const target = id ? (nodeStore.getNode(id) as HTMLElement) : this.c.sandBoxDoc.documentElement
const curTop = target.scrollTop
// prevent jump too long distance
const behavior = Math.abs(top - curTop) > window.__ReplayData__.snapshot.data.height * 3 ? 'auto' : 'smooth'
target.scrollTo({
top,
left,
behavior
})
break
}
case RecordType.WINDOW: {
const { width, height, id } = data as WindowWatcherData
let target: HTMLElement
if (id) {
target = nodeStore.getNode(id) as HTMLElement
;(target as HTMLElement).style.width = width + 'px'
;(target as HTMLElement).style.height = height + 'px'
} else {
target = this.c.sandBoxDoc.body
this.c.resize(width, height)
}
break
}
case RecordType.MOUSE: {
const { x, y, id, type } = data as MouseRecordData
if (!id) {
return
}
const node = nodeStore.getNode(id) as HTMLElement
const { left, top } = node?.getBoundingClientRect() || {}
if (type === MouseEventType.MOVE) {
this.pointer.move(x + left, y + top)
} else if (type === MouseEventType.CLICK) {
this.pointer.click(x + left, y + top)
}
break
}
case RecordType.DOM: {
// Reduce the delay caused by interactive animation
await delay(200)
const { addedNodes, movedNodes, removedNodes, attrs, texts } = data as DOMUpdateDataType
removedNodes.forEach((data: RemoveUpdateData) => {
const { parentId, id } = data
const parentNode = nodeStore.getNode(parentId)
const node = nodeStore.getNode(id)
if (node && parentNode && parentNode.contains(node)) {
parentNode.removeChild(node as Node)
}
})
const movedList = movedNodes.slice()
// node1 -> node2 -> node3
// insert node2 first
// insert node1 last
// => if nextId equal id, insert id first
const orderSet: Set<number> = new Set()
movedList.forEach(data => {
// Is there a relations between two nodes
if (data.nextId) {
if (movedList.some(a => a.id === data.nextId)) {
orderSet.add(data.nextId)
}
}
})
const addedList = movedList
.map(item => {
const { id, parentId, nextId } = item
return {
node: id,
parentId,
nextId
} as UpdateNodeData
})
.concat(addedNodes.slice())
// Math Termial
const n = addedList.length
const maxRevertCount = n > 0 ? (n * n + n) / 2 : 0
let revertCount = 0
while (addedList.length) {
const addData = addedList.shift()
if (addData) {
if (insertOrMoveNode(addData, orderSet)) {
// revert here
if (revertCount++ < maxRevertCount) {
addedList.push(addData)
}
}
}
}
attrs.forEach((attr: AttributesUpdateData) => {
const { id, key, value } = attr
const node = nodeStore.getNode(id) as HTMLElement
if (node) {
setAttribute(node as HTMLElement, key, value)
}
})
texts.forEach((text: CharacterDataUpdateData) => {
const { id, value, parentId } = text
const parentNode = nodeStore.getNode(parentId) as HTMLElement
const node = nodeStore.getNode(id) as HTMLElement
if (parentNode && node) {
if (isExistingNode(node)) {
node.textContent = value
return
}
parentNode.innerText = value
}
})
break
}
case RecordType.FORM_EL: {
// Reduce the delay caused by interactive animation
await delay(200)
const { id, key, type: formType, value, patches } = data as FormElementWatcherData
const node = nodeStore.getNode(id) as HTMLInputElement | undefined
const { mode } = window.__ReplayOptions__
if (node) {
if (formType === FormElementEvent.INPUT || formType === FormElementEvent.CHANGE) {
if (patches && patches.length) {
const newValue = revertStrByPatches(node.value, patches)
node.value = newValue
} else if (key) {
;(node as any)[key] = value
}
} else if (formType === FormElementEvent.FOCUS) {
if (mode === 'live') {
return
}
node.focus()
} else if (formType === FormElementEvent.BLUR) {
node.blur()
} else if (formType === FormElementEvent.PROP) {
if (key) {
;(node as any)[key] = value
}
}
}
break
}
case RecordType.LOCATION: {
const { path, hash, href, contextNodeId } = data as LocationRecordData
const contextNode = nodeStore.getNode(contextNodeId)
if (contextNode) {
const context = contextNode.ownerDocument!.defaultView!
context.__ReplayLocation__ = { ...context.__ReplayLocation__, ...{ path, hash, href } }
}
break
}
case RecordType.CANVAS: {
const { src, id, strokes } = data as UnionToIntersection<CanvasRecordData>
const target = nodeStore.getNode(id) as HTMLCanvasElement
if (!target) {
return
}
const ctx = target.getContext('2d')!
if (src) {
const image = new Image()
image.src = src
image.onload = function (this: HTMLImageElement) {
ctx.drawImage(this, 0, 0)
}
} else {
async function createChain() {
function splitStrokes(strokesArray: UnionToIntersection<CanvasRecordData>['strokes'][]) {
const result: UnionToIntersection<CanvasRecordData>['strokes'][] = []
strokesArray.forEach(strokes => {
const len = strokes.length
const pivot = Math.floor(len / 2)
result.push(...[strokes.splice(0, pivot), strokes])
})
return result
}
// TODO expect stroke smooth (elapsed time)
for (const strokesArray of splitStrokes(splitStrokes([strokes]))) {
// await delay(0) // have problem here
for (const stroke of strokesArray) {
const { name, args } = stroke
if (Array.isArray(args)) {
if (name === 'drawImage') {
args[0] = nodeStore.getNode(args[0])
}
;(ctx[name] as Function).apply(ctx, args)
} else {
const value = args
;(ctx[name] as Object) = value
}
}
}
}
createChain()
}
}
default: {
break
}
}
}
export function showStartMask() {
const startPage = document.querySelector('#cat-start-page')! as HTMLElement
startPage.setAttribute('style', '')
}
function showStartBtn() {
const startPage = document.querySelector('#cat-start-page')! as HTMLElement
const btn = startPage.querySelector('.play-btn') as HTMLElement
btn.classList.add('show')
return btn
}
export function removeStartPage() {
const startPage = document.querySelector('#cat-start-page') as HTMLElement
startPage.parentElement!.removeChild(startPage)
}
export async function waitStart(): Promise<void> {
const btn = showStartBtn()
return new Promise(r => {
btn.addEventListener('click', async () => {
btn.classList.remove('show')
await delay(500)
r()
})
})
}
export function createIframeDOM(contentDocument: Document, snapshotData: SnapshotRecord['data']) {
contentDocument.open()
const doctype = snapshotData.doctype
const doc = `<!DOCTYPE ${doctype.name} ${doctype.publicId ? 'PUBLIC ' + '"' + doctype.publicId + '"' : ''} ${
doctype.systemId ? '"' + doctype.systemId + '"' : ''
}><html><head></head><body></body></html>`
contentDocument.write(doc)
}
export function injectIframeContent(contentDocument: Document, snapshotData: SnapshotRecord['data']) {
const content = convertVNode(snapshotData.vNode)
if (content) {
const head = content.querySelector('head')
if (head) {
const style = document.createElement('style')
style.innerHTML = FIXED_CSS
head.appendChild(style)
}
const documentElement = contentDocument.documentElement
content.scrollLeft = snapshotData.scrollLeft
content.scrollTop = snapshotData.scrollTop
contentDocument.replaceChild(content, documentElement)
}
}