From 666e9b051135cb1367eac7e2649474f32ddbed08 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Tue, 1 Dec 2026 16:49:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(editor):=20=E6=96=B0=E5=A2=9E=20doNotFixPo?= =?UTF-8?q?sition=20=E9=80=89=E9=A1=B9=E5=B9=B6=E4=BF=AE=E6=AD=A3=E8=B6=85?= =?UTF-8?q?=E5=AE=BD=E8=8A=82=E7=82=B9=20left=20=E9=92=B3=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/sdk/services/Editor.ts | 10 ++- packages/editor/src/type.ts | 2 + packages/editor/src/utils/editor.ts | 4 + .../editor/tests/unit/utils/editor.spec.ts | 76 +++++++++++++++++++ 4 files changed, 88 insertions(+), 4 deletions(-) diff --git a/packages/editor/src/sdk/services/Editor.ts b/packages/editor/src/sdk/services/Editor.ts index 1155c052..b3eb47c5 100644 --- a/packages/editor/src/sdk/services/Editor.ts +++ b/packages/editor/src/sdk/services/Editor.ts @@ -487,11 +487,13 @@ class Editor extends BaseService { index: insertIndex, }); - const newStyle = fixNodePosition(node, parent, stage); + if (!options.doNotFixPosition) { + const newStyle = fixNodePosition(node, parent, stage); - if (newStyle && (newStyle.top !== node.style.top || newStyle.left !== node.style.left)) { - node.style = newStyle; - await stage?.update({ config: cloneDeep(node), parentId: parent.id, root: cloneDeep(root) }); + if (newStyle && (newStyle.top !== node.style.top || newStyle.left !== node.style.left)) { + node.style = newStyle; + await stage?.update({ config: cloneDeep(node), parentId: parent.id, root: cloneDeep(root) }); + } } this.addModifiedNodeId(node.id); diff --git a/packages/editor/src/type.ts b/packages/editor/src/type.ts index 65e91575..56387c4a 100644 --- a/packages/editor/src/type.ts +++ b/packages/editor/src/type.ts @@ -1498,9 +1498,11 @@ export interface DslOpOptions extends HistoryOpOptions { * 指定后不再按当前选中节点与上一次新增节点计算锚点,也不要求有选中节点;越界 / 非数字一律追加到末尾。 * 批量 add 时数字下标是本批节点的起始位置,按父容器分别累计偏移,保持传入顺序;'end' 逐个追加。 * 页面 / 页面片作用于 root.items,同样不再固定追加到末尾。人工添加不传,沿用锚点行为。 + * - doNotFixPosition: 跳过按画布尺寸修正节点位置(left / top),程序化 add 传入的设计坐标原样保留。 */ export interface AddOpOptions extends DslOpOptions { insertIndex?: number | 'end'; + doNotFixPosition?: boolean; } // #endregion AddOpOptions diff --git a/packages/editor/src/utils/editor.ts b/packages/editor/src/utils/editor.ts index 0b6827a9..8d07e74e 100644 --- a/packages/editor/src/utils/editor.ts +++ b/packages/editor/src/utils/editor.ts @@ -363,6 +363,10 @@ export const fixNodeLeft = (config: MNode, parent: MContainer, doc?: Document) = if (el && parentEl) { const calcParentOffsetWidth = calcValueByFontsize(doc, parentEl.offsetWidth); const calcElOffsetWidth = calcValueByFontsize(doc, el.offsetWidth); + // 比父容器还宽的节点右缘不可能收进父容器,钳制只会把内容推出左边界 + if (calcElOffsetWidth > calcParentOffsetWidth) { + return config.style.left; + } if (calcElOffsetWidth + left > calcParentOffsetWidth) { return calcParentOffsetWidth - calcElOffsetWidth; } diff --git a/packages/editor/tests/unit/utils/editor.spec.ts b/packages/editor/tests/unit/utils/editor.spec.ts index 491c319d..062271c9 100644 --- a/packages/editor/tests/unit/utils/editor.spec.ts +++ b/packages/editor/tests/unit/utils/editor.spec.ts @@ -26,6 +26,8 @@ import type { EditorNodeInfo } from '@editor/type'; import { LayerOffset, Layout } from '@editor/type'; import * as editor from '@editor/utils/editor'; +import { createSdk } from '../sdk/helpers'; + describe('util form', () => { test('getPageList', () => { const pageList = editor.getPageList({ @@ -1091,6 +1093,34 @@ describe('补充:fixNodeLeft / fixNodePosition / serializeConfig', () => { expect(editor.fixNodeLeft({ id: 'a2', style: { left: 30 } } as any, { id: 'p2' } as any, doc)).toBe(30); }); + test('fixNodeLeft - 子节点宽于父容器时保留原 left', () => { + const doc = document.implementation.createHTMLDocument(); + const parent = doc.createElement('div'); + parent.dataset.tmagicId = 'p-wide'; + Object.defineProperty(parent, 'offsetWidth', { value: 275 }); + const child = doc.createElement('div'); + child.dataset.tmagicId = 'a-wide'; + Object.defineProperty(child, 'offsetWidth', { value: 375 }); + parent.appendChild(child); + doc.body.appendChild(parent); + + expect(editor.fixNodeLeft({ id: 'a-wide', style: { left: -16 } } as any, { id: 'p-wide' } as any, doc)).toBe(-16); + }); + + test('fixNodeLeft - 子节点宽等于父容器且越界时仍修正', () => { + const doc = document.implementation.createHTMLDocument(); + const parent = doc.createElement('div'); + parent.dataset.tmagicId = 'p-equal'; + Object.defineProperty(parent, 'offsetWidth', { value: 100 }); + const child = doc.createElement('div'); + child.dataset.tmagicId = 'a-equal'; + Object.defineProperty(child, 'offsetWidth', { value: 100 }); + parent.appendChild(child); + doc.body.appendChild(parent); + + expect(editor.fixNodeLeft({ id: 'a-equal', style: { left: 10 } } as any, { id: 'p-equal' } as any, doc)).toBe(0); + }); + test('fixNodePosition - 非 absolute 时直接返回 style', () => { const style = { position: 'relative' } as any; expect(editor.fixNodePosition({ id: 'a', style } as any, { id: 'p', items: [] } as any, null)).toBe(style); @@ -1456,3 +1486,49 @@ describe('toggleFixedPosition', () => { expect(result.style?.position).toBe('fixed'); }); }); + +describe('doAdd 的 doNotFixPosition', () => { + const addPositionedNode = async (options: Record = {}) => { + const doc = document.implementation.createHTMLDocument(); + const parentEl = doc.createElement('div'); + parentEl.dataset.tmagicId = 'container_1'; + Object.defineProperty(parentEl, 'offsetWidth', { value: 275 }); + const childEl = doc.createElement('div'); + childEl.dataset.tmagicId = 'positioned_1'; + Object.defineProperty(childEl, 'offsetWidth', { value: 100 }); + parentEl.appendChild(childEl); + doc.body.appendChild(parentEl); + + const stage = { + renderer: { contentWindow: { document: doc } }, + add: vi.fn(async () => undefined), + update: vi.fn(async () => undefined), + } as any; + const sdk = createSdk(); + sdk.editor.set('stage', stage); + const parent = { + id: 'container_1', + type: 'container', + style: { position: 'absolute', width: 275, height: 300 }, + items: [], + } as any; + const node = { + id: 'positioned_1', + type: 'container', + items: [], + style: { position: 'absolute', left: 200, top: 10, width: 100, height: 20 }, + } as any; + + await sdk.editor.doAdd(node, parent, { insertIndex: 'end', ...options }); + + return node.style.left; + }; + + test('传入 doNotFixPosition 时保留设计坐标', async () => { + expect(await addPositionedNode({ doNotFixPosition: true })).toBe(200); + }); + + test('未传 doNotFixPosition 时仍修正越界坐标', async () => { + expect(await addPositionedNode()).toBe(175); + }); +});