@lvce-editor/virtual-dom 1.7.0 → 1.9.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/dist/index.d.ts CHANGED
@@ -22,3 +22,8 @@ export const rememberFocus: (
22
22
  ) => void
23
23
 
24
24
  export const registerEventListeners: any
25
+
26
+ export const applyPatch: (
27
+ $Element: HTMLElement,
28
+ patches: readonly any[],
29
+ ) => void
package/dist/index.js CHANGED
@@ -1,90 +1,3 @@
1
- // src/parts/UidSymbol/UidSymbol.ts
2
- var uidSymbol = Symbol("uid");
3
-
4
- // src/parts/GetUidTarget/GetUidTarget.ts
5
- var getUidTarget = ($Element) => {
6
- while ($Element) {
7
- if ($Element[uidSymbol]) {
8
- return $Element;
9
- }
10
- $Element = $Element.parentNode;
11
- }
12
- return void 0;
13
- };
14
-
15
- // src/parts/ComponentUid/ComponentUid.ts
16
- var setComponentUid = ($Element, uid) => {
17
- $Element[uidSymbol] = uid;
18
- };
19
- var getComponentUid = ($Element) => {
20
- const $Target = getUidTarget($Element);
21
- if (!$Target) {
22
- return 0;
23
- }
24
- return $Target[uidSymbol];
25
- };
26
- var getComponentUidFromEvent = (event) => {
27
- const { target, currentTarget } = event;
28
- return getComponentUid(currentTarget || target);
29
- };
30
-
31
- // src/parts/IpcState/IpcState.ts
32
- var state = {
33
- ipc: void 0
34
- };
35
- var getIpc = () => {
36
- return state.ipc;
37
- };
38
- var setIpc = (value) => {
39
- state.ipc = value;
40
- };
41
-
42
- // src/parts/NameAnonymousFunction/NameAnonymousFunction.ts
43
- var nameAnonymousFunction = (fn, name) => {
44
- Object.defineProperty(fn, "name", {
45
- value: name
46
- });
47
- };
48
-
49
- // src/parts/RegisterEventListeners/RegisterEventListeners.ts
50
- var listeners = /* @__PURE__ */ Object.create(null);
51
- var getArgs = (params, event) => {
52
- return [...params];
53
- };
54
- var createFn = (info) => {
55
- const fn = (event) => {
56
- if (info.preventDefault) {
57
- event.preventDefault(event);
58
- }
59
- const uid = getComponentUidFromEvent(event);
60
- const args = getArgs(info.params, event);
61
- if (args.length === 0) {
62
- return;
63
- }
64
- const ipc = getIpc();
65
- ipc.send("Viewlet.executeViewletCommand", uid, ...args);
66
- };
67
- nameAnonymousFunction(fn, info.name);
68
- return fn;
69
- };
70
- var registerEventListeners = (id, eventListeners) => {
71
- const map = /* @__PURE__ */ Object.create(null);
72
- for (const info of eventListeners) {
73
- const fn = createFn(info);
74
- map[info.name] = fn;
75
- }
76
- listeners[id] = map;
77
- };
78
- var getEventListenerMap = (id) => {
79
- const map = listeners[id];
80
- return map;
81
- };
82
-
83
- // src/parts/ClearNode/ClearNode.ts
84
- var clearNode = ($Node) => {
85
- $Node.textContent = "";
86
- };
87
-
88
1
  // src/parts/ElementTags/ElementTags.ts
89
2
  var Audio = "audio";
90
3
  var Button = "button";
@@ -314,6 +227,47 @@ var getEventListenerOptions = (eventName) => {
314
227
  }
315
228
  };
316
229
 
