j20 0.0.19 → 0.0.21

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.js CHANGED
@@ -373,8 +373,8 @@ const getChildren = (propChildren) => {
373
373
  }
374
374
  return arr;
375
375
  };
376
- let count = 0;
377
- const generateId = () => (++count).toString(32);
376
+ let count$1 = 0;
377
+ const generateId = () => (++count$1).toString(32);
378
378
  const styleObjectToString = (style) => {
379
379
  let styleString = "";
380
380
  for (const key in style) {
@@ -439,27 +439,29 @@ const add = (node, key, newValue) => {
439
439
  }
440
440
  }
441
441
  };
442
+ const nodeAttributesEffect = (node, propsFn) => {
443
+ let oldProps = {};
444
+ effect(() => {
445
+ const newProps = { ...propsFn() };
446
+ const newKeys = Object.keys(newProps);
447
+ const oldKeys = Object.keys(oldProps);
448
+ const allKeys = new Set([...newKeys, ...oldKeys]);
449
+ for (const key of allKeys) {
450
+ oldKeys.includes(key)
451
+ ? newKeys.includes(key)
452
+ ? Object.is(oldProps[key], newProps[key])
453
+ ? null
454
+ : update(node, key, oldProps[key], newProps[key])
455
+ : unset(node, key, oldProps[key])
456
+ : add(node, key, newProps[key]);
457
+ }
458
+ oldProps = newProps;
459
+ });
460
+ };
442
461
 
