From 41f5ed0a6b85bbfffbf0d595c500913cf04f0725 Mon Sep 17 00:00:00 2001 From: Benny Wydooghe Date: Wed, 25 Aug 2021 20:03:34 +0200 Subject: [PATCH] Improve performance by only calling getViewState() when necessary --- README.md | 1 - main.ts | 12 ++++++++---- manifest.json | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e59ede2..525b039 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,3 @@ obsidianUIMode: preview ... and this will always open the note in "preview" mode. This plug-in also ensures that a note is always opened in the configured default mode (suppose the Obsidian setting has "preview" as default mode but the pane is currently in "source" mode, then opening a new note in that same pane will open in "preview" mode). - diff --git a/main.ts b/main.ts index 70140d6..72885bc 100644 --- a/main.ts +++ b/main.ts @@ -11,8 +11,6 @@ export default class ViewModeByFrontmatterPlugin extends Plugin { return; } - let state = leaf.getViewState(); - // ... get frontmatter data and search for a key indicating the desired view mode // and when the given key is present ... set it to the declared mode const fileCache = this.app.metadataCache.getFileCache(view.file); @@ -20,8 +18,11 @@ export default class ViewModeByFrontmatterPlugin extends Plugin { if (null !== fileDeclaredUIMode) { if (['source', 'preview', 'live'].includes(fileDeclaredUIMode) - && state.state.mode !== fileDeclaredUIMode) { + && view.getMode() !== fileDeclaredUIMode) { + let state = leaf.getViewState(); + state.state.mode = fileDeclaredUIMode; + leaf.setViewState(state); } @@ -30,8 +31,11 @@ export default class ViewModeByFrontmatterPlugin extends Plugin { const defaultViewMode = this.app.vault.config.defaultViewMode ? this.app.vault.config.defaultViewMode : 'source'; - if (state.state.mode !== defaultViewMode) { + if (view.getMode() !== defaultViewMode) { + let state = leaf.getViewState(); + state.state.mode = defaultViewMode; + leaf.setViewState(state); } }; diff --git a/manifest.json b/manifest.json index 48a5bcf..8553772 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "id": "obsidian-view-mode-by-frontmatter", "name": "Force note view mode (preview, source) using front matter", - "version": "1.0.0", + "version": "1.0.1", "minAppVersion": "0.9.12", - "description": "This plugin allows to force the view mode for a note by using front matter: YAML block with 'obsidian_ui_mode' as key.", + "description": "This plugin allows to force the view mode for a note by using front matter", "author": "Benny Wydooghe", "authorUrl": "https://i-net.be", "isDesktopOnly": false