Skip to content

Commit 5708c63

Browse files
committed
feat: highlight opened diff view file
close #545
1 parent a9adfff commit 5708c63

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

src/main.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { SimpleGit } from "./gitManager/simpleGit";
3232
import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub";
3333
import { LocalStorageSettings } from "./setting/localStorageSettings";
3434
import {
35+
DiffViewState,
3536
FileStatusResult,
3637
mergeSettingsByPriority,
3738
ObsidianGitSettings,
@@ -68,6 +69,9 @@ export default class ObsidianGit extends Plugin {
6869
offlineMode = false;
6970
loading = false;
7071
cachedStatus: Status | undefined;
72+
// Used to store the path of the file that is currently shown in the diff view.
73+
lastDiffViewState: DiffViewState | undefined;
74+
openEvent: EventRef;
7175
modifyEvent: EventRef;
7276
deleteEvent: EventRef;
7377
createEvent: EventRef;
@@ -681,6 +685,7 @@ export default class ObsidianGit extends Plugin {
681685
"git-head-update",
682686
this.refreshUpdatedHead.bind(this)
683687
);
688+
this.app.workspace.offref(this.openEvent);
684689
this.app.metadataCache.offref(this.modifyEvent);
685690
this.app.metadataCache.offref(this.deleteEvent);
686691
this.app.metadataCache.offref(this.createEvent);
@@ -761,6 +766,11 @@ export default class ObsidianGit extends Plugin {
761766
this.gitReady = true;
762767
this.setState(PluginState.idle);
763768

769+
this.openEvent = this.app.workspace.on(
770+
"active-leaf-change",
771+
(leaf) => this.handleViewActiveState(leaf)
772+
);
773+
764774
this.modifyEvent = this.app.vault.on("modify", () => {
765775
this.debRefresh();
766776
});
@@ -1667,6 +1677,52 @@ I strongly recommend to use "Source mode" for viewing the conflicted files. For
16671677
}
16681678
}
16691679

1680+
handleViewActiveState(leaf: WorkspaceLeaf | null): void {
1681+
// Prevent removing focus when switching to other panes than file panes like search or GitView
1682+
if (!leaf?.view.getState().file) return;
1683+
1684+
const sourceControlLeaf = this.app.workspace
1685+
.getLeavesOfType(SOURCE_CONTROL_VIEW_CONFIG.type)
1686+
.first();
1687+
const historyLeaf = this.app.workspace
1688+
.getLeavesOfType(HISTORY_VIEW_CONFIG.type)
1689+
.first();
1690+
1691+
// Clear existing active state
1692+
sourceControlLeaf?.view.containerEl
1693+
.querySelector(`div.nav-file-title.is-active`)
1694+
?.removeClass("is-active");
1695+
historyLeaf?.view.containerEl
1696+
.querySelector(`div.nav-file-title.is-active`)
1697+
?.removeClass("is-active");
1698+
1699+
if (leaf?.view instanceof DiffView) {
1700+
const path = leaf.view.state.file;
1701+
this.lastDiffViewState = leaf.view.getState();
1702+
let el: Element | undefined | null;
1703+
if (sourceControlLeaf && leaf.view.state.staged) {
1704+
el = sourceControlLeaf.view.containerEl.querySelector(
1705+
`div.staged div.nav-file-title[data-path='${path}']`
1706+
);
1707+
} else if (
1708+
sourceControlLeaf &&
1709+
leaf.view.state.staged === false &&
1710+
!leaf.view.state.hash
1711+
) {
1712+
el = sourceControlLeaf.view.containerEl.querySelector(
1713+
`div.changes div.nav-file-title[data-path='${path}']`
1714+
);
1715+
} else if (historyLeaf && leaf.view.state.hash) {
1716+
el = historyLeaf.view.containerEl.querySelector(
1717+
`div.nav-file-title[data-path='${path}']`
1718+
);
1719+
}
1720+
el?.addClass("is-active");
1721+
} else {
1722+
this.lastDiffViewState = undefined;
1723+
}
1724+
}
1725+
16701726
// region: displaying / formatting messages
16711727
displayMessage(message: string, timeout: number = 4 * 1000): void {
16721728
this.statusBar?.displayMessage(message.toLowerCase(), timeout);

src/ui/history/components/logFileComponent.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
>
4646
<div
4747
class="tree-item-self is-clickable nav-file-title"
48+
class:is-active={view.plugin.lastDiffViewState?.file ==
49+
diff.vault_path && view.plugin.lastDiffViewState?.hash}
4850
data-path={diff.vault_path}
4951
aria-label-position={side}
5052
aria-label={diff.vault_path}

src/ui/sourceControl/components/fileComponent.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
>
8585
<div
8686
class="tree-item-self is-clickable nav-file-title"
87+
class:is-active={view.plugin.lastDiffViewState?.file ==
88+
change.vault_path &&
89+
!view.plugin.lastDiffViewState?.hash &&
90+
!view.plugin.lastDiffViewState?.staged}
8791
data-path={change.vault_path}
8892
aria-label-position={side}
8993
aria-label={change.vault_path}

src/ui/sourceControl/components/stagedFileComponent.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
>
6161
<div
6262
class="tree-item-self is-clickable nav-file-title"
63+
class:is-active={view.plugin.lastDiffViewState?.file ==
64+
change.vault_path &&
65+
!view.plugin.lastDiffViewState?.hash &&
66+
view.plugin.lastDiffViewState?.staged}
6367
data-path={change.vault_path}
6468
aria-label-position={side}
6569
aria-label={change.vault_path}

0 commit comments

Comments
 (0)