@bendyline/squisq-react 2.11.16 → 2.11.18

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.
@@ -0,0 +1,213 @@
1
+ import {
2
+ BlockRenderer
3
+ } from "../chunk-5FJZDX7H.js";
4
+ import "../chunk-PZCWGBRQ.js";
5
+ import "../chunk-QXZXQPL3.js";
6
+ import {
7
+ renderMermaidSvgForExport
8
+ } from "../chunk-63G5OJWY.js";
9
+
10
+ // src/export/diagramPictures.ts
11
+ import { createElement } from "react";
12
+ import { createRoot } from "react-dom/client";
13
+ import { VIEWPORT_PRESETS } from "@bendyline/squisq/schemas";
14
+ import {
15
+ DEFAULT_THEME,
16
+ flattenRenderableBlocks,
17
+ markdownToDoc,
18
+ materializeBlockLayers
19
+ } from "@bendyline/squisq/doc";
20
+ import { parseMarkdown } from "@bendyline/squisq/markdown";
21
+ var EXPORT_FONT_STACK = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
22
+ var EXPORT_MONO_STACK = "ui-monospace, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace";
23
+ var CANVAS_UNITS_PER_CSS_PIXEL = 2.5;
24
+ var PADDING = 16;
25
+ var SVG_NS = "http://www.w3.org/2000/svg";
26
+ var pictureCount = 0;
27
+ function settings(options) {
28
+ return {
29
+ theme: options.theme ?? DEFAULT_THEME,
30
+ scale: options.scale ?? 2,
31
+ maxWidth: options.maxWidth ?? 680,
32
+ background: options.background ?? "#ffffff"
33
+ };
34
+ }
35
+ async function svgToPng(svg, width, height, options = {}) {
36
+ const { scale, background } = settings(options);
37
+ const pixelWidth = Math.max(1, Math.round(width * scale));
38
+ const pixelHeight = Math.max(1, Math.round(height * scale));
39
+ const clone = svg.cloneNode(true);
40
+ clone.setAttribute("xmlns", SVG_NS);
41
+ clone.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
42
+ clone.setAttribute("width", String(pixelWidth));
43
+ clone.setAttribute("height", String(pixelHeight));
44
+ clone.removeAttribute("style");
45
+ const xml = new XMLSerializer().serializeToString(clone);
46
+ const image = new Image();
47
+ image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xml)}`;
48
+ await image.decode();
49
+ const canvas = document.createElement("canvas");
50
+ canvas.width = pixelWidth;
51
+ canvas.height = pixelHeight;
52
+ const context = canvas.getContext("2d");
53
+ if (!context) throw new Error("No 2D canvas is available to draw the diagram.");
54
+ context.fillStyle = background;
55
+ context.fillRect(0, 0, pixelWidth, pixelHeight);
56
+ context.drawImage(image, 0, 0, pixelWidth, pixelHeight);
57
+ const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/png"));
58
+ if (!blob) throw new Error("The diagram could not be encoded as PNG.");
59
+ return {
60
+ data: new Uint8Array(await blob.arrayBuffer()),
61
+ width: Math.round(width),
62
+ height: Math.round(height)
63
+ };
64
+ }
65
+ function parseViewBox(svg) {
66
+ const values = (svg.getAttribute("viewBox") ?? "").trim().split(/[\s,]+/u).map(Number);
67
+ if (values.length !== 4 || values.some((value) => !Number.isFinite(value))) return null;
68
+ const [x = 0, y = 0, width = 0, height = 0] = values;
69
+ return width > 0 && height > 0 ? { x, y, width, height } : null;
70
+ }
71
+ async function renderMermaidPicture(source, options = {}) {
72
+ const { theme, maxWidth } = settings(options);
73
+ const markup = await renderMermaidSvgForExport(
74
+ `squisq-export-mermaid-${String(++pictureCount)}`,
75
+ source,
76
+ theme,
77
+ EXPORT_FONT_STACK
78
+ );
79
+ const template = document.createElement("template");
80
+ template.innerHTML = markup;
81
+ const svg = template.content.querySelector("svg");
82
+ if (!svg) throw new Error("Mermaid produced no SVG.");
83
+ const box = parseViewBox(svg);
84
+ if (!box) throw new Error("The Mermaid SVG has no size.");
85
+ const style = document.createElementNS(SVG_NS, "style");
86
+ style.textContent = `.edgeLabel text, .edgeLabel tspan { fill: ${theme.colors.text} !important; }`;
87
+ svg.append(style);
88
+ const width = Math.min(maxWidth, box.width);
89
+ return svgToPng(svg, width, box.height * width / box.width, options);
90
+ }
91
+ async function renderTreePicture(source, options = {}) {
92
+ const { theme, maxWidth } = settings(options);
93
+ const lines = source.replace(/\s+$/u, "").split("\n");
94
+ if (lines.length === 0) return null;
95
+ const fontSize = 14;
96
+ const lineHeight = 20;
97
+ const longest = Math.max(...lines.map((line) => [...line].length));
98
+ const width = Math.min(maxWidth, Math.ceil(longest * fontSize * 0.62) + 2 * PADDING);
99
+ const height = lines.length * lineHeight + 2 * PADDING;
100
+ const svg = document.createElementNS(SVG_NS, "svg");
101
+ svg.setAttribute("viewBox", `0 0 ${String(width)} ${String(height)}`);
102
+ const text = document.createElementNS(SVG_NS, "text");
103
+ text.setAttribute("font-family", EXPORT_MONO_STACK);
104
+ text.setAttribute("font-size", String(fontSize));
105
+ text.setAttribute("fill", theme.colors.text);
106
+ text.setAttribute("xml:space", "preserve");
107
+ lines.forEach((line, index) => {
108
+ const span = document.createElementNS(SVG_NS, "tspan");
109
+ span.setAttribute("x", String(PADDING));
110
+ span.setAttribute("y", String(PADDING + (index + 0.75) * lineHeight));
111
+ span.textContent = line;
112
+ text.append(span);
113
+ });
114
+ svg.append(text);
115
+ return svgToPng(svg, width, height, options);
116
+ }
117
+ function isChrome(layer) {
118
+ const id = layer.id;
119
+ if (id === "bg" || id === "title" || id.endsWith("-title") || id.endsWith("-description")) {
120
+ return true;
121
+ }
122
+ if (id.endsWith("-empty")) return true;
123
+ return layer.type === "shape" && typeof layer.content.fill === "string" && layer.content.fill.includes("gradient(");
124
+ }
125
+ function withExportFont(layer) {
126
+ if (layer.type !== "text") return layer;
127
+ return {
128
+ ...layer,
129
+ content: { ...layer.content, style: { ...layer.content.style, fontFamily: EXPORT_FONT_STACK } }
130
+ };
131
+ }
132
+ async function settle() {
133
+ await document.fonts?.ready;
134
+ for (let frame = 0; frame < 2; frame++) {
135
+ await new Promise((resolve) => requestAnimationFrame(() => resolve()));
136
+ }
137
+ }
138
+ async function renderSquisqDiagramPicture(markdown, options = {}) {
139
+ const { theme, maxWidth } = settings(options);
140
+ const doc = markdownToDoc(parseMarkdown(markdown), { generateCoverBlock: false });
141
+ const block = flattenRenderableBlocks(doc.blocks)[0];
142
+ if (!block) return null;
143
+ const viewport = VIEWPORT_PRESETS.landscape;
144
+ const { layers } = materializeBlockLayers(block, { theme, viewport, persistentLayers: false });
145
+ const kept = layers.filter((layer) => !isChrome(layer)).map(withExportFont);
146
+ if (kept.length === 0) return null;
147
+ const host = document.createElement("div");
148
+ host.setAttribute("aria-hidden", "true");
149
+ host.style.cssText = `position:fixed;left:-${String(viewport.width * 2)}px;top:0;width:${String(
150
+ viewport.width
151
+ )}px;height:${String(viewport.height)}px;pointer-events:none;`;
152
+ document.body.append(host);
153
+ const root = createRoot(host);
154
+ try {
155
+ const pictureBlock = {
156
+ id: `squisq-export-${String(++pictureCount)}`,
157
+ startTime: 0,
158
+ duration: 0,
159
+ audioSegment: 0,
160
+ layers: kept
161
+ };
162
+ root.render(
163
+ createElement(BlockRenderer, {
164
+ block: pictureBlock,
165
+ blockTime: 0,
166
+ basePath: "",
167
+ viewport,
168
+ animationsEnabled: false,
169
+ theme
170
+ })
171
+ );
172
+ await settle();
173
+ const svg = host.querySelector("svg.block-svg");
174
+ if (!svg) return null;
175
+ const box = svg.getBBox();
176
+ if (box.width <= 0 || box.height <= 0) return null;
177
+ const x = box.x - PADDING;
178
+ const y = box.y - PADDING;
179
+ const contentWidth = box.width + 2 * PADDING;
180
+ const contentHeight = box.height + 2 * PADDING;
181
+ const cropped = svg.cloneNode(true);
182
+ cropped.setAttribute(
183
+ "viewBox",
184
+ `${String(x)} ${String(y)} ${String(contentWidth)} ${String(contentHeight)}`
185
+ );
186
+ cropped.removeAttribute("clip-path");
187
+ const width = Math.min(maxWidth, contentWidth / CANVAS_UNITS_PER_CSS_PIXEL);
188
+ return await svgToPng(cropped, width, contentHeight * width / contentWidth, options);
189
+ } finally {
190
+ root.unmount();
191
+ host.remove();
192
+ }
193
+ }
194
+ function createDiagramPictureRenderer(options = {}) {
195
+ return async (diagram) => {
196
+ switch (diagram.kind) {
197
+ case "mermaid":
198
+ return renderMermaidPicture(diagram.source, options);
199
+ case "tree":
200
+ return renderTreePicture(diagram.source, options);
201
+ default:
202
+ return renderSquisqDiagramPicture(diagram.markdown, options);
203
+ }
204
+ };
205
+ }
206
+ export {
207
+ EXPORT_FONT_STACK,
208
+ createDiagramPictureRenderer,
209
+ renderMermaidPicture,
210
+ renderSquisqDiagramPicture,
211
+ renderTreePicture,
212
+ svgToPng
213
+ };
@@ -6,7 +6,7 @@ import {
6
6
  useDocPlayback,
7
7
  useMediaSchedule,
8
8
  useViewportOrientation
9
- } from "../chunk-CYXFNHWK.js";
9
+ } from "../chunk-SVVPYDV3.js";
10
10
  import {
11
11
  useAutoSurface
12
12
  } from "../chunk-TT6ENR6T.js";
@@ -16,7 +16,7 @@ import {
16
16
  useMediaProvider,
17
17
  useMediaUrl,
18
18
  useResourcePolicy
19
- } from "../chunk-4L5MZ5OL.js";
19
+ } from "../chunk-63G5OJWY.js";
20
20
  export {
21
21
  MediaContext,
22
22
  ResourcePolicyContext,
package/dist/index.js CHANGED
@@ -15,31 +15,17 @@ import {
15
15
  formatTime,
16
16
  resolveDocPlayerAppearance,
17
17
  useMediaClipDurations
18
- } from "./chunk-5JGUJDPD.js";
18
+ } from "./chunk-CB2MCXJ6.js";
19
19
  import {
20
20
  PaginatedDocView
21
- } from "./chunk-UD3VI5UB.js";
21
+ } from "./chunk-PSAO2CTF.js";
22
22
  import {
23
- BlockRenderer,
24
23
  CanvasSection,
25
24
  LinearDocView,
26
25
  PageSectionView,
27
26
  PageViewContext,
28
27
  usePageView
29
- } from "./chunk-62UYYVCB.js";
30
- import {
31
- ImageLayer,
32
- MapLayer,
33
- MermaidLayer,
34
- PathLayer,
35
- ShapeLayer,
36
- TableLayer,
37
- TextLayer,
38
- TreeLayer,
39
- VideoLayer,
40
- getAnimationStyle,
41
- getTransitionClass
42
- } from "./chunk-GFUEFU6X.js";
28
+ } from "./chunk-2VSD55NT.js";
43
29
  import {
44
30
  useModalDialog
45
31
  } from "./chunk-65POMKHR.js";
@@ -48,11 +34,11 @@ import {
48
34
  useDocPlayback,
49
35
  useMediaSchedule,
50
36
  useViewportOrientation
51
- } from "./chunk-CYXFNHWK.js";
37
+ } from "./chunk-SVVPYDV3.js";
52
38
  import {
53
39
  JsonView,
54
40
  createJsonFormFenceRenderer
55
- } from "./chunk-VZ43ACHC.js";
41
+ } from "./chunk-L4LIIOU2.js";
56
42
  import {
57
43
  useAutoSurface
58
44
  } from "./chunk-TT6ENR6T.js";
@@ -64,17 +50,33 @@ import {
64
50
  MarkdownRenderer,
65
51
  useAnimatedImage,
66
52
  useFenceRenderers
67
- } from "./chunk-BSDTS72D.js";
53
+ } from "./chunk-2FZUFQO3.js";
54
+ import {
55
+ BlockRenderer
56
+ } from "./chunk-5FJZDX7H.js";
57
+ import {
58
+ ImageLayer,
59
+ MapLayer,
60
+ MermaidLayer,
61
+ PathLayer,
62
+ ShapeLayer,
63
+ TableLayer,
64
+ TextLayer,
65
+ TreeLayer,
66
+ VideoLayer,
67
+ getAnimationStyle,
68
+ getTransitionClass
69
+ } from "./chunk-PZCWGBRQ.js";
68
70
  import {
69
71
  MermaidDiagram
70
- } from "./chunk-RJ3SNKB7.js";
72
+ } from "./chunk-QXZXQPL3.js";
71
73
  import {
72
74
  MediaContext,
73
75
  ResourcePolicyContext,
74
76
  useMediaProvider,
75
77
  useMediaUrl,
76
78
  useResourcePolicy
77
- } from "./chunk-4L5MZ5OL.js";
79
+ } from "./chunk-63G5OJWY.js";
78
80
  export {
79
81
  AnimatedImageControls,
80
82
  BlockRenderer,
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  JsonView,
3
3
  createJsonFormFenceRenderer
4
- } from "../chunk-VZ43ACHC.js";
4
+ } from "../chunk-L4LIIOU2.js";
5
5
  import "../chunk-TT6ENR6T.js";
6
- import "../chunk-BSDTS72D.js";
7
- import "../chunk-RJ3SNKB7.js";
8
- import "../chunk-4L5MZ5OL.js";
6
+ import "../chunk-2FZUFQO3.js";
7
+ import "../chunk-QXZXQPL3.js";
8
+ import "../chunk-63G5OJWY.js";
9
9
  export {
10
10
  JsonView,
11
11
  createJsonFormFenceRenderer
@@ -8,9 +8,9 @@ import {
8
8
  TextLayer,
9
9
  TreeLayer,
10
10
  VideoLayer
11
- } from "../chunk-GFUEFU6X.js";
12
- import "../chunk-RJ3SNKB7.js";
13
- import "../chunk-4L5MZ5OL.js";
11
+ } from "../chunk-PZCWGBRQ.js";
12
+ import "../chunk-QXZXQPL3.js";
13
+ import "../chunk-63G5OJWY.js";
14
14
  export {
15
15
  ImageLayer,
16
16
  MapLayer,
@@ -2,11 +2,11 @@ import {
2
2
  FenceRendererContext,
3
3
  MarkdownRenderer,
4
4
  useFenceRenderers
5
- } from "../chunk-BSDTS72D.js";
5
+ } from "../chunk-2FZUFQO3.js";
6
6
  import {
7
7
  MermaidDiagram
8
- } from "../chunk-RJ3SNKB7.js";
9
- import "../chunk-4L5MZ5OL.js";
8
+ } from "../chunk-QXZXQPL3.js";
9
+ import "../chunk-63G5OJWY.js";
10
10
  export {
11
11
  FenceRendererContext,
12
12
  MarkdownRenderer,
@@ -1,18 +1,19 @@
1
1
  import {
2
2
  PaginatedDocView
3
- } from "../chunk-UD3VI5UB.js";
3
+ } from "../chunk-PSAO2CTF.js";
4
4
  import {
5
5
  CanvasSection,
6
6
  LinearDocView,
7
7
  PageSectionView,
8
8
  PageViewContext,
9
9
  usePageView
10
- } from "../chunk-62UYYVCB.js";
11
- import "../chunk-GFUEFU6X.js";
10
+ } from "../chunk-2VSD55NT.js";
12
11
  import "../chunk-TT6ENR6T.js";
13
- import "../chunk-BSDTS72D.js";
14
- import "../chunk-RJ3SNKB7.js";
15
- import "../chunk-4L5MZ5OL.js";
12
+ import "../chunk-2FZUFQO3.js";
13
+ import "../chunk-5FJZDX7H.js";
14
+ import "../chunk-PZCWGBRQ.js";
15
+ import "../chunk-QXZXQPL3.js";
16
+ import "../chunk-63G5OJWY.js";
16
17
  export {
17
18
  CanvasSection,
18
19
  LinearDocView,
@@ -12,19 +12,20 @@ import {
12
12
  MediaClipLayer,
13
13
  SocialCaptionOverlay,
14
14
  formatTime
15
- } from "../chunk-5JGUJDPD.js";
16
- import {
17
- BlockRenderer
18
- } from "../chunk-62UYYVCB.js";
19
- import "../chunk-GFUEFU6X.js";
20
- import "../chunk-CYXFNHWK.js";
15
+ } from "../chunk-CB2MCXJ6.js";
16
+ import "../chunk-2VSD55NT.js";
17
+ import "../chunk-SVVPYDV3.js";
21
18
  import "../chunk-TT6ENR6T.js";
22
19
  import {
23
20
  InlineAudioPlayer,
24
21
  InlineVideoPlayer
25
- } from "../chunk-BSDTS72D.js";
26
- import "../chunk-RJ3SNKB7.js";
27
- import "../chunk-4L5MZ5OL.js";
22
+ } from "../chunk-2FZUFQO3.js";
23
+ import {
24
+ BlockRenderer
25
+ } from "../chunk-5FJZDX7H.js";
26
+ import "../chunk-PZCWGBRQ.js";
27
+ import "../chunk-QXZXQPL3.js";
28
+ import "../chunk-63G5OJWY.js";
28
29
  export {
29
30
  BlockRenderer,
30
31
  CaptionOverlay,