From 659ed5ec0058cd3408f4147a999bb123cb4ea69e Mon Sep 17 00:00:00 2001 From: PJ Date: Fri, 1 Jul 2022 06:47:56 -0400 Subject: [PATCH] feat: Support refreshing on workspace load --- package.json | 1 + src/main.ts | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d4a8cc7..680d5fd 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "cross-env": "^7.0.3", "css-minify": "^2.0.0", "esbuild": "0.14.3", + "monkey-around": "^2.3.0", "obsidian": "^0.14.8", "prettier": "2.5.1", "sass": "^1.49.9", diff --git a/src/main.ts b/src/main.ts index a81d804..097c78c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ -import { Platform, Plugin, WorkspaceLeaf } from 'obsidian'; +import { Platform, Plugin, Workspace, WorkspaceLeaf } from 'obsidian'; +import { around } from "monkey-around"; import type { baseButton, enabledButton, @@ -20,6 +21,12 @@ import { } from './utils'; import { lucideIcons } from './lucide'; +declare module "obsidian" { + interface Workspace { + onLayoutChange(): void // tell plugins the layout changed + } +} + const DEFAULT_SETTINGS: TopBarButtonsSettings = { enabledButtons: [], desktop: false, @@ -186,6 +193,13 @@ export default class TopBarButtonsPlugin extends Plugin { }); if (Platform.isMobile || this.settings.desktop) { + const self = this; + this.register(around(Workspace.prototype, { + changeLayout(old) { return async function changeLayout(this: Workspace, ws: any){ + await old.call(this, ws); + self.addButtonsToAllLeaves(); + }} + })); this.registerEvent( this.app.workspace.on('file-open', () => { const activeLeaf = app.workspace.getLeaf(false); @@ -211,7 +225,7 @@ export default class TopBarButtonsPlugin extends Plugin { addButtonsToAllLeaves() { app.workspace.iterateAllLeaves(leaf => this.addButtonsToLeaf(leaf)); - (app.workspace as any).onLayoutChange(); + app.workspace.onLayoutChange(); } addButtonsToLeaf(leaf: WorkspaceLeaf) {