Skip to content

Commit 39a20a6

Browse files
author
Bernard Xie
authored
Merge pull request #26 from terrastruct/bernie/max-container-height
settings: add max container height
2 parents dd619d4 + d3f3969 commit 39a20a6

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Settings > Community plugins > Browse > Search for "D2"
2828
- `Theme ID`: For a list of available themes, visit the [D2 repository](https://github.com/terrastruct/d2/tree/master/d2themes).
2929
- `Pad`: Number of pixels padded around the rendered diagram.
3030
- `Sketch mode`: Render the diagram to look like it was sketched by hand.
31+
- `Container height`: Diagram max render height in pixels (Requires d2 v0.2.2 and up).
3132
- `Debounce`: Number of milliseconds to wait after a change has made to refresh the diagram (min 100).
3233
- `Path`: Customize the path to `d2` (optional). We check common places D2 might be installed, along with your system path. However, your OS or setup may require you to input your path to `d2` manually. To do so, type `where d2` into your terminal, and copy everything in the path up until `/d2` and paste it into this configuration.
3334

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "d2-obsidian",
33
"name": "D2",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"minAppVersion": "0.15.0",
66
"description": "The official D2 plugin for Obsidian. D2 is a modern diagram scripting language that turns text to diagrams.",
77
"author": "Terrastruct",

src/processor.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ export class D2Processor {
6969
insertImage(image: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) {
7070
const parser = new DOMParser();
7171
const svg = parser.parseFromString(image, "image/svg+xml");
72-
const svgEl = svg.documentElement;
72+
const containerEl = el.createDiv();
73+
74+
containerEl.style.maxHeight = `${this.plugin.settings.containerHeight}px`;
75+
containerEl.style.height = "100vh";
76+
containerEl.style.width = "100%";
77+
containerEl.style.position = "relative";
7378

74-
// Have svg image be contained within the obsidian window
75-
svgEl.setAttribute("preserveAspectRatio", "xMinYMin slice");
76-
svgEl.removeAttribute("width");
77-
svgEl.removeAttribute("height");
79+
const svgEl = svg.documentElement;
80+
svgEl.style.position = "absolute";
81+
svgEl.style.width = "100%";
82+
svgEl.style.height = "100%";
7883

79-
el.insertAdjacentHTML("beforeend", this.sanitizeSVGIDs(svgEl, ctx.docId));
84+
containerEl.innerHTML = this.sanitizeSVGIDs(svgEl, ctx.docId);
8085
}
8186

8287
export = async (

src/settings.ts

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface D2PluginSettings {
1111
d2Path: string;
1212
pad: number;
1313
sketch: boolean;
14+
containerHeight: number;
1415
}
1516

1617
export const DEFAULT_SETTINGS: D2PluginSettings = {
@@ -21,6 +22,7 @@ export const DEFAULT_SETTINGS: D2PluginSettings = {
2122
d2Path: "",
2223
pad: 100,
2324
sketch: false,
25+
containerHeight: 800,
2426
};
2527

2628
export class D2SettingsTab extends PluginSettingTab {
@@ -136,6 +138,30 @@ export class D2SettingsTab extends PluginSettingTab {
136138
})
137139
);
138140

141+
new Setting(containerEl)
142+
.setName("Container height")
143+
.setDesc("Diagram max render height in pixels (Requires d2 v0.2.2 and up)")
144+
.addText((text) =>
145+
text
146+
.setPlaceholder(String(DEFAULT_SETTINGS.containerHeight))
147+
.setValue(String(this.plugin.settings.containerHeight))
148+
.onChange(async (value) => {
149+
if (isNaN(Number(value))) {
150+
new Notice("Please specify a valid number");
151+
this.plugin.settings.containerHeight = Number(
152+
DEFAULT_SETTINGS.containerHeight
153+
);
154+
} else if (value === "") {
155+
this.plugin.settings.containerHeight = Number(
156+
DEFAULT_SETTINGS.containerHeight
157+
);
158+
} else {
159+
this.plugin.settings.containerHeight = Number(value);
160+
}
161+
await this.plugin.saveSettings();
162+
})
163+
);
164+
139165
new Setting(containerEl)
140166
.setName("Debounce")
141167
.setDesc("How often should the diagram refresh in milliseconds (min 100)")

versions.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"1.1.2": "0.15.0",
23
"1.1.1": "0.15.0",
34
"1.1.0": "0.15.0",
45
"1.0.1": "0.15.0",

0 commit comments

Comments
 (0)