Skip to content

Commit

Permalink
Merge pull request #26 from terrastruct/bernie/max-container-height
Browse files Browse the repository at this point in the history
settings: add max container height
  • Loading branch information
Bernard Xie authored Mar 1, 2023
2 parents dd619d4 + d3f3969 commit 39a20a6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Settings > Community plugins > Browse > Search for "D2"
- `Theme ID`: For a list of available themes, visit the [D2 repository](https://github.com/terrastruct/d2/tree/master/d2themes).
- `Pad`: Number of pixels padded around the rendered diagram.
- `Sketch mode`: Render the diagram to look like it was sketched by hand.
- `Container height`: Diagram max render height in pixels (Requires d2 v0.2.2 and up).
- `Debounce`: Number of milliseconds to wait after a change has made to refresh the diagram (min 100).
- `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.

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "d2-obsidian",
"name": "D2",
"version": "1.1.1",
"version": "1.1.2",
"minAppVersion": "0.15.0",
"description": "The official D2 plugin for Obsidian. D2 is a modern diagram scripting language that turns text to diagrams.",
"author": "Terrastruct",
Expand Down
17 changes: 11 additions & 6 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ export class D2Processor {
insertImage(image: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) {
const parser = new DOMParser();
const svg = parser.parseFromString(image, "image/svg+xml");
const svgEl = svg.documentElement;
const containerEl = el.createDiv();

containerEl.style.maxHeight = `${this.plugin.settings.containerHeight}px`;
containerEl.style.height = "100vh";
containerEl.style.width = "100%";
containerEl.style.position = "relative";

// Have svg image be contained within the obsidian window
svgEl.setAttribute("preserveAspectRatio", "xMinYMin slice");
svgEl.removeAttribute("width");
svgEl.removeAttribute("height");
const svgEl = svg.documentElement;
svgEl.style.position = "absolute";
svgEl.style.width = "100%";
svgEl.style.height = "100%";

el.insertAdjacentHTML("beforeend", this.sanitizeSVGIDs(svgEl, ctx.docId));
containerEl.innerHTML = this.sanitizeSVGIDs(svgEl, ctx.docId);
}

export = async (
Expand Down
26 changes: 26 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface D2PluginSettings {
d2Path: string;
pad: number;
sketch: boolean;
containerHeight: number;
}

export const DEFAULT_SETTINGS: D2PluginSettings = {
Expand All @@ -21,6 +22,7 @@ export const DEFAULT_SETTINGS: D2PluginSettings = {
d2Path: "",
pad: 100,
sketch: false,
containerHeight: 800,
};

export class D2SettingsTab extends PluginSettingTab {
Expand Down Expand Up @@ -136,6 +138,30 @@ export class D2SettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Container height")
.setDesc("Diagram max render height in pixels (Requires d2 v0.2.2 and up)")
.addText((text) =>
text
.setPlaceholder(String(DEFAULT_SETTINGS.containerHeight))
.setValue(String(this.plugin.settings.containerHeight))
.onChange(async (value) => {
if (isNaN(Number(value))) {
new Notice("Please specify a valid number");
this.plugin.settings.containerHeight = Number(
DEFAULT_SETTINGS.containerHeight
);
} else if (value === "") {
this.plugin.settings.containerHeight = Number(
DEFAULT_SETTINGS.containerHeight
);
} else {
this.plugin.settings.containerHeight = Number(value);
}
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Debounce")
.setDesc("How often should the diagram refresh in milliseconds (min 100)")
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.1.2": "0.15.0",
"1.1.1": "0.15.0",
"1.1.0": "0.15.0",
"1.0.1": "0.15.0",
Expand Down

0 comments on commit 39a20a6

Please sign in to comment.