Skip to content

Commit 0f6709a

Browse files
committed
fix(mdjs-preview): initial setting should come from the element
1 parent ed86ff2 commit 0f6709a

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mdjs/mdjs-preview': patch
3+
---
4+
5+
Make sure initial settings are taken from the element if nothing is yet stored

packages/mdjs-preview/src/MdJsPreview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class MdJsPreview extends LitElement {
7272
this.languages = [
7373
{ key: 'en', name: 'English' },
7474
{ key: 'en-US', name: 'English (United States)' },
75-
{ key: 'en-US', name: 'English (United Kingdom)' },
75+
{ key: 'en-GB', name: 'English (United Kingdom)' },
7676
{ key: 'de', name: 'German' },
7777
{ key: 'es', name: 'Spanish' },
7878
{ key: 'fi', name: 'Finnish' },

packages/mdjs-preview/src/mdjsViewerSharedStates.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function storeSettings() {
4040
}
4141
}
4242

43+
let hasGlobalStateBeenSetBefore = false;
44+
4345
function 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
*/
6871
export 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

Comments
 (0)