@bendyline/squisq-react 2.12.1 → 2.12.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.
@@ -2,7 +2,7 @@ import {
2
2
  renderMermaidSvg,
3
3
  useMediaProvider,
4
4
  useResourcePolicy
5
- } from "./chunk-HCKL2AFN.js";
5
+ } from "./chunk-JP2VHB5H.js";
6
6
 
7
7
  // src/hooks/useMediaSchedule.ts
8
8
  import { useMemo } from "react";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  LinearDocView
3
- } from "./chunk-2K74UJ4T.js";
3
+ } from "./chunk-PVRZGR5J.js";
4
4
 
5
5
  // src/PaginatedDocView.tsx
6
6
  import {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  mermaidRenderErrorMessage,
3
3
  renderMermaidSvg
4
- } from "./chunk-HCKL2AFN.js";
4
+ } from "./chunk-JP2VHB5H.js";
5
5
 
6
6
  // src/mermaid/MermaidDiagram.tsx
7
7
  import { useEffect, useState } from "react";
@@ -1,8 +1,70 @@
1
+ // src/svgIsland/svgImageUrl.ts
2
+ import { useEffect, useState } from "react";
3
+ import {
4
+ parseSvgIsland,
5
+ svgIslandDataUrl,
6
+ svgSourceFromBytes,
7
+ svgSourceFromDataUrl
8
+ } from "@bendyline/squisq/markdown";
9
+ function isLocalSvgUrl(url) {
10
+ if (/^(?:blob|data):/i.test(url)) return true;
11
+ return !/^[a-z][a-z0-9+.-]*:/i.test(url) && !url.startsWith("//");
12
+ }
13
+ var CACHE_LIMIT = 64;
14
+ var cache = /* @__PURE__ */ new Map();
15
+ async function load(url, isStatic) {
16
+ let source = svgSourceFromDataUrl(url);
17
+ if (source === null) {
18
+ const response = await fetch(url);
19
+ if (!response.ok) return "";
20
+ source = svgSourceFromBytes(await response.arrayBuffer());
21
+ }
22
+ const result = parseSvgIsland(source, { static: isStatic });
23
+ return result.ok ? svgIslandDataUrl(result.island) : "";
24
+ }
25
+ function sanitizedSvgImageUrl(url, options = {}) {
26
+ const isStatic = options.static === true;
27
+ const key = `${isStatic ? "s" : "a"}|${url}`;
28
+ let pending = cache.get(key);
29
+ if (!pending) {
30
+ pending = load(url, isStatic).catch(() => "");
31
+ cache.set(key, pending);
32
+ if (cache.size > CACHE_LIMIT) {
33
+ const oldest = cache.keys().next().value;
34
+ if (oldest !== void 0) cache.delete(oldest);
35
+ }
36
+ }
37
+ return pending;
38
+ }
39
+ function svgDataImageSrc(src) {
40
+ const trimmed = src?.trim() ?? "";
41
+ return /^data:image\/svg\+xml[;,]/i.test(trimmed) ? trimmed : null;
42
+ }
43
+ function useSanitizedSvgImage(url, isSvg, options = {}) {
44
+ const isStatic = options.static === true;
45
+ const needsSanitizing = isSvg && url !== "" && isLocalSvgUrl(url);
46
+ const [sanitized, setSanitized] = useState(null);
47
+ useEffect(() => {
48
+ if (!needsSanitizing) return;
49
+ let cancelled = false;
50
+ void sanitizedSvgImageUrl(url, { static: isStatic }).then((result) => {
51
+ if (!cancelled) setSanitized({ url, isStatic, result });
52
+ });
53
+ return () => {
54
+ cancelled = true;
55
+ };
56
+ }, [needsSanitizing, url, isStatic]);
57
+ if (!needsSanitizing) return url;
58
+ if (sanitized?.url !== url || sanitized.isStatic !== isStatic) return void 0;
59
+ return sanitized.result || null;
60
+ }
61
+
1
62
  // src/hooks/MediaContext.tsx
