Skip to content

Commit 2d53d2c

Browse files
committed
iframe experiment
1 parent 2052ba1 commit 2d53d2c

File tree

5 files changed

+106
-19
lines changed

5 files changed

+106
-19
lines changed

Diff for: packages/docs/src/content/docs/examples/d2-test.md

+30-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ title: d2 test
44

55
## Interesting ones
66

7-
### Image/icon
8-
9-
```d2 strategy=inline darkScheme=false
10-
my network: {
11-
icon: https://icons.terrastruct.com/infra/019-network.svg
12-
}
13-
github: {
14-
shape: image
15-
icon: https://icons.terrastruct.com/dev/github.svg
16-
}
17-
```
18-
197
### Interactive example
208

219
```d2 strategy=inline darkScheme=false graphFormat=dagre class=shadow svgo=false
@@ -31,6 +19,36 @@ a:@"shared/x.d2"
3119
a -> b
3220
```
3321

22+
## `strategy=iframe`
23+
24+
### Images/icons
25+
26+
```d2 strategy=iframe
27+
my-network: {
28+
icon: https://icons.terrastruct.com/infra/019-network.svg
29+
}
30+
github: {
31+
shape: image
32+
icon: https://icons.terrastruct.com/dev/github.svg
33+
}
34+
a
35+
b
36+
c
37+
```
38+
39+
### Link
40+
41+
```d2 strategy=iframe
42+
direction: right
43+
a -> b -> c -> d -> e
44+
a {
45+
link: https://example.com
46+
label: link
47+
}
48+
```
49+
50+
- `a` requires `target="_top"`
51+
3452
## containers
3553

3654
```d2

Diff for: packages/docs/src/content/docs/start-here/strategy.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ add `fsPath` (`public/beoe`) to `.gitignore`
7878
</figure>
7979
```
8080

81-
**Note**: this strategy requires to additional options:
81+
**Note**: this strategy requires two additional options:
8282

8383
```js
8484
use(rehypeDiagram, {
@@ -96,11 +96,10 @@ add `fsPath` (`public/beoe`) to `.gitignore`
9696

9797
**Note**:
9898

99-
- there is also `allowfullscreen`, `loading=lazy`, `role=img`
100-
- I wonder if there will be issues with fitting image or if it would allow navigation of top frame
101-
- `target=_top` + `allow-top-navigation`
99+
- there is issue with navigation of top frame: links need `target=_top`, iframe maybe needs `allow-top-navigation`
102100
- link preview won't work
103-
- maybe there will be issues with `@beoe/pan-zoom`
101+
- there are issues with `@beoe/pan-zoom`: gestures doesn't work, buttons work though
102+
- there is also `allowfullscreen`
104103

105104
## Pros and cons
106105

Diff for: packages/pan-zoom/css/PanZoomUi.css

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
}
1010

1111
.beoe svg,
12-
.beoe img {
12+
.beoe img,
13+
.beoe iframe {
1314
/* need to center smaller images to fix bug in zoom functionality */
1415
margin: auto;
1516
display: block;
@@ -22,6 +23,10 @@
2223
pointer-events: none;
2324
}
2425

26+
.beoe iframe {
27+
width: 100%;
28+
}
29+
2530
/* UI */
2631

2732
.beoe {

Diff for: packages/rehype-code-hook-img/src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ export type Strategy =
7272
*/
7373
| "data-url"
7474
/**
75-
* SVG stored as standalone file
75+
* SVG as standalone file in `img`
7676
*/
7777
| "file"
78+
/**
79+
* Experimental. SVG as standalone file in `iframe`
80+
*/
81+
| "iframe"
7882
/**
7983
* SVG as data-uri in img
8084
* @deprecated

Diff for: packages/rehype-code-hook-img/src/utils.ts

+61
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,39 @@ function image({
6666
}) {
6767
return h("img", {
6868
src: url ?? svgToMiniDataURI(svg!),
69+
// ...(width && height ? { style: `aspect-ratio: ${width} / ${height}` } : {}),
6970
...(url ? { loading: "lazy", decoding: "async" } : {}),
7071
...rest,
7172
});
7273
}
7374

75+
function iframe({
76+
svg,
77+
url,
78+
alt,
79+
width,
80+
height,
81+
...rest
82+
}: {
83+
svg?: string;
84+
url?: string;
85+
width?: string | number;
86+
height?: string | number;
87+
alt?: string;
88+
class?: string;
89+
}) {
90+
return h("iframe", {
91+
src: url,
92+
title: alt,
93+
...(width && height ? { style: `aspect-ratio: ${width} / ${height}` } : {}),
94+
loading: "lazy",
95+
role: "img",
96+
frameborder: "0",
97+
// allowfullscreen: true,
98+
...rest,
99+
});
100+
}
101+
74102
function figure(className: string | undefined, children: any[]) {
75103
return h(
76104
"figure",
@@ -217,6 +245,39 @@ export function svgStrategy(
217245
figure(className, [image({ width, height, alt, url })])
218246
);
219247
}
248+
case "iframe": {
249+
if (fsPath == undefined || webPath == undefined) return;
250+
251+
if (darkScheme === "class" && darkSvg) {
252+
return Promise.all([fileUrl(svg), fileUrl(darkSvg)]).then(
253+
([url, darkUrl]) =>
254+
figure(
255+
className,
256+
// wrap in additional div for svg-pan-zoom
257+
[
258+
h("div", [
259+
iframe({ width, height, alt, class: "beoe-light", url }),
260+
iframe({
261+
width,
262+
height,
263+
alt,
264+
class: "beoe-dark",
265+
url: darkUrl,
266+
}),
267+
]),
268+
]
269+
)
270+
);
271+
}
272+
273+
if (darkScheme === "media") {
274+
console.warn("darkScheme media doesn't work for iframe strategy");
275+
}
276+
277+
return fileUrl(svg).then((url) =>
278+
figure(className, [iframe({ width, height, alt, url })])
279+
);
280+
}
220281
default: {
221282
if (darkScheme === "class" && darkSvg) {
222283
const element = fromHtmlIsomorphic(removeWidthHeight(svg), {

0 commit comments

Comments
 (0)