Skip to content

Commit ed4fa44

Browse files
author
Bernard Xie
authored
Merge pull request #31 from terrastruct/bernie/29/pdf-exports
Fix PDF exports and Debouncer
2 parents 158be5b + ff87d14 commit ed4fa44

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

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.2",
4+
"version": "1.1.3",
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",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "d2-obsidian",
3-
"version": "1.1.0",
3+
"version": "1.1.3",
44
"description": "This is the official D2 plugin for Obsidian.",
55
"main": "main.js",
66
"scripts": {

src/processor.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class D2Processor {
1414
source: string,
1515
el: HTMLElement,
1616
ctx: MarkdownPostProcessorContext,
17-
signal: AbortSignal
17+
signal?: AbortSignal
1818
) => Promise<void>
1919
>;
2020
abortControllerMap: Map<string, AbortController>;
@@ -36,17 +36,31 @@ export class D2Processor {
3636
text: "Generating D2 diagram...",
3737
cls: "D2__Loading",
3838
});
39-
let debouncedFunc = this.debouncedMap.get(ctx.docId);
39+
40+
// we need to generate a debounce per split page, and ctx.containerEl is the only element we have access to that's page specific
41+
// however, it is not publically available in MarkdownPostProcessorContext, so we hack its access by casting it to an 'any' type
42+
const pageContainer = (ctx as any).containerEl;
43+
let pageID = pageContainer.dataset.pageID;
44+
if (!pageID) {
45+
pageID = Math.floor(Math.random() * Date.now()).toString();
46+
pageContainer.dataset.pageID = pageID;
47+
}
48+
49+
let debouncedFunc = this.debouncedMap.get(pageID);
4050
if (!debouncedFunc) {
51+
// No need to debounce initial render
52+
await this.export(source, el, ctx);
53+
4154
debouncedFunc = debounce(this.export, this.plugin.settings.debounce, {
4255
leading: true,
4356
});
44-
this.debouncedMap.set(ctx.docId, debouncedFunc);
57+
this.debouncedMap.set(pageID, debouncedFunc);
58+
return;
4559
}
4660

47-
this.abortControllerMap.get(ctx.docId)?.abort();
61+
this.abortControllerMap.get(pageID)?.abort();
4862
const newAbortController = new AbortController();
49-
this.abortControllerMap.set(ctx.docId, newAbortController);
63+
this.abortControllerMap.set(pageID, newAbortController);
5064

5165
await debouncedFunc(source, el, ctx, newAbortController.signal);
5266
};
@@ -88,7 +102,7 @@ export class D2Processor {
88102
source: string,
89103
el: HTMLElement,
90104
ctx: MarkdownPostProcessorContext,
91-
signal: AbortSignal
105+
signal?: AbortSignal
92106
) => {
93107
try {
94108
const image = await this.generatePreview(source, signal);
@@ -126,10 +140,13 @@ export class D2Processor {
126140
if (this.prevImage) {
127141
this.insertImage(this.prevImage, el, ctx);
128142
}
143+
} finally {
144+
const pageContainer = (ctx as any).containerEl;
145+
this.abortControllerMap.delete(pageContainer.dataset.id);
129146
}
130147
};
131148

132-
async generatePreview(source: string, signal: AbortSignal): Promise<string> {
149+
async generatePreview(source: string, signal?: AbortSignal): Promise<string> {
133150
const pathArray = [process.env.PATH, "/opt/homebrew/bin", "/usr/local/bin"];
134151

135152
// platform will be win32 even on 64 bit windows

styles.css

+6
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@
4141
.block-language-d2 {
4242
position: relative;
4343
}
44+
45+
@media print {
46+
.Preview__Recompile {
47+
display: none;
48+
}
49+
}

versions.json

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

0 commit comments

Comments
 (0)