diff --git a/manifest.json b/manifest.json index 0c0f47d..393daa9 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "1.5.29", + "version": "1.5.30", "minAppVersion": "0.12.16", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/src/Prompt.ts b/src/Prompt.ts index 0d01b09..5695291 100644 --- a/src/Prompt.ts +++ b/src/Prompt.ts @@ -10,7 +10,7 @@ import { } from "obsidian"; import ExcalidrawView from "./ExcalidrawView"; import ExcalidrawPlugin from "./main"; -import { getNewOrAdjacentLeaf } from "./Utils"; +import { getNewOrAdjacentLeaf, sleep } from "./Utils"; export class Prompt extends Modal { private promptEl: HTMLInputElement; @@ -399,7 +399,7 @@ export class NewFileActions extends Modal { const f = await this.app.fileManager.createNewMarkdownFileFromLinktext(this.path,this.viewFile) if(!f) return; await this.app.vault.modify(f,await this.plugin.getBlankDrawing()); - await new Promise(r => setTimeout(r, 200)); //wait for metadata cache to update, so file opens as excalidraw + await sleep(200); //wait for metadata cache to update, so file opens as excalidraw this.openFile(f); this.close(); }; diff --git a/src/main.ts b/src/main.ts index c4cc3af..1e3967e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -70,6 +70,7 @@ import { getNewUniqueFilepath, isObsidianThemeDark, log, + sleep, } from "./Utils"; import { OneOffs } from "./OneOffs"; import { FileId } from "@zsviczian/excalidraw/types/element/types"; @@ -661,7 +662,9 @@ export default class ExcalidrawPlugin extends Plugin { this.lastActiveExcalidrawFilePath != null ); } - this.embedDrawing(this.lastActiveExcalidrawFilePath); + const file = this.app.vault.getAbstractFileByPath(this.lastActiveExcalidrawFilePath); + if(!(file instanceof TFile)) return false; + this.embedDrawing(file); return true; }, }); @@ -707,8 +710,7 @@ export default class ExcalidrawPlugin extends Plugin { ) ).folder; const file = await this.createDrawing(filename, folder); - const path = this.app.metadataCache.fileToLinktext(file,activeView.file.path,true) - await this.embedDrawing(path); + await this.embedDrawing(file); this.openDrawing(file, inNewPane); }; @@ -1250,16 +1252,19 @@ export default class ExcalidrawPlugin extends Plugin { //delete PNG and SVG files as well if (self.settings.keepInSync) { - [".svg", ".png", ".excalidraw"].forEach(async (ext: string) => { - const imgPath = getIMGPathFromExcalidrawFile(file.path, ext); - const imgFile = self.app.vault.getAbstractFileByPath( - normalizePath(imgPath), - ); - if (imgFile && imgFile instanceof TFile) { - await self.app.vault.delete(imgFile); - } - }); + setTimeout(()=>{ + [".svg", ".png", ".excalidraw"].forEach(async (ext: string) => { + const imgPath = getIMGPathFromExcalidrawFile(file.path, ext); + const imgFile = self.app.vault.getAbstractFileByPath( + normalizePath(imgPath), + ); + if (imgFile && imgFile instanceof TFile) { + await self.app.vault.delete(imgFile); + } + }); + },500); } + }; self.registerEvent(self.app.vault.on("delete", deleteEventHandler)); @@ -1371,20 +1376,31 @@ export default class ExcalidrawPlugin extends Plugin { //this.saveSettings(); } - public async embedDrawing(data: string) { + public async embedDrawing(file: TFile) { const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); - if (activeView) { + if (activeView && activeView.file) { + const data = this.app.metadataCache.fileToLinktext( + file, + activeView.file.path, + this.settings.embedType === "excalidraw" + ) const editor = activeView.editor; if (this.settings.embedType === "excalidraw") { editor.replaceSelection(`![[${data}]]`); editor.focus(); return; } - const filename = `${data.substring( - 0, - data.lastIndexOf("."), - )}.${this.settings.embedType.toLowerCase()}`; - await this.app.vault.create(filename, ""); + + const filename = getIMGPathFromExcalidrawFile( + data,"."+this.settings.embedType.toLowerCase() + ); + const filepath = getIMGPathFromExcalidrawFile( + file.path,"."+this.settings.embedType.toLowerCase() + ); + + await this.app.vault.create(filepath, ""); + //await sleep(200); + editor.replaceSelection( `![[${filename}]]\n%%[[${data}|🖋 Edit in Excalidraw]]%%`, ); diff --git a/src/openDrawing.ts b/src/openDrawing.ts index 5b698c5..12761e2 100644 --- a/src/openDrawing.ts +++ b/src/openDrawing.ts @@ -58,7 +58,7 @@ export class OpenFileDialog extends FuzzySuggestModal { this.plugin.openDrawing(item, this.onNewPane); break; case openDialogAction.insertLinkToDrawing: - this.plugin.embedDrawing(item.path); + this.plugin.embedDrawing(item); break; } } diff --git a/versions.json b/versions.json index fd79c06..a0f7704 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,4 @@ { - "1.5.29": "0.12.16", + "1.5.30": "0.12.16", "1.4.2": "0.11.13" }