Skip to content

Commit abe0dfd

Browse files
committed
improve sizing of mermaid diagrams in narrow screens
1 parent e14ae07 commit abe0dfd

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

src/core/svg.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ export async function resolveSize(
8080
widthInInches = svgWidthInInches;
8181
}
8282

83+
console.log({ options });
8384
return {
8485
widthInInches,
8586
heightInInches,
8687
widthInPoints: Math.round(widthInInches * 96),
8788
heightInPoints: Math.round(heightInInches * 96),
89+
explicitWidth: options?.[kFigWidth] !== undefined,
90+
explicitHeight: options?.[kFigHeight] !== undefined,
8891
};
8992
}
9093

@@ -96,13 +99,13 @@ export const fixupAlignment = (svg: Element, align: string) => {
9699

97100
switch (align) {
98101
case "left":
99-
style = `${style} display: block; margin: auto auto auto 0`;
102+
style = `${style}; display: block; margin: auto auto auto 0`;
100103
break;
101104
case "right":
102-
style = `${style} display: block; margin: auto 0 auto auto`;
105+
style = `${style}; display: block; margin: auto 0 auto auto`;
103106
break;
104107
case "center":
105-
style = `${style} display: block; margin: auto auto auto auto`;
108+
style = `${style}; display: block; margin: auto auto auto auto`;
106109
break;
107110
}
108111
svg.setAttribute("style", style);
@@ -120,15 +123,46 @@ export async function setSvgSize(
120123
const {
121124
widthInPoints,
122125
heightInPoints,
126+
explicitHeight,
127+
explicitWidth,
123128
} = await resolveSize(mappedSvgSrc.value, options);
124129

130+
console.log({
131+
widthInPoints,
132+
heightInPoints,
133+
explicitHeight,
134+
explicitWidth,
135+
});
136+
125137
const dom = (await getDomParser()).parseFromString(
126138
mappedSvgSrc.value,
127139
"text/html",
128140
);
129141
const svg = dom!.querySelector("svg")!;
130-
svg.setAttribute("width", widthInPoints);
131-
svg.setAttribute("height", heightInPoints);
142+
if (explicitWidth && explicitHeight) {
143+
svg.setAttribute("width", widthInPoints);
144+
svg.setAttribute("height", heightInPoints);
145+
// if explicit width and height are given, we need to remove max-width and max-height
146+
// so that the figure doesn't get squished.
147+
svg.setAttribute(
148+
"style",
149+
(svg.attributes.getNamedItem("style")?.value || "") +
150+
"; max-width: none; max-height: none",
151+
);
152+
} else {
153+
// we don't have access to svg.style as a property here...
154+
// so we have to do it the roundabout way.
155+
let style = svg.attributes.getNamedItem("style")?.value || "";
156+
if (explicitWidth) {
157+
style = `${style}; max-width: ${widthInPoints}px`;
158+
}
159+
if (explicitHeight) {
160+
style = `${style}; max-height: ${heightInPoints}px`;
161+
}
162+
if (explicitWidth || explicitHeight) {
163+
svg.setAttribute("style", style);
164+
}
165+
}
132166
if (extra) {
133167
extra(svg);
134168
}

src/resources/formats/html/mermaid/mermaid-init.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ const _quartoMermaid = {
3030
// in `core/svg.ts`.
3131
// if you change something here, you must keep it consistent there as well.
3232
setSvgSize(svg) {
33-
const { widthInPoints, heightInPoints } = this.resolveSize(svg);
33+
const { widthInPoints, heightInPoints, explicitHeight, explicitWidth } =
34+
this.resolveSize(svg);
3435

35-
svg.setAttribute("width", widthInPoints);
36-
svg.setAttribute("height", heightInPoints);
37-
svg.style.maxWidth = null; // clear preset mermaid value.
36+
if (explicitWidth && explicitHeight) {
37+
svg.setAttribute("width", widthInPoints);
38+
svg.setAttribute("height", heightInPoints);
39+
svg.style.maxWidth = null; // remove mermaid's default max-width
40+
} else {
41+
if (explicitWidth) {
42+
svg.style.maxWidth = `${widthInPoints}px`;
43+
}
44+
if (explicitHeight) {
45+
svg.style.maxHeight = `${heightInPoints}px`;
46+
}
47+
}
3848
},
3949

4050
// NB: there's effectively a copy of this function
@@ -62,13 +72,13 @@ const _quartoMermaid = {
6272

6373
switch (align) {
6474
case "left":
65-
style = `${style} display: block; margin: auto auto auto 0`;
75+
style = `${style}; display: block; margin: auto auto auto 0`;
6676
break;
6777
case "right":
68-
style = `${style} display: block; margin: auto 0 auto auto`;
78+
style = `${style}; display: block; margin: auto 0 auto auto`;
6979
break;
7080
case "center":
71-
style = `${style} display: block; margin: auto auto auto auto`;
81+
style = `${style}; display: block; margin: auto auto auto auto`;
7282
break;
7383
}
7484
svg.setAttribute("style", style);
@@ -162,6 +172,8 @@ const _quartoMermaid = {
162172
heightInInches,
163173
widthInPoints: Math.round(widthInInches * 96),
164174
heightInPoints: Math.round(heightInInches * 96),
175+
explicitWidth: options?.[kFigWidth] !== undefined,
176+
explicitHeight: options?.[kFigHeight] !== undefined,
165177
};
166178
},
167179

0 commit comments

Comments
 (0)