diff --git a/packages/editor/src/utils/editor.ts b/packages/editor/src/utils/editor.ts index db15e58f..3e924fc0 100644 --- a/packages/editor/src/utils/editor.ts +++ b/packages/editor/src/utils/editor.ts @@ -375,11 +375,18 @@ export const fixNodePosition = (config: MNode, parent: MContainer, stage: StageC return config.style; } - return { - ...(config.style || {}), - top: getMiddleTop(config, parent, stage), - left: fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document), - }; + const style = { ...(config.style || {}) }; + const baseStyle = config.style || {}; + + if ('left' in baseStyle && !('right' in baseStyle)) { + style.left = fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document); + } + + if ('top' in baseStyle && !('bottom' in baseStyle)) { + style.top = getMiddleTop(config, parent, stage); + } + + return style; }; // 序列化配置 diff --git a/packages/editor/tests/unit/utils/editor.spec.ts b/packages/editor/tests/unit/utils/editor.spec.ts index e8748b7f..491c319d 100644 --- a/packages/editor/tests/unit/utils/editor.spec.ts +++ b/packages/editor/tests/unit/utils/editor.spec.ts @@ -1105,6 +1105,40 @@ describe('补充:fixNodeLeft / fixNodePosition / serializeConfig', () => { expect(result).toBeDefined(); }); + test('fixNodePosition - 仅设置 right 时不修正 left', () => { + const style = { position: 'absolute', right: 10 } as any; + const result = editor.fixNodePosition({ id: 'a', style } as any, { id: 'p', items: [] } as any, null); + expect(result).toEqual(style); + expect(result?.left).toBeUndefined(); + }); + + test('fixNodePosition - 仅设置 bottom 时不修正 top', () => { + const style = { position: 'absolute', bottom: 20 } as any; + const result = editor.fixNodePosition({ id: 'a', style } as any, { id: 'p', items: [] } as any, null); + expect(result).toEqual(style); + expect(result?.top).toBeUndefined(); + }); + + test('fixNodePosition - 设置 left 且无 right 时修正 left', () => { + const doc = document.implementation.createHTMLDocument(); + const parent = doc.createElement('div'); + parent.dataset.tmagicId = 'p3'; + Object.defineProperty(parent, 'offsetWidth', { value: 100 }); + const child = doc.createElement('div'); + child.dataset.tmagicId = 'a3'; + Object.defineProperty(child, 'offsetWidth', { value: 80 }); + parent.appendChild(child); + doc.body.appendChild(parent); + + const stage = { renderer: { contentWindow: { document: doc } } } as any; + const result = editor.fixNodePosition( + { id: 'a3', style: { position: 'absolute', left: 50 } } as any, + { id: 'p3', items: [] } as any, + stage, + ); + expect(result?.left).toBe(20); + }); + test('serializeConfig - 输出去掉了 key 引号', () => { const out = editor.serializeConfig({ a: 1 }); expect(out).toContain('a:');