230
+ // src/parts/UidSymbol/UidSymbol.ts
231
+ var uidSymbol = Symbol("uid");
232
+
233
+ // src/parts/GetUidTarget/GetUidTarget.ts
234
+ var getUidTarget = ($Element) => {
235
+ while ($Element) {
236
+ if ($Element[uidSymbol]) {
237
+ return $Element;
238
+ }
239
+ $Element = $Element.parentNode;
240
+ }
241
+ return void 0;
242
+ };
243
+
244
+ // src/parts/ComponentUid/ComponentUid.ts
245
+ var setComponentUid = ($Element, uid) => {
246
+ $Element[uidSymbol] = uid;
247
+ };
248
+ var getComponentUid = ($Element) => {
249
+ const $Target = getUidTarget($Element);
250
+ if (!$Target) {
251
+ return 0;
252
+ }
253
+ return $Target[uidSymbol];
254
+ };
255
+ var getComponentUidFromEvent = (event) => {
256
+ const { target, currentTarget } = event;
257
+ return getComponentUid(currentTarget || target);
258
+ };
259
+
260
+ // src/parts/IpcState/IpcState.ts
261
+ var state = {
262
+ ipc: void 0
263
+ };
264
+ var getIpc = () => {
265
+ return state.ipc;
266
+ };
267
+ var setIpc = (value) => {
268
+ state.ipc = value;
269
+ };
270
+
317
271
  // src/parts/ListenerCache/ListenerCache.ts
318
272
  var cache = /* @__PURE__ */ new Map();
