@lvce-editor/virtual-dom-worker 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/parts/AttributePatch/AttributePatch.ts +6 -0
- package/src/parts/Patch/Patch.ts +10 -0
- package/src/parts/PatchType/PatchType.ts +4 -0
- package/src/parts/RemoveAttributePatch/RemoveAttributePatch.ts +5 -0
- package/src/parts/ReplacePatch/ReplacePatch.ts +7 -0
- package/src/parts/TextPatch/TextPatch.ts +5 -0
- package/src/parts/VirtualDomDiff/VirtualDomDiff.ts +96 -0
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AttributePatch } from '../AttributePatch/AttributePatch.ts'
|
|
2
|
+
import type { ReplacePatch } from '../ReplacePatch/ReplacePatch.ts'
|
|
3
|
+
import type { TextPatch } from '../TextPatch/TextPatch.ts'
|
|
4
|
+
import type { RemoveAttributePatch } from '../RemoveAttributePatch/RemoveAttributePatch.ts'
|
|
5
|
+
|
|
6
|
+
export type Patch =
|
|
7
|
+
| TextPatch
|
|
8
|
+
| AttributePatch
|
|
9
|
+
| ReplacePatch
|
|
10
|
+
| RemoveAttributePatch
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Patch } from '../Patch/Patch.ts'
|
|
2
|
+
import type { VirtualDomNode } from '../VirtualDomNode/VirtualDomNode.ts'
|
|
3
|
+
import * as PatchType from '../PatchType/PatchType.ts'
|
|
4
|
+
import * as VirtualDomElements from '../VirtualDomElements/VirtualDomElements.ts'
|
|
5
|
+
|
|
6
|
+
export const diff = (
|
|
7
|
+
oldNodes: readonly VirtualDomNode[],
|
|
8
|
+
newNodes: readonly VirtualDomNode[],
|
|
9
|
+
): readonly Patch[] => {
|
|
10
|
+
const patches: Patch[] = []
|
|
11
|
+
|
|
12
|
+
// Compare nodes at each index
|
|
13
|
+
for (let i = 0; i < Math.max(oldNodes.length, newNodes.length); i++) {
|
|
14
|
+
const oldNode = oldNodes[i]
|
|
15
|
+
const newNode = newNodes[i]
|
|
16
|
+
|
|
17
|
+
// Handle node removal
|
|
18
|
+
if (!newNode) {
|
|
19
|
+
patches.push({
|
|
20
|
+
type: PatchType.Replace,
|
|
21
|
+
index: i,
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
node: undefined,
|
|
24
|
+
})
|
|
25
|
+
continue
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Handle node addition
|
|
29
|
+
if (!oldNode) {
|
|
30
|
+
patches.push({
|
|
31
|
+
type: PatchType.Replace,
|
|
32
|
+
index: i,
|
|
33
|
+
node: newNode,
|
|
34
|
+
})
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Different node types - complete replacement
|
|
39
|
+
if (oldNode.type !== newNode.type) {
|
|
40
|
+
patches.push({
|
|
41
|
+
type: PatchType.Replace,
|
|
42
|
+
index: i,
|
|
43
|
+
node: newNode,
|
|
44
|
+
})
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Text node changes
|
|
49
|
+
if (
|
|
50
|
+
oldNode.type === VirtualDomElements.Text &&
|
|
51
|
+
newNode.type === VirtualDomElements.Text
|
|
52
|
+
) {
|
|
53
|
+
if (oldNode.text !== newNode.text) {
|
|
54
|
+
patches.push({
|
|
55
|
+
type: PatchType.SetText,
|
|
56
|
+
index: i,
|
|
57
|
+
value: newNode.text,
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Attribute changes
|
|
64
|
+
const oldKeys = Object.keys(oldNode).filter(
|
|
65
|
+
(key) => key !== 'type' && key !== 'childCount',
|
|
66
|
+
)
|
|
67
|
+
const newKeys = Object.keys(newNode).filter(
|
|
68
|
+
(key) => key !== 'type' && key !== 'childCount',
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
// Check for changed or added attributes
|
|
72
|
+
for (const key of newKeys) {
|
|
73
|
+
if (oldNode[key] !== newNode[key]) {
|
|
74
|
+
patches.push({
|
|
75
|
+
type: PatchType.SetAttribute,
|
|
76
|
+
index: i,
|
|
77
|
+
key,
|
|
78
|
+
value: newNode[key],
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Check for removed attributes
|
|
84
|
+
for (const key of oldKeys) {
|
|
85
|
+
if (!(key in newNode)) {
|
|
86
|
+
patches.push({
|
|
87
|
+
type: PatchType.RemoveAttribute,
|
|
88
|
+
index: i,
|
|
89
|
+
key,
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return patches
|
|
96
|
+
}
|