lithent 0.1.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +202 -0
  3. package/dist/diff.d.ts +14 -0
  4. package/dist/hook/mountCallback.d.ts +3 -0
  5. package/dist/hook/ref.d.ts +3 -0
  6. package/dist/hook/unmount.d.ts +3 -0
  7. package/dist/hook/updateCallback.d.ts +2 -0
  8. package/dist/hook/useUpdate.d.ts +3 -0
  9. package/dist/index.d.ts +29 -0
  10. package/dist/lithent.mjs +867 -0
  11. package/dist/lithent.mjs.map +1 -0
  12. package/dist/lithent.umd.js +2 -0
  13. package/dist/lithent.umd.js.map +1 -0
  14. package/dist/render.d.ts +15 -0
  15. package/dist/types/index.d.ts +65 -0
  16. package/dist/utils/index.d.ts +3 -0
  17. package/dist/utils/predicator.d.ts +36 -0
  18. package/dist/utils/universalRef.d.ts +59 -0
  19. package/dist/wDom.d.ts +17 -0
  20. package/examples/dist/assets/index.e76a0ef8.js +2 -0
  21. package/examples/dist/assets/index.e76a0ef8.js.map +1 -0
  22. package/examples/dist/index.html +13 -0
  23. package/examples/package.json +33 -0
  24. package/examples/src/index.tsx +109 -0
  25. package/helper/dist/hook/computed.d.ts +4 -0
  26. package/helper/dist/hook/effect.d.ts +1 -0
  27. package/helper/dist/hook/state.d.ts +4 -0
  28. package/helper/dist/hook/store.d.ts +8 -0
  29. package/helper/dist/index.d.ts +4 -0
  30. package/helper/dist/lithentHelper.mjs +56 -0
  31. package/helper/dist/lithentHelper.mjs.map +1 -0
  32. package/helper/dist/lithentHelper.umd.js +2 -0
  33. package/helper/dist/lithentHelper.umd.js.map +1 -0
  34. package/helper/package.json +32 -0
  35. package/helper/src/hook/computed.ts +23 -0
  36. package/helper/src/hook/effect.ts +20 -0
  37. package/helper/src/hook/state.ts +26 -0
  38. package/helper/src/hook/store.ts +25 -0
  39. package/helper/src/index.ts +4 -0
  40. package/package.json +90 -0
  41. package/router/dist/Router.d.ts +4 -0
  42. package/router/dist/Router.mjs +56 -0
  43. package/router/dist/Router.mjs.map +1 -0
  44. package/router/dist/Router.umd.js +2 -0
  45. package/router/dist/Router.umd.js.map +1 -0
  46. package/router/dist/hook/params.d.ts +2 -0
  47. package/router/package.json +37 -0
  48. package/router/src/Router.tsx +63 -0
  49. package/router/src/components/Main.tsx +14 -0
  50. package/router/src/components/Sub.tsx +7 -0
  51. package/router/src/components/Sub2.tsx +17 -0
  52. package/router/src/example.tsx +19 -0
  53. package/router/src/hook/params.ts +9 -0
  54. package/src/diff.ts +235 -0
  55. package/src/examples/callback.tsx +81 -0
  56. package/src/examples/insert.tsx +36 -0
  57. package/src/examples/loop.tsx +33 -0
  58. package/src/examples/nested.tsx +44 -0
  59. package/src/examples/renew.tsx +29 -0
  60. package/src/examples/selectbox.tsx +42 -0
  61. package/src/examples/textarea.tsx +24 -0
  62. package/src/hook/mountCallback.ts +41 -0
  63. package/src/hook/ref.ts +1 -0
  64. package/src/hook/unmount.ts +26 -0
  65. package/src/hook/updateCallback.ts +29 -0
  66. package/src/hook/useUpdate.ts +51 -0
  67. package/src/index.ts +50 -0
  68. package/src/render.ts +409 -0
  69. package/src/types/index.ts +107 -0
  70. package/src/utils/index.ts +52 -0
  71. package/src/utils/predicator.ts +119 -0
  72. package/src/utils/universalRef.ts +137 -0
  73. package/src/wDom.ts +284 -0
