@@ -40,6 +40,8 @@ function storeSettings() {
4040 }
4141}
4242
43+ let hasGlobalStateBeenSetBefore = false ;
44+
4345function restoreSettings ( ) {
4446 for ( const _sharedStateKey of Object . keys ( _sharedStates ) ) {
4547 const sharedStateKey = /** @type {keyof _sharedStates } */ ( _sharedStateKey ) ;
@@ -55,6 +57,7 @@ function restoreSettings() {
5557 default :
5658 _sharedStates [ sharedStateKey ] = restoredValue ;
5759 }
60+ hasGlobalStateBeenSetBefore = true ;
5861 }
5962 }
6063}
@@ -66,27 +69,30 @@ restoreSettings();
6669 * @param {import('./MdJsPreview.js').MdJsPreview } target
6770 */
6871export function applySharedStates ( target ) {
69- for ( const _sharedStateKey of Object . keys ( _sharedStates ) ) {
70- const sharedStateKey = /** @type {keyof _sharedStates } */ ( _sharedStateKey ) ;
71- switch ( sharedStateKey ) {
72- case 'autoHeight' :
73- case 'deviceMode' :
74- case 'rememberSettings' :
75- case 'edgeDistance' :
76- target [ sharedStateKey ] = _sharedStates [ sharedStateKey ] ;
77- break ;
78- default :
79- target [ sharedStateKey ] = _sharedStates [ sharedStateKey ] ;
72+ if ( hasGlobalStateBeenSetBefore ) {
73+ for ( const _sharedStateKey of Object . keys ( _sharedStates ) ) {
74+ const sharedStateKey = /** @type {keyof _sharedStates } */ ( _sharedStateKey ) ;
75+ switch ( sharedStateKey ) {
76+ case 'autoHeight' :
77+ case 'deviceMode' :
78+ case 'rememberSettings' :
79+ case 'edgeDistance' :
80+ target [ sharedStateKey ] = _sharedStates [ sharedStateKey ] ;
81+ break ;
82+ default :
83+ target [ sharedStateKey ] = _sharedStates [ sharedStateKey ] ;
84+ }
8085 }
86+ } else {
87+ _saveToSharedStates ( target ) ;
8188 }
8289}
8390
8491/**
8592 *
8693 * @param {import('./MdJsPreview.js').MdJsPreview } target
87- * @param {Function } subscribedFn
8894 */
89- export function saveToSharedStates ( target , subscribedFn ) {
95+ function _saveToSharedStates ( target ) {
9096 let updated = false ;
9197 for ( const _sharedStateKey of Object . keys ( _sharedStates ) ) {
9298 const sharedStateKey = /** @type {keyof _sharedStates } */ ( _sharedStateKey ) ;
@@ -102,8 +108,19 @@ export function saveToSharedStates(target, subscribedFn) {
102108 _sharedStates [ sharedStateKey ] = target [ sharedStateKey ] ;
103109 }
104110 updated = true ;
111+ hasGlobalStateBeenSetBefore = true ;
105112 }
106113 }
114+ return updated ;
115+ }
116+
117+ /**
118+ *
119+ * @param {import('./MdJsPreview.js').MdJsPreview } target
120+ * @param {Function } subscribedFn
121+ */
122+ export function saveToSharedStates ( target , subscribedFn ) {
123+ const updated = _saveToSharedStates ( target ) ;
107124 if ( updated ) {
108125 storeSettings ( ) ;
109126 for ( const subscribeFn of subscribeFns ) {
0 commit comments