diff --git a/manifest.json b/manifest.json index 1384565..64e125b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "excalibrain", "name": "ExcaliBrain", - "version": "0.2.0", + "version": "0.2.1", "minAppVersion": "1.1.6", "description": "A clean, intuitive and editable graph view for Obsidian", "author": "Zsolt Viczian", diff --git a/src/Scene.ts b/src/Scene.ts index 975769f..86fdb8f 100644 --- a/src/Scene.ts +++ b/src/Scene.ts @@ -20,7 +20,7 @@ export class Scene { app: App; leaf: WorkspaceLeaf; centralPagePath: string; //path of the page in the center of the graph - private centralLeaf: WorkspaceLeaf; //workspace leaf containing the central page + public centralLeaf: WorkspaceLeaf; //workspace leaf containing the central page textSize: {width:number, height:number}; nodeWidth: number; nodeHeight: number; @@ -107,8 +107,8 @@ export class Scene { //@ts-ignore this.centralLeaf.view?.file?.path !== centralPage.file.path ) { - this.centralLeaf.openFile(centralPage.file, {active: true}); - app.workspace.revealLeaf(this.centralLeaf); + this.centralLeaf.openFile(centralPage.file, {active: false}); + //app.workspace.revealLeaf(this.centralLeaf); } } await this.render(); @@ -848,7 +848,7 @@ export class Scene { } - public unloadScene() { + public unloadScene(saveSettings:boolean = true, silent: boolean = false) { if(this.removeEH) { this.removeEH(); this.removeEH = undefined; @@ -899,11 +899,13 @@ export class Scene { // timout is to make sure Obsidian is not being terminated when scene closes, // becasue that can lead to crippled settings file // if the plugin is still there after 400ms, it is safe to save the settings - setTimeout(async () => { - await this.plugin.loadSettings(); //only overwrite the navigation history, save other synchronized settings - this.plugin.settings.navigationHistory = [...this.plugin.navigationHistory]; - await this.plugin.saveSettings(); - },400); + if(saveSettings) { + setTimeout(async () => { + await this.plugin.loadSettings(); //only overwrite the navigation history, save other synchronized settings + this.plugin.settings.navigationHistory = [...this.plugin.navigationHistory]; + await this.plugin.saveSettings(); + },400); + } this.toolsPanel?.terminate(); this.toolsPanel = undefined; this.historyPanel?.terminate(); @@ -914,9 +916,18 @@ export class Scene { this.centralPagePath = undefined; this.terminated = true; //@ts-ignore - if(!app.plugins.plugins["obsidian-excalidraw-plugin"]) { + if(!this.app.plugins.plugins["obsidian-excalidraw-plugin"]) { this.plugin.EA = null; } - new Notice("Brain Graph Off"); + if(!silent) { + new Notice("Brain Graph Off"); + } + const mostRecentLeaf = this.app.workspace.getMostRecentLeaf(); + if(mostRecentLeaf) { + this.app.workspace.setActiveLeaf( + mostRecentLeaf, + { focus: true }, + ) + } } } \ No newline at end of file diff --git a/src/excalibrain-main.ts b/src/excalibrain-main.ts index 9fafe65..704874e 100644 --- a/src/excalibrain-main.ts +++ b/src/excalibrain-main.ts @@ -22,6 +22,9 @@ declare module "obsidian" { disablePlugin(plugin: string):Promise; }; } + interface WorkspaceLeaf { + id: string; + } } declare global { @@ -418,8 +421,11 @@ export default class ExcaliBrain extends Plugin { if(!this.excalidrawAvailable()) return; //still need this in case user sets a hotkey if(this.scene && !this.scene.terminated) { - this.revealBrainLeaf(); - return; + if(this.app.workspace.getLeafById(this.scene.leaf?.id)) { + this.revealBrainLeaf(); + return; + } + this.scene.unloadScene(false, true); } const leaf = this.getBrainLeaf(); if(leaf) { @@ -443,8 +449,11 @@ export default class ExcaliBrain extends Plugin { if(!this.excalidrawAvailable() || this.EA.DEVICE.isMobile) return; //still need this in case user sets a hotkey if(this.scene && !this.scene.terminated) { - this.revealBrainLeaf(); - return; + if(this.app.workspace.getLeafById(this.scene.leaf?.id)) { + this.revealBrainLeaf(); + return; + } + this.scene.unloadScene(false, true); } const leaf = this.getBrainLeaf(); if(leaf) { @@ -464,15 +473,18 @@ export default class ExcaliBrain extends Plugin { name: t("COMMAND_START_HOVER"), checkCallback: (checking: boolean) => { //@ts-ignore - const hoverEditor = app.plugins.getPlugin("obsidian-hover-editor"); + const hoverEditor = this.app.plugins.getPlugin("obsidian-hover-editor"); if(checking) { return hoverEditor && this.excalidrawAvailable(); } if(!this.excalidrawAvailable() || !hoverEditor) return; if(this.scene && !this.scene.terminated) { - this.revealBrainLeaf(); - return; + if(this.app.workspace.getLeafById(this.scene.leaf?.id)) { + this.revealBrainLeaf(); + return; + } + this.scene.unloadScene(false, true); } try { //getBrainLeaf will only return one leaf. If there are multiple leaves open, some in hover editors other docked, the @@ -512,7 +524,7 @@ export default class ExcaliBrain extends Plugin { getBrainLeaf():WorkspaceLeaf { let brainLeaf: WorkspaceLeaf; - app.workspace.iterateAllLeaves(leaf=>{ + this.app.workspace.iterateAllLeaves(leaf=>{ if( leaf.view && this.EA.isExcalidrawView(leaf.view) && @@ -560,7 +572,8 @@ export default class ExcaliBrain extends Plugin { } this.EA.onLinkClickHook = (element,linkText,event) => { - const path = linkText.match(/\[\[([^\]]*)/)[1]; + const path = linkText.match(/\[\[([^\]]*)/)?.[1]; + if(!path) return true; const page = this.pages.get(path); const ea = this.EA; @@ -594,6 +607,15 @@ export default class ExcaliBrain extends Plugin { //if centralPage is in embeddedFrame, simply render the scene if(this.settings.embedCentralNode) { + if(this.scene.centralPagePath === page.path) { + if(this.scene.isCentralLeafStillThere()) { + this.scene.centralLeaf.openFile(page.file,{active:true}); + return false; + } + ea.targetView.linksAlwaysOpenInANewPane = false; + setTimeout(()=>ea.targetView.linksAlwaysOpenInANewPane = true,300); + return true; + } this.scene.renderGraphForPath(path); return false; } @@ -610,7 +632,7 @@ export default class ExcaliBrain extends Plugin { if(this.scene.isCentralLeafStillThere()) { const f = app.vault.getAbstractFileByPath(path.split("#")[0]); if(f && f instanceof TFile) { - centralLeaf.openFile(f); + centralLeaf.openFile(f,{active:false}); this.scene.renderGraphForPath(path, false); return false; }