From 600c9015a7cbc449ae638d7c3574939f915b7e71 Mon Sep 17 00:00:00 2001 From: novov Date: Tue, 1 Oct 2024 00:01:58 +1300 Subject: [PATCH] Optimise replacing all notes --- src/homepage.ts | 7 ++----- src/utils.ts | 29 ++++++++++++++++++++++++----- tests/harness.ts | 6 +++--- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/homepage.ts b/src/homepage.ts index 112c46c..85afad7 100644 --- a/src/homepage.ts +++ b/src/homepage.ts @@ -2,10 +2,9 @@ import { App, FileView, MarkdownView, Notice, View as OView, WorkspaceLeaf } fro import HomepagePlugin from "./main"; import { PERIODIC_KINDS, getAutorun, getPeriodicNote } from "./periodic"; import { DEFAULT_DATA } from "./settings"; -import { detachLeavesOfTypes, emptyActiveView, equalsCaseless, hasLayoutChange, randomFile, sleep, trimFile, untrimName } from "./utils"; +import { detachAllLeaves, emptyActiveView, equalsCaseless, hasLayoutChange, randomFile, sleep, trimFile, untrimName } from "./utils"; export const LEAF_TYPES: string[] = ["markdown", "canvas", "kanban"]; -export const CLOSED_LEAVES: string[] = [...LEAF_TYPES, "audio", "graph", "image", "pdf", "video"]; export const DEFAULT: string = "Main Homepage"; export const MOBILE: string = "Mobile Homepage"; @@ -164,10 +163,8 @@ export class Homepage { this.app.workspace.floatingSplit.children.forEach(c => c.win.close()); } - //replacing leaf types we don't know can cause issues, so limit to known - detachLeavesOfTypes(this.app, CLOSED_LEAVES); + detachAllLeaves(this.app); await sleep(0); - this.app.workspace.iterateRootLeaves(l => this.app.workspace.setActiveLeaf(l)); } if (this.data.kind === Kind.Graph) leaf = await this.launchGraph(mode); diff --git a/src/utils.ts b/src/utils.ts index 8c37dd8..3594ca4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,11 +39,30 @@ export function sleep(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } -export function detachLeavesOfTypes(app: App, types: string[]): void { - app.workspace.iterateRootLeaves(leaf => { - if (!leaf.view || !types.contains(leaf.view.getViewType())) return; - leaf.detach(); - }); +export function detachAllLeaves(app: App): void { + const layout = app.workspace.getLayout(); + + layout.main = { + "id": "5324373015726ba8", + "type": "split", + "children": [ + { + "id": "4509724f8bf84da7", + "type": "tabs", + "children": [ + { + "id": "e7a7b303c61786dc", + "type": "leaf", + "state": {"type": "empty", "state": {}, "icon": "lucide-file", "title": "New tab"} + } + ] + } + ], + "direction": "vertical" + } + layout.active = "e7a7b303c61786dc"; + + app.workspace.changeLayout(layout); } export function hasLayoutChange(app: App): Promise { diff --git a/tests/harness.ts b/tests/harness.ts index 319fea3..f6bcd05 100644 --- a/tests/harness.ts +++ b/tests/harness.ts @@ -1,8 +1,8 @@ import { ButtonComponent, Modal, getIcon } from "obsidian"; -import { CLOSED_LEAVES, HomepageData } from "src/homepage"; +import { HomepageData } from "src/homepage"; import HomepagePlugin from "src/main"; import { DEFAULT_DATA } from "src/settings"; -import { detachLeavesOfTypes, sleep } from "src/utils"; +import { detachAllLeaves, sleep } from "src/utils"; type Result = { name: string, @@ -117,7 +117,7 @@ export default class HomepageTestPlugin extends HomepagePlugin { //reset state this.homepage.data = { ...DEFAULT_DATA }; await this.homepage.save(); - detachLeavesOfTypes(this.app, CLOSED_LEAVES); + detachAllLeaves(this.app); await sleep(50); try {