Skip to content

Commit

Permalink
Optimise replacing all notes
Browse files Browse the repository at this point in the history
  • Loading branch information
mirnovov committed Sep 30, 2024
1 parent 54af0f8 commit 600c901
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 24 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,30 @@ export function sleep(ms: number): Promise<void> {
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<void> {
Expand Down
6 changes: 3 additions & 3 deletions tests/harness.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 600c901

Please sign in to comment.