319
273
  var has = (listener) => {
@@ -326,6 +280,13 @@ var get = (listener) => {
326
280
  return cache.get(listener);
327
281
  };
328
282
 
283
+ // src/parts/NameAnonymousFunction/NameAnonymousFunction.ts
284
+ var nameAnonymousFunction = (fn, name) => {
285
+ Object.defineProperty(fn, "name", {
286
+ value: name
287
+ });
288
+ };
289
+
329
290
  // src/parts/GetWrappedListener/GetWrappedListener.ts
330
291
  var getWrappedListener = (listener, returnValue) => {
331
292
  if (!returnValue) {
@@ -500,6 +461,115 @@ var renderInternal2 = ($Parent, elements, eventMap, newEventMap) => {
500
461
  $Parent.append(...stack);
501
462
  };
502
463
 
464
+ // src/parts/PatchFunctions/PatchFunctions.ts
465
+ var setAttribute = ($Element, key, value) => {
466
+ $Element.setAttribute(key, value);
467
+ };
468
+ var removeAttribute = ($Element, key) => {
469
+ $Element.removeAttribute(key);
470
+ };
471
+ var setText = ($Element, value) => {
472
+ $Element.nodeValue = value;
473
+ };
474
+ var removeChild = ($Element, index) => {
475
+ console.log("el", $Element.className);
476
+ console.log("index", index);
477
+ console.log("ch", $Element.children.length);
478
+ const $Child = $Element.children[index];
479
+ $Child.remove();
480
+ };
481
+ var add = ($Element, nodes) => {
482
+ renderInternal2($Element, nodes, {}, {});
483
+ };
484
+
485
+ // src/parts/PatchType/PatchType.ts
486
+ var SetText = 1;
487
+ var SetAttribute = 3;
488
+ var RemoveAttribute = 4;
489
+ var Add = 6;
490
+ var NavigateChild = 7;
491
+ var NavigateParent = 8;
492
+ var RemoveChild = 9;
493
+ var NavigateSibling = 10;
494
+
495
+ // src/parts/ApplyPatch/ApplyPatch.ts
496
+ var applyPatch = ($Element, patches) => {
497
+ let $Current = $Element;
498
+ for (const patch of patches) {
499
+ switch (patch.type) {
500
+ case SetAttribute:
501
+ setAttribute(
502
+ $Current,
503
+ patch.key,
504
+ patch.value
505
+ );
506
+ break;
507
+ case RemoveAttribute:
508
+ removeAttribute($Current, patch.key);
509
+ break;
510
+ case SetText:
511
+ setText($Current, patch.value);
512
+ break;
513
+ case RemoveChild:
514
+ removeChild($Current, patch.index);
515
+ break;
516
+ case Add:
517
+ add($Current, patch.nodes);
518
+ break;
519
+ case NavigateSibling:
520
+ $Current = $Current.parentNode.childNodes[patch.index];
521
+ break;
522
+ case NavigateChild:
523
+ $Current = $Current.childNodes[patch.index];
524
+ break;
525
+ case NavigateParent:
526
+ $Current = $Current.parentNode;
527
+ break;
528
+ default:
529
+ break;
530
+ }
531
+ }
532
+ };
533
+
534
+ // src/parts/RegisterEventListeners/RegisterEventListeners.ts
535
+ var listeners = /* @__PURE__ */ Object.create(null);
536
+ var getArgs = (params, event) => {
537
+ return [...params];
538
+ };
539
+ var createFn = (info) => {
540
+ const fn = (event) => {
541
+ if (info.preventDefault) {
542
+ event.preventDefault(event);
543
+ }
544
+ const uid = getComponentUidFromEvent(event);
545
+ const args = getArgs(info.params, event);
546
+ if (args.length === 0) {
547
+ return;
548
+ }
549
+ const ipc = getIpc();
550
+ ipc.send("Viewlet.executeViewletCommand", uid, ...args);
551
+ };
552
+ nameAnonymousFunction(fn, info.name);
553
+ return fn;
554
+ };
555
+ var registerEventListeners = (id, eventListeners) => {
556
+ const map = /* @__PURE__ */ Object.create(null);
557
+ for (const info of eventListeners) {
558
+ const fn = createFn(info);
559
+ map[info.name] = fn;
560
+ }
561
+ listeners[id] = map;
562
+ };
563
+ var getEventListenerMap = (id) => {
564
+ const map = listeners[id];
565
+ return map;
566
+ };
567
+
568
+ // src/parts/ClearNode/ClearNode.ts
569
+ var clearNode = ($Node) => {
570
+ $Node.textContent = "";
571
+ };
572
+
503
573
  // src/parts/VirtualDom/VirtualDom.ts
504
574
  var renderInto = ($Parent, dom, eventMap = {}) => {
505
575
  clearNode($Parent);
@@ -562,6 +632,7 @@ var rememberFocus = ($Viewlet, dom, eventMap, uid = 0) => {
562
632
  return $Viewlet;
563
633
  };
564
634
  export {
635
+ applyPatch,
565
636
  getComponentUid,
566
637
  getComponentUidFromEvent,
567
638
  registerEventListeners,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/virtual-dom",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -0,0 +1,6 @@
1
+ import type { VirtualDomNode } from '../VirtualDomNode/VirtualDomNode.ts'
2
+
3
+ export interface AddPatch {
4
+ readonly type: 6
5
+ readonly nodes: readonly VirtualDomNode[]
6
+ }
@@ -0,0 +1,41 @@
1
+ import { Patch } from '../Patch/Patch.ts'
2
+ import * as PatchFunctions from '../PatchFunctions/PatchFunctions.ts'
3
+ import * as PatchType from '../PatchType/PatchType.ts'
4
+
5
+ export const applyPatch = ($Element: Node, patches: readonly Patch[]): void => {
6
+ let $Current = $Element
7
+ for (const patch of patches) {
8
+ switch (patch.type) {
9
+ case PatchType.SetAttribute:
10
+ PatchFunctions.setAttribute(
11
+ $Current as HTMLElement,
12
+ patch.key,
13
+ patch.value,
14
+ )
15
+ break
16
+ case PatchType.RemoveAttribute:
17
+ PatchFunctions.removeAttribute($Current as HTMLElement, patch.key)
18
+ break
19
+ case PatchType.SetText:
20
+ PatchFunctions.setText($Current as Text, patch.value)
21
+ break
22
+ case PatchType.RemoveChild:
23
+ PatchFunctions.removeChild($Current as HTMLElement, patch.index)
24
+ break
25
+ case PatchType.Add:
26
+ PatchFunctions.add($Current as HTMLElement, patch.nodes)
27
+ break
28
+ case PatchType.NavigateSibling:
29
+ $Current = ($Current.parentNode as HTMLElement).childNodes[patch.index]
30
+ break
31
+ case PatchType.NavigateChild:
32
+ $Current = ($Current as HTMLElement).childNodes[patch.index]
33
+ break
34
+ case PatchType.NavigateParent:
35
+ $Current = $Current.parentNode as HTMLElement
36
+ break
37
+ default:
38
+ break
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,5 @@
1
+ export interface AttributePatch {
2
+ readonly type: 3
3
+ readonly key: string
4
+ readonly value: any
5
+ }
@@ -1,3 +1,4 @@
1
+ export { applyPatch } from '../ApplyPatch/ApplyPatch.ts'
1
2
  export {
2
3
  getComponentUid,
3
4
  getComponentUidFromEvent,
@@ -0,0 +1,4 @@
1
+ export type NavigateChildPatch = {
2
+ readonly type: 7
3
+ readonly index: number
4
+ }
@@ -0,0 +1,3 @@
1
+ export type NavigateParentPatch = {
2
+ readonly type: 8
3
+ }
@@ -0,0 +1,4 @@
1
+ export type NavigateSiblingPatch = {
2
+ readonly type: 10
3
+ readonly index: number
4
+ }
@@ -0,0 +1,22 @@
1
+ import type { AddPatch } from '../AddPatch/AddPatch.ts'
2
+ import type { AttributePatch } from '../AttributePatch/AttributePatch.ts'
3
+ import type { NavigateChildPatch } from '../NavigateChildPatch/NavigateChildPatch.ts'
4
+ import type { NavigateParentPatch } from '../NavigateParentPatch/NavigateParentPatch.ts'
5
+ import type { NavigateSiblingPatch } from '../NavigateSiblingPatch/NavigateSiblingPatch.ts'
6
+ import type { RemoveAttributePatch } from '../RemoveAttributePatch/RemoveAttributePatch.ts'
7
+ import type { RemoveChildPatch } from '../RemoveChildPatch/RemoveChildPatch.ts'
8
+ import type { RemovePatch } from '../RemovePatch/RemovePatch.ts'
9
+ import type { ReplacePatch } from '../ReplacePatch/ReplacePatch.ts'
10
+ import type { TextPatch } from '../TextPatch/TextPatch.ts'
11
+
12
+ export type Patch =
13
+ | TextPatch
14
+ | AttributePatch
15
+ | ReplacePatch
16
+ | RemoveAttributePatch
17
+ | RemovePatch
18
+ | AddPatch
19
+ | NavigateChildPatch
20
+ | NavigateParentPatch
21
+ | RemoveChildPatch
22
+ | NavigateSiblingPatch
@@ -0,0 +1,33 @@
1
+ import * as RenderInternal from '../RenderInternal/RenderInternal.ts'
2
+ import { VirtualDomNode } from '../VirtualDomNode/VirtualDomNode.ts'
3
+
4
+ export const setAttribute = (
5
+ $Element: HTMLElement,
6
+ key: string,
7
+ value: any,
8
+ ): void => {
9
+ $Element.setAttribute(key, value)
10
+ }
11
+
12
+ export const removeAttribute = ($Element: HTMLElement, key: string): void => {
13
+ $Element.removeAttribute(key)
14
+ }
15
+
16
+ export const setText = ($Element: Text, value: string): void => {
17
+ $Element.nodeValue = value
18
+ }
19
+
20
+ export const removeChild = ($Element: HTMLElement, index: number): void => {
21
+ console.log('el', $Element.className)
22
+ console.log('index', index)
23
+ console.log('ch', $Element.children.length)
24
+ const $Child = $Element.children[index]
25
+ $Child.remove()
26
+ }
27
+
28
+ export const add = (
29
+ $Element: HTMLElement,
30
+ nodes: readonly VirtualDomNode[],
31
+ ): void => {
32
+ RenderInternal.renderInternal($Element, nodes, {}, {})
33
+ }
@@ -0,0 +1,10 @@
1
+ export const SetText = 1
2
+ export const Replace = 2
3
+ export const SetAttribute = 3
4
+ export const RemoveAttribute = 4
5
+ export const Remove = 5
6
+ export const Add = 6
7
+ export const NavigateChild = 7
8
+ export const NavigateParent = 8
9
+ export const RemoveChild = 9
10
+ export const NavigateSibling = 10
@@ -0,0 +1,5 @@
1
+ export interface RemoveAttributePatch {
2
+ readonly type: 4
3
+ readonly index: number
4
+ readonly key: string
5
+ }
@@ -0,0 +1,4 @@
1
+ export interface RemoveChildPatch {
2
+ readonly type: 9
3
+ readonly index: number
4
+ }
@@ -0,0 +1,4 @@
1
+ export interface RemovePatch {
2
+ readonly type: 5
3
+ readonly index: number
4
+ }
@@ -2,7 +2,7 @@ import * as VirtualDomElement from '../VirtualDomElement/VirtualDomElement.ts'
2
2
 
3
3
  export const renderInternal = (
4
4
  $Parent: HTMLElement,
5
- elements: any[],
5
+ elements: readonly any[],
6
6
  eventMap: any,
7
7
  newEventMap?: any,
8
8
  ) => {
@@ -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,4 @@
1
+ export interface TextPatch {
2
+ readonly type: 1
3
+ readonly value: string
4
+ }
@@ -0,0 +1,4 @@
1
+ export interface VirtualDomNode {
2
+ readonly type: number
3
+ readonly [key: string]: any
4
+ }