@wsxjs/wsx-core 0.0.22 → 0.0.24

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.mjs CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  createLogger,
5
5
  h,
6
6
  shouldPreserveElement
7
- } from "./chunk-OXFZ575O.mjs";
7
+ } from "./chunk-5Q2VEEUH.mjs";
8
8
 
9
9
  // src/styles/style-manager.ts
10
10
  var StyleManager = class {
@@ -401,6 +401,12 @@ var BaseComponent = class extends HTMLElement {
401
401
  * @internal
402
402
  */
403
403
  this._isRendering = false;
404
+ /**
405
+ * 已调度渲染标志(防止在同一事件循环中重复注册 requestAnimationFrame)
406
+ * 用于批量更新:同一事件循环中的多个状态变化只触发一次渲染
407
+ * @internal
408
+ */
409
+ this._hasScheduledRender = false;
404
410
  /**
405
411
  * 处理 blur 事件,在用户停止输入时执行待处理的重渲染
406
412
  * @internal
@@ -507,6 +513,9 @@ var BaseComponent = class extends HTMLElement {
507
513
  if (this._isRendering) {
508
514
  return;
509
515
  }
516
+ if (this._hasScheduledRender) {
517
+ return;
518
+ }
510
519
  const root = this.getActiveRoot();
511
520
  let activeElement = null;
512
521
  if (root instanceof ShadowRoot) {
@@ -532,8 +541,16 @@ var BaseComponent = class extends HTMLElement {
532
541
  if (this._pendingRerender) {
533
542
  this._pendingRerender = false;
534
543
  }
544
+ this._hasScheduledRender = true;
535
545
  requestAnimationFrame(() => {
546
+ console.warn("[scheduleRerender] RAF callback:", {
547
+ component: this.constructor.name,
548
+ connected: this.connected,
549
+ isRendering: this._isRendering
550
+ });
551
+ this._hasScheduledRender = false;
536
552
  if (this.connected && !this._isRendering) {
553
+ console.warn("[scheduleRerender] calling _rerender()");
537
554
  this._isRendering = true;
538
555
  this._rerender();
539
556
  } else if (!this.connected) {
@@ -790,6 +807,7 @@ var WebComponent = class extends BaseComponent {
790
807
  (child) => child instanceof HTMLElement && child.style.color === "red" && child.textContent?.includes("Component Error")
791
808
  );
792
809
  const hasActualContent = allChildren.length > styleElements.length + slotElements.length;
810
+ this.onConnected?.();
793
811
  if (hasActualContent && !hasErrorElement) {
794
812
  } else {
795
813
  this.shadowRoot.innerHTML = "";
@@ -801,7 +819,6 @@ var WebComponent = class extends BaseComponent {
801
819
  this.shadowRoot.appendChild(content);
802
820
  }
803
821
  this.initializeEventListeners();
804
- this.onConnected?.();
805
822
  if (hasActualContent === false || hasErrorElement) {
806
823
  requestAnimationFrame(() => {
807
824
  this.onRendered?.();
@@ -853,8 +870,12 @@ var WebComponent = class extends BaseComponent {
853
870
  const focusState = this.captureFocusState();
854
871
  this._pendingFocusState = focusState;
855
872
  const adoptedStyleSheets = this.shadowRoot.adoptedStyleSheets || [];
873
+ const hasActualAdoptedStyles = this.shadowRoot.adoptedStyleSheets && this.shadowRoot.adoptedStyleSheets.length > 0;
874
+ const hasFallbackStyleElement = Array.from(this.shadowRoot.children).some(
875
+ (child) => child instanceof HTMLStyleElement
876
+ );
856
877
  try {
857
- if (adoptedStyleSheets.length === 0) {
878
+ if (!hasActualAdoptedStyles && !hasFallbackStyleElement) {
858
879
  const stylesToApply = this._autoStyles || this.config.styles;
859
880
  if (stylesToApply) {
860
881
  const styleName = this.config.styleName || this.constructor.name;
@@ -872,31 +893,40 @@ var WebComponent = class extends BaseComponent {
872
893
  }
873
894
  }
874
895
  }
875
- if (this.shadowRoot.adoptedStyleSheets) {
876
- this.shadowRoot.adoptedStyleSheets = adoptedStyleSheets;
877
- }
878
- requestAnimationFrame(() => {
896
+ const isContentAlreadyInShadowRoot = content.parentNode === this.shadowRoot;
897
+ if (!isContentAlreadyInShadowRoot) {
879
898
  this.shadowRoot.appendChild(content);
880
- const oldChildren = Array.from(this.shadowRoot.children).filter((child) => {
881
- if (child === content) {
882
- return false;
883
- }
884
- if (child instanceof HTMLStyleElement) {
885
- return false;
886
- }
887
- if (shouldPreserveElement(child)) {
888
- return false;
889
- }
890
- return true;
891
- });
892
- oldChildren.forEach((child) => child.remove());
893
- requestAnimationFrame(() => {
894
- this.restoreFocusState(focusState);
895
- this._pendingFocusState = null;
896
- this.onRendered?.();
897
- this._isRendering = false;
898
- });
899
+ }
900
+ const oldChildren = Array.from(this.shadowRoot.children).filter((child) => {
901
+ if (child === content) {
902
+ return false;
903
+ }
904
+ if (child instanceof HTMLStyleElement) {
905
+ return false;
906
+ }
907
+ if (shouldPreserveElement(child)) {
908
+ return false;
909
+ }
910
+ return true;
899
911
  });
912
+ oldChildren.forEach((child) => child.remove());
913
+ const hasStylesAfterDOM = this.shadowRoot.adoptedStyleSheets && this.shadowRoot.adoptedStyleSheets.length > 0;
914
+ const hasStyleElementAfterDOM = Array.from(this.shadowRoot.children).some(
915
+ (child) => child instanceof HTMLStyleElement
916
+ );
917
+ if (adoptedStyleSheets.length > 0) {
918
+ this.shadowRoot.adoptedStyleSheets = adoptedStyleSheets;
919
+ } else if (!hasStylesAfterDOM && !hasStyleElementAfterDOM) {
920
+ const stylesToApply = this._autoStyles || this.config.styles;
921
+ if (stylesToApply) {
922
+ const styleName = this.config.styleName || this.constructor.name;
923
+ StyleManager.applyStyles(this.shadowRoot, styleName, stylesToApply);
924
+ }
925
+ }
926
+ this.restoreFocusState(focusState);
927
+ this._pendingFocusState = null;
928
+ this.onRendered?.();
929
+ this._isRendering = false;
900
930
  } catch (error) {
901
931
  logger3.error("Error in _rerender:", error);
902
932
  this.renderError(error);
@@ -958,6 +988,7 @@ var LightComponent = class extends BaseComponent {
958
988
  const hasActualContent = Array.from(this.children).some(
959
989
  (child) => child !== styleElement && !(child instanceof HTMLSlotElement)
960
990
  );
991
+ this.onConnected?.();
961
992
  if (hasActualContent && !hasErrorElement) {
962
993
  this.markJSXChildren();
963
994
  if (styleElement && styleElement !== this.firstChild) {
@@ -975,7 +1006,6 @@ var LightComponent = class extends BaseComponent {
975
1006
  }
976
1007
  }
977
1008
  this.initializeEventListeners();
978
- this.onConnected?.();
979
1009
  if (hasActualContent === false || hasErrorElement) {
980
1010
  requestAnimationFrame(() => {
981
1011
  this.onRendered?.();
@@ -1074,11 +1104,18 @@ var LightComponent = class extends BaseComponent {
1074
1104
  return true;
1075
1105
  });
1076
1106
  oldChildren.forEach((child) => child.remove());
1077
- if (stylesToApply && this.children.length > 1) {
1078
- const styleElement = this.querySelector(
1107
+ if (stylesToApply) {
1108
+ let styleElement = this.querySelector(
1079
1109
  `style[data-wsx-light-component="${styleName}"]`
1080
1110
  );
1081
- if (styleElement && styleElement !== this.firstChild) {
1111
+ if (!styleElement) {
1112
+ styleElement = document.createElement("style");
1113
+ styleElement.setAttribute("data-wsx-light-component", styleName);
1114
+ styleElement.textContent = stylesToApply;
1115
+ this.insertBefore(styleElement, this.firstChild);
1116
+ } else if (styleElement.textContent !== stylesToApply) {
1117
+ styleElement.textContent = stylesToApply;
1118
+ } else if (styleElement !== this.firstChild) {
1082
1119
  this.insertBefore(styleElement, this.firstChild);
1083
1120
  }
1084
1121
  }