diff --git a/eslint.config.mjs b/eslint.config.mjs index 7a51061..9db79a8 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -14,6 +14,9 @@ export default defineConfig([ languageOptions: { globals: { ...globals.browser, + // Obsidian-injected globals for popout-window-aware code + activeDocument: "readonly", + activeWindow: "readonly", }, parser: tsparser, parserOptions: { project: "./tsconfig.json" }, diff --git a/manifest.json b/manifest.json index 115fe21..da61983 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "hexmaker", "name": "Hexmap World Creator", - "version": "1.2.0", + "version": "1.2.1", "minAppVersion": "1.12.0", "description": "Create a hex map and a guidebook level wiki for your setting with minimal fuss. Hex crawl worldbuilding toolbox. System agnostic, ready for any TRPG you care to run in it.", "author": "morkdev", diff --git a/package-lock.json b/package-lock.json index 2ed793a..f0d9e7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hexmaker-plugin", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hexmaker-plugin", - "version": "1.2.0", + "version": "1.2.1", "license": "MIT", "dependencies": { "minisearch": "^7.2.0" diff --git a/package.json b/package.json index 05d38d0..37fdcf3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexmaker-plugin", - "version": "1.2.0", + "version": "1.2.1", "description": "A plugin to power hex crawl worldbuilding and running.", "main": "main.js", "scripts": { diff --git a/src/HexmakerModal.ts b/src/HexmakerModal.ts index 0ce81fd..ca02a58 100644 --- a/src/HexmakerModal.ts +++ b/src/HexmakerModal.ts @@ -34,11 +34,11 @@ export class HexmakerModal extends Modal { modalEl.setCssProps({ left: `${ox + ev.clientX - sx}px`, top: `${oy + ev.clientY - sy}px` }); }; const onUp = () => { - document.removeEventListener("mousemove", onMove); - document.removeEventListener("mouseup", onUp); + activeDocument.removeEventListener("mousemove", onMove); + activeDocument.removeEventListener("mouseup", onUp); }; - document.addEventListener("mousemove", onMove); - document.addEventListener("mouseup", onUp); + activeDocument.addEventListener("mousemove", onMove); + activeDocument.addEventListener("mouseup", onUp); }); } } diff --git a/src/HexmakerSettingTab.ts b/src/HexmakerSettingTab.ts index 0269e90..00bc121 100644 --- a/src/HexmakerSettingTab.ts +++ b/src/HexmakerSettingTab.ts @@ -11,6 +11,10 @@ export class HexmakerSettingTab extends PluginSettingTab { } display(): void { + this.renderSettings(); + } + + private renderSettings(): void { const { containerEl } = this; containerEl.empty(); @@ -460,7 +464,7 @@ export class HexmakerSettingTab extends PluginSettingTab { ); } new Notice("Folders generated."); - this.display(); + this.renderSettings(); }), ); diff --git a/src/export/exporters/mapWithTable.ts b/src/export/exporters/mapWithTable.ts index 2fa07bc..e826a5e 100644 --- a/src/export/exporters/mapWithTable.ts +++ b/src/export/exporters/mapWithTable.ts @@ -436,10 +436,8 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await app.vault.modifyBinary(existing, buf); diff --git a/src/export/exporters/randomTable.ts b/src/export/exporters/randomTable.ts index b8176a2..490a84c 100644 --- a/src/export/exporters/randomTable.ts +++ b/src/export/exporters/randomTable.ts @@ -215,12 +215,9 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - // vault.createBinary / modifyBinary require ArrayBuffer; copy out of the - // (possibly larger) underlying buffer to get a tight ArrayBuffer. - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + // vault.createBinary / modifyBinary require a plain ArrayBuffer. + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await app.vault.modifyBinary(existing, buf); diff --git a/src/export/exporters/singleHex.ts b/src/export/exporters/singleHex.ts index 6ac7121..83a1a75 100644 --- a/src/export/exporters/singleHex.ts +++ b/src/export/exporters/singleHex.ts @@ -230,10 +230,8 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await app.vault.modifyBinary(existing, buf); diff --git a/src/export/exporters/singleNote.ts b/src/export/exporters/singleNote.ts index cb1e012..5ae5691 100644 --- a/src/export/exporters/singleNote.ts +++ b/src/export/exporters/singleNote.ts @@ -99,10 +99,8 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await app.vault.modifyBinary(existing, buf); diff --git a/src/export/exporters/workflow.ts b/src/export/exporters/workflow.ts index ee0f9c5..bef0928 100644 --- a/src/export/exporters/workflow.ts +++ b/src/export/exporters/workflow.ts @@ -317,10 +317,8 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await app.vault.modifyBinary(existing, buf); diff --git a/src/export/htmlRenderer.ts b/src/export/htmlRenderer.ts index ef4c230..370a97d 100644 --- a/src/export/htmlRenderer.ts +++ b/src/export/htmlRenderer.ts @@ -2,7 +2,7 @@ * HTML rendering primitive β€” markdown to a {@link RenderedDoc} ready for printToPDF. * * Uses Obsidian's own `MarkdownRenderer.render()` so the export matches the - * user's installed theme. CSS is captured by walking `document.styleSheets`. + * user's installed theme. CSS is captured by walking `activeDocument.styleSheets`. */ import { App, Component, MarkdownRenderer } from "obsidian"; @@ -29,7 +29,7 @@ export async function renderMarkdownToHtml( const { app, title, sourcePath, markdown } = opts; // Off-screen container so MarkdownRenderer.render has a real DOM target. - const container = document.body.createDiv({ + const container = activeDocument.body.createDiv({ cls: "duckmage-export-render-host markdown-preview-view markdown-rendered", }); const component = new Component(); @@ -49,13 +49,13 @@ export async function renderMarkdownToHtml( } /** - * Walk `document.styleSheets` and concatenate all readable CSS rules. + * Walk `activeDocument.styleSheets` and concatenate all readable CSS rules. * Skips Svelte-injected sheets (transient, irrelevant) and silently skips * sheets that throw on cssRules access (CORS). */ function captureStyles(): string { const parts: string[] = []; - for (const sheet of Array.from(document.styleSheets)) { + for (const sheet of Array.from(activeDocument.styleSheets)) { const node = sheet.ownerNode as Element | null; const id = node?.getAttribute("id") ?? ""; if (id.startsWith("svelte-")) continue; diff --git a/src/export/mapPngRenderer.ts b/src/export/mapPngRenderer.ts index 9adb23b..8709e84 100644 --- a/src/export/mapPngRenderer.ts +++ b/src/export/mapPngRenderer.ts @@ -725,10 +725,8 @@ async function writeBinaryToVault( path: string, data: Uint8Array, ): Promise { - const buf = data.buffer.slice( - data.byteOffset, - data.byteOffset + data.byteLength, - ) as ArrayBuffer; + const buf = new ArrayBuffer(data.byteLength); + new Uint8Array(buf).set(data); const existing = plugin.app.vault.getAbstractFileByPath(path); if (existing instanceof TFile) { await plugin.app.vault.modifyBinary(existing, buf); diff --git a/src/export/pdfExporter.ts b/src/export/pdfExporter.ts index a214c58..8aa4a59 100644 --- a/src/export/pdfExporter.ts +++ b/src/export/pdfExporter.ts @@ -46,7 +46,7 @@ export interface PdfExportOptions { export interface RenderedDoc { /** HTML for the page β€” does NOT include the tag itself. */ bodyHtml: string; - /** CSS rules captured from document.styleSheets, joined with newlines. */ + /** CSS rules captured from activeDocument.styleSheets, joined with newlines. */ css: string; /** Document title. */ title: string; @@ -60,11 +60,11 @@ export async function exportToPdfBytes( doc: RenderedDoc, opts: PdfExportOptions = {}, ): Promise { - const webview = document.createElement("webview") as WebviewElement; + const webview = activeDocument.createElement("webview") as WebviewElement; webview.setAttribute("src", "app://obsidian.md/help.html"); webview.setAttribute("nodeintegration", "true"); webview.addClass("duckmage-export-webview"); - document.body.appendChild(webview); + activeDocument.body.appendChild(webview); try { await waitForWebviewLoad(webview); @@ -113,7 +113,7 @@ function waitForWebviewLoad(webview: HTMLElement): Promise { resolve(); }; webview.addEventListener("did-finish-load", onLoad); - setTimeout(() => { + window.setTimeout(() => { if (settled) return; settled = true; webview.removeEventListener("did-finish-load", onLoad); @@ -139,20 +139,20 @@ function buildInjectScript(doc: RenderedDoc): string { // Replace head with a single