@@ -29,12 +29,14 @@ interface CustomSortPluginSettings {
2929 additionalSortspecFile : string
3030 suspended : boolean
3131 statusBarEntryEnabled : boolean
32+ notificationsEnabled : boolean
3233}
3334
3435const DEFAULT_SETTINGS : CustomSortPluginSettings = {
3536 additionalSortspecFile : 'Inbox/Inbox.md' ,
3637 suspended : true , // if false by default, it would be hard to handle the auto-parse after plugin install
37- statusBarEntryEnabled : true
38+ statusBarEntryEnabled : true ,
39+ notificationsEnabled : true
3840}
3941
4042const SORTSPEC_FILE_NAME : string = 'sortspec.md'
@@ -53,6 +55,12 @@ export default class CustomSortPlugin extends Plugin {
5355 sortSpecCache ?: SortSpecsCollection | null
5456 initialAutoOrManualSortingTriggered : boolean
5557
58+ showNotice ( message : string , timeout ?: number ) {
59+ if ( this . settings . notificationsEnabled ) {
60+ new Notice ( message , timeout )
61+ }
62+ }
63+
5664 readAndParseSortingSpec ( ) {
5765 const mCache : MetadataCache = this . app . metadataCache
5866 let failed : boolean = false
@@ -91,14 +99,14 @@ export default class CustomSortPlugin extends Plugin {
9199 } )
92100
93101 if ( this . sortSpecCache ) {
94- new Notice ( `Parsing custom sorting specification SUCCEEDED!` )
102+ this . showNotice ( `Parsing custom sorting specification SUCCEEDED!` )
95103 } else {
96104 if ( anySortingSpecFound ) {
97105 errorMessage = errorMessage ? errorMessage : `No valid '${ SORTINGSPEC_YAML_KEY } :' key(s) in YAML front matter or multiline YAML indentation error or general YAML syntax error`
98106 } else {
99107 errorMessage = `No custom sorting specification found or only empty specification(s)`
100108 }
101- new Notice ( `Parsing custom sorting specification FAILED. Suspending the plugin.\n${ errorMessage } ` , ERROR_NOTICE_TIMEOUT )
109+ this . showNotice ( `Parsing custom sorting specification FAILED. Suspending the plugin.\n${ errorMessage } ` , ERROR_NOTICE_TIMEOUT )
102110 this . settings . suspended = true
103111 this . saveSettings ( )
104112 }
@@ -110,13 +118,13 @@ export default class CustomSortPlugin extends Plugin {
110118 this . saveSettings ( )
111119 let iconToSet : string
112120 if ( this . settings . suspended ) {
113- new Notice ( 'Custom sort OFF' ) ;
121+ this . showNotice ( 'Custom sort OFF' ) ;
114122 this . sortSpecCache = null
115123 iconToSet = ICON_SORT_SUSPENDED
116124 } else {
117125 this . readAndParseSortingSpec ( ) ;
118126 if ( this . sortSpecCache ) {
119- new Notice ( 'Custom sort ON' ) ;
127+ this . showNotice ( 'Custom sort ON' ) ;
120128 this . initialAutoOrManualSortingTriggered = true
121129 iconToSet = ICON_SORT_ENABLED_ACTIVE
122130 } else {
@@ -180,7 +188,7 @@ export default class CustomSortPlugin extends Plugin {
180188 this . readAndParseSortingSpec ( )
181189 this . initialAutoOrManualSortingTriggered = true
182190 if ( this . sortSpecCache ) { // successful read of sorting specifications?
183- new Notice ( 'Custom sort ON' )
191+ this . showNotice ( 'Custom sort ON' )
184192 const fileExplorerView : FileExplorerView = this . getFileExplorer ( )
185193 if ( fileExplorerView ) {
186194 setIcon ( this . ribbonIconEl , ICON_SORT_ENABLED_ACTIVE )
@@ -342,5 +350,18 @@ class CustomSortSettingTab extends PluginSettingTab {
342350 }
343351 await this . plugin . saveSettings ( ) ;
344352 } ) ) ;
353+
354+ new Setting ( containerEl )
355+ . setName ( 'Enable notifications of plugin state changes' )
356+ . setDesc ( 'The plugin can show notifications about its state changes: e.g. when successfully parsed and applied'
357+ + ' the custom sorting specification, or, when the parsing failed. If the notifications are disabled,'
358+ + ' the only indicator of plugin state is the ribbon button icon. The developer console presents the parsing'
359+ + ' error messages regardless if the notifications are enabled or not.' )
360+ . addToggle ( toggle => toggle
361+ . setValue ( this . plugin . settings . notificationsEnabled )
362+ . onChange ( async ( value ) => {
363+ this . plugin . settings . notificationsEnabled = value ;
364+ await this . plugin . saveSettings ( ) ;
365+ } ) ) ;
345366 }
346367}
0 commit comments