diff --git a/src/ExcalidrawAutomate.ts b/src/ExcalidrawAutomate.ts index 4d338c0..aa160d6 100644 --- a/src/ExcalidrawAutomate.ts +++ b/src/ExcalidrawAutomate.ts @@ -361,7 +361,7 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface { params?.filename ? params.filename + (params.filename.endsWith(".md") ? "": ".excalidraw.md") : getDrawingFilename(this.plugin.settings), - params?.onNewPane ? params.onNewPane : false, + (params?.onNewPane ? params.onNewPane : false)?"new-pane":"active-pane", params?.foldername ? params.foldername : this.plugin.settings.folder, this.plugin.settings.compatibilityMode ? JSON.stringify(scene, null, "\t") @@ -1141,7 +1141,7 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface { */ setView(view: ExcalidrawView | "first" | "active"): ExcalidrawView { if (view == "active") { - const v = this.plugin.app.workspace.activeLeaf.view; + const v = this.plugin.app.workspace.getActiveViewOfType(ExcalidrawView); if (!(v instanceof ExcalidrawView)) { return; } diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 08abfcd..118fe65 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -995,7 +995,7 @@ export default class ExcalidrawView extends TextFileView { if (this.slidingPanesListner) { ( this.app.workspace.rootSplit as WorkspaceItem as WorkspaceItemExt - ).containerEl.removeEventListener("scroll", this.slidingPanesListner); + ).containerEl?.removeEventListener("scroll", this.slidingPanesListner); } } @@ -1150,8 +1150,8 @@ export default class ExcalidrawView extends TextFileView { //save current drawing when user closes workspace leaf onunload() { this.semaphores.viewunload = true; - this.ownerWindow.removeEventListener("keydown", this.onKeyDown, false); - this.ownerWindow.removeEventListener("keyup", this.onKeyUp, false); + this.ownerWindow?.removeEventListener("keydown", this.onKeyDown, false); + this.ownerWindow?.removeEventListener("keyup", this.onKeyUp, false); if(this.getHookServer().onViewUnloadHook) { try { @@ -1160,11 +1160,11 @@ export default class ExcalidrawView extends TextFileView { errorlog({where: "ExcalidrawView.onunload", fn: this.getHookServer().onViewUnloadHook, error: e}); } } - const tooltip = this.ownerDocument.body.querySelector( + const tooltip = this.containerEl?.ownerDocument?.body.querySelector( "body>div.excalidraw-tooltip,div.excalidraw-tooltip--visible", ); if (tooltip) { - this.ownerDocument.body.removeChild(tooltip); + this.containerEl?.ownerDocument?.body.removeChild(tooltip); } this.removeParentMoveObserver(); this.removeSlidingPanesListner(); @@ -1605,7 +1605,7 @@ export default class ExcalidrawView extends TextFileView { justloaded, ); if ( - this.app.workspace.activeLeaf === this.leaf && + this.app.workspace.getActiveViewOfType(ExcalidrawView) === this.leaf.view && this.excalidrawWrapperRef ) { //.firstElmentChild solves this issue: https://github.com/zsviczian/obsidian-excalidraw-plugin/pull/346 @@ -1729,7 +1729,7 @@ export default class ExcalidrawView extends TextFileView { await this.save(); this.plugin.openDrawing( await this.plugin.convertSingleExcalidrawToMD(this.file), - false, + "active-pane", ); } @@ -1933,7 +1933,7 @@ export default class ExcalidrawView extends TextFileView { } }; this.ownerWindow.addEventListener("resize", onResize); - return () => this.ownerWindow.removeEventListener("resize", onResize); + return () => this.ownerWindow?.removeEventListener("resize", onResize); }, [excalidrawWrapperRef]); this.getSelectedTextElement = (): { id: string; text: string } => { diff --git a/src/MarkdownPostProcessor.ts b/src/MarkdownPostProcessor.ts index ade156b..c3ed01e 100644 --- a/src/MarkdownPostProcessor.ts +++ b/src/MarkdownPostProcessor.ts @@ -198,7 +198,7 @@ const createImageDiv = async ( if (src) { plugin.openDrawing( vault.getAbstractFileByPath(src) as TFile, - ev[CTRL_OR_CMD], + ev[CTRL_OR_CMD]?"new-pane":"active-pane", ); } //.ctrlKey||ev.metaKey); }); @@ -514,7 +514,7 @@ export const observer = new MutationObserver(async (m) => { if (src) { plugin.openDrawing( vault.getAbstractFileByPath(src) as TFile, - ev[CTRL_OR_CMD], + ev[CTRL_OR_CMD]?"new-pane":"active-pane", ); } //.ctrlKey||ev.metaKey); }); diff --git a/src/Scripts.ts b/src/Scripts.ts index 52f6cf7..16ad361 100644 --- a/src/Scripts.ts +++ b/src/Scripts.ts @@ -163,13 +163,10 @@ export class ScriptEngine { name: `(Script) ${scriptName}`, checkCallback: (checking: boolean) => { if (checking) { - return ( - this.plugin.app.workspace.activeLeaf.view.getViewType() == - VIEW_TYPE_EXCALIDRAW - ); + return Boolean(app.workspace.getActiveViewOfType(ExcalidrawView)); } - const view = this.plugin.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { (async()=>{ const script = await this.plugin.app.vault.read(f); if(script) { diff --git a/src/dialogs/OpenDrawing.ts b/src/dialogs/OpenDrawing.ts index 5008b91..e1bd492 100644 --- a/src/dialogs/OpenDrawing.ts +++ b/src/dialogs/OpenDrawing.ts @@ -33,7 +33,7 @@ export class OpenFileDialog extends FuzzySuggestModal { if (this.containerEl.innerText.includes(EMPTY_MESSAGE)) { this.plugin.createAndOpenDrawing( `${this.plugin.settings.folder}/${this.inputEl.value}.excalidraw.md`, - this.onNewPane, + this.onNewPane?"new-pane":"active-pane", ); this.close(); } @@ -55,7 +55,7 @@ export class OpenFileDialog extends FuzzySuggestModal { onChooseItem(item: TFile): void { switch (this.action) { case openDialogAction.openFile: - this.plugin.openDrawing(item, this.onNewPane); + this.plugin.openDrawing(item, this.onNewPane?"new-pane":"active-pane"); break; case openDialogAction.insertLinkToDrawing: this.plugin.embedDrawing(item); diff --git a/src/dialogs/Prompt.ts b/src/dialogs/Prompt.ts index 09d1609..23e8219 100644 --- a/src/dialogs/Prompt.ts +++ b/src/dialogs/Prompt.ts @@ -219,7 +219,7 @@ export class GenericInputPrompt extends Modal { } private removeInputListener() { - this.inputComponent.inputEl.removeEventListener( + this.inputComponent?.inputEl?.removeEventListener( "keydown", this.submitEnterCallback, ); diff --git a/src/lang/locale/en.ts b/src/lang/locale/en.ts index 27a1780..550b318 100644 --- a/src/lang/locale/en.ts +++ b/src/lang/locale/en.ts @@ -30,10 +30,12 @@ export default { TRANSCLUDE_MOST_RECENT: "Transclude (embed) the most recently edited drawing", NEW_IN_NEW_PANE: "Create a new drawing - IN A NEW PANE", NEW_IN_ACTIVE_PANE: "Create a new drawing - IN THE CURRENT ACTIVE PANE", + NEW_IN_POPOUT_WINDOW: "Create a new drawing - IN A POPOUT WINDOW", NEW_IN_NEW_PANE_EMBED: "Create a new drawing - IN A NEW PANE - and embed into active document", NEW_IN_ACTIVE_PANE_EMBED: "Create a new drawing - IN THE CURRENT ACTIVE PANE - and embed into active document", + NEW_IN_POPOUT_WINDOW_EMBED: "Create a new drawing - IN A POPOUT WINDOW - and embedd into active document", EXPORT_SVG: "Save as SVG next to the current file", EXPORT_PNG: "Save as PNG next to the current file", TOGGLE_LOCK: "Toggle Text Element edit RAW/PREVIEW", @@ -189,10 +191,14 @@ export default { "If you don't want text accidentally changing in your drawings use [[links|with aliases]].", ADJACENT_PANE_NAME: "Open in adjacent pane", ADJACENT_PANE_DESC: - "When CTRL/CMD+SHIFT clicking a link in Excalidraw by default the plugin will open the link in a new pane. " + + "When CTRL/CMD+SHIFT clicking a link in Excalidraw, by default the plugin will open the link in a new pane. " + "Turning this setting on, Excalidraw will first look for an existing adjacent pane, and try to open the link there. " + - "Excalidraw will first look too the right, then to the left, then down, then up. If no pane is found, Excalidraw will open " + - "a new pane.", + "Excalidraw will look for the adjacent pane based on your focus/navigation history, i.e. the workpane that was active before you " + + "activated Excalidraw.", + MAINWORKSPACE_PANE_NAME: "Open in main workspace", + MAINWORKSPACE_PANE_DESC: + "When CTRL/CMD+SHIFT clicking a link in Excalidraw, by default the plugin will open the link in a new pane in the current active window. " + + "Turning this setting on, Excalidraw will open the link in an existing or new pane in the main workspace. ", LINK_BRACKETS_NAME: "Show [[brackets]] around links", LINK_BRACKETS_DESC: `${ "In PREVIEW mode, when parsing Text Elements, place brackets around links. " + diff --git a/src/main.ts b/src/main.ts index 44e7b2c..cdfb505 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ import { loadMathJax, request, MetadataCache, - FrontMatterCache, + FrontMatterCache } from "obsidian"; import { BLANK_DRAWING, @@ -645,7 +645,7 @@ export default class ExcalidrawPlugin extends Plugin { this.addRibbonIcon(ICON_NAME, t("CREATE_NEW"), async (e) => { this.createAndOpenDrawing( getDrawingFilename(this.settings), - e[CTRL_OR_CMD], + e[CTRL_OR_CMD]?"new-pane":"active-pane", ); //.ctrlKey||e.metaKey); }); @@ -663,7 +663,7 @@ export default class ExcalidrawPlugin extends Plugin { } this.createAndOpenDrawing( getDrawingFilename(this.settings), - false, + "active-pane", folderpath, ); }); @@ -735,7 +735,7 @@ export default class ExcalidrawPlugin extends Plugin { name: t("TRANSCLUDE"), checkCallback: (checking: boolean) => { if (checking) { - return this.app.workspace.activeLeaf.view.getViewType() == "markdown"; + return Boolean(this.app.workspace.getActiveViewOfType(MarkdownView)) } this.openDialog.start(openDialogAction.insertLinkToDrawing, false); return true; @@ -748,7 +748,7 @@ export default class ExcalidrawPlugin extends Plugin { checkCallback: (checking: boolean) => { if (checking) { return ( - this.app.workspace.activeLeaf.view.getViewType() == "markdown" && + Boolean(this.app.workspace.getActiveViewOfType(MarkdownView)) && this.lastActiveExcalidrawFilePath != null ); } @@ -767,7 +767,7 @@ export default class ExcalidrawPlugin extends Plugin { id: "excalidraw-autocreate", name: t("NEW_IN_NEW_PANE"), callback: () => { - this.createAndOpenDrawing(getDrawingFilename(this.settings), true); + this.createAndOpenDrawing(getDrawingFilename(this.settings), "new-pane"); }, }); @@ -775,11 +775,21 @@ export default class ExcalidrawPlugin extends Plugin { id: "excalidraw-autocreate-on-current", name: t("NEW_IN_ACTIVE_PANE"), callback: () => { - this.createAndOpenDrawing(getDrawingFilename(this.settings), false); + this.createAndOpenDrawing(getDrawingFilename(this.settings), "active-pane"); }, }); - const insertDrawingToDoc = async (inNewPane: boolean) => { + this.addCommand({ + id: "excalidraw-autocreate-popout", + name: t("NEW_IN_POPOUT_WINDOW"), + callback: () => { + this.createAndOpenDrawing(getDrawingFilename(this.settings), "popout-window"); + }, + }); + + const insertDrawingToDoc = async ( + location: "active-pane"|"new-pane"|"popout-window" + ) => { const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); if (!activeView) { return; @@ -799,7 +809,7 @@ export default class ExcalidrawPlugin extends Plugin { ).folder; const file = await this.createDrawing(filename, folder); await this.embedDrawing(file); - this.openDrawing(file, inNewPane); + this.openDrawing(file, location); }; this.addCommand({ @@ -807,9 +817,9 @@ export default class ExcalidrawPlugin extends Plugin { name: t("NEW_IN_NEW_PANE_EMBED"), checkCallback: (checking: boolean) => { if (checking) { - return this.app.workspace.activeLeaf.view.getViewType() == "markdown"; + return Boolean(this.app.workspace.getActiveViewOfType(MarkdownView)); } - insertDrawingToDoc(true); + insertDrawingToDoc("new-pane"); return true; }, }); @@ -819,25 +829,36 @@ export default class ExcalidrawPlugin extends Plugin { name: t("NEW_IN_ACTIVE_PANE_EMBED"), checkCallback: (checking: boolean) => { if (checking) { - return this.app.workspace.activeLeaf.view.getViewType() == "markdown"; + return Boolean(this.app.workspace.getActiveViewOfType(MarkdownView)); } - insertDrawingToDoc(false); + insertDrawingToDoc("active-pane"); return true; }, }); + this.addCommand({ + id: "excalidraw-autocreate-and-embed-popout", + name: t("NEW_IN_POPOUT_WINDOW_EMBED"), + checkCallback: (checking: boolean) => { + if (checking) { + return Boolean(this.app.workspace.getActiveViewOfType(MarkdownView)); + } + insertDrawingToDoc("popout-window"); + return true; + }, + }); + this.addCommand({ id: "export-svg", name: t("EXPORT_SVG"), checkCallback: (checking: boolean) => { if (checking) { return ( - this.app.workspace.activeLeaf.view.getViewType() == - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ); } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { view.saveSVG(); return true; } @@ -851,12 +872,11 @@ export default class ExcalidrawPlugin extends Plugin { checkCallback: (checking: boolean) => { if (checking) { return ( - this.app.workspace.activeLeaf.view.getViewType() === - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ); } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { search(view); return true; } @@ -870,12 +890,11 @@ export default class ExcalidrawPlugin extends Plugin { checkCallback: (checking: boolean) => { if (checking) { return ( - this.app.workspace.activeLeaf.view.getViewType() === - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ); } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { if (view.isFullscreen()) { view.exitFullscreen(); } else { @@ -887,67 +906,17 @@ export default class ExcalidrawPlugin extends Plugin { }, }); - /* this.addCommand({ - id: "ocr", - name: "Test OCR",//t("EXPORT_PNG"), - checkCallback: (checking: boolean) => { - if (checking) { - return ( - this.app.workspace.activeLeaf.view.getViewType() === - //@ts-ignore - VIEW_TYPE_EXCALIDRAW && typeof Tesseract !== "undefined" - ); - } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { - //@ts-ignore - const worker = Tesseract.createWorker({logger: m => console.log(m)}); - //@ts-ignore - Tesseract.setLogging(true); - - const exportSettings: ExportSettings = { - withBackground: true, - withTheme: false, - }; - const blobToBase64 = async (blob:any) => { - return new Promise((resolve, _) => { - const reader = new FileReader(); - reader.onloadend = () => resolve(reader.result); - reader.readAsDataURL(blob); - }); - } - - (async () => { - const png = await getPNG( - view.getScene(), - exportSettings, - 3, - ); - await worker.load(); - await worker.loadLanguage('https://https://raw.githubusercontent.com/thecodingone/trained-tesseract-handwriting-fonts/blob/master/eng.traineddata'); - await worker.initialize('https://raw.githubusercontent.com/thecodingone/trained-tesseract-handwriting-fonts/blob/master/eng.traineddata'); - const { data: { text } } = await worker.recognize(await blobToBase64(png)); - console.log(text); - await worker.terminate(); - })(); - return true; - } - return false; - }, - });*/ - this.addCommand({ id: "export-png", name: t("EXPORT_PNG"), checkCallback: (checking: boolean) => { if (checking) { return ( - this.app.workspace.activeLeaf.view.getViewType() == - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ); } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { view.savePNG(); return true; } @@ -962,16 +931,15 @@ export default class ExcalidrawPlugin extends Plugin { checkCallback: (checking: boolean) => { if (checking) { if ( - this.app.workspace.activeLeaf.view.getViewType() === - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ) { - return !(this.app.workspace.activeLeaf.view as ExcalidrawView) + return !(this.app.workspace.getActiveViewOfType(ExcalidrawView)) .compatibilityMode; } return false; } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { view.changeTextMode( view.textMode === TextMode.parsed ? TextMode.raw : TextMode.parsed, ); @@ -986,11 +954,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("DELETE_FILE"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { this.ea.reset(); this.ea.setView(view); const el = this.ea.getViewSelectedElement(); @@ -1023,11 +990,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("INSERT_LINK"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { this.insertLinkDialog.start(view.file.path, view.addText); return true; } @@ -1041,11 +1007,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("INSERT_LINK_TO_ELEMENT"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { view.copyLinkToSelectedElementToClipboard(); return true; } @@ -1058,11 +1023,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("INSERT_IMAGE"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { this.insertImageDialog.start(view); return true; } @@ -1075,8 +1039,7 @@ export default class ExcalidrawPlugin extends Plugin { name: t("READ_RELEASE_NOTES"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } const version: string = //@ts-ignore @@ -1091,8 +1054,8 @@ export default class ExcalidrawPlugin extends Plugin { name: t("TRAY_MODE"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - if (!(view instanceof ExcalidrawView) || !view.excalidrawRef) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (!view || !view.excalidrawRef) { return false; } const st = view.excalidrawAPI.getAppState(); @@ -1101,8 +1064,8 @@ export default class ExcalidrawPlugin extends Plugin { } return true; } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView && view.excalidrawAPI) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view && view.excalidrawAPI) { view.toggleTrayMode(); return true; } @@ -1115,11 +1078,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("INSERT_MD"), checkCallback: (checking: boolean) => { if (checking) { - const view = this.app.workspace.activeLeaf.view; - return view instanceof ExcalidrawView; + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { this.insertMDDialog.start(view); return true; } @@ -1132,13 +1094,10 @@ export default class ExcalidrawPlugin extends Plugin { name: t("INSERT_LATEX"), checkCallback: (checking: boolean) => { if (checking) { - return ( - this.app.workspace.activeLeaf.view.getViewType() == - VIEW_TYPE_EXCALIDRAW - ); + return Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)); } - const view = this.app.workspace.activeLeaf.view; - if (view instanceof ExcalidrawView) { + const view = this.app.workspace.getActiveViewOfType(ExcalidrawView); + if (view) { insertLaTeXToView(view); return true; } @@ -1158,25 +1117,30 @@ export default class ExcalidrawPlugin extends Plugin { if (checking) { if ( - this.app.workspace.activeLeaf.view.getViewType() == - VIEW_TYPE_EXCALIDRAW + Boolean(this.app.workspace.getActiveViewOfType(ExcalidrawView)) ) { - return !(this.app.workspace.activeLeaf.view as ExcalidrawView) + return !(this.app.workspace.getActiveViewOfType(ExcalidrawView)) .compatibilityMode; } return fileIsExcalidraw; } - const activeLeaf = this.app.workspace.activeLeaf; - - if (activeLeaf?.view && activeLeaf.view instanceof ExcalidrawView) { + const excalidrawView = this.app.workspace.getActiveViewOfType(ExcalidrawView) + if (excalidrawView) { + const activeLeaf = excalidrawView.leaf; this.excalidrawFileModes[(activeLeaf as any).id || activeFile.path] = "markdown"; this.setMarkdownView(activeLeaf); - } else if (fileIsExcalidraw) { + return; + } + + const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView) + if (markdownView && fileIsExcalidraw) { + const activeLeaf = markdownView.leaf; this.excalidrawFileModes[(activeLeaf as any).id || activeFile.path] = VIEW_TYPE_EXCALIDRAW; this.setExcalidrawView(activeLeaf); + return; } }, }); @@ -1186,9 +1150,9 @@ export default class ExcalidrawPlugin extends Plugin { name: t("CONVERT_NOTE_TO_EXCALIDRAW"), checkCallback: (checking) => { const activeFile = this.app.workspace.getActiveFile(); - const activeLeaf = this.app.workspace.activeLeaf; + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); - if (!activeFile || !activeLeaf) { + if (!activeFile || !activeView) { return false; } @@ -1197,13 +1161,14 @@ export default class ExcalidrawPlugin extends Plugin { if (checking) { return isFileEmpty; } + if (isFileEmpty) { (async () => { await this.app.vault.modify( activeFile, await this.getBlankDrawing(), ); - this.setExcalidrawView(activeLeaf); + this.setExcalidrawView(activeView.leaf); })(); } }, @@ -1819,12 +1784,20 @@ export default class ExcalidrawPlugin extends Plugin { }) } - public openDrawing(drawingFile: TFile, onNewPane: boolean) { - - let leaf = this.app.workspace.activeLeaf; - - if (!leaf || onNewPane) { - leaf = getNewOrAdjacentLeaf(this, app.workspace.activeLeaf); + public openDrawing( + drawingFile: TFile, + location: "active-pane"|"new-pane"|"popout-window" + ) { + let leaf: WorkspaceLeaf; + if(location === "popout-window") { + //@ts-ignore + leaf = app.workspace.openPopoutLeaf(); + } + else { + leaf = this.app.workspace.getLeaf(false); + if ((leaf.view.getViewType() !== 'empty') && (location === "new-pane")) { + leaf = getNewOrAdjacentLeaf(this, leaf) + } } leaf.setViewState({ @@ -1919,12 +1892,12 @@ export default class ExcalidrawPlugin extends Plugin { public async createAndOpenDrawing( filename: string, - onNewPane: boolean, + location: "active-pane"|"new-pane"|"popout-window", foldername?: string, initData?: string, ): Promise { const file = await this.createDrawing(filename, foldername, initData); - this.openDrawing(file, onNewPane); + this.openDrawing(file, location); return file.path; } diff --git a/src/menu/ToolsPanel.tsx b/src/menu/ToolsPanel.tsx index 56e3a95..1cf6d79 100644 --- a/src/menu/ToolsPanel.tsx +++ b/src/menu/ToolsPanel.tsx @@ -227,8 +227,8 @@ export class ToolsPanel extends React.Component { }; const onPointerUp = () => { - this.props.view.ownerDocument.removeEventListener("pointerup", onPointerUp); - this.props.view.ownerDocument.removeEventListener("pointermove", onDrag); + this.props.view.ownerDocument?.removeEventListener("pointerup", onPointerUp); + this.props.view.ownerDocument?.removeEventListener("pointermove", onDrag); }; event.preventDefault(); diff --git a/src/settings.ts b/src/settings.ts index 1c2f745..11e160c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -44,6 +44,7 @@ export interface ExcalidrawSettings { zoomToFitOnResize: boolean; zoomToFitMaxLevel: number; openInAdjacentPane: boolean; + openInMainWorkspace: boolean; showLinkBrackets: boolean; linkPrefix: string; urlPrefix: string; @@ -133,6 +134,7 @@ export const DEFAULT_SETTINGS: ExcalidrawSettings = { hoverPreviewWithoutCTRL: false, linkOpacity: 1, openInAdjacentPane: false, + openInMainWorkspace: true, showLinkBrackets: true, allowCtrlClick: true, forceWrap: false, @@ -565,6 +567,19 @@ export class ExcalidrawSettingTab extends PluginSettingTab { }), ); + + new Setting(containerEl) + .setName(t("MAINWORKSPACE_PANE_NAME")) + .setDesc(fragWithHTML(t("MAINWORKSPACE_PANE_DESC"))) + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.openInMainWorkspace) + .onChange(async (value) => { + this.plugin.settings.openInMainWorkspace = value; + this.applySettingsUpdate(true); + }), + ); + new Setting(containerEl) .setName(t("LINK_BRACKETS_NAME")) .setDesc(fragWithHTML(t("LINK_BRACKETS_DESC"))) diff --git a/src/utils/ObsidianUtils.ts b/src/utils/ObsidianUtils.ts index 4ef88cf..aaa5aac 100644 --- a/src/utils/ObsidianUtils.ts +++ b/src/utils/ObsidianUtils.ts @@ -21,45 +21,38 @@ export const getNewOrAdjacentLeaf = ( plugin: ExcalidrawPlugin, leaf: WorkspaceLeaf ): WorkspaceLeaf => { - const inHoverEditorLeaf = leaf.view?.containerEl - ? getParentOfClass(leaf.view.containerEl, "popover") !== null - : false; - - if (inHoverEditorLeaf) { - const mainLeaves = app.workspace.getLayout().main.children.filter((c:any) => c.type === "leaf"); - if(mainLeaves.length === 0) { - //@ts-ignore - return app.workspace.createLeafInParent(app.workspace.rootSplit); + if(plugin.settings.openInMainWorkspace) { + leaf.view.navigation = false; + const mainLeaf = app.workspace.getLeaf(false) + leaf.view.navigation = true; + if(plugin.settings.openInAdjacentPane || mainLeaf.view.getViewType() === 'empty') { + return mainLeaf; } - const targetLeaf = app.workspace.getLeafById(mainLeaves[0].id); - if (plugin.settings.openInAdjacentPane) { - return targetLeaf; - } - return plugin.app.workspace.createLeafBySplit(targetLeaf); + return app.workspace.createLeafBySplit(mainLeaf); } if (plugin.settings.openInAdjacentPane) { - let leafToUse = plugin.app.workspace.getAdjacentLeafInDirection( - leaf, - "right" - ); - if (!leafToUse) { - leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(leaf, "left"); + //if in popout window + if(leaf.view.containerEl.ownerDocument !== document) { + const popoutLeaves = new Set(); + app.workspace.iterateAllLeaves(l=>{ + if(l !== leaf && l.view.navigation && l.view.containerEl.ownerDocument === leaf.view.containerEl.ownerDocument) { + popoutLeaves.add(l); + } + }); + if(popoutLeaves.size === 0) { + return app.workspace.getLeaf(true); + } + return Array.from(popoutLeaves)[0]; } - if (!leafToUse) { - leafToUse = plugin.app.workspace.getAdjacentLeafInDirection( - leaf, - "bottom" - ); - } - if (!leafToUse) { - leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(leaf, "top"); - } - if (!leafToUse) { - leafToUse = plugin.app.workspace.createLeafBySplit(leaf); - } - return leafToUse; + + leaf.view.navigation = false; + const leafToUse = app.workspace.getLeaf(false) + leaf.view.navigation = true; + return leafToUse } + + return plugin.app.workspace.createLeafBySplit(leaf); };