@@ -32,6 +32,7 @@ import { SimpleGit } from "./gitManager/simpleGit";
32
32
import { openHistoryInGitHub , openLineInGitHub } from "./openInGitHub" ;
33
33
import { LocalStorageSettings } from "./setting/localStorageSettings" ;
34
34
import {
35
+ DiffViewState ,
35
36
FileStatusResult ,
36
37
mergeSettingsByPriority ,
37
38
ObsidianGitSettings ,
@@ -68,6 +69,9 @@ export default class ObsidianGit extends Plugin {
68
69
offlineMode = false ;
69
70
loading = false ;
70
71
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 ;
71
75
modifyEvent : EventRef ;
72
76
deleteEvent : EventRef ;
73
77
createEvent : EventRef ;
@@ -681,6 +685,7 @@ export default class ObsidianGit extends Plugin {
681
685
"git-head-update" ,
682
686
this . refreshUpdatedHead . bind ( this )
683
687
) ;
688
+ this . app . workspace . offref ( this . openEvent ) ;
684
689
this . app . metadataCache . offref ( this . modifyEvent ) ;
685
690
this . app . metadataCache . offref ( this . deleteEvent ) ;
686
691
this . app . metadataCache . offref ( this . createEvent ) ;
@@ -761,6 +766,11 @@ export default class ObsidianGit extends Plugin {
761
766
this . gitReady = true ;
762
767
this . setState ( PluginState . idle ) ;
763
768
769
+ this . openEvent = this . app . workspace . on (
770
+ "active-leaf-change" ,
771
+ ( leaf ) => this . handleViewActiveState ( leaf )
772
+ ) ;
773
+
764
774
this . modifyEvent = this . app . vault . on ( "modify" , ( ) => {
765
775
this . debRefresh ( ) ;
766
776
} ) ;
@@ -1667,6 +1677,52 @@ I strongly recommend to use "Source mode" for viewing the conflicted files. For
1667
1677
}
1668
1678
}
1669
1679
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
+
1670
1726
// region: displaying / formatting messages
1671
1727
displayMessage ( message : string , timeout : number = 4 * 1000 ) : void {
1672
1728
this . statusBar ?. displayMessage ( message . toLowerCase ( ) , timeout ) ;
0 commit comments