2
- import { createContext, useContext, useState, useEffect, useMemo } from "react";
63
+ import { createContext, useCallback, useContext, useState as useState2, useEffect as useEffect2, useMemo } from "react";
3
64
  import {
4
65
  DEFAULT_INTERACTIVE_RESOURCE_POLICY,
5
- isResourceUrlAllowed
66
+ isResourceUrlAllowed,
67
+ isSvgImageSource
6
68
  } from "@bendyline/squisq/markdown";
7
69
  var MediaContext = createContext(null);
8
70
  var ResourcePolicyContext = createContext(
@@ -14,18 +76,23 @@ function useResourcePolicy() {
14
76
  function useMediaProvider() {
15
77
  return useContext(MediaContext);
16
78
  }
17
- function useMediaUrl(relativePath, basePath) {
79
+ function useMediaUrl(relativePath, basePath, options = {}) {
18
80
  const provider = useMediaProvider();
19
81
  const resourcePolicy = useResourcePolicy();
20
82
  const safePath = typeof relativePath === "string" ? relativePath : "";
83
+ const isSvg = isSvgImageSource(safePath);
84
+ const allowed = useCallback(
85
+ (url2) => isResourceUrlAllowed(url2, resourcePolicy) || isSvg && /^data:image\/svg\+xml[;,]/i.test(url2),
86
+ [resourcePolicy, isSvg]
87
+ );
21
88
  const isAbsolute = !safePath || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(safePath);
22
89
  const fallback = useMemo(() => {
23
90
  const candidate = isAbsolute ? safePath : `${basePath.replace(/\/$/, "")}/${safePath}`;
24
- return isResourceUrlAllowed(candidate, resourcePolicy) ? candidate : "";
25
- }, [isAbsolute, safePath, basePath, resourcePolicy]);
91
+ return allowed(candidate) ? candidate : "";
92
+ }, [isAbsolute, safePath, basePath, allowed]);
26
93
  const needsProvider = !isAbsolute && !!provider;
27
- const [resolved, setResolved] = useState(null);
28
- useEffect(() => {
94
+ const [resolved, setResolved] = useState2(null);
95
+ useEffect2(() => {
29
96
  if (!needsProvider) {
30
97
  return;
31
98
  }
@@ -37,7 +104,7 @@ function useMediaUrl(relativePath, basePath) {
37
104
  path: safePath,
38
105
  provider,
39
106
  policy: resourcePolicy,
40
- url: isResourceUrlAllowed(resolved2, resourcePolicy) ? resolved2 : ""
107
+ url: allowed(resolved2) ? resolved2 : ""
41
108
  });
42
109
  }
43
110
  },
@@ -49,8 +116,9 @@ function useMediaUrl(relativePath, basePath) {
49
116
  return () => {
50
117
  cancelled = true;
51
118
  };
52
- }, [needsProvider, provider, safePath, fallback, resourcePolicy]);
53
- return !needsProvider ? fallback : resolved?.path === safePath && resolved.provider === provider && resolved.policy === resourcePolicy ? resolved.url : "";
119
+ }, [needsProvider, provider, safePath, fallback, resourcePolicy, allowed]);
120
+ const url = !needsProvider ? fallback : resolved?.path === safePath && resolved.provider === provider && resolved.policy === resourcePolicy ? resolved.url : "";
121
+ return useSanitizedSvgImage(url, isSvg, { static: options.staticSvg === true }) ?? "";
54
122
  }
55
123
 
56
124
  // src/mermaid/mermaidRuntime.ts
@@ -116,6 +184,10 @@ function mermaidRenderErrorMessage(error) {
116
184
  }
117
185
 
