Skip to content

Commit f58ca11

Browse files
committed
fix content-only update from SDK
1 parent 69cf99d commit f58ca11

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

src/livecodes/core.ts

Lines changed: 11 additions & 18 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,16 +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
};
15261525

15271526
const hasEditorConfig = Object.keys(editorConfig).some((k) => k in newConfig);
1528-
if (hasEditorConfig && newConfig.editor && newConfig.editor !== currentEditorConfig.editor) {
1529-
shouldReloadEditors = true;
1530-
}
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;
1532+
}
1533+
return false;
1534+
})();
15311535
if ('configureTailwindcss' in editors.markup) {
15321536
if (newConfig.processors?.includes('tailwindcss')) {
15331537
editors.markup.configureTailwindcss?.(true);
@@ -5536,14 +5540,6 @@ const createApi = (): API => {
55365540
const shouldRun =
55375541
newConfig.mode != null && newConfig.mode !== 'editor' && newConfig.mode !== 'codeblock';
55385542
const shouldReloadCompiler = shouldRun && compiler.isFake;
5539-
const shouldReloadCodeEditors = (() => {
5540-
if (newConfig.editor != null && !(newConfig.editor in editors.markup)) return true;
5541-
if (newConfig.mode != null) {
5542-
if (newConfig.mode !== 'result' && editors.markup.isFake) return true;
5543-
if (newConfig.mode !== 'codeblock' && editors.markup.codejar) return true;
5544-
}
5545-
return false;
5546-
})();
55475543
const isContentOnlyChange = compareObjects(
55485544
newConfig,
55495545
currentConfig as Record<string, any>,
@@ -5568,10 +5564,7 @@ const createApi = (): API => {
55685564
if (shouldReloadCompiler) {
55695565
await reloadCompiler(newAppConfig);
55705566
}
5571-
if (shouldReloadCodeEditors) {
5572-
await createEditors(newAppConfig);
5573-
}
5574-
await applyConfig(newConfig, /* reload = */ true);
5567+
await applyConfig(newConfig, /* reload = */ true, currentConfig);
55755568
const content = getContentConfig(newConfig as Config);
55765569
const hasContent = Object.values(content).some((value) => value != null);
55775570
if (hasContent) {

src/livecodes/utils/utils.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,24 @@ export const compareObjects = /* @__PURE__ */ (
633633
} else if (!(key in dstObj)) {
634634
diff.push(key);
635635
} 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);
636+
if (Array.isArray(srcObj[key])) {
637+
if (!Array.isArray(dstObj[key])) {
638+
diff.push(key);
639+
} else if (srcObj[key].length !== dstObj[key].length) {
640+
diff.push(key);
641+
} else {
642+
for (let i = 0; i < srcObj[key].length; i++) {
643+
if (srcObj[key][i] !== dstObj[key][i]) {
644+
diff.push(`${key}[${i}]`);
645+
}
646+
}
647+
}
648+
} else {
649+
const objDiff = compareObjects(srcObj[key] as any, dstObj[key] as any).map(
650+
(k) => `${key}.${k}`,
651+
);
652+
diff.push(...objDiff);
653+
}
640654
} else if (srcObj[key] !== dstObj[key]) {
641655
diff.push(key);
642656
}

0 commit comments

Comments
 (0)