@skillpet/circuit 0.5.2 → 0.6.1

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.
@@ -14387,6 +14387,7 @@ var Circuit = (() => {
14387
14387
  BusLine: () => BusLine,
14388
14388
  Button: () => Button,
14389
14389
  CPE: () => CPE,
14390
+ CSS_VARS: () => CSS_VARS,
14390
14391
  Capacitor: () => Capacitor,
14391
14392
  Capacitor2: () => Capacitor2,
14392
14393
  CapacitorTrim: () => CapacitorTrim,
@@ -30565,15 +30566,37 @@ var Circuit = (() => {
30565
30566
  lw: 1.5
30566
30567
  }
30567
30568
  };
30569
+ var CSS_VARS = {
30570
+ bg: "--circuit-bg",
30571
+ stroke: "--circuit-stroke",
30572
+ fill: "--circuit-fill",
30573
+ text: "--circuit-text",
30574
+ font: "--circuit-font",
30575
+ lw: "--circuit-lw"
30576
+ };
30577
+ function readCssTheme() {
30578
+ if (typeof getComputedStyle === "undefined") return { ...BUILTIN_THEMES.light };
30579
+ const s = getComputedStyle(document.documentElement);
30580
+ const v = (name) => s.getPropertyValue(name).trim();
30581
+ return {
30582
+ bg: v(CSS_VARS.bg) || "transparent",
30583
+ stroke: v(CSS_VARS.stroke) || BUILTIN_THEMES.light.stroke,
30584
+ fill: v(CSS_VARS.fill) || "none",
30585
+ text: v(CSS_VARS.text) || BUILTIN_THEMES.light.text,
30586
+ font: v(CSS_VARS.font) || BUILTIN_THEMES.light.font,
30587
+ lw: parseFloat(v(CSS_VARS.lw)) || BUILTIN_THEMES.light.lw
30588
+ };
30589
+ }
30568
30590
  function resolveTheme(theme2) {
30569
30591
  if (theme2 === void 0) {
30570
30592
  return { ...BUILTIN_THEMES.light };
30571
30593
  }
30572
30594
  if (typeof theme2 === "string") {
30595
+ if (theme2 === "auto") return readCssTheme();
30573
30596
  const found = BUILTIN_THEMES[theme2];
30574
30597
  if (!found) {
30575
30598
  throw new Error(
30576
- `Unknown theme "${theme2}". Available: ${Object.keys(BUILTIN_THEMES).join(", ")}`
30599
+ `Unknown theme "${theme2}". Available: ${Object.keys(BUILTIN_THEMES).join(", ")}, auto`
30577
30600
  );
30578
30601
  }
30579
30602
  return { ...found };
@@ -30794,6 +30817,9 @@ var Circuit = (() => {
30794
30817
  __publicField(this, "_theme");
30795
30818
  __publicField(this, "_destroyed", false);
30796
30819
  __publicField(this, "_tooltipEl", null);
30820
+ __publicField(this, "_autoObserver", null);
30821
+ __publicField(this, "_mediaQuery", null);
30822
+ __publicField(this, "_mediaHandler", null);
30797
30823
  __publicField(this, "_boundClick");
30798
30824
  __publicField(this, "_boundMouseover");
30799
30825
  __publicField(this, "_boundMouseout");
@@ -30811,6 +30837,42 @@ var Circuit = (() => {
30811
30837
  this._render();
30812
30838
  this._bindEvents();
30813
30839
  this._initTooltip();
30840
+ this._initAutoTheme();
30841
+ }
30842
+ _isAutoTheme() {
30843
+ return (this._opts.theme ?? this._json.theme) === "auto";
30844
+ }
30845
+ _initAutoTheme() {
30846
+ if (!this._isAutoTheme()) return;
30847
+ const rerender = () => {
30848
+ if (this._destroyed) return;
30849
+ this._theme = resolveTheme("auto");
30850
+ this._unbindEvents();
30851
+ this._render();
30852
+ this._bindEvents();
30853
+ this._selectedId = null;
30854
+ };
30855
+ this._autoObserver = new MutationObserver(rerender);
30856
+ this._autoObserver.observe(document.documentElement, {
30857
+ attributes: true,
30858
+ attributeFilter: ["class", "data-theme", "data-color-mode", "style"]
30859
+ });
30860
+ if (typeof matchMedia !== "undefined") {
30861
+ this._mediaQuery = matchMedia("(prefers-color-scheme: dark)");
30862
+ this._mediaHandler = rerender;
30863
+ this._mediaQuery.addEventListener("change", this._mediaHandler);
30864
+ }
30865
+ }
30866
+ _teardownAutoTheme() {
30867
+ if (this._autoObserver) {
30868
+ this._autoObserver.disconnect();
30869
+ this._autoObserver = null;
30870
+ }
30871
+ if (this._mediaQuery && this._mediaHandler) {
30872
+ this._mediaQuery.removeEventListener("change", this._mediaHandler);
30873
+ this._mediaQuery = null;
30874
+ this._mediaHandler = null;
30875
+ }
30814
30876
  }
30815
30877
  _render() {
30816
30878
  const svgString = renderFromJson(this._json, this._opts);
@@ -30858,13 +30920,28 @@ var Circuit = (() => {
30858
30920
  }
30859
30921
  _initTooltip() {
30860
30922
  const tip = document.createElement("div");
30861
- tip.style.cssText = "position:fixed;z-index:99999;pointer-events:none;opacity:0;transition:opacity .12s ease;background:rgba(30,30,30,.92);color:#fff;font-size:12px;line-height:1.4;padding:5px 10px;border-radius:4px;max-width:260px;white-space:pre-wrap;box-shadow:0 2px 8px rgba(0,0,0,.25)";
30923
+ tip.className = "schemdraw-tooltip";
30924
+ tip.style.cssText = "position:fixed;z-index:99999;pointer-events:none;opacity:0;transition:opacity .15s ease;font-size:12px;line-height:1.4;padding:6px 12px;border-radius:6px;max-width:260px;white-space:pre-wrap;";
30925
+ this._applyTooltipTheme(tip);
30862
30926
  document.body.appendChild(tip);
30863
30927
  this._tooltipEl = tip;
30864
30928
  }
30929
+ _applyTooltipTheme(tip) {
30930
+ const isDark = document.documentElement.classList.contains("dark") || document.documentElement.getAttribute("data-theme") === "dark" || typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: dark)").matches && !document.documentElement.classList.contains("light");
30931
+ if (isDark) {
30932
+ tip.style.background = "rgba(250,250,250,.95)";
30933
+ tip.style.color = "#1a1a1a";
30934
+ tip.style.boxShadow = "0 2px 12px rgba(0,0,0,.4)";
30935
+ } else {
30936
+ tip.style.background = "rgba(15,15,15,.92)";
30937
+ tip.style.color = "#fff";
30938
+ tip.style.boxShadow = "0 2px 8px rgba(0,0,0,.2)";
30939
+ }
30940
+ }
30865
30941
  _showTooltip(text2, clientX, clientY) {
30866
30942
  const tip = this._tooltipEl;
30867
30943
  if (!tip) return;
30944
+ this._applyTooltipTheme(tip);
30868
30945
  tip.textContent = text2;
30869
30946
  tip.style.opacity = "1";
30870
30947
  const gap2 = 10;
@@ -31044,9 +31121,11 @@ var Circuit = (() => {
31044
31121
  this._selectedId = null;
31045
31122
  }
31046
31123
  setTheme(theme2) {
31124
+ this._teardownAutoTheme();
31047
31125
  this._opts = { ...this._opts, theme: theme2 };
31048
31126
  this._theme = resolveTheme(theme2);
31049
31127
  this.update(this._json);
31128
+ this._initAutoTheme();
31050
31129
  }
31051
31130
  update(json) {
31052
31131
  const circuit = typeof json === "string" ? JSON.parse(json) : json;
@@ -31060,6 +31139,7 @@ var Circuit = (() => {
31060
31139
  if (this._destroyed) return;
31061
31140
  this._destroyed = true;
31062
31141
  this._unbindEvents();
31142
+ this._teardownAutoTheme();
31063
31143
  if (this._tooltipEl) {
31064
31144
  this._tooltipEl.remove();
31065
31145
  this._tooltipEl = null;