118
186
  export {
187
+ isLocalSvgUrl,
188
+ sanitizedSvgImageUrl,
189
+ svgDataImageSrc,
190
+ useSanitizedSvgImage,
119
191
  MediaContext,
120
192
  ResourcePolicyContext,
121
193
  useResourcePolicy,
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  MermaidDiagram
3
- } from "./chunk-Z7OW6RGR.js";
3
+ } from "./chunk-EY3DUCK7.js";
4
4
  import {
5
5
  useMediaUrl,
6
6
  useResourcePolicy
7
- } from "./chunk-HCKL2AFN.js";
7
+ } from "./chunk-JP2VHB5H.js";
8
8
 
9
9
  // src/utils/animationUtils.ts
10
10
  import {
@@ -51,7 +51,8 @@ function ImageLayer({
51
51
  basePath,
52
52
  viewport,
53
53
  blockTime,
54
- animationsEnabled = true
54
+ animationsEnabled = true,
55
+ isStatic = false
55
56
  }) {
56
57
  const { content, position, animation } = layer;
57
58
  const x = resolveValue(position.x, viewport.width);
@@ -61,7 +62,7 @@ function ImageLayer({
61
62
  const offset = getAnchorOffset(position.anchor, width, height);
62
63
  const finalX = x + offset.x;
63
64
  const finalY = y + offset.y;
64
- const src = useMediaUrl(content.src, basePath);
65
+ const src = useMediaUrl(content.src, basePath, { staticSvg: isStatic });
65
66
  const animStyle = getAnimationStyle(animation, blockTime);
66
67
  const filter = cssFilterForTreatment(content.treatment, content.blur);
67
68
  const preserveAspectRatio = getPreserveAspectRatio(content.fit);
@@ -2061,6 +2062,80 @@ function MermaidLayer({ layer, viewport, blockTime, theme }) {
2061
2062
  );
2062
2063
  }
2063
2064
 
2065
+ // src/layers/SvgLayer.tsx
2066
+ import { useMemo as useMemo2 } from "react";
2067
+ import { parseSvgIsland, svgIslandDataUrl } from "@bendyline/squisq/markdown";
2068
+ import { jsx as jsx11, jsxs as jsxs8 } from "react/jsx-runtime";
2069
+ function SvgLayer({ layer, viewport, blockTime, isStatic = false }) {
2070
+ const { position, content, animation } = layer;
2071
+ const width = resolveValue(position.width ?? viewport.width, viewport.width);
2072
+ const height = resolveValue(position.height ?? viewport.height, viewport.height);
2073
+ const offset = getAnchorOffset(position.anchor, width, height);
2074
+ const x = resolveValue(position.x, viewport.width) + offset.x;
2075
+ const y = resolveValue(position.y, viewport.height) + offset.y;
2076
+ const result = useMemo2(
2077
+ () => parseSvgIsland(content.source, { static: isStatic }),
2078
+ [content.source, isStatic]
2079
+ );
2080
+ const href = useMemo2(
2081
+ () => result.ok ? svgIslandDataUrl(result.island, { color: content.color }) : null,
2082
+ [result, content.color]
2083
+ );
2084
+ const animStyle = getAnimationStyle(animation, blockTime);
2085
+ const label = content.alt ?? (result.ok ? result.island.title : void 0) ?? "Illustration";
2086
+ return /* @__PURE__ */ jsx11(
2087
+ "g",
2088
+ {
2089
+ className: `block-layer block-layer--svg ${animStyle.className}`,
2090
+ "data-layer-id": layer.id,
2091
+ style: animStyle.style,
2092
+ children: href ? /* @__PURE__ */ jsx11(
2093
+ "image",
2094
+ {
2095
+ href,
2096
+ x,
2097
+ y,
2098
+ width,
2099
+ height,
2100
+ preserveAspectRatio: "xMidYMid meet",
2101
+ role: "img",
2102
+ "aria-label": label,
2103
+ style: { pointerEvents: "none" }
2104
+ }
2105
+ ) : /* @__PURE__ */ jsxs8("g", { role: "img", "aria-label": "SVG that could not be rendered", children: [
2106
+ /* @__PURE__ */ jsx11(
2107
+ "rect",
2108
+ {
2109
+ x,
2110
+ y,
2111
+ width,
2112
+ height,
2113
+ rx: Math.min(width, height) * 0.04,
2114
+ fill: "none",
2115
+ stroke: content.color ?? "currentColor",
2116
+ strokeOpacity: 0.35,
2117
+ strokeWidth: 2,
2118
+ strokeDasharray: "8 6"
2119
+ }
2120
+ ),
2121
+ /* @__PURE__ */ jsx11(
2122
+ "text",
2123
+ {
2124
+ x: x + width / 2,
2125
+ y: y + height / 2,
2126
+ textAnchor: "middle",
2127
+ dominantBaseline: "middle",
2128
+ fill: content.color ?? "currentColor",
2129
+ fillOpacity: 0.6,
2130
+ fontSize: Math.max(12, Math.min(width, height) * 0.06),
2131
+ children: "Couldn\u2019t render this SVG"
2132
+ }
2133
+ )
2134
+ ] })
2135
+ }
2136
+ );
2137
+ }
2138
+
2064
2139
  export {
2065
2140
  isCaptureDrivenVideo,
2066
2141
  seekVideoToFrame,
@@ -2076,5 +2151,6 @@ export {
2076
2151
  VideoLayer,
2077
2152
  TableLayer,
2078
2153
  TreeLayer,
2079
- MermaidLayer
2154
+ MermaidLayer,
2155
+ SvgLayer
2080
2156
  };
@@ -4,6 +4,7 @@ import {
4
4
  MermaidLayer,
5
5
  PathLayer,
6
6
  ShapeLayer,
7
+ SvgLayer,
7
8
  TableLayer,
8
9
  TextLayer,
9
10
  TreeLayer,
@@ -11,7 +12,7 @@ import {
11
12
  getAnchorOffset,
12
13
  getTransitionClass,
13
14
  resolveValue
14
- } from "./chunk-3RVUSY4V.js";
15
+ } from "./chunk-LWB4AM3W.js";
15
16
 
16
17
  // src/BlockRenderer.tsx
17
18
  import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
@@ -268,7 +269,8 @@ function BlockRenderer({
268
269
  theme,
269
270
  tableContentRenderer,
270
271
  growTextToFit = false,
271
- videoEmbeds = "poster"
272
+ videoEmbeds = "poster",
273
+ staticMedia = false
272
274
  }) {
273
275
  let transitionClass = "";
274
276
  const transitionStyle = {};
@@ -400,7 +402,8 @@ function BlockRenderer({
400
402
  theme,
401
403
  tableContentRenderer,
402
404
  textScale,
403
- videoEmbeds
405
+ videoEmbeds,
406
+ staticMedia: staticMedia || reducedMotion || !animationsEnabled
404
407
  },
405
408
  layer.id
406
409
  )) })
@@ -420,7 +423,8 @@ function LayerRenderer({
420
423
  theme,
421
424
  tableContentRenderer,
422
425
  textScale,
423
- videoEmbeds
426
+ videoEmbeds,
427
+ staticMedia
424
428
  }) {
425
429
  const renderedLayer = animationsEnabled || !layer.animation ? layer : { ...layer, animation: void 0 };
426
430
  switch (renderedLayer.type) {
@@ -432,7 +436,8 @@ function LayerRenderer({
432
436
  basePath,
433
437
  viewport,
434
438
  blockTime,
435
- animationsEnabled
439
+ animationsEnabled,
440
+ isStatic: staticMedia
436
441
  }
437
442
  );
438
443
  case "text":
@@ -496,6 +501,16 @@ function LayerRenderer({
496
501
  theme
497
502
  }
498
503
  );
504
+ case "svg":
505
+ return /* @__PURE__ */ jsx2(
506
+ SvgLayer,
507
+ {
508
+ layer: renderedLayer,
509
+ viewport,
510
+ blockTime,
511
+ isStatic: staticMedia
512
+ }
513
+ );
499
514
  default:
500
515
  console.warn(`Unknown layer type: ${renderedLayer.type}`);
501
516
  return null;
@@ -1,30 +1,30 @@
1
1
  import {
2
2
  LinearDocView
3
- } from "./chunk-2K74UJ4T.js";
3
+ } from "./chunk-PVRZGR5J.js";
4
4
  import {
5
5
  useAudioSync,
6
6
  useDocPlayback,
7
7
  useMediaSchedule,
8
8
  useViewportOrientation
9
- } from "./chunk-AA6NGEP7.js";
9
+ } from "./chunk-3H6QBEYX.js";
10
10
  import {
11
11
  useAutoSurface
12
12
  } from "./chunk-TT6ENR6T.js";
13
13
  import {
14
14
  MarkdownRenderer
15
- } from "./chunk-T7NVETOH.js";
15
+ } from "./chunk-YVQ7KEYI.js";
16
16
  import {
17
17
  BlockRenderer
18
- } from "./chunk-XSSIHHDY.js";
18
+ } from "./chunk-M7DM4NOX.js";
19
19
  import {
20
20
  isCaptureDrivenVideo,
21
21
  seekVideoToFrame
22
- } from "./chunk-3RVUSY4V.js";
22
+ } from "./chunk-LWB4AM3W.js";
23
23
  import {
24
24
  useMediaProvider,
25
25
  useMediaUrl,
26
26
  useResourcePolicy
27
- } from "./chunk-HCKL2AFN.js";
27
+ } from "./chunk-JP2VHB5H.js";
28
28
 
