@markdy/renderer-dom 1.3.2 → 1.3.3

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.d.ts CHANGED
@@ -128,6 +128,24 @@ interface DiagramOptions {
128
128
  onTimeUpdate?: (seconds: number, durationSeconds: number) => void;
129
129
  onPlayStateChange?: (playing: boolean) => void;
130
130
  onEnded?: () => void;
131
+ /**
132
+ * Adaptive orientation & responsive layout mode.
133
+ * When true or "auto", switches to vertical Top-to-Bottom (TB) on mobile / portrait viewports,
134
+ * and horizontal Left-to-Right (LR) on desktop / landscape viewports.
135
+ * Defaults to true.
136
+ */
137
+ responsiveLayout?: boolean;
138
+ /**
139
+ * Auto-scaling mode for diagram framing:
140
+ * - "width" (default): Width-First Responsiveness (scale factor = containerWidth / contentWidth, filling 95% - 100% width).
141
+ * - "contain": Scales to fit both width and height within container bounds.
142
+ */
143
+ fitMode?: "width" | "contain";
144
+ /**
145
+ * Target container width fill fraction (0.95 - 1.0) when fitMode is "width".
146
+ * Defaults to 0.98 (edge-to-edge with 1% clean margins).
147
+ */
148
+ targetWidthRatio?: number;
131
149
  }
