@@ -82,7 +82,11 @@ type ComponentRelayParams = {
82
82
/**
83
83
* A callback that is executed after the component has been registered.
84
84
*/
85
- onReady ?: ( ) => void
85
+ onReady ?: ( ) => void ,
86
+ /**
87
+ * A callback that is executed after themes have been changed.
88
+ */
89
+ onThemesChange ?: ( ) => void ,
86
90
}
87
91
88
92
type ItemPayload = {
@@ -114,6 +118,7 @@ export default class ComponentRelay {
114
118
private keyDownEventListener ?: ( event : KeyboardEvent ) => void ;
115
119
private keyUpEventListener ?: ( event : KeyboardEvent ) => void ;
116
120
private clickEventListener ?: ( event : MouseEvent ) => void ;
121
+ private onThemesChangeCallback ?: ( ) => void ;
117
122
118
123
constructor ( params : ComponentRelayParams ) {
119
124
if ( ! params || ! params . targetWindow ) {
@@ -127,7 +132,7 @@ export default class ComponentRelay {
127
132
}
128
133
129
134
private processParameters ( params : ComponentRelayParams ) {
130
- const { initialPermissions, options, onReady } = params
135
+ const { initialPermissions, options, onReady, onThemesChange } = params
131
136
132
137
if ( initialPermissions && initialPermissions . length > 0 ) {
133
138
this . initialPermissions = initialPermissions
@@ -145,10 +150,14 @@ export default class ComponentRelay {
145
150
if ( isNotUndefinedOrNull ( onReady ) ) {
146
151
this . onReadyCallback = onReady
147
152
}
153
+ if ( isNotUndefinedOrNull ( onThemesChange ) ) {
154
+ this . onThemesChangeCallback = onThemesChange
155
+ }
156
+
148
157
Logger . enabled = options ?. debug ?? false
149
158
}
150
159
151
- public deinit ( ) : void {
160
+ public deinit ( ) : void {
152
161
this . onReadyCallback = undefined
153
162
this . component = {
154
163
acceptsThemes : true ,
@@ -351,21 +360,21 @@ export default class ComponentRelay {
351
360
/**
352
361
* Gets the component UUID.
353
362
*/
354
- public getSelfComponentUUID ( ) : string | undefined {
363
+ public getSelfComponentUUID ( ) : string | undefined {
355
364
return this . component . uuid
356
365
}
357
366
358
367
/**
359
368
* Checks if the component is running in a Desktop application.
360
369
*/
361
- public isRunningInDesktopApplication ( ) : boolean {
370
+ public isRunningInDesktopApplication ( ) : boolean {
362
371
return this . component . environment === environmentToString ( Environment . Desktop )
363
372
}
364
373
365
374
/**
366
375
* Checks if the component is running in a Mobile application.
367
376
*/
368
- public isRunningInMobileApplication ( ) : boolean {
377
+ public isRunningInMobileApplication ( ) : boolean {
369
378
return this . component . environment === environmentToString ( Environment . Mobile )
370
379
}
371
380
@@ -374,7 +383,7 @@ export default class ComponentRelay {
374
383
* @param key The key for the data object.
375
384
* @returns `undefined` if the value for the key does not exist. Returns the stored value otherwise.
376
385
*/
377
- public getComponentDataValueForKey ( key : string ) : any {
386
+ public getComponentDataValueForKey ( key : string ) : any {
378
387
if ( ! this . component . data ) {
379
388
return
380
389
}
@@ -386,7 +395,7 @@ export default class ComponentRelay {
386
395
* @param key The key for the data object.
387
396
* @param value The value to store under the specified key.
388
397
*/
389
- public setComponentDataValueForKey ( key : string , value : any ) : void {
398
+ public setComponentDataValueForKey ( key : string , value : any ) : void {
390
399
if ( ! this . component . data ) {
391
400
throw new Error ( 'The component has not been initialized.' )
392
401
}
@@ -403,7 +412,7 @@ export default class ComponentRelay {
403
412
/**
404
413
* Clears the component's data object.
405
414
*/
406
- public clearComponentData ( ) : void {
415
+ public clearComponentData ( ) : void {
407
416
this . component . data = { }
408
417
this . postMessage ( ComponentAction . SetComponentData , { componentData : this . component . data } )
409
418
}
@@ -506,6 +515,8 @@ export default class ComponentRelay {
506
515
link . className = 'custom-theme'
507
516
this . contentWindow . document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( link )
508
517
}
518
+
519
+ this . onThemesChangeCallback && this . onThemesChangeCallback ( )
509
520
}
510
521
511
522
private themeElementForUrl ( themeUrl : string ) {
@@ -531,14 +542,14 @@ export default class ComponentRelay {
531
542
/**
532
543
* Gets the current platform where the component is running.
533
544
*/
534
- public get platform ( ) : string | undefined {
545
+ public get platform ( ) : string | undefined {
535
546
return this . component . platform
536
547
}
537
548
538
549
/**
539
550
* Gets the current environment where the component is running.
540
551
*/
541
- public get environment ( ) : string | undefined {
552
+ public get environment ( ) : string | undefined {
542
553
return this . component . environment
543
554
}
544
555
@@ -548,7 +559,7 @@ export default class ComponentRelay {
548
559
* @param contentTypes A collection of Content Types.
549
560
* @param callback A callback to process the streamed items.
550
561
*/
551
- public streamItems ( contentTypes : ContentType [ ] , callback : ( data : any ) => void ) : void {
562
+ public streamItems ( contentTypes : ContentType [ ] , callback : ( data : any ) => void ) : void {
552
563
this . postMessage ( ComponentAction . StreamItems , { content_types : contentTypes } , ( data : any ) => {
553
564
callback ( data . items )
554
565
} )
@@ -558,7 +569,7 @@ export default class ComponentRelay {
558
569
* Streams the current Item in context.
559
570
* @param callback A callback to process the streamed item.
560
571
*/
561
- public streamContextItem ( callback : ( data : any ) => void ) : void {
572
+ public streamContextItem ( callback : ( data : any ) => void ) : void {
562
573
this . postMessage ( ComponentAction . StreamContextItem , { } , ( data ) => {
563
574
const { item } = data
564
575
/**
@@ -585,14 +596,14 @@ export default class ComponentRelay {
585
596
* Selects a `Tag` item.
586
597
* @param item The Item (`Tag` or `SmartTag`) to select.
587
598
*/
588
- public selectItem ( item : ItemPayload ) : void {
599
+ public selectItem ( item : ItemPayload ) : void {
589
600
this . postMessage ( ComponentAction . SelectItem , { item : this . jsonObjectForItem ( item ) } )
590
601
}
591
602
592
603
/**
593
604
* Clears current selected `Tag` (if any).
594
605
*/
595
- public clearSelection ( ) : void {
606
+ public clearSelection ( ) : void {
596
607
this . postMessage ( ComponentAction . ClearSelection , { content_type : ContentType . Tag } )
597
608
}
598
609
@@ -601,7 +612,7 @@ export default class ComponentRelay {
601
612
* @param item The Item's payload content.
602
613
* @param callback The callback to process the created Item.
603
614
*/
604
- public createItem ( item : ItemPayload , callback : ( data : any ) => void ) : void {
615
+ public createItem ( item : ItemPayload , callback : ( data : any ) => void ) : void {
605
616
this . postMessage ( ComponentAction . CreateItem , { item : this . jsonObjectForItem ( item ) } , ( data : any ) => {
606
617
let { item } = data
607
618
/**
@@ -621,7 +632,7 @@ export default class ComponentRelay {
621
632
* @param items The Item(s) payload collection.
622
633
* @param callback The callback to process the created Item(s).
623
634
*/
624
- public createItems ( items : ItemPayload [ ] , callback : ( data : any ) => void ) : void {
635
+ public createItems ( items : ItemPayload [ ] , callback : ( data : any ) => void ) : void {
625
636
const mapped = items . map ( ( item ) => this . jsonObjectForItem ( item ) )
626
637
this . postMessage ( ComponentAction . CreateItems , { items : mapped } , ( data : any ) => {
627
638
callback && callback ( data . items )
@@ -632,24 +643,24 @@ export default class ComponentRelay {
632
643
* Associates a `Tag` with the current Note.
633
644
* @param item The `Tag` item to associate.
634
645
*/
635
- public associateItem ( item : ItemPayload ) : void {
646
+ public associateItem ( item : ItemPayload ) : void {
636
647
this . postMessage ( ComponentAction . AssociateItem , { item : this . jsonObjectForItem ( item ) } )
637
648
}
638
649
639
650
/**
640
651
* Deassociates a `Tag` with the current Note.
641
652
* @param item The `Tag` item to deassociate.
642
653
*/
643
- public deassociateItem ( item : ItemPayload ) : void {
644
- this . postMessage ( ComponentAction . DeassociateItem , { item : this . jsonObjectForItem ( item ) } )
654
+ public deassociateItem ( item : ItemPayload ) : void {
655
+ this . postMessage ( ComponentAction . DeassociateItem , { item : this . jsonObjectForItem ( item ) } )
645
656
}
646
657
647
658
/**
648
659
* Deletes an Item from the item store.
649
660
* @param item The Item to delete.
650
661
* @param callback The callback with the result of the operation.
651
662
*/
652
- public deleteItem ( item : ItemPayload , callback : ( data : ItemMessagePayload ) => void ) : void {
663
+ public deleteItem ( item : ItemPayload , callback : ( data : ItemMessagePayload ) => void ) : void {
653
664
this . deleteItems ( [ item ] , callback )
654
665
}
655
666
@@ -658,7 +669,7 @@ export default class ComponentRelay {
658
669
* @param items The Item(s) to delete.
659
670
* @param callback The callback with the result of the operation.
660
671
*/
661
- public deleteItems ( items : ItemPayload [ ] , callback : ( data : ItemMessagePayload ) => void ) : void {
672
+ public deleteItems ( items : ItemPayload [ ] , callback : ( data : ItemMessagePayload ) => void ) : void {
662
673
const params = {
663
674
items : items . map ( ( item ) => {
664
675
return this . jsonObjectForItem ( item )
@@ -675,7 +686,7 @@ export default class ComponentRelay {
675
686
* @param data
676
687
* @param callback The callback with the result of the operation.
677
688
*/
678
- public sendCustomEvent ( action : ComponentAction , data : any , callback ?: ( data : any ) => void ) : void {
689
+ public sendCustomEvent ( action : ComponentAction , data : any , callback ?: ( data : any ) => void ) : void {
679
690
this . postMessage ( action , data , ( data : any ) => {
680
691
callback && callback ( data )
681
692
} )
@@ -687,7 +698,7 @@ export default class ComponentRelay {
687
698
* @param callback
688
699
* @param skipDebouncer
689
700
*/
690
- public saveItem ( item : ItemPayload , callback ?: ( ) => void , skipDebouncer = false ) : void {
701
+ public saveItem ( item : ItemPayload , callback ?: ( ) => void , skipDebouncer = false ) : void {
691
702
this . saveItems ( [ item ] , callback , skipDebouncer )
692
703
}
693
704
@@ -699,7 +710,7 @@ export default class ComponentRelay {
699
710
* hook into the debounce cycle so that clients don't have to implement their own debouncing.
700
711
* @param callback
701
712
*/
702
- public saveItemWithPresave ( item : ItemPayload , presave : any , callback ?: ( ) => void ) : void {
713
+ public saveItemWithPresave ( item : ItemPayload , presave : any , callback ?: ( ) => void ) : void {
703
714
this . saveItemsWithPresave ( [ item ] , presave , callback )
704
715
}
705
716
@@ -711,7 +722,7 @@ export default class ComponentRelay {
711
722
* hook into the debounce cycle so that clients don't have to implement their own debouncing.
712
723
* @param callback
713
724
*/
714
- public saveItemsWithPresave ( items : ItemPayload [ ] , presave : any , callback ?: ( ) => void ) : void {
725
+ public saveItemsWithPresave ( items : ItemPayload [ ] , presave : any , callback ?: ( ) => void ) : void {
715
726
this . saveItems ( items , callback , false , presave )
716
727
}
717
728
@@ -739,7 +750,7 @@ export default class ComponentRelay {
739
750
* This should be used when saving items via other means besides keystrokes.
740
751
* @param presave
741
752
*/
742
- public saveItems ( items : ItemPayload [ ] , callback ?: ( ) => void , skipDebouncer = false , presave ?: any ) : void {
753
+ public saveItems ( items : ItemPayload [ ] , callback ?: ( ) => void , skipDebouncer = false , presave ?: any ) : void {
743
754
/**
744
755
* We need to make sure that when we clear a pending save timeout,
745
756
* we carry over those pending items into the new save.
@@ -788,30 +799,30 @@ export default class ComponentRelay {
788
799
* @param width The new width.
789
800
* @param height The new height.
790
801
*/
791
- public setSize ( width : string | number , height : string | number ) : void {
802
+ public setSize ( width : string | number , height : string | number ) : void {
792
803
this . postMessage ( ComponentAction . SetSize , { type : 'container' , width, height } )
793
804
}
794
805
795
806
/**
796
807
* Sends the KeyDown keyboard event to the Standard Notes parent application.
797
808
* @param keyboardModifier The keyboard modifier that was pressed.
798
809
*/
799
- private keyDownEvent ( keyboardModifier : KeyboardModifier ) : void {
810
+ private keyDownEvent ( keyboardModifier : KeyboardModifier ) : void {
800
811
this . postMessage ( ComponentAction . KeyDown , { keyboardModifier } )
801
812
}
802
813
803
814
/**
804
815
* Sends the KeyUp keyboard event to the Standard Notes parent application.
805
816
* @param keyboardModifier The keyboard modifier that was released.
806
817
*/
807
- private keyUpEvent ( keyboardModifier : KeyboardModifier ) : void {
818
+ private keyUpEvent ( keyboardModifier : KeyboardModifier ) : void {
808
819
this . postMessage ( ComponentAction . KeyUp , { keyboardModifier } )
809
820
}
810
821
811
822
/**
812
823
* Sends the Click mouse event to the Standard Notes parent application.
813
824
*/
814
- private mouseClickEvent ( ) : void {
825
+ private mouseClickEvent ( ) : void {
815
826
this . postMessage ( ComponentAction . Click , { } )
816
827
}
817
828
@@ -829,7 +840,7 @@ export default class ComponentRelay {
829
840
* @param item The Item to get the appData value from.
830
841
* @param key The key to get the value from.
831
842
*/
832
- public getItemAppDataValue ( item : ItemMessagePayload | undefined , key : AppDataField | string ) : any {
843
+ public getItemAppDataValue ( item : ItemMessagePayload | undefined , key : AppDataField | string ) : any {
833
844
const defaultDomain = 'org.standardnotes.sn'
834
845
return item ?. content ?. appData ?. [ defaultDomain ] [ key ]
835
846
}
0 commit comments