From 5708c63ad7cad72c3939a4d554a5b98bc04783ed Mon Sep 17 00:00:00 2001 From: Vinzent Date: Tue, 29 Aug 2023 20:58:51 +0200 Subject: [PATCH] feat: highlight opened diff view file close #545 --- src/main.ts | 56 +++++++++++++++++++ .../components/logFileComponent.svelte | 2 + .../components/fileComponent.svelte | 4 ++ .../components/stagedFileComponent.svelte | 4 ++ 4 files changed, 66 insertions(+) diff --git a/src/main.ts b/src/main.ts index 98378c2c..f518ab6f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,6 +32,7 @@ import { SimpleGit } from "./gitManager/simpleGit"; import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub"; import { LocalStorageSettings } from "./setting/localStorageSettings"; import { + DiffViewState, FileStatusResult, mergeSettingsByPriority, ObsidianGitSettings, @@ -68,6 +69,9 @@ export default class ObsidianGit extends Plugin { offlineMode = false; loading = false; cachedStatus: Status | undefined; + // Used to store the path of the file that is currently shown in the diff view. + lastDiffViewState: DiffViewState | undefined; + openEvent: EventRef; modifyEvent: EventRef; deleteEvent: EventRef; createEvent: EventRef; @@ -681,6 +685,7 @@ export default class ObsidianGit extends Plugin { "git-head-update", this.refreshUpdatedHead.bind(this) ); + this.app.workspace.offref(this.openEvent); this.app.metadataCache.offref(this.modifyEvent); this.app.metadataCache.offref(this.deleteEvent); this.app.metadataCache.offref(this.createEvent); @@ -761,6 +766,11 @@ export default class ObsidianGit extends Plugin { this.gitReady = true; this.setState(PluginState.idle); + this.openEvent = this.app.workspace.on( + "active-leaf-change", + (leaf) => this.handleViewActiveState(leaf) + ); + this.modifyEvent = this.app.vault.on("modify", () => { this.debRefresh(); }); @@ -1667,6 +1677,52 @@ I strongly recommend to use "Source mode" for viewing the conflicted files. For } } + handleViewActiveState(leaf: WorkspaceLeaf | null): void { + // Prevent removing focus when switching to other panes than file panes like search or GitView + if (!leaf?.view.getState().file) return; + + const sourceControlLeaf = this.app.workspace + .getLeavesOfType(SOURCE_CONTROL_VIEW_CONFIG.type) + .first(); + const historyLeaf = this.app.workspace + .getLeavesOfType(HISTORY_VIEW_CONFIG.type) + .first(); + + // Clear existing active state + sourceControlLeaf?.view.containerEl + .querySelector(`div.nav-file-title.is-active`) + ?.removeClass("is-active"); + historyLeaf?.view.containerEl + .querySelector(`div.nav-file-title.is-active`) + ?.removeClass("is-active"); + + if (leaf?.view instanceof DiffView) { + const path = leaf.view.state.file; + this.lastDiffViewState = leaf.view.getState(); + let el: Element | undefined | null; + if (sourceControlLeaf && leaf.view.state.staged) { + el = sourceControlLeaf.view.containerEl.querySelector( + `div.staged div.nav-file-title[data-path='${path}']` + ); + } else if ( + sourceControlLeaf && + leaf.view.state.staged === false && + !leaf.view.state.hash + ) { + el = sourceControlLeaf.view.containerEl.querySelector( + `div.changes div.nav-file-title[data-path='${path}']` + ); + } else if (historyLeaf && leaf.view.state.hash) { + el = historyLeaf.view.containerEl.querySelector( + `div.nav-file-title[data-path='${path}']` + ); + } + el?.addClass("is-active"); + } else { + this.lastDiffViewState = undefined; + } + } + // region: displaying / formatting messages displayMessage(message: string, timeout: number = 4 * 1000): void { this.statusBar?.displayMessage(message.toLowerCase(), timeout); diff --git a/src/ui/history/components/logFileComponent.svelte b/src/ui/history/components/logFileComponent.svelte index 3341ad99..d6e3e38b 100644 --- a/src/ui/history/components/logFileComponent.svelte +++ b/src/ui/history/components/logFileComponent.svelte @@ -45,6 +45,8 @@ >