11import type moment from "moment" ;
22import { App , Plugin } from "obsidian" ;
33
4- import { getCommands } from "./commands" ;
4+ import { getCommands , openPeriodicNote } from "./commands" ;
55import { SETTINGS_UPDATED } from "./events" ;
66import {
77 DEFAULT_SETTINGS ,
@@ -11,6 +11,7 @@ import {
1111} from "./settings" ;
1212import {
1313 getLegacyWeeklyNoteSettings ,
14+ hasCoreDailyNotesPluginEnabled ,
1415 hasLegacyWeeklyNoteSettings ,
1516} from "./utils" ;
1617
@@ -25,26 +26,28 @@ export default class PeriodicNotesPlugin extends Plugin {
2526 public settings : ISettings ;
2627 public isInitialLoad : boolean ;
2728
29+ private ribbonEls : HTMLElement [ ] ;
30+
2831 async onload ( ) : Promise < void > {
32+ this . ribbonEls = [ ] ;
33+
2934 this . updateSettings = this . updateSettings . bind ( this ) ;
3035
3136 await this . loadSettings ( ) ;
3237 this . addSettingTab ( new PeriodicNotesSettingsTab ( this . app , this ) ) ;
3338
34- if ( this . app . workspace . layoutReady ) {
35- this . onLayoutReady ( ) ;
36- } else {
37- this . app . workspace . on ( "layout-ready" , this . onLayoutReady . bind ( this ) ) ;
38- }
39+ this . app . workspace . onLayoutReady ( this . onLayoutReady . bind ( this ) ) ;
3940 }
4041
41- private onLayoutReady ( ) {
42+ onLayoutReady ( ) : void {
4243 // If the user has Calendar Weekly Notes settings, migrate them automatically,
4344 // since the functionality will be deprecated.
4445 if ( this . isInitialLoad && hasLegacyWeeklyNoteSettings ( ) ) {
4546 this . migrateWeeklySettings ( ) ;
4647 this . settings . weekly . enabled = true ;
4748 }
49+
50+ this . configureRibbonIcons ( ) ;
4851 this . configureCommands ( ) ;
4952 }
5053
@@ -59,11 +62,37 @@ export default class PeriodicNotesPlugin extends Plugin {
5962 } ) ;
6063 }
6164
65+ private configureRibbonIcons ( ) {
66+ for ( const ribbonEl of this . ribbonEls ) {
67+ ribbonEl . detach ( ) ;
68+ }
69+
70+ // Only show ribbon icon if daily notes plugin is disabled to avoid confusion
71+ if ( this . settings . daily . enabled && ! hasCoreDailyNotesPluginEnabled ( ) ) {
72+ this . ribbonEls . push (
73+ this . addRibbonIcon ( "calendar-with-checkmark" , "Open today's note" , ( ) =>
74+ openPeriodicNote ( "daily" , window . moment ( ) , false )
75+ )
76+ ) ;
77+ }
78+ }
79+
6280 private configureCommands ( ) {
63- // TODO: There's currently no way to unload the commands when any of these
64- // are toggled off
81+ // Remove disabled commands
82+ [ "daily" , "weekly" , "monthly" ]
83+ . filter ( ( periodicity ) => ! this . settings [ periodicity ] . enabled )
84+ . forEach ( ( periodicity : IPeriodicity ) => {
85+ getCommands ( periodicity ) . forEach ( ( command ) =>
86+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87+ ( this . app as any ) . commands . removeCommand (
88+ `periodic-notes:${ command . id } `
89+ )
90+ ) ;
91+ } ) ;
92+
93+ // register enabled commands
6594 [ "daily" , "weekly" , "monthly" ]
66- . filter ( ( periodicity : IPeriodicity ) => this . settings [ periodicity ] . enabled )
95+ . filter ( ( periodicity ) => this . settings [ periodicity ] . enabled )
6796 . forEach ( ( periodicity : IPeriodicity ) => {
6897 getCommands ( periodicity ) . forEach ( this . addCommand . bind ( this ) ) ;
6998 } ) ;
@@ -93,6 +122,7 @@ export default class PeriodicNotesPlugin extends Plugin {
93122
94123 private onSettingsUpdate ( ) : void {
95124 this . configureCommands ( ) ;
125+ this . configureRibbonIcons ( ) ;
96126
97127 // Integrations (i.e. Calendar Plugin) can listen for changes to settings
98128 this . app . workspace . trigger ( SETTINGS_UPDATED ) ;
0 commit comments