@diagrammo/dgmo 0.44.1 → 0.46.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/src/render.ts CHANGED
@@ -24,11 +24,29 @@ const DOM_GLOBALS = [
24
24
  'HTMLElement',
25
25
  'SVGElement',
26
26
  ] as const;
27
- let domRefCount = 0;
28
- let domInstallPromise: Promise<void> | null = null;
29
- let domInstalledByUs = false;
27
+ // The ref-count state lives on `globalThis` under a well-known symbol rather
28
+ // than in module state: dist/index and dist/block are each self-contained
29
+ // bundles, so a host importing both gets two copies of this module. With
30
+ // per-copy state, each copy keeps its own ref-count over the SAME
31
+ // `globalThis.document` — one copy's release tears down globals mid-render of
32
+ // the other. `Symbol.for` puts every copy on one shared count.
33
+ interface DomGlobalsState {
34
+ refCount: number;
35
+ installPromise: Promise<void> | null;
36
+ installedByUs: boolean;
37
+ }
38
+ const DOM_STATE_KEY = Symbol.for('diagrammo.dgmo.dom-globals');
39
+
40
+ function domState(): DomGlobalsState {
41
+ const g = globalThis as { [DOM_STATE_KEY]?: DomGlobalsState };
42
+ return (g[DOM_STATE_KEY] ??= {
43
+ refCount: 0,
44
+ installPromise: null,
45
+ installedByUs: false,
46
+ });
47
+ }
30
48
 
31
- async function installDom(): Promise<void> {
49
+ async function installDom(state: DomGlobalsState): Promise<void> {
32
50
  const { JSDOM } = await loadJsdom();
33
51
  // Concrete URL → non-opaque origin, so host code that touches
34
52
  // window.localStorage during a same-process render doesn't throw.
@@ -49,7 +67,7 @@ async function installDom(): Promise<void> {
49
67
  configurable: true,
50
68
  });
51
69
  }
52
- domInstalledByUs = true;
70
+ state.installedByUs = true;
53
71
  }
54
72
 
55
73
  /**
@@ -58,22 +76,24 @@ async function installDom(): Promise<void> {
58
76
  * we did not install). Pair every successful call with `releaseDom()`.
59
77
  */
60
78
  async function acquireDom(): Promise<void> {
61
- if (typeof document !== 'undefined' && !domInstalledByUs) return;
62
- domRefCount++;
63
- if (!domInstallPromise) domInstallPromise = installDom();
64
- await domInstallPromise;
79
+ const state = domState();
80
+ if (typeof document !== 'undefined' && !state.installedByUs) return;
81
+ state.refCount++;
82
+ if (!state.installPromise) state.installPromise = installDom(state);
83
+ await state.installPromise;
65
84
  }
66
85
 
67
86
  /** Tear down the jsdom globals once no render is in flight. */
68
87
  function releaseDom(): void {
69
- if (!domInstalledByUs) return;
70
- if (--domRefCount > 0) return;
88
+ const state = domState();
89
+ if (!state.installedByUs) return;
90
+ if (--state.refCount > 0) return;
71
91
  for (const key of DOM_GLOBALS) {
72
92
  delete (globalThis as Record<string, unknown>)[key];
73
93
  }
74
- domInstalledByUs = false;
75
- domInstallPromise = null;
76
- domRefCount = 0;
94
+ state.installedByUs = false;
95
+ state.installPromise = null;
96
+ state.refCount = 0;
77
97
  }
78
98
 
79
99
  /**
@@ -663,7 +663,7 @@ export function renderSitemap(
663
663
  .attr('class', 'sitemap-legend-fixed')
664
664
  .attr('transform', `translate(0, ${sDiagramPadding + fixedTitleH})`);
665
665
  if (activeTagGroup) {
666
- legendParent.attr('data-legend-active', activeTagGroup.toLowerCase());
666
+ legendParent.attr('data-legend-active', tagAttrKey(activeTagGroup));
667
667
  }
668
668
  renderLegend(
669
669
  legendParent,
@@ -153,6 +153,7 @@ export function renderSwimlaneForExport(
153
153
  .attr('width', width)
154
154
  .attr('height', height)
155
155
  .attr('viewBox', `0 0 ${width} ${height}`)
156
+ .attr('preserveAspectRatio', 'xMidYMin meet')
156
157
  .style('font-family', FONT_FAMILY) as unknown as D3Svg;
157
158
 
158
159
  const defs = svg.append('defs');
@@ -1299,7 +1299,7 @@ function renderTimelineTagLegendOverlay(
1299
1299
  if (currentActiveGroup) {
1300
1300
  legendContainer.attr(
1301
1301
  'data-legend-active',
1302
- currentActiveGroup.toLowerCase()
1302
+ tagAttrKey(currentActiveGroup)
1303
1303
  );
1304
1304
  }
1305
1305
 
@@ -25,6 +25,8 @@
25
25
  // there is no opacity double-up. `render({ bakeHover: false })` opts out.
26
26
  // ============================================================
27
27
 
28
+ import { tagAttrKey } from './tag-groups';
29
+
28
30
  /** How a chart's cross-highlight rules are shaped. `self` = self-emphasis only
29
31
  * (no cross-highlight — single-series / non-relational charts). */
30
32
  export type HoverStrategy = 'enumerated' | 'structural' | 'connection' | 'self';
@@ -599,7 +601,10 @@ function deriveFromSvg(svg: string, spec: HoverSpec): HoverDerived {
599
601
  .querySelector('[data-legend-active]')
600
602
  ?.getAttribute('data-legend-active');
601
603
  if (slug) {
602
- groupAttr = `data-tag-${slug}`;
604
+ // Defensive re-slug: legacy SVGs (pre-0.46) carried the raw lowercased
605
+ // group name here, which is not a valid attribute name when the group
606
+ // name contains spaces/parens — querySelectorAll would throw.
607
+ groupAttr = `data-tag-${tagAttrKey(slug)}`;
603
608
  } else {
604
609
  // Fallback for charts with no legend-active marker (e.g. mindmap): if
605
610
  // the marks carry exactly ONE distinct `data-tag-*` group, use it.
@@ -74,10 +74,7 @@ export function renderLegendD3(
74
74
 
75
75
  // Set active attribute on container
76
76
  if (currentState.activeGroup) {
77
- legendG.attr(
78
- 'data-legend-active',
79
- currentState.activeGroup.toLowerCase()
80
- );
77
+ legendG.attr('data-legend-active', tagAttrKey(currentState.activeGroup));
81
78
  } else {
82
79
  legendG.attr('data-legend-active', null);
83
80
  }
@@ -180,7 +180,7 @@ export function renderLegendSvg(
180
180
 
181
181
  const classAttr = className ? ` class="${esc(className)}"` : '';
182
182
  const activeAttr = activeGroup
183
- ? ` data-legend-active="${esc(activeGroup.toLowerCase())}"`
183
+ ? ` data-legend-active="${esc(tagAttrKey(activeGroup))}"`
184
184
  : '';
185
185
  const svg = `<g${classAttr}${activeAttr}>${parts.join('')}</g>`;
186
186