fix(editor): 仅在使用 left/top 定位时修正节点位置

避免对使用 right/bottom 定位的绝对定位节点误写 left 或 top。
This commit is contained in:
roymondchen 2026-06-12 15:04:45 +08:00
parent 1298104732
commit 27fac02e99
2 changed files with 46 additions and 5 deletions

View File

@ -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;
};
// 序列化配置

View File

@ -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:');