29
29
  // src/hooks/useBuildNavigation.ts
30
30
  import { useCallback, useEffect, useMemo, useState } from "react";
@@ -1621,6 +1621,23 @@ function DocControlsSlideshow({
1621
1621
  );
1622
1622
  }
1623
1623
 
1624
+ // src/utils/decodeSvgImages.ts
1625
+ async function decodeSvgImages(root) {
1626
+ if (typeof Image === "undefined") return;
1627
+ const hrefs = /* @__PURE__ */ new Set();
1628
+ for (const element of Array.from(root.querySelectorAll("svg image"))) {
1629
+ const href = element.getAttribute("href") ?? element.getAttribute("xlink:href");
1630
+ if (href && /^(?:data|blob):/i.test(href)) hrefs.add(href);
1631
+ }
1632
+ await Promise.allSettled(
1633
+ [...hrefs].map((href) => {
1634
+ const image = new Image();
1635
+ image.src = href;
1636
+ return typeof image.decode === "function" ? image.decode().catch(() => void 0) : Promise.resolve();
1637
+ })
1638
+ );
1639
+ }
1640
+
1624
1641
  // src/DashboardView.tsx
1625
1642
  import { Fragment as Fragment3, useEffect as useEffect5, useMemo as useMemo4, useRef as useRef4 } from "react";
