Skip to content

Commit

Permalink
fix(editor): model-value变化后需要重新生成依赖后再通知runtime更新
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Nov 6, 2023
1 parent 7a228b1 commit f0f94d4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
42 changes: 21 additions & 21 deletions packages/editor/src/initService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,6 @@ export const initServiceEvents = (
};

const rootChangeHandler = async (value: MApp | null, preValue?: MApp | null) => {
const nodeId = editorService.get('node')?.id || props.defaultSelected;
let node;
if (nodeId) {
node = editorService.getNodeById(nodeId);
}

if (node && node !== value) {
await editorService.select(node.id);
} else if (value?.items?.length) {
await editorService.select(value.items[0]);
} else if (value?.id) {
editorService.set('nodes', [value]);
editorService.set('parent', null);
editorService.set('page', null);
}

if (toRaw(value) !== toRaw(preValue)) {
emit('update:modelValue', value);
}

if (!value) return;

value.codeBlocks = value.codeBlocks || {};
Expand All @@ -314,12 +294,32 @@ export const initServiceEvents = (
initDataSourceDepTarget(ds);
});

if (value && Array.isArray(value.items)) {
if (Array.isArray(value.items)) {
depService.collect(value.items, true);
} else {
depService.clear();
delete value.dataSourceDeps;
}

const nodeId = editorService.get('node')?.id || props.defaultSelected;
let node;
if (nodeId) {
node = editorService.getNodeById(nodeId);
}

if (node && node !== value) {
await editorService.select(node.id);
} else if (value.items?.length) {
await editorService.select(value.items[0]);
} else if (value.id) {
editorService.set('nodes', [value]);
editorService.set('parent', null);
editorService.set('page', null);
}

if (toRaw(value) !== toRaw(preValue)) {
emit('update:modelValue', value);
}
};

// 新增节点,收集依赖
Expand Down
17 changes: 10 additions & 7 deletions packages/editor/src/layouts/workspace/viewer/Stage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { computed, inject, markRaw, nextTick, onMounted, onUnmounted, ref, toRaw, watch, watchEffect } from 'vue';
import { cloneDeep } from 'lodash-es';
import type { MContainer } from '@tmagic/schema';
import type { MApp, MContainer } from '@tmagic/schema';
import StageCore, { calcValueByFontsize, getOffset, Runtime } from '@tmagic/stage';
import ScrollViewer from '@editor/components/ScrollViewer.vue';
Expand Down Expand Up @@ -105,12 +105,6 @@ watch(zoom, (zoom) => {
stage.setZoom(zoom);
});
watch(root, (root) => {
if (runtime && root) {
runtime.updateRootConfig?.(cloneDeep(toRaw(root)));
}
});
watch(page, (page) => {
if (runtime && page) {
runtime.updatePageId?.(page.id);
Expand All @@ -120,6 +114,14 @@ watch(page, (page) => {
}
});
const rootChangeHandler = (root: MApp) => {
if (runtime && root) {
runtime.updateRootConfig?.(cloneDeep(toRaw(root)));
}
};
services?.editorService.on('root-change', rootChangeHandler);
const resizeObserver = new ResizeObserver((entries) => {
for (const { contentRect } of entries) {
services?.uiService.set('stageContainerRect', {
Expand All @@ -141,6 +143,7 @@ onUnmounted(() => {
resizeObserver.disconnect();
services?.editorService.set('stage', null);
services?.keybindingService.unregisteEl('stage');
services?.editorService.off('root-change', rootChangeHandler);
});
const parseDSL = getConfig('parseDSL');
Expand Down

0 comments on commit f0f94d4

Please sign in to comment.