refactor(editor): 提取 UpdateOptions 并统一 DSL 操作 options 传递

This commit is contained in:
roymondchen 2026-08-10 20:44:10 +08:00
parent 8d720f0bf1
commit 496df1f5e2
2 changed files with 42 additions and 62 deletions

View File

@ -56,6 +56,7 @@ import type {
StepValue,
StoreState,
StoreStateKey,
UpdateOptions,
} from '@editor/type';
import { canUsePluginMethods, LayerOffset, Layout } from '@editor/type';
import {
@ -413,7 +414,7 @@ class Editor extends BaseService {
this.set('highlightNode', null);
}
public async doAdd(node: MNode, parent: MContainer): Promise<MNode> {
public async doAdd(node: MNode, parent: MContainer, _options: DslOpOptions = {}): Promise<MNode> {
const root = this.get('root');
if (!root) throw new Error('root为空');
@ -465,24 +466,23 @@ class Editor extends BaseService {
*
* @param addConfig
* @param parent
* @param options
* @param options.doNotSelect false
* @param options.doNotSwitchPage false / true
* @param options.doNotPushHistory false
* @param options {@link DslOpOptions}
* @returns
*/
public async add(
addNode: AddMNode | MNode[],
parent?: MContainer | null,
{
options: DslOpOptions = {},
): Promise<MNode | MNode[]> {
this.captureSelectionBeforeOp();
const {
doNotSelect = false,
doNotSwitchPage = false,
doNotPushHistory = false,
historyDescription,
historySource,
}: DslOpOptions = {},
): Promise<MNode | MNode[]> {
this.captureSelectionBeforeOp();
} = options;
const stage = this.get('stage');
@ -505,7 +505,7 @@ class Editor extends BaseService {
const root = this.get('root');
const parentNode = isPageOrFragment(node) && root ? root : (parent ?? getAddParent(node));
if (!parentNode) throw new Error('未找到父元素');
newNodes.push(await this.doAdd(node, parentNode));
newNodes.push(await this.doAdd(node, parentNode, options));
}
if (newNodes.length > 1) {
@ -665,16 +665,8 @@ class Editor extends BaseService {
* @param options.doNotSwitchPage false / true
* @param options.doNotPushHistory false
*/
public async remove(
nodeOrNodeList: MNode | MNode[],
{
doNotSelect = false,
doNotSwitchPage = false,
doNotPushHistory = false,
historyDescription,
historySource,
}: DslOpOptions = {},
): Promise<void> {
public async remove(nodeOrNodeList: MNode | MNode[], options: DslOpOptions = {}): Promise<void> {
const { doNotPushHistory = false, historyDescription, historySource } = options;
this.captureSelectionBeforeOp();
const nodes = Array.isArray(nodeOrNodeList) ? nodeOrNodeList : [nodeOrNodeList];
@ -701,7 +693,7 @@ class Editor extends BaseService {
}
}
await Promise.all(nodes.map((node) => this.doRemove(node, { doNotSelect, doNotSwitchPage })));
await Promise.all(nodes.map((node) => this.doRemove(node, options)));
// 删除节点时同步清理其(含子树)的校验错误记录;置于 pushOpHistory 之前,使历史快照与本次删除对齐。
this.removeInvalidNodesBySubtree(nodes);
@ -731,12 +723,9 @@ class Editor extends BaseService {
public async doUpdate(
config: MNode,
{
changeRecords = [],
historySource,
replace = false,
}: { changeRecords?: ChangeRecord[]; historySource?: HistoryOpSource; replace?: boolean } = {},
data: UpdateOptions = {},
): Promise<{ newNode: MNode; oldNode: MNode; changeRecords?: ChangeRecord[] }> {
const { changeRecords = [], historySource, replace = false } = data;
const root = this.get('root');
if (!root) throw new Error('root为空');
@ -815,34 +804,10 @@ class Editor extends BaseService {
*
* update后会触发依赖收集stage.update方法
* @param config id信息
* @param data
* @param data.changeRecords form 使 changeRecordList
* @param data.changeRecordList form config changeRecords
* @param data.doNotPushHistory false
* @param data.historyDescription undo/redo
* @param data.replace true mergeWith / toggleFixedPosition / setChildrenLayout false
* @param data {@link UpdateOptions}
* @returns
*/
public async update(
config: MNode | MNode[],
data: {
changeRecords?: ChangeRecord[];
changeRecordList?: ChangeRecord[][];
doNotPushHistory?: boolean;
historyDescription?: string;
historySource?: HistoryOpSource;
/**
* true
* DSL false merge
*/
replace?: boolean;
/**
*
* 使 undo/redo
*/
invalidInfo?: { id: Id; source: NodeInvalidSource; error?: string };
} = {},
): Promise<MNode | MNode[]> {
public async update(config: MNode | MNode[], data: UpdateOptions = {}): Promise<MNode | MNode[]> {
this.captureSelectionBeforeOp();
const {
@ -851,7 +816,6 @@ class Editor extends BaseService {
changeRecords,
historyDescription,
historySource,
replace = false,
invalidInfo,
} = data;
@ -862,7 +826,7 @@ class Editor extends BaseService {
const updateData = await Promise.all(
nodes.map((node, index) => {
const recordsForNode = changeRecordList ? (changeRecordList[index] ?? []) : (changeRecords ?? []);
return this.doUpdate(node, { changeRecords: recordsForNode, historySource, replace });
return this.doUpdate(node, { ...data, changeRecords: recordsForNode });
}),
);
@ -1371,14 +1335,7 @@ class Editor extends BaseService {
/** 等价于 {@link update},并额外返回本次写入历史记录的 uuid 列表(未入栈时 historyIds 为 `[]`)。 */
public async updateAndGetHistoryId(
config: MNode | MNode[],
data: {
changeRecords?: ChangeRecord[];
changeRecordList?: ChangeRecord[][];
doNotPushHistory?: boolean;
historyDescription?: string;
historySource?: HistoryOpSource;
replace?: boolean;
} = {},
data: UpdateOptions = {},
): Promise<DslOpWithHistoryIdsResult<MNode | MNode[]>> {
this.lastPushedHistoryId = null;
const result = await this.update(config, data);

View File

@ -1464,6 +1464,29 @@ export interface DslOpOptions extends HistoryOpOptions {
}
// #endregion DslOpOptions
// #region UpdateOptions
/**
* {@link EditorService.update} / {@link EditorService.doUpdate}
* - changeRecords: 单节点 form 使 changeRecordList
* - changeRecordList: 多节点 form config changeRecords
* - replace: true mergeWith / toggleFixedPosition / setChildrenLayout
* - invalidInfo: 属性面板提交时携带的校验错误信息
*/
export interface UpdateOptions extends HistoryOpOptionsWithChangeRecords {
changeRecordList?: ChangeRecord[][];
/**
* true
* DSL false merge
*/
replace?: boolean;
/**
*
* 使 undo/redo
*/
invalidInfo?: { id: Id; source: NodeInvalidSource; error?: string };
}
// #endregion UpdateOptions
/** 差异对话框的入参 */
export interface DiffDialogPayload {
/** 表单类别 */