1626
1643
  import { materializeDashboard, resolveThemeForDoc, VIEWPORT_PRESETS } from "@bendyline/squisq/doc";
@@ -1643,6 +1660,7 @@ async function waitForDashboardAssets(root) {
1643
1660
  });
1644
1661
  })
1645
1662
  );
1663
+ await decodeSvgImages(root);
1646
1664
  await new Promise((resolve) => {
1647
1665
  if (typeof requestAnimationFrame === "function") {
1648
1666
  requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
@@ -1819,7 +1837,8 @@ function DashboardView({
1819
1837
  muted,
1820
1838
  animationsEnabled,
1821
1839
  theme: activeTheme,
1822
- videoEmbeds: renderMode ? "placeholder" : "player"
1840
+ videoEmbeds: renderMode ? "placeholder" : "player",
1841
+ staticMedia: renderMode
1823
1842
  }
1824
1843
  ),
1825
1844
  cell.frame && cell.frame.overlayLayers.length > 0 && // Borders and accents ride above the block: a template that
@@ -3873,7 +3892,8 @@ function DocPlayerContent({
3873
3892
  theme: effectiveTheme,
3874
3893
  tableContentRenderer,
3875
3894
  growTextToFit: isSlideshowMode,
3876
- videoEmbeds: renderMode ? "placeholder" : "poster"
3895
+ videoEmbeds: renderMode ? "placeholder" : "poster",
3896
+ staticMedia: renderMode
3877
3897
  }
3878
3898
  ) }),
3879
3899
  animationsEnabled && !showCoverBlock && previousBlock && isExiting && // Keyed by block id so each block is its own DOM subtree: React never
@@ -3896,7 +3916,8 @@ function DocPlayerContent({
3896
3916
  theme: effectiveTheme,
3897
3917
  tableContentRenderer,
3898
3918
  growTextToFit: isSlideshowMode,
3899
- videoEmbeds: renderMode ? "placeholder" : "poster"
3919
+ videoEmbeds: renderMode ? "placeholder" : "poster",
3920
+ staticMedia: renderMode
3900
3921
  }
3901
3922
  ) }, previousBlock.id),
