1
1
import { Errors } from "isomorphic-git" ;
2
- import type {
3
- Debouncer ,
4
- EventRef ,
5
- Menu ,
6
- TAbstractFile ,
7
- WorkspaceLeaf ,
8
- } from "obsidian" ;
2
+ import type { Debouncer , Menu , TAbstractFile , WorkspaceLeaf } from "obsidian" ;
9
3
import {
10
4
debounce ,
11
5
MarkdownView ,
@@ -77,11 +71,7 @@ export default class ObsidianGit extends Plugin {
77
71
cachedStatus : Status | undefined ;
78
72
// Used to store the path of the file that is currently shown in the diff view.
79
73
lastDiffViewState : Record < string , unknown > | undefined ;
80
- openEvent : EventRef ;
81
- modifyEvent : EventRef ;
82
- deleteEvent : EventRef ;
83
- createEvent : EventRef ;
84
- renameEvent : EventRef ;
74
+ intervalsToClear : number [ ] = [ ] ;
85
75
lineAuthoringFeature : LineAuthoringFeature = new LineAuthoringFeature ( this ) ;
86
76
87
77
debRefresh : Debouncer < [ ] , void > ;
@@ -156,11 +146,62 @@ export default class ObsidianGit extends Plugin {
156
146
this . addSettingTab ( this . settingsTab ) ;
157
147
158
148
if ( ! this . localStorage . getPluginDisabled ( ) ) {
159
- this . loadPlugin ( ) ;
149
+ this . registerStuff ( ) ;
150
+
151
+ this . app . workspace . onLayoutReady ( ( ) =>
152
+ this . init ( { fromReload : false } ) . catch ( ( e ) =>
153
+ this . displayError ( e )
154
+ )
155
+ ) ;
160
156
}
161
157
}
162
158
163
- loadPlugin ( ) {
159
+ onExternalSettingsChange ( ) {
160
+ this . reloadSettings ( ) . catch ( ( e ) => this . displayError ( e ) ) ;
161
+ }
162
+
163
+ /** Reloads the settings from disk and applies them by unloading the plugin
164
+ * and initializing it again.
165
+ */
166
+ async reloadSettings ( ) : Promise < void > {
167
+ const previousSettings = JSON . stringify ( this . settings ) ;
168
+
169
+ await this . loadSettings ( ) ;
170
+
171
+ const newSettings = JSON . stringify ( this . settings ) ;
172
+
173
+ // Only reload plugin if the settings have actually changed
174
+ if ( previousSettings !== newSettings ) {
175
+ this . log ( "Reloading settings" ) ;
176
+
177
+ this . unloadPlugin ( ) ;
178
+
179
+ await this . init ( { fromReload : true } ) ;
180
+
181
+ this . app . workspace
182
+ . getLeavesOfType ( SOURCE_CONTROL_VIEW_CONFIG . type )
183
+ . forEach ( ( leaf ) => {
184
+ if ( ! ( leaf . isDeferred ?? false ) )
185
+ return ( leaf . view as GitView ) . reload ( ) ;
186
+ } ) ;
187
+
188
+ this . app . workspace
189
+ . getLeavesOfType ( HISTORY_VIEW_CONFIG . type )
190
+ . forEach ( ( leaf ) => {
191
+ if ( ! ( leaf . isDeferred ?? false ) )
192
+ return ( leaf . view as HistoryView ) . reload ( ) ;
193
+ } ) ;
194
+ }
195
+ }
196
+
197
+ /** This method only registers events, views, commands and more.
198
+ *
199
+ * This only needs to be called once since the registered events are
200
+ * unregistered when the plugin is unloaded.
201
+ *
202
+ * This mustn't depend on the plugin's settings.
203
+ */
204
+ registerStuff ( ) : void {
164
205
this . registerEvent (
165
206
this . app . workspace . on ( "obsidian-git:refresh" , ( ) => {
166
207
this . refresh ( ) . catch ( ( e ) => this . displayError ( e ) ) ;
@@ -172,9 +213,46 @@ export default class ObsidianGit extends Plugin {
172
213
} )
173
214
) ;
174
215
216
+ this . registerEvent (
217
+ this . app . workspace . on ( "file-menu" , ( menu , file , source ) => {
218
+ this . handleFileMenu ( menu , file , source ) ;
219
+ } )
220
+ ) ;
221
+
222
+ this . registerEvent (
223
+ this . app . workspace . on ( "active-leaf-change" , ( leaf ) => {
224
+ this . handleViewActiveState ( leaf ) ;
225
+ } )
226
+ ) ;
227
+ this . registerEvent (
228
+ this . app . vault . on ( "modify" , ( ) => {
229
+ this . debRefresh ( ) ;
230
+ this . autoCommitDebouncer ?.( ) ;
231
+ } )
232
+ ) ;
233
+ this . registerEvent (
234
+ this . app . vault . on ( "delete" , ( ) => {
235
+ this . debRefresh ( ) ;
236
+ this . autoCommitDebouncer ?.( ) ;
237
+ } )
238
+ ) ;
239
+ this . registerEvent (
240
+ this . app . vault . on ( "create" , ( ) => {
241
+ this . debRefresh ( ) ;
242
+ this . autoCommitDebouncer ?.( ) ;
243
+ } )
244
+ ) ;
245
+ this . registerEvent (
246
+ this . app . vault . on ( "rename" , ( ) => {
247
+ this . debRefresh ( ) ;
248
+ this . autoCommitDebouncer ?.( ) ;
249
+ } )
250
+ ) ;
251
+
175
252
this . registerView ( SOURCE_CONTROL_VIEW_CONFIG . type , ( leaf ) => {
176
253
return new GitView ( leaf , this ) ;
177
254
} ) ;
255
+
178
256
this . registerView ( HISTORY_VIEW_CONFIG . type , ( leaf ) => {
179
257
return new HistoryView ( leaf , this ) ;
180
258
} ) ;
@@ -207,44 +285,16 @@ export default class ObsidianGit extends Plugin {
207
285
}
208
286
) ;
209
287
210
- this . lineAuthoringFeature . onLoadPlugin ( ) ;
211
-
212
288
this . registerHoverLinkSource ( SOURCE_CONTROL_VIEW_CONFIG . type , {
213
289
display : "Git View" ,
214
290
defaultMod : true ,
215
291
} ) ;
216
292
293
+ this . lineAuthoringFeature . onLoadPlugin ( ) ;
294
+
217
295
this . setRefreshDebouncer ( ) ;
218
296
219
297
addCommmands ( this ) ;
220
-
221
- this . registerEvent (
222
- this . app . workspace . on ( "file-menu" , ( menu , file , source ) => {
223
- this . handleFileMenu ( menu , file , source ) ;
224
- } )
225
- ) ;
226
-
227
- if ( this . settings . showStatusBar ) {
228
- // init statusBar
229
- const statusBarEl = this . addStatusBarItem ( ) ;
230
- this . statusBar = new StatusBar ( statusBarEl , this ) ;
231
- this . registerInterval (
232
- window . setInterval ( ( ) => this . statusBar ?. display ( ) , 1000 )
233
- ) ;
234
- }
235
-
236
- if ( Platform . isDesktop && this . settings . showBranchStatusBar ) {
237
- const branchStatusBarEl = this . addStatusBarItem ( ) ;
238
- this . branchBar = new BranchStatusBar ( branchStatusBarEl , this ) ;
239
- this . registerInterval (
240
- window . setInterval (
241
- ( ) => void this . branchBar ?. display ( ) . catch ( console . error ) ,
242
- 60000
243
- )
244
- ) ;
245
- }
246
-
247
- this . app . workspace . onLayoutReady ( ( ) => this . init ( ) ) ;
248
298
}
249
299
250
300
setRefreshDebouncer ( ) : void {
@@ -373,11 +423,14 @@ export default class ObsidianGit extends Plugin {
373
423
374
424
this . lineAuthoringFeature . deactivateFeature ( ) ;
375
425
this . automaticsManager . unload ( ) ;
376
- this . app . workspace . offref ( this . openEvent ) ;
377
- this . app . metadataCache . offref ( this . modifyEvent ) ;
378
- this . app . metadataCache . offref ( this . deleteEvent ) ;
379
- this . app . metadataCache . offref ( this . createEvent ) ;
380
- this . app . metadataCache . offref ( this . renameEvent ) ;
426
+ this . branchBar ?. remove ( ) ;
427
+ this . statusBar ?. remove ( ) ;
428
+
429
+ for ( const interval of this . intervalsToClear ) {
430
+ window . clearInterval ( interval ) ;
431
+ }
432
+ this . intervalsToClear = [ ] ;
433
+
381
434
this . debRefresh . cancel ( ) ;
382
435
}
383
436
@@ -406,7 +459,15 @@ export default class ObsidianGit extends Plugin {
406
459
return Platform . isDesktopApp ;
407
460
}
408
461
409
- async init ( ) : Promise < void > {
462
+ async init ( { fromReload = false } ) : Promise < void > {
463
+ if ( this . settings . showStatusBar ) {
464
+ const statusBarEl = this . addStatusBarItem ( ) ;
465
+ this . statusBar = new StatusBar ( statusBarEl , this ) ;
466
+ this . intervalsToClear . push (
467
+ window . setInterval ( ( ) => this . statusBar ?. display ( ) , 1000 )
468
+ ) ;
469
+ }
470
+
410
471
try {
411
472
if ( this . useSimpleGit ) {
412
473
this . gitManager = new SimpleGit ( this ) ;
@@ -432,40 +493,32 @@ export default class ObsidianGit extends Plugin {
432
493
this . gitReady = true ;
433
494
this . setPluginState ( { gitAction : CurrentGitAction . idle } ) ;
434
495
435
- this . openEvent = this . app . workspace . on (
436
- "active-leaf-change" ,
437
- ( leaf ) => this . handleViewActiveState ( leaf )
438
- ) ;
439
-
440
- this . modifyEvent = this . app . vault . on ( "modify" , ( ) => {
441
- this . debRefresh ( ) ;
442
- this . autoCommitDebouncer ?.( ) ;
443
- } ) ;
444
- this . deleteEvent = this . app . vault . on ( "delete" , ( ) => {
445
- this . debRefresh ( ) ;
446
- this . autoCommitDebouncer ?.( ) ;
447
- } ) ;
448
- this . createEvent = this . app . vault . on ( "create" , ( ) => {
449
- this . debRefresh ( ) ;
450
- this . autoCommitDebouncer ?.( ) ;
451
- } ) ;
452
- this . renameEvent = this . app . vault . on ( "rename" , ( ) => {
453
- this . debRefresh ( ) ;
454
- this . autoCommitDebouncer ?.( ) ;
455
- } ) ;
456
-
457
- this . registerEvent ( this . modifyEvent ) ;
458
- this . registerEvent ( this . deleteEvent ) ;
459
- this . registerEvent ( this . createEvent ) ;
460
- this . registerEvent ( this . renameEvent ) ;
461
-
496
+ if (
497
+ Platform . isDesktop &&
498
+ this . settings . showBranchStatusBar
499
+ ) {
500
+ const branchStatusBarEl = this . addStatusBarItem ( ) ;
501
+ this . branchBar = new BranchStatusBar (
502
+ branchStatusBarEl ,
503
+ this
504
+ ) ;
505
+ this . intervalsToClear . push (
506
+ window . setInterval (
507
+ ( ) =>
508
+ void this . branchBar
509
+ ?. display ( )
510
+ . catch ( console . error ) ,
511
+ 60000
512
+ )
513
+ ) ;
514
+ }
462
515
await this . branchBar ?. display ( ) ;
463
516
464
517
this . lineAuthoringFeature . conditionallyActivateBySettings ( ) ;
465
518
466
519
this . app . workspace . trigger ( "obsidian-git:refresh" ) ;
467
520
468
- if ( this . settings . autoPullOnBoot ) {
521
+ if ( ! fromReload && this . settings . autoPullOnBoot ) {
469
522
this . promiseQueue . addTask ( ( ) =>
470
523
this . pullChangesFromRemote ( )
471
524
) ;
@@ -490,7 +543,7 @@ export default class ObsidianGit extends Plugin {
490
543
try {
491
544
await this . gitManager . init ( ) ;
492
545
new Notice ( "Initialized new repo" ) ;
493
- await this . init ( ) ;
546
+ await this . init ( { fromReload : true } ) ;
494
547
} catch ( e ) {
495
548
this . displayError ( e ) ;
496
549
}
@@ -595,7 +648,7 @@ export default class ObsidianGit extends Plugin {
595
648
*/
596
649
async isAllInitialized ( ) : Promise < boolean > {
597
650
if ( ! this . gitReady ) {
598
- await this . init ( ) ;
651
+ await this . init ( { fromReload : true } ) ;
599
652
}
600
653
return this . gitReady ;
601
654
}
0 commit comments