feat(editor): 新增 doNotFixPosition 选项并修正超宽节点 left 钳制

This commit is contained in:
roymondchen 2026-12-01 16:49:27 +08:00
parent e838a2c37c
commit 666e9b0511
4 changed files with 88 additions and 4 deletions

View File

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

View File

@ -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

View File

@ -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;
}

View File

@ -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<string, unknown> = {}) => {
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);
});
});