Skip to content

Commit ba8ceff

Browse files
authored
Merge pull request #882 from live-codes/fix-changing-editor-config
fix changing editor config from SDK
2 parents 98837f6 + 5b0ba07 commit ba8ceff

2 files changed

Lines changed: 43 additions & 36 deletions

File tree

src/livecodes/core.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,8 @@ const loadConfig = async (
14321432
changingContent = false;
14331433
};
14341434

1435-
const applyConfig = async (newConfig: Partial<Config>, reload = false) => {
1436-
const currentConfig = getConfig();
1435+
const applyConfig = async (newConfig: Partial<Config>, reload = false, oldConfig?: Config) => {
1436+
const currentConfig = oldConfig || getConfig();
14371437
const combinedConfig: Config = { ...currentConfig, ...newConfig };
14381438
if (reload) {
14391439
await updateEditors(editors, getConfig());
@@ -1518,24 +1518,20 @@ const applyConfig = async (newConfig: Partial<Config>, reload = false) => {
15181518
});
15191519
}
15201520

1521-
let shouldReloadEditors = false;
15221521
const editorConfig = {
15231522
...getEditorConfig(newConfig as Config),
15241523
...getFormatterConfig(newConfig as Config),
15251524
};
1526-
const hasEditorConfig = Object.values(editorConfig).some((value) => value != null);
1527-
if (hasEditorConfig) {
1528-
for (const key in editorConfig) {
1529-
if (
1530-
key in newConfig &&
1531-
(editorConfig as any)[key] !== (currentEditorConfig as any)[key] &&
1532-
!key.toLowerCase().includes('theme')
1533-
) {
1534-
shouldReloadEditors = true;
1535-
break;
1536-
}
1525+
1526+
const hasEditorConfig = Object.keys(editorConfig).some((k) => k in newConfig);
1527+
let shouldReloadEditors = (() => {
1528+
if (newConfig.editor != null && !(newConfig.editor in editors.markup)) return true;
1529+
if (newConfig.mode != null) {
1530+
if (newConfig.mode !== 'result' && editors.markup.isFake) return true;
1531+
if (newConfig.mode !== 'codeblock' && editors.markup.codejar) return true;
15371532
}
1538-
}
1533+
return false;
1534+
})();
15391535
if ('configureTailwindcss' in editors.markup) {
15401536
if (newConfig.processors?.includes('tailwindcss')) {
15411537
editors.markup.configureTailwindcss?.(true);
@@ -1550,6 +1546,12 @@ const applyConfig = async (newConfig: Partial<Config>, reload = false) => {
15501546
}
15511547
if (shouldReloadEditors) {
15521548
await reloadEditors(combinedConfig);
1549+
} else if (hasEditorConfig) {
1550+
currentEditorConfig = {
1551+
...getEditorConfig(combinedConfig),
1552+
...getFormatterConfig(combinedConfig),
1553+
};
1554+
getAllEditors().forEach((editor) => editor.changeSettings(currentEditorConfig));
15531555
}
15541556

15551557
parent.dispatchEvent(new Event(customEvents.ready));
@@ -5538,14 +5540,6 @@ const createApi = (): API => {
55385540
const shouldRun =
55395541
newConfig.mode != null && newConfig.mode !== 'editor' && newConfig.mode !== 'codeblock';
55405542
const shouldReloadCompiler = shouldRun && compiler.isFake;
5541-
const shouldReloadCodeEditors = (() => {
5542-
if (newConfig.editor != null && !(newConfig.editor in editors.markup)) return true;
5543-
if (newConfig.mode != null) {
5544-
if (newConfig.mode !== 'result' && editors.markup.isFake) return true;
5545-
if (newConfig.mode !== 'codeblock' && editors.markup.codejar) return true;
5546-
}
5547-
return false;
5548-
})();
55495543
const isContentOnlyChange = compareObjects(
55505544
newConfig,
55515545
currentConfig as Record<string, any>,
@@ -5570,10 +5564,7 @@ const createApi = (): API => {
55705564
if (shouldReloadCompiler) {
55715565
await reloadCompiler(newAppConfig);
55725566
}
5573-
if (shouldReloadCodeEditors) {
5574-
await createEditors(newAppConfig);
5575-
}
5576-
await applyConfig(newConfig, /* reload = */ true);
5567+
await applyConfig(newConfig, /* reload = */ true, currentConfig);
55775568
const content = getContentConfig(newConfig as Config);
55785569
const hasContent = Object.values(content).some((value) => value != null);
55795570
if (hasContent) {

src/livecodes/utils/utils.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -623,21 +623,37 @@ export const isFocusable = /* @__PURE__ */ (item: any | null): boolean => {
623623
* whose values are different from the destination object.
624624
*/
625625
export const compareObjects = /* @__PURE__ */ (
626-
srcObj: Record<string, unknown>,
627-
dstObj: Record<string, unknown>,
626+
srcObj: Partial<Record<string, unknown>>,
627+
dstObj: Partial<Record<string, unknown>>,
628628
) => {
629629
const diff: string[] = [];
630630
for (const key of Object.keys(srcObj)) {
631-
if (typeof srcObj[key] === 'function') {
631+
const srcObjProp = srcObj[key];
632+
const dstObjProp = dstObj[key];
633+
if (typeof srcObjProp === 'function') {
632634
continue;
633635
} else if (!(key in dstObj)) {
634636
diff.push(key);
635-
} else if (srcObj[key] !== null && typeof srcObj[key] === 'object') {
636-
const objDiff = compareObjects(srcObj[key] as any, dstObj[key] as any).map(
637-
(k) => `${key}.${k}`,
638-
);
639-
diff.push(...objDiff);
640-
} else if (srcObj[key] !== dstObj[key]) {
637+
} else if (srcObjProp !== null && typeof srcObjProp === 'object') {
638+
if (!dstObjProp || typeof dstObjProp !== 'object') {
639+
diff.push(key);
640+
} else if (Array.isArray(srcObjProp)) {
641+
if (!Array.isArray(dstObjProp)) {
642+
diff.push(key);
643+
} else if (srcObjProp.length !== dstObjProp.length) {
644+
diff.push(key);
645+
} else {
646+
for (let i = 0; i < srcObjProp.length; i++) {
647+
if (srcObjProp[i] !== dstObjProp[i]) {
648+
diff.push(`${key}[${i}]`);
649+
}
650+
}
651+
}
652+
} else {
653+
const objDiff = compareObjects(srcObjProp, dstObjProp).map((k) => `${key}.${k}`);
654+
diff.push(...objDiff);
655+
}
656+
} else if (srcObjProp !== dstObjProp) {
641657
diff.push(key);
642658
}
643659
}

0 commit comments

Comments
 (0)