1
- import { BookmarksPluginView , EventRef } from "obsidian" ;
1
+ import { BookmarksPluginLeaf , BookmarksPluginView , EventRef } from "obsidian" ;
2
2
import { inject , injectable , named } from "inversify" ;
3
3
import SI from "@config/inversify.types" ;
4
4
import ObsidianFacade from "@src/Obsidian/ObsidianFacade" ;
@@ -11,12 +11,19 @@ import EventDispatcherInterface from "@src/Components/EventDispatcher/Interfaces
11
11
import { AppEvents } from "@src/Types" ;
12
12
import ListenerRef from "@src/Components/EventDispatcher/Interfaces/ListenerRef" ;
13
13
14
+ enum State {
15
+ Disabled = "disabled" ,
16
+ Enabled = "enabled" ,
17
+ Awaiting = "awaiting" ,
18
+ }
19
+
14
20
@injectable ( )
15
21
export default class BookmarksManager extends AbstractFeature < Feature > {
16
- private enabled = false ;
22
+ private state : State = State . Disabled ;
17
23
private view : BookmarksPluginView = null ;
18
24
private ref : EventRef = null ;
19
25
private metaRef : ListenerRef < "metadata:cache:changed" > = null ;
26
+ private activeLeafChangeRef : ListenerRef < "active:leaf:change" > = null ;
20
27
private resolver : ResolverInterface ;
21
28
private requestUpdate : ( ) => void ;
22
29
@@ -33,29 +40,27 @@ export default class BookmarksManager extends AbstractFeature<Feature> {
33
40
) {
34
41
super ( ) ;
35
42
this . requestUpdate = ( ) => setTimeout ( this . onChanged . bind ( this ) , 100 ) ;
36
- this . metaRef = this . dispatcher . addListener ( {
37
- name : "metadata:cache:changed" ,
38
- cb : this . requestUpdate . bind ( this ) ,
39
- } ) ;
40
43
this . resolver = service . createResolver ( this . getId ( ) ) ;
41
44
}
42
45
46
+ private setState ( state : State ) : void {
47
+ this . logger . log ( `State changed from ${ this . state } to ${ state } ` ) ;
48
+ this . state = state ;
49
+ }
43
50
static getId ( ) : Feature {
44
51
return Feature . Bookmarks ;
45
52
}
46
53
47
54
enable ( ) : void {
48
- if ( ! this . isEnabled ( ) && this . initView ( ) && this . subscribe ( ) ) {
49
- this . enabled = true ;
50
- this . requestUpdate ( ) ;
51
- }
55
+ this . tryEnable ( true ) ;
52
56
}
53
57
54
58
disable ( ) : void {
59
+ this . logger . log ( "disable" ) ;
55
60
if ( this . isEnabled ( ) ) {
56
61
this . unsubscribe ( ) ;
57
62
this . view = null ;
58
- this . enabled = false ;
63
+ this . setState ( State . Disabled ) ;
59
64
}
60
65
}
61
66
@@ -64,7 +69,28 @@ export default class BookmarksManager extends AbstractFeature<Feature> {
64
69
}
65
70
66
71
isEnabled ( ) : boolean {
67
- return this . enabled ;
72
+ return this . state !== State . Disabled ;
73
+ }
74
+
75
+ private tryEnable ( subscribeOnActive = false ) : void {
76
+ const leaf = this . facade . getLeavesOfType < BookmarksPluginLeaf > ( Leaves . Bookmarks ) ?. [ 0 ] ;
77
+
78
+ if ( leaf . isVisible ( ) ) {
79
+ if ( this . state !== State . Enabled && this . initView ( ) && this . subscribe ( ) ) {
80
+ this . setState ( State . Enabled ) ;
81
+ this . requestUpdate ( ) ;
82
+ }
83
+ if ( this . activeLeafChangeRef ) {
84
+ this . dispatcher . removeListener ( this . activeLeafChangeRef ) ;
85
+ this . activeLeafChangeRef = null ;
86
+ }
87
+ } else if ( subscribeOnActive ) {
88
+ this . setState ( State . Awaiting ) ;
89
+ this . activeLeafChangeRef = this . dispatcher . addListener ( {
90
+ name : "active:leaf:change" ,
91
+ cb : ( ) => this . tryEnable ( ) ,
92
+ } ) ;
93
+ }
68
94
}
69
95
70
96
private initView ( ) : boolean {
@@ -87,16 +113,34 @@ export default class BookmarksManager extends AbstractFeature<Feature> {
87
113
this . logger . log ( "Triggered by plugin event" ) ;
88
114
this . requestUpdate ( ) ;
89
115
} ) ;
116
+ this . metaRef = this . dispatcher . addListener ( {
117
+ name : "metadata:cache:changed" ,
118
+ cb : this . requestUpdate . bind ( this ) ,
119
+ } ) ;
90
120
return true ;
91
121
}
92
122
93
123
private unsubscribe ( ) : void {
94
- this . dispatcher . removeListener ( this . metaRef ) ;
95
- this . view . plugin . offref ( this . ref ) ;
96
- this . view . plugin . trigger ( "changed" ) ;
124
+ if ( this . metaRef ) {
125
+ this . dispatcher . removeListener ( this . metaRef ) ;
126
+ }
127
+ if ( this . activeLeafChangeRef ) {
128
+ this . dispatcher . removeListener ( this . activeLeafChangeRef ) ;
129
+ }
130
+ if ( this . state === State . Enabled ) {
131
+ this . view . plugin . offref ( this . ref ) ;
132
+ this . view . plugin . trigger ( "changed" ) ;
133
+ }
97
134
}
98
135
99
136
private onChanged ( path : string = null ) : { [ k : string ] : boolean } {
137
+ if ( this . state !== State . Enabled ) {
138
+ this . logger . log ( `Called onChanged when is ${ this . state } state` ) ;
139
+ return ;
140
+ }
141
+ if ( ! this . view . plugin ) {
142
+ return ;
143
+ }
100
144
const items = this . view . plugin . items ;
101
145
const itemDoms = this . view . itemDoms ;
102
146
const result : { [ k : string ] : boolean } = { } ;
0 commit comments