443
462
  const createDom = (tag, props, children) => {
444
463
  const node = tag;
445
- let oldProps = {};
446
- props &&
447
- effect(() => {
448
- const newProps = { ...props() };
449
- const newKeys = Object.keys(newProps);
450
- const oldKeys = Object.keys(oldProps);
451
- const allKeys = new Set([...newKeys, ...oldKeys]);
452
- for (const key of allKeys) {
453
- oldKeys.includes(key)
454
- ? newKeys.includes(key)
455
- ? Object.is(oldProps[key], newProps[key])
456
- ? null
457
- : update(node, key, oldProps[key], newProps[key])
458
- : unset(node, key, oldProps[key])
459
- : add(node, key, newProps[key]);
460
- }
461
- oldProps = newProps;
462
- });
464
+ props && nodeAttributesEffect(node, props);
463
465
  // 独立 wc 初始化
464
466
  node.lazyInit?.();
465
467
  if (children &&
@@ -476,6 +478,21 @@ const createDom = (tag, props, children) => {
476
478
 
477
479
  const BRAND = "j20";
478
480
 
481
+ const isSignal = (a) => a?.SIGNAL === true;
482
+ const addStyleSheet = (node, styleSheet) => {
483
+ if (node.adoptedStyleSheets &&
484
+ !node.adoptedStyleSheets.includes(styleSheet)) {
485
+ node.adoptedStyleSheets = [...node.adoptedStyleSheets, styleSheet];
486
+ }
487
+ };
488
+ const removeStyleSheet = (node, styleSheet) => {
489
+ if (node.adoptedStyleSheets && node.adoptedStyleSheets.includes(styleSheet)) {
490
+ const newSet = new Set(node.adoptedStyleSheets);
491
+ newSet.delete(styleSheet);
492
+ node.adoptedStyleSheets = Array.from(newSet);
493
+ }
494
+ };
495
+
479
496
  const registerWebComponent = (Comp) => {
480
497
  if (!Comp.customElement) {
481
498
  throw new Error("Custom element options is not provided");
@@ -565,16 +582,13 @@ const buildClass = (Comp) => {
565
582
  this.append(...elements);
566
583
  }
567
584
  }
568
- addStyleSheet(sheet) {
569
- if (this.#shadow) {
570
- this.#shadow.adoptedStyleSheets.push(sheet);
571
- }
585
+ addStyleSheet(styleSheet) {
586
+ if (this.#shadow)
587
+ addStyleSheet(this.#shadow, styleSheet);
572
588
  }
573
- removeStyleSheet(sheet) {
574
- if (this.#shadow) {
575
- this.#shadow.adoptedStyleSheets =
576
- this.#shadow.adoptedStyleSheets.filter((s) => s !== sheet);
577
- }
589
+ removeStyleSheet(styleSheet) {
590
+ if (this.#shadow)
591
+ removeStyleSheet(this.#shadow, styleSheet);
578
592
  }
579
593
  attributeChangedCallback(name, oldValue, newValue) {
580
594
  const attrs = NewElementClass.observedAttributes;
@@ -657,6 +671,16 @@ const createWebComponent = (tag, props) => {
657
671
  else {
658
672
  el.appendToBody(getChildren([].concat(ret)));
659
673
  }
674
+ let attributesGetter = () => {
675
+ const attrs = props ? props() : {};
676
+ const attrList = Object.values(tag.customElement.props ?? {}).map((i) => i.attribute);
677
+ for (const attr of attrList) {
678
+ delete attrs[attr];
679
+ }
680
+ delete attrs.children;
681
+ return attrs;
682
+ };
683
+ nodeAttributesEffect(el, attributesGetter);
660
684
  return el;
661
685
  };
662
686
  hostStack.push(el);
@@ -818,8 +842,6 @@ const wc = () => {
818
842
 
819
843
  const $ = (val) => val;
820
844
 
821
- const isSignal = (a) => a?.SIGNAL === true;
822
-
823
845
  const contextWeakMap = new WeakMap();
824
846
  const context = (Context) => {
825
847
  const instance = securityGetCurrentInstance();
@@ -891,16 +913,68 @@ const onMount = (callback) => {
891
913
 
892
914
  const onDestroy = (callback) => effect(() => callback);
893
915
 
894
- const sheet = (css) => {
916
+ let count = BigInt(0);
917
+ const getId = () => (++count).toString(32);
918
+ const refWeakMap = new WeakMap();
919
+ const createCss = (css) => {
920
+ const id = getId();
921
+ return () => {
922
+ const styleId = id;
923
+ const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
924
+ return `.${className}_${styleId} {`;
925
+ });
926
+ styleSheet(id, replacedCss);
927
+ return new Proxy({}, {
928
+ get: (_, prop) => {
929
+ return `${prop}_${styleId}`;
930
+ },
931
+ });
932
+ };
933
+ };
934
+ const addReference = (host, id, add) => {
935
+ if (!refWeakMap.get(host)) {
936
+ refWeakMap.set(host, []);
937
+ }
938
+ const states = refWeakMap.get(host) ?? [];
939
+ const state = states.find((state) => state.id === id);
940
+ if (!state) {
941
+ refWeakMap.set(host, [
942
+ ...states,
943
+ {
944
+ id,
945
+ refCount: 1,
946
+ },
947
+ ]);
948
+ add();
949
+ }
950
+ else {
951
+ state.refCount++;
952
+ }
953
+ };
954
+ const removeReference = (host, id, remove) => {
955
+ const states = refWeakMap.get(host) ?? [];
956
+ const state = states.find((state) => state.id === id);
957
+ if (state) {
958
+ state.refCount--;
959
+ if (state.refCount === 0) {
960
+ remove();
961
+ refWeakMap.set(host, states.filter((state) => state.id !== id));
962
+ }
963
+ }
964
+ };
965
+ const styleSheet = (id, css) => {
895
966
  let instance = getCurrentInstance();
896
967
  const sheet = new CSSStyleSheet();
897
968
  css && sheet.replaceSync(css);
898
969
  let root;
899
970
  while (instance) {
900
971
  if (instance.host) {
901
- instance.host.addStyleSheet(sheet);
972
+ const host = instance.host;
973
+ addReference(host, id, () => {
974
+ host.addStyleSheet(sheet);
975
+ });
902
976
  onDestroy(() => {
903
- instance.host.removeStyleSheet(sheet);
977
+ removeReference(host, id, () => host.removeStyleSheet(sheet));
904
978
  });
905
979
  break;
906
980
  }
@@ -913,33 +987,22 @@ const sheet = (css) => {
913
987
  if (root) {
914
988
  let node = root;
915
989
  do {
916
- if (node instanceof ShadowRoot) {
917
- break;
918
- }
919
- if (node === document) {
990
+ if (node instanceof ShadowRoot || node === document) {
920
991
  break;
921
992
  }
922
993
  node = node.parentNode;
923
994
  } while (node);
924
- node.adoptedStyleSheets.push(sheet);
995
+ addReference(node, id, () => {
996
+ addStyleSheet(node, sheet);
997
+ });
925
998
  onDestroy(() => {
926
- node.adoptedStyleSheets = node.adoptedStyleSheets.filter((s) => s !== sheet);
999
+ removeReference(node, id, () => {
1000
+ removeStyleSheet(node, sheet);
1001
+ });
927
1002
  });
928
1003
  }
929
1004
  return sheet;
930
1005
  };
931
- const css = (css) => {
932
- const styleId = id();
933
- const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
934
- return `.${className}_${styleId} {`;
935
- });
936
- sheet(replacedCss);
937
- return new Proxy({}, {
938
- get: (_, prop) => {
939
- return `${prop}_${styleId}`;
940
- },
941
- });
942
- };
943
1006
 
944
1007
  function insertAfter(parentNode, newNode, targetNode) {
945
1008
  parentNode?.insertBefore(newNode, targetNode.nextSibling);
@@ -1380,5 +1443,5 @@ const template = (template) => {
1380
1443
  };
1381
1444
  };
1382
1445
 
1383
- export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createDom, createElement, createLogicComponent, createRoot, css, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, sheet, signal, template, untrack, wc };
1446
+ export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createCss, createDom, createElement, createLogicComponent, createRoot, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, signal, styleSheet, template, untrack, wc };
1384
1447
  //# sourceMappingURL=index.js.map