77 Setting ,
88 WorkspaceLeaf ,
99 Notice ,
10+ debounce ,
1011} from "obsidian" ;
1112
1213// Define the constant for the ClockView type
@@ -291,6 +292,7 @@ export default class ClockPlugin extends Plugin {
291292 view : ClockView ;
292293 settings : ClockSettings ;
293294 updateInterval : NodeJS . Timeout | null = null ;
295+ settingsChanged : boolean ;
294296
295297 // Load plugin settings, register the ClockView, add the 'Open Clock'
296298 // command, and set up layout readiness check
@@ -352,6 +354,26 @@ export default class ClockPlugin extends Plugin {
352354 // Save plugin settings
353355 async saveSettings ( ) : Promise < void > {
354356 await this . saveData ( this . settings ) ;
357+ this . settingsChanged = true ;
358+ }
359+
360+ checkAndSortTimezones ( ) {
361+ if ( this . settingsChanged ) {
362+ this . settings . timeZonePairs = this . sortTimeZones ( ) ;
363+ this . settingsChanged = false ;
364+ }
365+ }
366+
367+ sortTimeZones ( ) : TimezonePair [ ] {
368+ const currentOffset = DateTime . local ( ) . offset ;
369+ return this . settings . timeZonePairs . slice ( ) . sort ( ( a , b ) => {
370+ const offsetA = parseFloat ( a . offset ) ;
371+ const offsetB = parseFloat ( b . offset ) ;
372+ return (
373+ Math . abs ( currentOffset - offsetA ) -
374+ Math . abs ( currentOffset - offsetB )
375+ ) ;
376+ } ) ;
355377 }
356378}
357379
@@ -415,7 +437,7 @@ class ClockSettingTab extends PluginSettingTab {
415437 } ) ;
416438 h2El . createEl ( "a" , {
417439 text : " Luxon Reference" ,
418- href : "https://moment.github.io/luxon/docs/manual/ formatting.html " ,
440+ href : "https://moment.github.io/luxon/#/ formatting?id=table-of-tokens " ,
419441 attr : {
420442 target : "_blank" ,
421443 } ,
@@ -434,18 +456,32 @@ class ClockSettingTab extends PluginSettingTab {
434456 hint . classList . add ( "small-hint" ) ;
435457 textField . inputEl . after ( hint ) ;
436458
437- textField . inputEl . addEventListener ( "input" , ( ) => {
438- const value = textField . getValue ( ) ;
439- if ( value . trim ( ) !== "" ) {
459+ const handleInputDebounced = debounce ( ( ) => {
460+ const value = textField . getValue ( ) . trim ( ) ;
461+ if ( value !== "" ) {
440462 this . plugin . settings . timeFormat = value ;
441463 this . plugin . saveSettings ( ) ;
442464 this . plugin . view . displayTime ( ) ;
443465 hint . textContent = "" ;
466+ }
467+ } , 1250 ) ;
468+
469+ // Immediate function to handle empty input
470+ const handleInputImmediate = ( ) => {
471+ const value = textField . getValue ( ) . trim ( ) ;
472+ if ( value === "" ) {
473+ hint . textContent = "Enter a valid format" ;
444474 } else {
445- hint . textContent = "Enter a format" ;
475+ handleInputDebounced ( ) ;
446476 }
447- } ) ;
477+ } ;
478+
479+ textField . inputEl . addEventListener (
480+ "input" ,
481+ handleInputImmediate ,
482+ ) ;
448483 } )
484+
449485 . addExtraButton ( ( button ) =>
450486 button
451487 . setIcon ( "reset" )
@@ -529,17 +565,31 @@ class ClockSettingTab extends PluginSettingTab {
529565 hint . classList . add ( "small-hint" ) ;
530566 textField . inputEl . after ( hint ) ;
531567
532- textField . inputEl . addEventListener ( "input" , ( ) => {
533- const value = textField . getValue ( ) ;
534- if ( value . trim ( ) !== "" ) {
568+ // Debounced function to handle non-empty input
569+ const handleInputDebounced = debounce ( ( ) => {
570+ const value = textField . getValue ( ) . trim ( ) ;
571+ if ( value !== "" ) {
535572 this . plugin . settings . dateFormat = value ;
536573 this . plugin . saveSettings ( ) ;
537574 this . plugin . view . displayTime ( ) ;
538575 hint . textContent = "" ;
539- } else {
576+ }
577+ } , 1250 ) ;
578+
579+ // Immediate function to handle empty input
580+ const handleInputImmediate = ( ) => {
581+ const value = textField . getValue ( ) . trim ( ) ;
582+ if ( value === "" ) {
540583 hint . textContent = "Enter a format" ;
584+ } else {
585+ handleInputDebounced ( ) ;
541586 }
542- } ) ;
587+ } ;
588+
589+ textField . inputEl . addEventListener (
590+ "input" ,
591+ handleInputImmediate ,
592+ ) ;
543593 } )
544594 . addExtraButton ( ( button ) =>
545595 button
@@ -737,17 +787,29 @@ class ClockSettingTab extends PluginSettingTab {
737787 hint . classList . add ( "small-hint" ) ;
738788 textField . inputEl . after ( hint ) ;
739789
740- textField . inputEl . addEventListener ( "input" , ( ) => {
741- const value = textField . getValue ( ) ;
742- if ( value . trim ( ) !== "" ) {
790+ // Debounced function to handle non-empty input
791+ const handleInputDebounced = debounce ( ( ) => {
792+ const value = textField . getValue ( ) . trim ( ) ;
793+ if ( value !== "" ) {
743794 this . plugin . settings . timezoneFormat = value ;
744795 this . plugin . saveSettings ( ) ;
745796 this . plugin . view . displayTime ( ) ;
746797 hint . textContent = "" ;
747- } else {
798+ }
799+ } , 1250 ) ;
800+
801+ // Immediate function to handle empty input
802+ const handleInputImmediate = ( ) => {
803+ const value = textField . getValue ( ) . trim ( ) ;
804+ if ( value === "" ) {
748805 hint . textContent = "Enter a format" ;
806+ } else {
807+ handleInputDebounced ( ) ;
749808 }
750- } ) ;
809+ } ;
810+
811+ textField . inputEl . addEventListener ( "input" , handleInputImmediate ) ;
812+
751813 } )
752814 . addExtraButton ( ( button ) =>
753815 button
@@ -819,12 +881,13 @@ class ClockSettingTab extends PluginSettingTab {
819881 const nameInput = nameCell . createEl ( "input" , {
820882 type : "text" ,
821883 value : entry . name ,
884+ cls : "custom-input-width" ,
822885 } ) ;
823- nameInput . classList . add ( "custom-input-width" ) ;
824886
825887 const hint = createHintElement ( ) ;
826888
827- function handleInput ( ) {
889+ // Debounce function for name input
890+ const handleInput = debounce ( ( ) => {
828891 const newName = nameInput . value . trim ( ) ;
829892 if ( newName !== "" ) {
830893 entry . name = newName ;
@@ -834,7 +897,7 @@ class ClockSettingTab extends PluginSettingTab {
834897 } else {
835898 hint . textContent = "Invalid format" ;
836899 }
837- }
900+ } , 1250 ) ;
838901
839902 nameInput . addEventListener ( "input" , handleInput ) ;
840903 nameInput . addEventListener ( "focus" , handleInput ) ;
@@ -843,11 +906,11 @@ class ClockSettingTab extends PluginSettingTab {
843906 const offsetInput = offsetCell . createEl ( "input" , {
844907 type : "text" ,
845908 value : entry . offset ,
909+ cls : "custom-input-width" ,
846910 } ) ;
847911
848- offsetInput . classList . add ( "custom-input-width" ) ;
849-
850- const handleOffsetChange = ( ) => {
912+ // Debounce function for offset input
913+ const handleOffsetChange = debounce ( ( ) => {
851914 const newOffset = offsetInput . value . trim ( ) ;
852915 if ( this . isValidTimeZoneOffset ( newOffset ) ) {
853916 entry . offset = newOffset ;
@@ -857,7 +920,7 @@ class ClockSettingTab extends PluginSettingTab {
857920 } else {
858921 hint . textContent = "Invalid format" ;
859922 }
860- } ;
923+ } , 1250 ) ;
861924
862925 offsetInput . addEventListener ( "input" , handleOffsetChange ) ;
863926 offsetInput . addEventListener ( "focus" , handleOffsetChange ) ;
0 commit comments