From 2e84835e133eec2d8b8faf899a100a33bcd605d1 Mon Sep 17 00:00:00 2001 From: lnuxe Date: Wed, 11 Nov 2026 17:20:22 +0800 Subject: [PATCH 1/2] fix(runtime): allow adding a page when the DSL has no pages The add handler required selectedId to be set for every kind of node. A DSL with an empty items array leaves selectedId undefined, because updateRoot only fills it from items[0], so clicking add-page in that state threw a bare Error and no page was created. Only require selectedId for regular nodes. Pages and page fragments are attached to the root and need no anchor node. fix #653 --- .../src/hooks/use-editor-dsl.ts | 5 +- .../tests/use-editor-dsl.test.ts | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/runtime/vue-runtime-help/src/hooks/use-editor-dsl.ts b/runtime/vue-runtime-help/src/hooks/use-editor-dsl.ts index e513e70f..deacde15 100644 --- a/runtime/vue-runtime-help/src/hooks/use-editor-dsl.ts +++ b/runtime/vue-runtime-help/src/hooks/use-editor-dsl.ts @@ -96,7 +96,10 @@ export const useEditorDsl = (app = inject('app'), runtimeApi: Runtime } throw new Error('error'); } - if (!selectedId.value) throw new Error('error'); + // 新增页面/页面片时 parentId 就是 root,不需要选中锚点。 + // 空 DSL(items: [])下 selectedId 一直是 undefined,旧逻辑会直接抛错, + // 导致「无页面时新增页面」失败(issue #653)。 + if (!isPageNode(config) && !selectedId.value) throw new Error('error'); if (!parentId) throw new Error('error'); const parent = getNodePath(parentId, [root.value]).pop(); diff --git a/runtime/vue-runtime-help/tests/use-editor-dsl.test.ts b/runtime/vue-runtime-help/tests/use-editor-dsl.test.ts index dd1bfda1..8e9ecfd7 100644 --- a/runtime/vue-runtime-help/tests/use-editor-dsl.test.ts +++ b/runtime/vue-runtime-help/tests/use-editor-dsl.test.ts @@ -116,6 +116,55 @@ describe('useEditorDsl add', () => { expect(dsl.items[0].items?.map((item) => item.id)).toEqual(['btn', 'n1']); }); + test('空 DSL(没有任何页面)时新增页面不应抛错(issue #653)', () => { + const app = new Core({}); + let runtime: Runtime = {}; + + (window as any).magic = { + onRuntimeReady: (rt: Runtime) => { + runtime = rt; + }, + }; + + useEditorDsl(app); + + // 与 issue 中的复现数据一致:根节点存在但 items 为空 + const dsl = { type: NodeType.ROOT, id: 'app', items: [] } as unknown as MApp; + runtime.updateRootConfig?.(dsl); + + // 没有任何页面时 selectedId 一直是 undefined; + // 新增页面只需挂到 root 下,不应该因为缺少选中节点而失败。 + expect(() => + runtime.add?.({ + config: { id: 'p9', type: NodeType.PAGE, items: [] } as any, + parentId: 'app', + root: dsl, + index: 0, + }), + ).not.toThrow(); + + expect(dsl.items.map((item) => item.id)).toEqual(['p9']); + }); + + test('空 DSL 时新增普通节点仍然要求选中节点', () => { + const app = new Core({}); + let runtime: Runtime = {}; + + (window as any).magic = { + onRuntimeReady: (rt: Runtime) => { + runtime = rt; + }, + }; + + useEditorDsl(app); + + const dsl = { type: NodeType.ROOT, id: 'app', items: [] } as unknown as MApp; + runtime.updateRootConfig?.(dsl); + + // 普通组件必须挂在某个父容器里,没有选中节点时依旧要报错 + expect(() => runtime.add?.({ config: { id: 'n1', type: 'text' } as any, parentId: 'app', root: dsl })).toThrow(); + }); + test('连续按递增 index 插入时保持顺序', () => { const { runtime, dsl } = setup(); runtime.select?.('btn'); From 65654574a1ab470bab475d4f4d874bb97c85060c Mon Sep 17 00:00:00 2001 From: lnuxe Date: Wed, 11 Nov 2026 17:47:56 +0800 Subject: [PATCH 2/2] fix(stage): follow content resize when refreshing the moveable box StageCore.update refreshed the selection box once, 0ms after the runtime update. Content size is not always known by then: an image starts loading after its src is set and only expands its container once the load completes, so updateMoveable measured the old size and the box stayed stale. Users worked around it with their own setTimeout of 100ms. Observe the selected element after the first refresh and refresh again when its size actually changes. Only one observer is kept per selection and it disconnects itself after the first callback, so a component that resizes repeatedly does not accumulate observers or fire endlessly. fix #666 --- packages/stage/src/StageCore.ts | 37 +++++++++++++++++- packages/stage/tests/unit/StageCore.spec.ts | 42 +++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/packages/stage/src/StageCore.ts b/packages/stage/src/StageCore.ts index 16456fd1..d5531ac8 100644 --- a/packages/stage/src/StageCore.ts +++ b/packages/stage/src/StageCore.ts @@ -55,6 +55,8 @@ export default class StageCore extends EventEmitter { public flashHighlight: StageFlashHighlight | null = null; private pageResizeObserver: ResizeObserver | null = null; + /** 监听选中元素内容尺寸变化,用于异步内容的选框刷新 */ + private moveableResizeObserver: ResizeObserver | null = null; private autoScrollIntoView: boolean | undefined; private customizedRender?: CustomizeRender; /** 非点击画布选中组件时,是否对选中区域做高亮闪烁提示,默认开启 */ @@ -203,6 +205,10 @@ export default class StageCore extends EventEmitter { // 组件有更新,需要set this.actionManager.setSelectedEl(el); this.actionManager.updateMoveable(el); + // 内容尺寸可能不是本轮同步渲染就确定的(例如图片 setAttribute 后才开始加载, + // 加载完成才会撑开容器)。此时上面的 updateMoveable 量到的仍是旧尺寸, + // 选框会一直停留在旧大小。这里再监听一次该元素的尺寸变化,变化后重新对齐。 + this.updateMoveableOnResize(el); } }); } @@ -322,13 +328,14 @@ export default class StageCore extends EventEmitter { * 销毁实例 */ public destroy(): void { - const { mask, renderer, actionManager, flashHighlight, pageResizeObserver } = this; + const { mask, renderer, actionManager, flashHighlight, pageResizeObserver, moveableResizeObserver } = this; renderer?.destroy(); mask?.destroy(); actionManager?.destroy(); flashHighlight?.destroy(); pageResizeObserver?.disconnect(); + moveableResizeObserver?.disconnect(); this.removeAllListeners(); @@ -354,6 +361,34 @@ export default class StageCore extends EventEmitter { /** * 监听页面大小变化 */ + /** + * 选中元素的内容尺寸变化后重新对齐选框。 + * + * 组件更新后内容尺寸不一定在本轮同步渲染就确定:图片要等加载完成才会把容器撑开, + * 视频、字体同理。只调用一次 updateMoveable 会量到旧尺寸,选框停在旧大小上。 + * 这里对同一个元素只保留一个 observer,尺寸真正变化时再刷新一次。 + */ + private updateMoveableOnResize(el: HTMLElement): void { + if (typeof ResizeObserver === 'undefined') { + return; + } + + this.moveableResizeObserver?.disconnect(); + this.moveableResizeObserver = new ResizeObserver(() => { + this.moveableResizeObserver?.disconnect(); + this.moveableResizeObserver = null; + + // 元素可能在异步期间已被替换或取消选中 + if (!this.actionManager?.isSelectedEl(el)) { + return; + } + + this.mask?.setLayout(el); + this.actionManager.updateMoveable(el); + }); + this.moveableResizeObserver.observe(el); + } + private observePageResize(page: HTMLElement): void { if (this.pageResizeObserver) { this.pageResizeObserver.disconnect(); diff --git a/packages/stage/tests/unit/StageCore.spec.ts b/packages/stage/tests/unit/StageCore.spec.ts index c71b4787..db927582 100644 --- a/packages/stage/tests/unit/StageCore.spec.ts +++ b/packages/stage/tests/unit/StageCore.spec.ts @@ -351,4 +351,46 @@ describe('StageCore', () => { vi.useRealTimers(); stage.destroy(); }); + test('内容尺寸异步变化后重新对齐选框(issue #666)', async () => { + vi.useFakeTimers(); + + const observers: { cb: ResizeObserverCallback; el: Element }[] = []; + const originalResizeObserver = globalThis.ResizeObserver; + globalThis.ResizeObserver = class { + private cb: ResizeObserverCallback; + constructor(cb: ResizeObserverCallback) { + this.cb = cb; + } + observe(el: Element) { + observers.push({ cb: this.cb, el }); + } + unobserve() {} + disconnect() {} + } as any; + + const { host, page, stage } = createStage(); + await stage.mount(host); + mountRuntime(stage); + + const node = globalThis.document.createElement('div'); + setIdToEl()(node, 'img-node'); + node.style.cssText = 'position:absolute;width:10px;height:10px;'; + page.appendChild(node); + await stage.select('img-node'); + + const spy = vi.spyOn(stage.actionManager!, 'updateMoveable'); + await stage.update({ config: { id: 'img-node' } } as any); + vi.advanceTimersByTime(10); + + const before = spy.mock.calls.length; + // 模拟图片加载完成后容器被撑开 + observers[observers.length - 1].cb([] as any, {} as any); + + // 内容尺寸变化后必须再刷新一次选框,否则选框停留在旧尺寸 + expect(spy.mock.calls.length).toBeGreaterThan(before); + + globalThis.ResizeObserver = originalResizeObserver; + vi.useRealTimers(); + stage.destroy(); + }); });