3902
3923
  currentBlock && /* @__PURE__ */ jsx10(
@@ -3924,7 +3945,8 @@ function DocPlayerContent({
3924
3945
  theme: effectiveTheme,
3925
3946
  tableContentRenderer,
3926
3947
  growTextToFit: isSlideshowMode,
3927
- videoEmbeds: renderMode ? "placeholder" : "player"
3948
+ videoEmbeds: renderMode ? "placeholder" : "player",
3949
+ staticMedia: renderMode
3928
3950
  }
3929
3951
  )
3930
3952
  },
@@ -4400,6 +4422,7 @@ export {
4400
4422
  DocProgressBar,
4401
4423
  DocControlsOverlay,
4402
4424
  DocControlsSlideshow,
4425
+ decodeSvgImages,
4403
4426
  DashboardView,
4404
4427
  FlashcardFaceView,
4405
4428
  FlashcardView,
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-TT6ENR6T.js";
4
4
  import {
5
5
  MarkdownRenderer
6
- } from "./chunk-T7NVETOH.js";
6
+ } from "./chunk-YVQ7KEYI.js";
7
7
 
8
8
  // src/jsonView/useJsonViewTokens.ts
9
9
  import { useMemo } from "react";
@@ -5,13 +5,14 @@ import {
5
5
  InlineVideoPlayer,
6
6
  MarkdownRenderer,
7
7
  VideoEmbedFrame
8
- } from "./chunk-T7NVETOH.js";
8
+ } from "./chunk-YVQ7KEYI.js";
9
9
  import {
10
10
  BlockRenderer
11
- } from "./chunk-XSSIHHDY.js";
11
+ } from "./chunk-M7DM4NOX.js";
12
12
  import {
13
+ svgDataImageSrc,
13
14
  useMediaUrl
14
- } from "./chunk-HCKL2AFN.js";
15
+ } from "./chunk-JP2VHB5H.js";
15
16
 
16
17
  // src/page/PageViewContext.ts
17
18
  import { createContext, useContext } from "react";
@@ -87,7 +88,7 @@ function PageImg({
87
88
  className
88
89
  }) {
89
90
  const { basePath } = usePageView();
90
- const safeSrc = sanitizeUrl(media.src, "media");
91
+ const safeSrc = sanitizeUrl(media.src, "media") ?? svgDataImageSrc(media.src);
91
92
  const resolved = useMediaUrl(safeSrc ?? "", basePath);
92
93
  if (!safeSrc) return null;
93
94
  return /* @__PURE__ */ jsx2(