Skip to content

Commit 72baa97

Browse files
committed
feat(StageShield): 添加工具切换的撤销重做支持
在 `setCreator` 方法中新增 `isSupportUndoRedo` 参数,以支持工具切换时的撤销重做功能。同时,移除了冗余的 `_handleSelectMoveable` 和 `_handleSelectHand` 方法,简化了代码结构。
1 parent 95c90ea commit 72baa97

File tree

4 files changed

+29
-48
lines changed

4 files changed

+29
-48
lines changed

src/components/stage/container.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const stageShieldRef = ref<InstanceType<typeof StageShieldVue> & StageShieldInst
1111
const handleCreatorSelect = creator => {
1212
const { type } = creator;
1313
if (type !== CreatorTypes.image) {
14-
stageStore.setCreator(creator);
14+
stageStore.setCreator(creator, true);
1515
}
1616
};
1717

src/modules/stage/StageShield.ts

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,8 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
11061106
this.event.on("selectCopy", this._handleSelectCopy.bind(this));
11071107
this.event.on("pasteElements", this._handlePasteElements.bind(this));
11081108
this.event.on("cancel", this._handleCancel.bind(this));
1109-
this.event.on("selectMoveable", this._handleSelectMoveable.bind(this));
1110-
this.event.on("selectHand", this._handleSelectHand.bind(this));
1109+
this.event.on("selectMoveable", () => this.setCreator(MoveableCreator));
1110+
this.event.on("selectHand", () => this.setCreator(HandCreator));
11111111
this.event.on("groupAdd", this._handleGroupAdd.bind(this));
11121112
this.event.on("groupRemove", this._handleGroupCancel.bind(this));
11131113
this.event.on("undo", this.execUndo.bind(this));
@@ -1444,9 +1444,9 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
14441444
// 尝试激活控制器
14451445
const controller = this._tryActiveController();
14461446
if (controller) {
1447-
this._refreshElementsOriginals([...this.store.selectedElements, this.selection.rangeElement], {
1448-
deepSubs: true,
1449-
});
1447+
const elements = [...this.store.selectedElements, this.selection.rangeElement];
1448+
this.store.refreshElementsOriginalAngles(elements, { deepSubs: true });
1449+
this.store.refreshElementsOriginals(elements, { deepSubs: true });
14501450
}
14511451
if (controller instanceof ElementRotation) {
14521452
this._preProcessRotationStates(controller);
@@ -1493,14 +1493,16 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
14931493
}
14941494

14951495
/**
1496-
* 刷新组件原始数据
1497-
*
1498-
* @param elements
1499-
* @param options
1496+
* 添加一个工具切换的命令
15001497
*/
1501-
private _refreshElementsOriginals(elements: IElement[], options?: RefreshSubOptions): void {
1502-
this.store.refreshElementsOriginalAngles(elements, options);
1503-
this.store.refreshElementsOriginals(elements, options);
1498+
private async _addCreatorChangedCommand(): Promise<void> {
1499+
// 组件创建生效,生成一个切换绘图工具切换的命令
1500+
const creatorCommand = await CommandHelper.createCommandByActionParams([], ElementsCommandTypes.ElementsCreatorChanged, this.store);
1501+
Object.assign(creatorCommand.payload, {
1502+
prevCreatorType: this.prevCreatorType,
1503+
creatorType: this.currentCreator.type,
1504+
});
1505+
this.undoRedo.add(creatorCommand);
15041506
}
15051507

15061508
/**
@@ -1522,13 +1524,6 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
15221524
}
15231525
element = this.store.creatingArbitraryElement(this.cursor.worldValue, true);
15241526
if ((element as IElementArbitrary).tailCoordIndex === 0) {
1525-
// 组件创建生效,生成一个切换绘图工具切换的命令
1526-
const creatorCommand = await CommandHelper.createCommandByActionParams([], ElementsCommandTypes.ElementsCreatorChanged, this.store);
1527-
Object.assign(creatorCommand.payload, {
1528-
prevCreatorType: this.prevCreatorType,
1529-
creatorType: CreatorTypes.arbitrary,
1530-
});
1531-
this.undoRedo.add(creatorCommand);
15321527
uDataList.push({ type: ElementActionTypes.StartCreating, model: { id: element.id } });
15331528
// 组件创建生效,生成一个组件创建的命令
15341529
rDataList.push(
@@ -1926,7 +1921,7 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
19261921
* @param elements - 组件列表
19271922
*/
19281923
private _emitElementsCreated(elements: IElement[]): void {
1929-
this.setCreator(MoveableCreator);
1924+
this.setCreator(MoveableCreator, false);
19301925
this.emit(ShieldDispatcherNames.elementCreated, elements);
19311926
}
19321927

@@ -2027,9 +2022,12 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
20272022
*
20282023
* @param creator
20292024
*/
2030-
async setCreator(creator: Creator): Promise<void> {
2025+
async setCreator(creator: Creator, isSupportUndoRedo?: boolean): Promise<void> {
2026+
if (creator.type === this.currentCreator?.type) return;
2027+
isSupportUndoRedo && this._addCreatorChangedCommand();
20312028
this.prevCreatorType = this.currentCreator?.type;
20322029
this.currentCreator = creator;
2030+
this.cursor.updateStyle();
20332031
this.emit(ShieldDispatcherNames.creatorChanged, creator);
20342032
}
20352033

@@ -2361,26 +2359,6 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
23612359
}
23622360
}
23632361

