@lvce-editor/virtual-dom-worker 1.5.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/virtual-dom-worker",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -0,0 +1,6 @@
1
+ export interface AttributePatch {
2
+ readonly type: 3
3
+ readonly index: number
4
+ readonly key: string
5
+ readonly value: any
6
+ }
@@ -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,4 @@
1
+ export const SetText = 1
2
+ export const Replace = 2
3
+ export const SetAttribute = 3
4
+ export const RemoveAttribute = 4
@@ -0,0 +1,5 @@
1
+ export interface RemoveAttributePatch {
2
+ readonly type: 4
3
+ readonly index: number
4
+ readonly key: string
5
+ }
@@ -0,0 +1,7 @@
1
+ import type { VirtualDomNode } from '../VirtualDomNode/VirtualDomNode.ts'
2
+
3
+ export interface ReplacePatch {
4
+ readonly type: 2
5
+ readonly index: number
6
+ readonly node: VirtualDomNode
7
+ }
@@ -0,0 +1,5 @@
1
+ export interface TextPatch {
2
+ readonly type: 1
3
+ readonly index: number
4
+ readonly value: string
5
+ }
@@ -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
+ }