@@ -0,0 +1,867 @@
1
+ const checkCustemComponentFunction = (target) => typeof target === "function" && !checkFragmentFunction(target) || typeof target === "object" && "resolve" in target;
2
+ const checkFragmentFunction = (target) => typeof target === "function" && target === Fragment;
3
+ const checkPlainWDomType = (wDom) => typeof wDom === "object" && !("resolve" in wDom);
4
+ const checkPlainType = (wDom, typeName) => checkPlainWDomType(wDom) && wDom.type === typeName;
5
+ const checkEmptyElement = (wDom) => checkPlainWDomType(wDom) && !wDom.type;
6
+ const checkSameCustomComponent = ({
7
+ originalWDom,
8
+ newWDom
9
+ }) => newWDom.constructor === (originalWDom == null ? void 0 : originalWDom.constructor);
10
+ const checkSameFragment = ({ originalWDom, newWDom }) => {
11
+ var _a, _b;
12
+ return checkPlainWDomType(newWDom) && (originalWDom == null ? void 0 : originalWDom.type) === "fragment" && ((_a = originalWDom == null ? void 0 : originalWDom.children) == null ? void 0 : _a.length) === ((_b = newWDom == null ? void 0 : newWDom.children) == null ? void 0 : _b.length);
13
+ };
14
+ const checkSameTagElement = ({ originalWDom, newWDom }) => checkPlainWDomType(newWDom) && (originalWDom == null ? void 0 : originalWDom.type) === "element" && (originalWDom == null ? void 0 : originalWDom.tag) === newWDom.tag;
15
+ const checkNormalTypeElement = ({ originalWDom, newWDom }) => checkPlainWDomType(newWDom) && (originalWDom == null ? void 0 : originalWDom.type) === newWDom.type;
16
+ const checkExisty = (value) => value !== null && value !== void 0;
17
+ const checkFunction = (target) => typeof target === "function";
18
+ const checkStyleData = (dataKey, dataValue) => dataKey === "style" && typeof dataValue === "object";
19
+ const checkRefData = (dataKey, dataValue) => dataKey === "ref" && typeof dataValue === "object";
20
+ const checkOptionElement = (element) => {
21
+ return element.nodeType === 1 && element.tagName === "OPTION";
22
+ };
23
+ const checkNormalAttribute = (dataValue) => typeof dataValue === "number" || typeof dataValue === "string";
24
+ const getWDomType = (wDom) => {
25
+ let result;
26
+ if (checkCustemComponentFunction(wDom)) {
27
+ result = "c";
28
+ } else if (checkPlainType(wDom, "fragment")) {
29
+ result = "f";
30
+ } else if (checkPlainType(wDom, "element")) {
31
+ result = "e";
32
+ } else if (checkPlainType(wDom, "loop")) {
33
+ result = "l";
34
+ } else if (checkPlainType(wDom, "text")) {
35
+ result = "t";
36
+ } else if (checkEmptyElement(wDom)) {
37
+ result = "et";
38
+ }
39
+ return result;
40
+ };
41
+ const checkSameWDomWithOriginal = {
42
+ c: checkSameCustomComponent,
43
+ l: checkNormalTypeElement,
44
+ t: checkNormalTypeElement,
45
+ e: checkSameTagElement,
46
+ f: checkSameFragment,
47
+ et: checkNormalTypeElement
48
+ };
49
+ const getParent = (vDom) => {
50
+ const parentVDom = vDom.getParent && vDom.getParent();
51
+ if (!parentVDom) {
52
+ throw Error("Not found parent vDom");
53
+ }
54
+ return parentVDom;
55
+ };
56
+ const reRender = (vDom, infoVdom) => {
57
+ const { componentProps: props, componentChildren: children } = vDom;
58
+ const { props: infoProps, children: infoChidren } = infoVdom;
59
+ if (props) {
60
+ updateProps$1(props, infoProps);
61
+ }
62
+ if (children) {
63
+ updateChildren(children, infoChidren);
64
+ }
65
+ const newVDom = vDom.reRender && vDom.reRender();
66
+ if (!newVDom) {
67
+ throw Error("Unable reRender");
68
+ }
69
+ return newVDom;
70
+ };
71
+ const updateProps$1 = (props, infoProps) => {
72
+ if (props && infoProps !== props) {
73
+ Object.keys(props).forEach((key) => delete props[key]);
74
+ Object.entries(infoProps || {}).forEach(
75
+ ([key, value]) => props[key] = value
76
+ );
77
+ }
78
+ };
79
+ const updateChildren = (children, infoChidren) => {
80
+ if (children && infoChidren !== children) {
81
+ children.splice(0, children.length);
82
+ if (infoChidren) {
83
+ infoChidren.forEach((childrenItem) => children.push(childrenItem));
84
+ }
85
+ }
86
+ };
87
+ const componentKeyRef = { value: {} };
88
+ const needDiffRef = { value: false };
89
+ const componentRef = /* @__PURE__ */ new WeakMap();
90
+ const redrawQueue = { value: [] };
91
+ const redrawQueueTimeout = { value: null };
92
+ const nodeChildKeyList = { value: [] };
93
+ const pushNodeChildKey = (key) => nodeChildKeyList.value.forEach((item) => item.value.push(key));
94
+ const removeNodeChildKey = (item) => {
95
+ nodeChildKeyList.value.splice(nodeChildKeyList.value.indexOf(item), 1);
96
+ };
97
+ const cleanNodeChildKey = () => nodeChildKeyList.value = [];
98
+ const dataStoreStore = {};
99
+ const dataStoreRenderQueue = {};
100
+ const componentRender = (componentKey) => () => {
101
+ var _a;
102
+ const up = (_a = componentRef.get(componentKey)) == null ? void 0 : _a.up;
103
+ if (up) {
104
+ up();
105
+ return true;
106
+ } else {
107
+ return false;
108
+ }
109
+ };
110
+ const setComponetRef = (componentKey) => {
111
+ componentRef.set(componentKey, {
112
+ up: () => {
113
+ },
114
+ upR: [],
115
+ upS: { value: 0 },
116
+ upD: [],
117
+ upCB: [],
118
+ mts: [],
119
+ umts: []
120
+ });
121
+ };
122
+ const getComponentKey = () => componentKeyRef.value;
123
+ const setRedrawAction = ({
124
+ componentKey,
125
+ nodeChildKey,
126
+ exec
127
+ }) => {
128
+ componentRef.get(componentKey).up = () => {
129
+ redrawQueue.value.push({
130
+ componentKey,
131
+ nodeChildKey: nodeChildKey.value,
132
+ exec
133
+ });
134
+ if (!redrawQueueTimeout.value) {
135
+ redrawQueueTimeout.value = setTimeout(execRedrawQueue);
136
+ }
137
+ };
138
+ };
139
+ const initUpdateHookState = (componentKey) => componentKeyRef.value = componentKey;
140
+ const initMountHookState = (componentKey) => {
141
+ componentKeyRef.value = componentKey;
142
+ setComponetRef(componentKey);
143
+ };
144
+ const execRedrawQueue = () => {
145
+ let childItemList = [];
146
+ redrawQueue.value.forEach((item) => {
147
+ childItemList = childItemList.concat(item.nodeChildKey);
148
+ });
149
+ const addedKey = [];
150
+ const result = redrawQueue.value.reduce((acc, item) => {
151
+ const { componentKey } = item;
152
+ if (childItemList.includes(componentKey) || addedKey.includes(componentKey)) {
153
+ return acc;
154
+ }
155
+ addedKey.push(componentKey);
156
+ acc.push(item);
157
+ return acc;
158
+ }, []);
159
+ while (result.length) {
160
+ const action = result.shift();
161
+ if (action) {
162
+ action.exec();
163
+ }
164
+ }
165
+ };
166
+ const unmount = (effectAction) => {
167
+ componentRef.get(getComponentKey()).umts.push(effectAction);
168
+ };
169
+ const runUnmountQueueFromWDom = (newWDom) => {
170
+ const { componentKey, nodeChildKey } = newWDom;
171
+ const childKeys = nodeChildKey == null ? void 0 : nodeChildKey.value;
172
+ if (componentKey) {
173
+ removeItem(componentKey);
174
+ }
175
+ if (childKeys) {
176
+ childKeys.forEach((childKey) => removeItem(childKey));
177
+ }
178
+ };
179
+ const removeItem = (componentKey) => {
180
+ const queue = componentRef.get(componentKey).umts;
181
+ componentRef.get(componentKey).umts = [];
182
+ queue.forEach((effect) => effect());
183
+ componentRef.delete(componentKey);
184
+ };
185
+ const mountCallback = (effectAction) => {
186
+ const mts = componentRef.get(getComponentKey()).mts;
187
+ mts.push(effectAction);
188
+ };
189
+ const runMountedQueueFromWDom = (newWDom) => {
190
+ const { componentKey } = newWDom;
191
+ if (componentKey) {
192
+ const component = componentRef.get(componentKey);
193
+ const queue = component == null ? void 0 : component.mts;
194
+ const sequence = component == null ? void 0 : component.upS;
195
+ componentKeyRef.value = componentKey;
196
+ if (sequence) {
197
+ sequence.value = 0;
198
+ }
199
+ if (queue) {
200
+ component.mts = [];
201
+ queue.forEach((effect) => {
202
+ const callback = effect();
203
+ if (callback) {
204
+ unmount(callback);
205
+ }
206
+ });
207
+ }
208
+ }
209
+ };
210
+ const useUpdated = (effectAction, dependencies = () => []) => {
211
+ const component = componentRef.get(getComponentKey());
212
+ const { upD, upS } = component;
213
+ const def = upD[upS.value];
214
+ if (def && checkNeedPushQueue(def, dependencies())) {
215
+ const callback = effectAction();
216
+ if (callback) {
217
+ component.upCB.push(callback);
218
+ }
219
+ }
220
+ upD[upS.value] = dependencies();
221
+ upS.value += 1;
222
+ };
223
+ const runUpdatedQueueFromWDom = (newWDom) => {
224
+ const { componentKey } = newWDom;
225
+ if (componentKey) {
226
+ const component = componentRef.get(componentKey);
227
+ const queue = component == null ? void 0 : component.upCB;
228
+ const sequence = component == null ? void 0 : component.upS;
229
+ componentKeyRef.value = componentKey;
230
+ if (sequence) {
231
+ sequence.value = 0;
232
+ }
233
+ if (newWDom.constructor && queue) {
234
+ component.upCB = [];
235
+ queue.forEach((effect) => effect());
236
+ }
237
+ }
238
+ };
239
+ const checkNeedPushQueue = (originalDefs, newDefs) => !originalDefs.length && !originalDefs.length ? true : originalDefs.some((def, index) => def !== newDefs[index]);
240
+ const render = (wDom, wrapElement, afterElement) => {
241
+ if (!wrapElement) {
242
+ throw Error("WrapELement is null");
243
+ }
244
+ wDom.isRoot = true;
245
+ wDom.wrapElement = wrapElement;
246
+ const Dom = wDomToDom(wDom, true);
247
+ if (afterElement) {
248
+ wrapElement.insertBefore(Dom, afterElement);
249
+ } else {
250
+ wrapElement.appendChild(Dom);
251
+ }
252
+ };
253
+ const wDomUpdate = (newWDomTree) => {
254
+ const { needRerender } = newWDomTree;
255
+ if (needRerender && needRerender !== "N") {
256
+ const exec = {
257
+ A: typeAdd,
258
+ D: typeDelete,
259
+ R: typeReplace,
260
+ U: typeUpdate,
261
+ SR: typeSortedReplace,
262
+ SU: typeSortedUpdate
263
+ }[needRerender];
264
+ exec(newWDomTree);
265
+ }
266
+ };
267
+ const recursiveRemoveEvent = (originalWDom) => {
268
+ if (originalWDom.props && originalWDom.el) {
269
+ removeEvent(originalWDom.props, originalWDom.el);
270
+ }
271
+ (originalWDom.children || []).forEach((childItem) => {
272
+ recursiveRemoveEvent(childItem);
273
+ });
274
+ };
275
+ const typeDelete = (newWDom) => {
276
+ var _a, _b;
277
+ const parent = findRealParentElement(newWDom);
278
+ if (newWDom.oldProps && newWDom.el) {
279
+ removeEvent(newWDom.oldProps, newWDom.el);
280
+ }
281
+ if (parent && newWDom.el) {
282
+ if (((_a = newWDom.el) == null ? void 0 : _a.nodeType) === 1) {
283
+ parent.removeChild(newWDom.el);
284
+ } else if (((_b = newWDom.el) == null ? void 0 : _b.nodeType) === 11) {
285
+ findChildWithRemoveElement(newWDom, parent);
286
+ }
287
+ delete newWDom.el;
288
+ }
289
+ };
290
+ const findChildWithRemoveElement = (newWDom, parent) => {
291
+ ((newWDom == null ? void 0 : newWDom.children) || []).forEach((item) => {
292
+ var _a, _b;
293
+ if (((_a = item.el) == null ? void 0 : _a.nodeType) === 1) {
294
+ parent.removeChild(item == null ? void 0 : item.el);
295
+ } else if (((_b = item.el) == null ? void 0 : _b.nodeType) === 11) {
296
+ findChildWithRemoveElement(item, parent);
297
+ }
298
+ });
299
+ };
300
+ const typeSortedReplace = (newWDom) => {
301
+ typeDelete(newWDom);
302
+ typeAdd(newWDom);
303
+ };
304
+ const typeSortedUpdate = (newWDom) => {
305
+ typeDelete(newWDom);
306
+ typeAdd(newWDom, newWDom.el);
307
+ };
308
+ const typeAdd = (newWDom, newElement) => {
309
+ if (!newElement) {
310
+ newElement = wDomToDom(newWDom, true);
311
+ }
312
+ const parentWDom = getParent(newWDom);
313
+ if (parentWDom.type) {
314
+ const parentEl = findRealParentElement(parentWDom);
315
+ const nextEl = startFindNextBrotherElement(newWDom, parentWDom);
316
+ if (newElement && parentEl) {
317
+ if (nextEl) {
318
+ parentEl.insertBefore(newElement, nextEl);
319
+ } else {
320
+ parentEl.appendChild(newElement);
321
+ }
322
+ }
323
+ }
324
+ runMountedQueueFromWDom(newWDom);
325
+ };
326
+ const startFindNextBrotherElement = (wDom, parentWDom) => {
327
+ const brothers = parentWDom.children || [];
328
+ const index = brothers.indexOf(wDom);
329
+ const nextIndex = index + 1;
330
+ const candidiateBrothers = brothers.slice(nextIndex);
331
+ const finedNextEl = findChildFragmentNextElement(candidiateBrothers);
332
+ const parentType = parentWDom.type || "";
333
+ if (finedNextEl) {
334
+ return finedNextEl;
335
+ }
336
+ if (!parentWDom.isRoot && ["fragment", "loop"].includes(parentType)) {
337
+ return startFindNextBrotherElement(parentWDom, getParent(parentWDom));
338
+ }
339
+ return void 0;
340
+ };
341
+ const findChildFragmentNextElement = (candidiateBrothers) => candidiateBrothers.reduce(
342
+ (targetEl, bItem) => {
343
+ const type = bItem.type;
344
+ const el = bItem.el;
345
+ const isFragment = type && ["fragment", "loop"].includes(type);
346
+ if (targetEl) {
347
+ return targetEl;
348
+ } else if (isFragment) {
349
+ return findChildFragmentNextElement(bItem.children || []);
350
+ } else if (el && el.nodeType !== 11) {
351
+ return el;
352
+ }
353
+ return targetEl;
354
+ },
355
+ void 0
356
+ );
357
+ const typeReplace = (newWDom) => {
358
+ const parentWDom = getParent(newWDom);
359
+ const orignalElement = newWDom.el;
360
+ if (parentWDom.type && orignalElement) {
361
+ const parentElement = parentWDom.el;
362
+ const newElement = wDomToDom(newWDom, true);
363
+ if (newWDom.oldProps) {
364
+ removeEvent(newWDom.oldProps, orignalElement);
365
+ }
366
+ if (parentElement) {
367
+ parentElement.replaceChild(newElement, orignalElement);
368
+ }
369
+ }
370
+ runMountedQueueFromWDom(newWDom);
371
+ };
372
+ const removeEvent = (oldProps, element) => {
373
+ if (element) {
374
+ Object.entries(oldProps || {}).forEach(
375
+ ([dataKey, dataValue]) => {
376
+ if (dataKey.match(/^on/)) {
377
+ element.removeEventListener(dataKey, dataValue);
378
+ }
379
+ }
380
+ );
381
+ }
382
+ };
383
+ const typeUpdate = (newWDom) => {
384
+ var _a;
385
+ const element = newWDom.el;
386
+ if (newWDom.type === "text") {
387
+ updateText(newWDom);
388
+ return;
389
+ }
390
+ if (element) {
391
+ const { oldProps, props } = newWDom;
392
+ updateProps({ oldProps, props, element });
393
+ delete newWDom.oldProps;
394
+ if (newWDom.tag === "input") {
395
+ element.value = String(((_a = newWDom == null ? void 0 : newWDom.props) == null ? void 0 : _a.value) || "");
396
+ }
397
+ }
398
+ (newWDom.children || []).forEach((childItem) => wDomUpdate(childItem));
399
+ runUpdatedQueueFromWDom(newWDom);
400
+ };
401
+ const updateText = (newWDom) => {
402
+ const element = newWDom.el;
403
+ if (element) {
404
+ element.nodeValue = String(newWDom.text);
405
+ }
406
+ };
407
+ const updateProps = ({
408
+ oldProps,
409
+ props,
410
+ element
411
+ }) => {
412
+ const originalProps = { ...oldProps };
413
+ Object.entries(props || {}).forEach(
414
+ ([dataKey, dataValue]) => {
415
+ if (dataKey === "key")
416
+ ;
417
+ else if (checkStyleData(dataKey, dataValue)) {
418
+ const style = dataValue;
419
+ const oldStyle = checkStyleData(dataKey, originalProps.style) || {};
420
+ updateStyle({ style, oldStyle, element });
421
+ } else if (checkRefData(dataKey, dataValue)) {
422
+ dataValue.value = element;
423
+ } else if (dataKey.match(/^on/)) {
424
+ updateEvent({
425
+ element,
426
+ eventKey: dataKey,
427
+ newEventHandler: dataValue,
428
+ oldEventHandler: originalProps[dataKey]
429
+ });
430
+ } else if (checkNormalAttribute(dataValue)) {
431
+ element.setAttribute(dataKey, String(dataValue));
432
+ } else if (checkOptionElement(element) && dataKey === "selected") {
433
+ element.selected = !!dataValue;
434
+ }
435
+ delete originalProps[dataKey];
436
+ }
437
+ );
438
+ Object.keys(originalProps).forEach(
439
+ (dataKey) => element.removeAttribute(dataKey)
440
+ );
441
+ };
442
+ const wDomToDom = (wDom, init) => {
443
+ let element;
444
+ const { type, tag, text, props, children = [] } = wDom;
445
+ const isVirtualType = type === "fragment" || type === "loop";
446
+ if (isVirtualType) {
447
+ element = new DocumentFragment();
448
+ } else if (type === "element" && tag) {
449
+ element = document.createElement(tag);
450
+ } else if (type === "text" && checkExisty(text)) {
451
+ element = document.createTextNode(String(text));
452
+ }
453
+ wDomChildrenToDom(children, element, init);
454
+ updateProps({ props, element });
455
+ wDom.el = element;
456
+ runMountedQueueFromWDom(wDom);
457
+ return element || document.createElement("div");
458
+ };
459
+ const wDomChildrenToDom = (children, parentElement, init) => {
460
+ if (init) {
461
+ const elementChildren = children.reduce(
462
+ (acc, childItem) => {
463
+ if (childItem.type) {
464
+ acc.appendChild(wDomToDom(childItem, init));
465
+ }
466
+ return acc;
467
+ },
468
+ new DocumentFragment()
469
+ );
470
+ if (parentElement && elementChildren.hasChildNodes()) {
471
+ parentElement.appendChild(elementChildren);
472
+ }
473
+ }
474
+ };
475
+ const updateEvent = ({
476
+ element,
477
+ eventKey,
478
+ newEventHandler,
479
+ oldEventHandler
480
+ }) => {
481
+ const eventName = eventKey.replace(
482
+ /^on(.*)/,
483
+ (_match, p1) => p1.toLowerCase()
484
+ );
485
+ if (oldEventHandler !== newEventHandler) {
486
+ if (oldEventHandler) {
487
+ element.removeEventListener(eventName, oldEventHandler);
488
+ }
489
+ if (newEventHandler) {
490
+ element.addEventListener(eventName, newEventHandler);
491
+ }
492
+ }
493
+ };
494
+ const updateStyle = ({
495
+ style,
496
+ oldStyle,
497
+ element
498
+ }) => {
499
+ const originalStyle = { ...oldStyle };
500
+ const elementStyle = element == null ? void 0 : element.style;
501
+ if (elementStyle) {
502
+ Object.entries(style).forEach(([styleKey, dataValue]) => {
503
+ elementStyle.setProperty(styleKey, dataValue);
504
+ delete originalStyle[styleKey];
505
+ });
506
+ Object.entries(originalStyle).forEach(
507
+ ([styleKey]) => elementStyle.removeProperty(styleKey)
508
+ );
509
+ }
510
+ };
511
+ const findRealParentElement = (vDom) => {
512
+ var _a, _b;
513
+ const isVirtualType = vDom.type === "fragment" || vDom.type === "loop" || !vDom.type && ((_a = vDom == null ? void 0 : vDom.el) == null ? void 0 : _a.nodeType) === 11;
514
+ if (vDom.isRoot && isVirtualType) {
515
+ return vDom.wrapElement;
516
+ }
517
+ if (!isVirtualType) {
518
+ return (_b = vDom.el) == null ? void 0 : _b.parentNode;
519
+ }
520
+ const parentVDom = getParent(vDom);
521
+ return findRealParentElement(parentVDom);
522
+ };
523
+ const makeNewWDomTree = ({
524
+ originalWDom,
525
+ newWDom
526
+ }) => {
527
+ const type = getWDomType(newWDom);
528
+ if (!type) {
529
+ throw new Error("Unknown type wdom");
530
+ }
531
+ const isSameType = checkSameWDomWithOriginal[type]({ originalWDom, newWDom });
532
+ const result = remakeNewWDom({ originalWDom, newWDom, isSameType });
533
+ if (result == null ? void 0 : result.nodeChildKey) {
534
+ removeNodeChildKey(result.nodeChildKey);
535
+ }
536
+ return result;
537
+ };
538
+ const remakeNewWDom = ({
539
+ newWDom,
540
+ originalWDom,
541
+ isSameType
542
+ }) => {
543
+ const remakeWDom = generalize({ newWDom, originalWDom, isSameType });
544
+ remakeWDom.children = remakeChildrenForDiff({
545
+ isSameType,
546
+ newWDom: remakeWDom,
547
+ originalWDom
548
+ });
549
+ const needRerender = addReRenderTypeProperty({
550
+ originalWDom,
551
+ newWDom: remakeWDom,
552
+ isSameType
553
+ });
554
+ remakeWDom.needRerender = needRerender;
555
+ if (needRerender !== "A" && originalWDom) {
556
+ remakeWDom.el = originalWDom.el;
557
+ }
558
+ if (needRerender && ["D", "R", "SR"].includes(needRerender) && (originalWDom == null ? void 0 : originalWDom.componentKey)) {
559
+ runUnmountQueueFromWDom(originalWDom);
560
+ recursiveRemoveEvent(originalWDom);
561
+ remakeWDom.children = originalWDom.children;
562
+ }
563
+ remakeWDom.oldProps = originalWDom == null ? void 0 : originalWDom.props;
564
+ return remakeWDom;
565
+ };
566
+ const addReRenderTypeProperty = ({
567
+ originalWDom,
568
+ newWDom,
569
+ isSameType
570
+ }) => {
571
+ const existOriginalWDom = originalWDom && originalWDom.type;
572
+ const isEmptyElement = checkEmptyElement(newWDom);
573
+ const isRoot = !newWDom.getParent;
574
+ const parentType = !isRoot && getParent(newWDom).type;
575
+ const key = getKey(newWDom);
576
+ const isKeyCheckedWDom = parentType === "loop" && checkExisty(key);
577
+ const isSameText = newWDom.type === "text" && isSameType && newWDom.text === (originalWDom == null ? void 0 : originalWDom.text);
578
+ let result;
579
+ if (isEmptyElement) {
580
+ result = "D";
581
+ } else if (isSameText) {
582
+ result = "N";
583
+ } else if (!existOriginalWDom) {
584
+ result = "A";
585
+ } else if (isSameType) {
586
+ result = isKeyCheckedWDom ? "SU" : "U";
587
+ } else {
588
+ result = isKeyCheckedWDom ? "SR" : "R";
589
+ }
590
+ return result;
591
+ };
592
+ const generalize = ({
593
+ newWDom,
594
+ originalWDom,
595
+ isSameType
596
+ }) => {
597
+ if (checkCustemComponentFunction(newWDom)) {
598
+ return isSameType && originalWDom ? reRender(originalWDom, newWDom) : newWDom.resolve();
599
+ }
600
+ return newWDom;
601
+ };
602
+ const remakeChildrenForDiff = ({
603
+ isSameType,
604
+ newWDom,
605
+ originalWDom
606
+ }) => isSameType && originalWDom ? remakeChildrenForUpdate(newWDom, originalWDom) : remakeChildrenForAdd(newWDom);
607
+ const remakeChildrenForAdd = (newWDom) => (newWDom.children || []).map((item) => {
608
+ const childItem = makeNewWDomTree({ newWDom: item });
609
+ childItem.getParent = () => newWDom;
610
+ return childItem;
611
+ });
612
+ const remakeChildrenForUpdate = (newWDom, originalWDom) => {
613
+ if (newWDom.type === "loop" && checkExisty(getKey((newWDom.children || [])[0]))) {
614
+ return remakeChildrenForLoopUpdate(newWDom, originalWDom);
615
+ }
616
+ return (newWDom.children || []).map((item, index) => {
617
+ const childItem = makeNewWDomTree({
618
+ newWDom: item,
619
+ originalWDom: (originalWDom.children || [])[index]
620
+ });
621
+ childItem.getParent = () => newWDom;
622
+ return childItem;
623
+ });
624
+ };
625
+ const remakeChildrenForLoopUpdate = (newWDom, originalWDom) => {
626
+ const { remakedChildren, unUsedChildren } = diffLoopChildren(
627
+ newWDom,
628
+ originalWDom
629
+ );
630
+ unUsedChildren.map((unusedItem) => {
631
+ const el = unusedItem.el;
632
+ if (el && el.parentNode) {
633
+ el.parentNode.removeChild(el);
634
+ }
635
+ });
636
+ return remakedChildren;
637
+ };
638
+ const diffLoopChildren = (newWDom, originalWDom) => {
639
+ const newChildren = [...newWDom.children || []];
640
+ const originalChildren = [...originalWDom.children || []];
641
+ const remakedChildren = newChildren.map((item) => {
642
+ const originalItem = findSameKeyOriginalItem(item, originalChildren);
643
+ const childItem = makeNewWDomTree({
644
+ newWDom: item,
645
+ originalWDom: originalItem
646
+ });
647
+ if (originalItem) {
648
+ originalChildren.splice(originalChildren.indexOf(originalItem), 1);
649
+ }
650
+ childItem.getParent = () => newWDom;
651
+ return childItem;
652
+ });
653
+ return {
654
+ remakedChildren,
655
+ unUsedChildren: originalChildren
656
+ };
657
+ };
658
+ const findSameKeyOriginalItem = (item, originalChildren) => {
659
+ const key = getKey(item);
660
+ return originalChildren.find(
661
+ (orignalChildItem) => getKey(orignalChildItem) === key
662
+ );
663
+ };
664
+ const getKey = (target) => {
665
+ var _a, _b, _c;
666
+ return (_c = (_a = target == null ? void 0 : target.componentProps) == null ? void 0 : _a.key) != null ? _c : (_b = target == null ? void 0 : target.props) == null ? void 0 : _b.key;
667
+ };
668
+ const updateCallback = (effectAction, dependencies = () => []) => {
669
+ const updateReqs = componentRef.get(getComponentKey()).upR;
670
+ updateReqs.push(() => useUpdated(effectAction, dependencies));
671
+ useUpdated(effectAction, dependencies);
672
+ };
673
+ const runUpdateCallback = () => {
674
+ var _a;
675
+ const updateReqs = (_a = componentRef.get(getComponentKey())) == null ? void 0 : _a.upR;
676
+ if (updateReqs == null ? void 0 : updateReqs.length) {
677
+ updateReqs.forEach((callback) => callback());
678
+ }
679
+ redrawQueue.value = [];
680
+ redrawQueueTimeout.value = null;
681
+ };
682
+ const Fragment = (_props, ...children) => ({
683
+ type: "fragment",
684
+ children
685
+ });
686
+ const h = (tag, props, ...children) => {
687
+ const nodeParentPointer = { value: void 0 };
688
+ const newProps = props || {};
689
+ const newChildren = remakeChildren(nodeParentPointer, children);
690
+ const node = makeNode({
691
+ tag,
692
+ props: newProps,
693
+ children: newChildren
694
+ });
695
+ if (!checkCustemComponentFunction(node)) {
696
+ nodeParentPointer.value = node;
697
+ }
698
+ return node;
699
+ };
700
+ const mount = (component) => (_props, _children) => component;
701
+ const reRenderCustomComponent = ({
702
+ tag,
703
+ props,
704
+ children,
705
+ originalWDom
706
+ }) => {
707
+ var _a;
708
+ needDiffRef.value = true;
709
+ cleanNodeChildKey();
710
+ const newWDom = makeWDomResolver({ tag, props, children });
711
+ const newWDomTree = makeNewWDomTree({ originalWDom, newWDom });
712
+ const { isRoot, getParent: getParent2, wrapElement } = originalWDom;
713
+ newWDomTree.getParent = getParent2;
714
+ if (!isRoot && getParent2) {
715
+ const brothers = ((_a = getParent2()) == null ? void 0 : _a.children) || [];
716
+ const index = brothers.indexOf(originalWDom);
717
+ brothers.splice(index, 1, newWDomTree);
718
+ } else {
719
+ newWDomTree.isRoot = true;
720
+ newWDomTree.wrapElement = wrapElement;
721
+ }
722
+ wDomUpdate(newWDomTree);
723
+ needDiffRef.value = false;
724
+ };
725
+ const makeWDomResolver = ({
726
+ tag,
727
+ props,
728
+ children
729
+ }) => {
730
+ const tagName = tag.name;
731
+ const constructor = tag;
732
+ const resolve = (componentKey = props) => {
733
+ initMountHookState(componentKey);
734
+ const nodeChildKey = { value: [] };
735
+ pushNodeChildKey(componentKey);
736
+ nodeChildKeyList.value.push(nodeChildKey);
737
+ const component = tag(props, children);
738
+ const componentMaker = component(
739
+ componentRender(componentKey),
740
+ props,
741
+ children
742
+ );
743
+ const customNode = makeCustomNode({
744
+ componentMaker,
745
+ componentKey,
746
+ tag,
747
+ props,
748
+ children,
749
+ nodeChildKey
750
+ });
751
+ const originalWDom = customNode;
752
+ setRedrawAction({
753
+ componentKey,
754
+ nodeChildKey,
755
+ exec: () => reRenderCustomComponent({ tag, props, children, originalWDom })
756
+ });
757
+ if (!needDiffRef.value) {
758
+ removeNodeChildKey(nodeChildKey);
759
+ }
760
+ return customNode;
761
+ };
762
+ return { tagName, constructor, props, children, resolve };
763
+ };
764
+ const makeCustomNode = (wDomInfo) => {
765
+ const { componentMaker } = wDomInfo;
766
+ const customNode = componentMaker();
767
+ const reRender2 = makeReRender(wDomInfo);
768
+ addComponentProps(customNode, { ...wDomInfo, reRender: reRender2 });
769
+ return customNode;
770
+ };
771
+ const makeReRender = (wDomInfo) => {
772
+ const reRender2 = () => wDomMaker({ ...wDomInfo, reRender: reRender2 });
773
+ return reRender2;
774
+ };
775
+ const wDomMaker = (wDomInfo) => {
776
+ const { componentMaker, componentKey, tag, props, children } = wDomInfo;
777
+ initUpdateHookState(componentKey);
778
+ runUpdateCallback();
779
+ const nodeChildKey = { value: [] };
780
+ pushNodeChildKey(componentKey);
781
+ nodeChildKeyList.value.push(nodeChildKey);
782
+ const originalWDom = componentMaker();
783
+ wDomInfo.nodeChildKey = nodeChildKey;
784
+ setRedrawAction({
785
+ componentKey,
786
+ nodeChildKey,
787
+ exec: () => reRenderCustomComponent({ tag, props, children, originalWDom })
788
+ });
789
+ addComponentProps(originalWDom, wDomInfo);
790
+ if (!needDiffRef.value) {
791
+ removeNodeChildKey(nodeChildKey);
792
+ }
793
+ return originalWDom;
794
+ };
795
+ const addComponentProps = (wDom, info) => {
796
+ const { componentKey, tag, props, children, reRender: reRender2, nodeChildKey } = info;
797
+ Object.assign(wDom, {
798
+ componentProps: props,
799
+ componentChildren: children,
800
+ constructor: tag,
801
+ tagName: tag.name,
802
+ componentKey,
803
+ nodeChildKey,
804
+ reRender: reRender2
805
+ });
806
+ };
807
+ const makeNode = ({
808
+ tag,
809
+ props,
810
+ children
811
+ }) => {
812
+ if (checkFragmentFunction(tag)) {
813
+ return Fragment(props, ...children);
814
+ } else if (checkCustemComponentFunction(tag)) {
815
+ const componetMakeResolver = makeWDomResolver({ tag, props, children });
816
+ return needDiffRef.value ? componetMakeResolver : componetMakeResolver.resolve();
817
+ }
818
+ return {
819
+ type: "element",
820
+ tag,
821
+ props,
822
+ children
823
+ };
824
+ };
825
+ const remakeChildren = (nodeParentPointer, children) => children.map((item) => {
826
+ const childItem = makeChildrenItem(item);
827
+ childItem.getParent = () => nodeParentPointer.value;
828
+ return childItem;
829
+ });
830
+ const makeChildrenItem = (item) => {
831
+ if (item === null || item === void 0 || item === false) {
832
+ return { type: null };
833
+ } else if (Array.isArray(item)) {
834
+ const nodeParentPointer = { value: void 0 };
835
+ const children = remakeChildren(nodeParentPointer, item);
836
+ const node = {
837
+ type: "loop",
838
+ children
839
+ };
840
+ nodeParentPointer.value = node;
841
+ return node;
842
+ } else if (typeof item === "string" || typeof item === "number") {
843
+ return { type: "text", text: item };
844
+ }
845
+ return item;
846
+ };
847
+ const ref = (initValue) => ({ value: initValue });
848
+ const ext = {
849
+ componentRef,
850
+ componentKeyRef,
851
+ dataStoreStore,
852
+ dataStoreRenderQueue,
853
+ checkFunction,
854
+ getComponentKey,
855
+ componentRender
856
+ };
857
+ export {
858
+ Fragment,
859
+ ext,
860
+ h,
861
+ mount,
862
+ mountCallback,
863
+ ref,
864
+ render,
865
+ updateCallback
866
+ };
867
+ //# sourceMappingURL=lithent.mjs.map