132
150
  interface Diagram {
133
151
  play(): void;
@@ -155,6 +173,17 @@ interface Diagram {
155
173
  * Default light mode is "paper", default dark mode is "nebula".
156
174
  */
157
175
  declare function detectHostTheme(container?: HTMLElement): "nebula" | "paper";
176
+ declare function computeDiagramContentBounds(plan: RenderPlan, options?: {
177
+ tight?: boolean;
178
+ padding?: number;
179
+ }): {
180
+ minX: number;
181
+ minY: number;
182
+ maxX: number;
183
+ maxY: number;
184
+ width: number;
185
+ height: number;
186
+ };
158
187
  declare function createDiagram(opts: DiagramOptions): Diagram;
159
188
 
160
189
  type SvgSpec = Array<[string, Record<string, string>]>;
@@ -277,4 +306,4 @@ declare function exportRouteShareCard(containerEl: HTMLElement, route: RouteCard
277
306
  */
278
307
  declare function exportReachShareCard(containerEl: HTMLElement, reach: ReachCardOptions, options?: ShareCardOptions): Promise<Blob>;
279
308
 
280
- export { type AnimationRecordFrame, type BlastRadiusResult, type ControllerOptions, type Diagram, type DiagramOptions, DiagramPresentationController, type GifDiagramExportOptions, type GifExportOptions, ICON_REGISTRY, type IconSpec, type PngExportOptions, type ReachCardOptions, type RouteCardOptions, type ShareCardOptions, type SvgExportOptions, type TimelineController, applyImpactHighlight, calculateBlastRadius, clearImpactHighlight, createDiagram, detectHostTheme, encodeGifSequence, exportDiagramAsGif, exportDiagramAsPng, exportDiagramAsShareCard, exportDiagramAsVectorSvg, exportLiveSceneAsPureVectorSvg, exportReachShareCard, exportRouteShareCard, findShortestRoute, iconKeyForNode, renderPureVectorSvg, renderShareCardSvg };
309
+ export { type AnimationRecordFrame, type BlastRadiusResult, type ControllerOptions, type Diagram, type DiagramOptions, DiagramPresentationController, type GifDiagramExportOptions, type GifExportOptions, ICON_REGISTRY, type IconSpec, type PngExportOptions, type ReachCardOptions, type RouteCardOptions, type ShareCardOptions, type SvgExportOptions, type TimelineController, applyImpactHighlight, calculateBlastRadius, clearImpactHighlight, computeDiagramContentBounds, createDiagram, detectHostTheme, encodeGifSequence, exportDiagramAsGif, exportDiagramAsPng, exportDiagramAsShareCard, exportDiagramAsVectorSvg, exportLiveSceneAsPureVectorSvg, exportReachShareCard, exportRouteShareCard, findShortestRoute, iconKeyForNode, renderPureVectorSvg, renderShareCardSvg };
package/dist/index.js CHANGED
@@ -5741,7 +5741,7 @@ function detectHostTheme(container) {
5741
5741
  }
5742
5742
  return "paper";
5743
5743
  }
5744
- function computeDiagramContentBounds(plan) {
5744
+ function computeDiagramContentBounds(plan, options) {
5745
5745
  if (!plan.nodes || plan.nodes.length === 0) {
5746
5746
  return { minX: 0, minY: 0, maxX: plan.meta.width, maxY: plan.meta.height, width: plan.meta.width, height: plan.meta.height };
5747
5747
  }
@@ -5770,23 +5770,37 @@ function computeDiagramContentBounds(plan) {
5770
5770
  if (plan.diagramType === "sequence") {
5771
5771
  let maxMsgY = minY;
5772
5772
  for (const msg of plan.sequenceMessages ?? []) {
5773
- maxMsgY = Math.max(maxMsgY, msg.y + 60);
5773
+ maxMsgY = Math.max(maxMsgY, msg.y + 70);
5774
5774
  }
5775
5775
  for (const act of plan.sequenceActivations ?? []) {
5776
5776
  maxMsgY = Math.max(maxMsgY, act.y + act.height + 40);
5777
5777
  }
5778
- maxY = Math.max(maxY, maxMsgY, Math.min(plan.meta.height, 420));
5778
+ maxY = Math.max(maxY, maxMsgY, plan.meta.height - 24);
5779
5779
  }
5780
- if (plan.title) {
5780
+ const titleText = (plan.title ?? plan.meta?.title ?? "").trim();
5781
+ if (titleText.length > 0) {
5781
5782
  minY = Math.min(minY, 20);
5782
5783
  }
5783
- const pad = 36;
5784
+ const hasBeatCaptions = plan.beats && plan.beats.some((b) => b.label && b.label.trim().length > 0);
5785
+ if (hasBeatCaptions) {
5786
+ maxY = Math.max(maxY, plan.meta.height - 18);
5787
+ }
5788
+ for (const ann of plan.annotations ?? []) {
5789
+ const targetNode = plan.nodes.find((n) => n.id === ann.target);
5790
+ if (targetNode) {
5791
+ minX = Math.min(minX, targetNode.x - 24);
5792
+ maxX = Math.max(maxX, targetNode.x + targetNode.width + 24);
5793
+ minY = Math.min(minY, targetNode.y - 24);
5794
+ maxY = Math.max(maxY, targetNode.y + targetNode.height + 24);
5795
+ }
5796
+ }
5797
+ const pad = options?.padding ?? (options?.tight === false ? 24 : 10);
5784
5798
  minX = Math.max(0, minX - pad);
5785
5799
  minY = Math.max(0, minY - pad);
5786
5800
  maxX = Math.min(plan.meta.width, maxX + pad);
5787
5801
  maxY = Math.min(plan.meta.height, maxY + pad);
5788
- const width = Math.max(maxX - minX, 200);
5789
- const height = Math.max(maxY - minY, 140);
5802
+ const width = Math.max(maxX - minX, 100);
5803
+ const height = Math.max(maxY - minY, 80);
5790
5804
  return { minX, minY, maxX, maxY, width, height };
5791
5805
  }
5792
5806
  function createDiagram(opts) {
@@ -5809,16 +5823,39 @@ function createDiagram(opts) {
5809
5823
  onWarning = (w) => console.warn(`[markdy] line ${w.line}: ${w.message}`),
5810
5824
  onTimeUpdate,
5811
5825
  onPlayStateChange,
5812
- onEnded
5826
+ onEnded,
5827
+ responsiveLayout = true,
5828
+ fitMode = "width",
5829
+ targetWidthRatio = 0.98
5813
5830
  } = opts;
5831
+ function detectContainerOrientation() {
5832
+ const cWidth = container?.clientWidth || 0;
5833
+ const cHeight = container?.clientHeight || 0;
5834
+ if (typeof window === "undefined") {
5835
+ if (cWidth > 0 && cHeight > 0) {
5836
+ return cHeight > cWidth * 1.05 ? "portrait" : "landscape";
5837
+ }
5838
+ return "landscape";
5839
+ }
5840
+ const isMobileViewport = window.innerWidth < 768;
5841
+ if (isMobileViewport) {
5842
+ return "portrait";
5843
+ }
5844
+ if (cWidth > 0 && cHeight > 0) {
5845
+ if (cHeight > cWidth * 1.18) return "portrait";
5846
+ return "landscape";
5847
+ }
5848
+ return window.innerWidth < window.innerHeight ? "portrait" : "landscape";
5849
+ }
5814
5850
  const ast = parse(code);
5815
5851
  for (const w of ast.diagnostics) {
5816
5852
  if (w.severity === "warning") onWarning(w);
5817
5853
  }
5818
5854
  const hasExplicitDirection = ast.meta.explicitDirection === true;
5819
- if (!hasExplicitDirection) {
5820
- const isPortrait = typeof window !== "undefined" ? window.innerWidth < window.innerHeight || container && container.clientWidth > 0 && container.clientWidth < 640 : false;
5821
- ast.meta.direction = isPortrait ? "TB" : "LR";
5855
+ const shouldAdaptOrientation = responsiveLayout !== false && (!hasExplicitDirection || responsiveLayout === true);
5856
+ let activeOrientation = detectContainerOrientation();
5857
+ if (shouldAdaptOrientation) {
5858
+ ast.meta.direction = activeOrientation === "portrait" ? "TB" : "LR";
5822
5859
  }
5823
5860
  if (!ast.meta.explicitWidth || !ast.meta.explicitHeight) {
5824
5861
  const dims = computeAdaptiveDimensions(ast);
@@ -5894,7 +5931,7 @@ function createDiagram(opts) {
5894
5931
  if (container.style.aspectRatio) container.style.aspectRatio = "unset";
5895
5932
  if (container.style.overflow === "hidden") container.style.overflow = "visible";
5896
5933
  const initialBounds = computeDiagramContentBounds(plan);
5897
- const naturalRatio = `${plan.meta.width} / ${plan.meta.height}`;
5934
+ const contentRatio = `${initialBounds.width} / ${initialBounds.height}`;
5898
5935
  const viewport = document.createElement("div");
5899
5936
  viewport.className = "markdy-viewport";
5900
5937
  applyThemeVariables(viewport, plan.theme);
@@ -5903,7 +5940,7 @@ function createDiagram(opts) {
5903
5940
  width: "100%",
5904
5941
  maxWidth: "100%",
5905
5942
  maxHeight: "100%",
5906
- aspectRatio: naturalRatio,
5943
+ aspectRatio: contentRatio,
5907
5944
  overflow: "hidden",
5908
5945
  boxSizing: "border-box",
5909
5946
  flex: "1 1 auto",
@@ -6118,51 +6155,9 @@ function createDiagram(opts) {
6118
6155
  nodeLayer.appendChild(el);
6119
6156
  nodeEls.set(node.id, el);
6120
6157
  }
6121
- let fitScale = 1;
6122
- let sceneOffsetX = 0;
6123
- let sceneOffsetY = 0;
6124
- function computeContentBounds() {
6125
- return computeDiagramContentBounds(plan);
6126
- }
6127
- function scaleScene() {
6128
- const isBrowserLayout = (viewport.clientWidth || container.clientWidth || 0) > 0;
6129
- if (!isBrowserLayout) {
6130
- fitScale = 1;
6131
- sceneOffsetX = 0;
6132
- sceneOffsetY = 0;
6133
- scene.style.left = "0px";
6134
- scene.style.top = "0px";
6135
- scene.style.transformOrigin = "0 0";
6136
- scene.style.transform = "scale(1)";
6137
- return;
6138
- }
6139
- const vWidth = viewport.clientWidth || container.clientWidth || plan.meta.width;
6140
- const fallbackHeight = vWidth * plan.meta.height / plan.meta.width;
6141
- const vHeight = viewport.clientHeight || container.clientHeight || fallbackHeight;
6142
- const padX = Math.max(12, Math.min(32, vWidth * 0.03));
6143
- const padY = Math.max(12, Math.min(28, vHeight * 0.03));
6144
- const availW = Math.max(80, vWidth - padX * 2);
6145
- const availH = Math.max(80, vHeight - padY * 2);
6146
- const canvasScaleX = availW / plan.meta.width;
6147
- const canvasScaleY = availH / plan.meta.height;
6148
- fitScale = Math.min(canvasScaleX, canvasScaleY);
6149
- if (!Number.isFinite(fitScale) || fitScale <= 0) fitScale = 1;
6150
- const scaledWidth = plan.meta.width * fitScale;
6151
- const scaledHeight = plan.meta.height * fitScale;
6152
- sceneOffsetX = (vWidth - scaledWidth) / 2;
6153
- sceneOffsetY = (vHeight - scaledHeight) / 2;
6154
- scene.style.left = `${sceneOffsetX}px`;
6155
- scene.style.top = `${sceneOffsetY}px`;
6156
- scene.style.transformOrigin = "0 0";
6157
- scene.style.transform = `scale(${fitScale})`;
6158
- }
6159
- scaleScene();
6160
- const resizeObserver = typeof ResizeObserver === "function" ? new ResizeObserver(scaleScene) : null;
6161
- resizeObserver?.observe(viewport);
6162
- if (container !== viewport) resizeObserver?.observe(container);
6163
6158
  const edgeRuntimes = /* @__PURE__ */ new Map();
6164
6159
  const edgeSceneId = createEdgeSceneId();
6165
- const sequenceAnims = plan.diagramType === "sequence" ? mountSequenceLayer(
6160
+ let sequenceAnims = plan.diagramType === "sequence" ? mountSequenceLayer(
6166
6161
  sequenceLayer,
6167
6162
  plan.nodes,
6168
6163
  plan.sequenceMessages,
@@ -6170,7 +6165,7 @@ function createDiagram(opts) {
6170
6165
  plan.theme,
6171
6166
  { width: plan.meta.width, height: plan.meta.height }
6172
6167
  ) : [];
6173
- const allAnims = [
6168
+ let allAnims = [
6174
6169
  ...sequenceAnims,
6175
6170
  ...buildStructuralEdgeAnimations(
6176
6171
  plan.edges,
@@ -6197,6 +6192,140 @@ function createDiagram(opts) {
6197
6192
  anim.pause();
6198
6193
  anim.currentTime = 0;
6199
6194
  }
6195
+ let fitScale = 1;
6196
+ let sceneOffsetX = 0;
6197
+ let sceneOffsetY = 0;
6198
+ function computeContentBounds() {
6199
+ return computeDiagramContentBounds(plan);
6200
+ }
6201
+ function relayoutOrientation(newDirection) {
6202
+ ast.meta.direction = newDirection;
6203
+ const dims = computeAdaptiveDimensions(ast);
6204
+ if (!ast.meta.explicitWidth) ast.meta.width = dims.width;
6205
+ if (!ast.meta.explicitHeight) ast.meta.height = dims.height;
6206
+ const newPlan = compilePlan(ast, plan.theme);
6207
+ const newBounds = computeDiagramContentBounds(newPlan);
6208
+ scene.style.width = `${newPlan.meta.width}px`;
6209
+ scene.style.height = `${newPlan.meta.height}px`;
6210
+ viewport.style.aspectRatio = `${newBounds.width} / ${newBounds.height}`;
6211
+ plan.nodes = newPlan.nodes;
6212
+ plan.edges = newPlan.edges;
6213
+ plan.groupBoundaries = newPlan.groupBoundaries;
6214
+ plan.treeBuses = newPlan.treeBuses;
6215
+ plan.cues = newPlan.cues;
6216
+ plan.beats = newPlan.beats;
6217
+ plan.meta = newPlan.meta;
6218
+ for (const node of plan.nodes) {
6219
+ const el = nodeEls.get(node.id);
6220
+ if (el) {
6221
+ el.style.left = `${node.x}px`;
6222
+ el.style.top = `${node.y}px`;
6223
+ el.style.width = `${node.width}px`;
6224
+ el.style.height = `${node.height}px`;
6225
+ }
6226
+ }
6227
+ treeLayer.innerHTML = "";
6228
+ mountTreeBuses(treeLayer, plan.treeBuses, plan.theme);
6229
+ groupLayer.innerHTML = "";
6230
+ mountGroupBoundaries(groupLayer, plan.groupBoundaries, plan.theme);
6231
+ structuralEdgeHost.innerHTML = "";
6232
+ edgeRuntimes.clear();
6233
+ const newStructuralAnims = buildStructuralEdgeAnimations(
6234
+ plan.edges,
6235
+ plan.nodes,
6236
+ plan.theme,
6237
+ structuralEdgeHost,
6238
+ { width: plan.meta.width, height: plan.meta.height },
6239
+ edgeRuntimes,
6240
+ edgeSceneId,
6241
+ plan.diagramType,
6242
+ plan.cues
6243
+ );
6244
+ for (const anim of allAnims) anim.cancel();
6245
+ allAnims = [
6246
+ ...plan.diagramType === "sequence" ? mountSequenceLayer(
6247
+ sequenceLayer,
6248
+ plan.nodes,
6249
+ plan.sequenceMessages,
6250
+ plan.sequenceActivations,
6251
+ plan.theme,
6252
+ { width: plan.meta.width, height: plan.meta.height }
6253
+ ) : [],
6254
+ ...newStructuralAnims,
6255
+ ...buildCueAnimations(
6256
+ plan.cues,
6257
+ nodeEls,
6258
+ plan.nodes,
6259
+ plan.theme,
6260
+ cameraLayer,
6261
+ titleEl,
6262
+ { width: plan.meta.width, height: plan.meta.height },
6263
+ plan.edges,
6264
+ edgeRuntimes,
6265
+ edgeSceneId,
6266
+ plan.diagramType
6267
+ ),
6268
+ ...buildBeatCaptionAnimations(plan.beats, captionLayer)
6269
+ ];
6270
+ applyCurrentTime();
6271
+ scaleScene();
6272
+ }
6273
+ function scaleScene() {
6274
+ const isBrowserLayout = (viewport.clientWidth || container.clientWidth || 0) > 0;
6275
+ if (!isBrowserLayout) {
6276
+ fitScale = 1;
6277
+ sceneOffsetX = 0;
6278
+ sceneOffsetY = 0;
6279
+ scene.style.left = "0px";
6280
+ scene.style.top = "0px";
6281
+ scene.style.transformOrigin = "0 0";
6282
+ scene.style.transform = "scale(1)";
6283
+ return;
6284
+ }
6285
+ if (shouldAdaptOrientation) {
6286
+ const currentOrientation = detectContainerOrientation();
6287
+ if (currentOrientation !== activeOrientation) {
6288
+ activeOrientation = currentOrientation;
6289
+ relayoutOrientation(activeOrientation === "portrait" ? "TB" : "LR");
6290
+ return;
6291
+ }
6292
+ }
6293
+ const vWidth = viewport.clientWidth || container.clientWidth || plan.meta.width;
6294
+ const fallbackHeight = vWidth * plan.meta.height / plan.meta.width;
6295
+ const vHeight = viewport.clientHeight || container.clientHeight || fallbackHeight;
6296
+ const bounds = computeContentBounds();
6297
+ const contentW = bounds.width;
6298
+ const contentH = bounds.height;
6299
+ const targetRatio = Math.min(1, Math.max(0.95, targetWidthRatio ?? 0.98));
6300
+ const widthScale = vWidth * targetRatio / contentW;
6301
+ if (fitMode === "contain") {
6302
+ const heightScale = (vHeight - 16) * targetRatio / contentH;
6303
+ fitScale = Math.min(widthScale, heightScale);
6304
+ } else {
6305
+ fitScale = widthScale;
6306
+ }
6307
+ if (!Number.isFinite(fitScale) || fitScale <= 0) fitScale = 1;
6308
+ scene.style.setProperty("--markdy-scale", fitScale.toFixed(4));
6309
+ viewport.style.setProperty("--markdy-scale", fitScale.toFixed(4));
6310
+ container.style.setProperty("--markdy-scale", fitScale.toFixed(4));
6311
+ const scaledContentW = contentW * fitScale;
6312
+ const scaledContentH = contentH * fitScale;
6313
+ sceneOffsetX = (vWidth - scaledContentW) / 2 - bounds.minX * fitScale;
6314
+ if (fitMode === "contain") {
6315
+ sceneOffsetY = (vHeight - scaledContentH) / 2 - bounds.minY * fitScale;
6316
+ } else {
6317
+ const topSafeMargin = scaledContentH <= vHeight ? Math.max(8, Math.min(18, (vHeight - scaledContentH) / 2)) : 8;
6318
+ sceneOffsetY = topSafeMargin - bounds.minY * fitScale;
6319
+ }
6320
+ scene.style.left = `${sceneOffsetX}px`;
6321
+ scene.style.top = `${sceneOffsetY}px`;
6322
+ scene.style.transformOrigin = "0 0";
6323
+ scene.style.transform = `scale(${fitScale})`;
6324
+ }
6325
+ scaleScene();
6326
+ const resizeObserver = typeof ResizeObserver === "function" ? new ResizeObserver(scaleScene) : null;
6327
+ resizeObserver?.observe(viewport);
6328
+ if (container !== viewport) resizeObserver?.observe(container);
6200
6329
  let sceneMs = 0;
6201
6330
  let lastRafTs = null;
6202
6331
  let isPlaying = false;
@@ -7659,6 +7788,7 @@ export {
7659
7788
  applyImpactHighlight,
7660
7789
  calculateBlastRadius,
7661
7790
  clearImpactHighlight,
7791
+ computeDiagramContentBounds,
7662
7792
  createDiagram,
7663
7793
  detectHostTheme,
7664
7794
  encodeGifSequence,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markdy/renderer-dom",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "Browser renderer for diagram-native animated MarkdyScript architecture diagrams, built on the Web Animations API.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -46,14 +46,14 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "html2canvas": "^1.4.1",
49
- "@markdy/core": "1.3.2"
49
+ "@markdy/core": "1.3.3"
50
50
  },
51
51
  "devDependencies": {
52
52
  "jsdom": "^29.1.1",
53
53
  "tsup": "^8.5.1",
54
54
  "typescript": "^5.9.3",
55
55
  "vitest": "^4.1.7",
56
- "@markdy/stdlib-systems": "1.3.2"
56
+ "@markdy/stdlib-systems": "1.3.3"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "tsup",