2364-
/**
2365-
* 处理组件移动操作
2366-
*/
2367-
_handleSelectMoveable(): void {
2368-
if (this.currentCreator.type === HandCreator.type) {
2369-
this.setCreator(MoveableCreator);
2370-
this.cursor.updateStyle();
2371-
}
2372-
}
2373-
2374-
/**
2375-
* 处理组件手型操作
2376-
*/
2377-
_handleSelectHand(): void {
2378-
if (this.currentCreator.type === MoveableCreator.type) {
2379-
this.setCreator(HandCreator);
2380-
this.cursor.updateStyle();
2381-
}
2382-
}
2383-
23842362
/**
23852363
* 处理组件组合操作
23862364
*/
@@ -2454,7 +2432,7 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
24542432
this.store.deSelectAll();
24552433
}
24562434
if (tailCommand.payload.type === ElementsCommandTypes.ElementsCreatorChanged) {
2457-
this.setCreator(CreatorHelper.getCreatorByType(isRedo ? tailCommand.payload.creatorType : tailCommand.payload.prevCreatorType || MoveableCreator.type));
2435+
this.setCreator(CreatorHelper.getCreatorByType(isRedo ? tailCommand.payload.creatorType : tailCommand.payload.prevCreatorType || MoveableCreator.type), false);
24582436
}
24592437
isRedo ? await this.undoRedo.redo() : await this.undoRedo.undo();
24602438
if (isRedo && tailCommand.payload.type === ElementsCommandTypes.ElementsCreating) {
@@ -2463,7 +2441,7 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
24632441
this.store.clearCreatingElements();
24642442
// 再把组件加回来
24652443
await this.undoRedo.redo();
2466-
this.setCreator(MoveableCreator);
2444+
this.setCreator(MoveableCreator, false);
24672445
tailCommand = this.undoRedo.tailRedoCommand;
24682446
}
24692447
}
@@ -2475,7 +2453,7 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
24752453
const rDataList = this.undoRedo.tailUndoCommand.payload.rDataList;
24762454
// 数据恢复
24772455
await CommandHelper.restoreDataList(rDataList, true, this.store);
2478-
this.setCreator(CreatorHelper.getCreatorByType(rDataList[0].model.type));
2456+
this.setCreator(CreatorHelper.getCreatorByType(rDataList[0].model.type), false);
24792457
tailCommand = this.undoRedo.tailUndoCommand;
24802458
}
24812459
}

src/stores/stage.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export const useStageStore = defineStore("stage", {
230230
await container.init(params.containerEl);
231231
await shield.init(params.shieldEl);
232232

233-
this.setCreator(MoveableCreator);
233+
this.setCreator(MoveableCreator, false);
234234

235235
[
236236
[ShieldDispatcherNames.elementCreated, this.onElementCreated],
@@ -297,9 +297,10 @@ export const useStageStore = defineStore("stage", {
297297
* 设置绘制工具
298298
*
299299
* @param creator
300+
* @param isSupportUndoRedo
300301
*/
301-
async setCreator(creator: Creator) {
302-
await shield.setCreator(creator);
302+
async setCreator(creator: Creator, isSupportUndoRedo?: boolean) {
303+
await shield.setCreator(creator, isSupportUndoRedo);
303304
},
304305
/**
305306
* 创作工具变更

src/types/IStageShield.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export default interface IStageShield extends IStageDrawer, IStageSetter {
113113
execUndo(): Promise<void>;
114114
// 执行重做
115115
execRedo(): Promise<void>;
116+
// 切换工具
117+
setCreator(creator: Creator, isSupportUndoRedo?: boolean): Promise<void>;
116118
}
117119

118120
// 舞台计算参数

0 commit comments

Comments
 (0)