Skip to content

Commit 105adea

Browse files
committed
代码优化与bug修复
1 parent fd3f8f6 commit 105adea

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

src/modules/command/helpers/CommandHelper.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ export default class CommandHelper {
224224
);
225225
}
226226

227+
/**
228+
* 创建组件组合添加命令
229+
*
230+
* @param uDataList 组件数据
231+
* @param rDataList 组件数据
232+
* @param store 画板
233+
* @returns
234+
*/
235+
static createGroupAddedCommand(uDataList: Array<ICommandElementObject>, rDataList: Array<ICommandElementObject>, store: IStageStore): ICommand<IElementCommandPayload> {
236+
const command = new GroupAddedCommand(
237+
nanoid(),
238+
{
239+
type: ElementCommandTypes.GroupAdded,
240+
uDataList,
241+
rDataList,
242+
},
243+
store,
244+
);
245+
return command;
246+
}
247+
227248
/**
228249
* 组合创建命令
229250
*
@@ -232,36 +253,45 @@ export default class CommandHelper {
232253
* @param store 画板
233254
* @returns
234255
*/
235-
static async createGroupAddedCommand(elements: IElement[], groupAddFunction: () => { group: IElementGroup; elements: IElement[] }, store: IStageStore): Promise<ICommand<IElementCommandPayload>> {
256+
static async createGroupAddedCommandBy(elements: IElement[], groupAddFunction: () => { group: IElementGroup; elements: IElement[] }, store: IStageStore): Promise<ICommand<IElementCommandPayload>> {
236257
const uDataList: Array<IGroupCommandElementObject> = await Promise.all(
237258
elements.map(async element => ({ model: { id: element.id }, isGroupSubject: !element.isGroupSubject, ...CommandHelper.createRearrangeModel(element) })),
238259
);
239-
const { group, elements: newElements } = groupAddFunction();
260+
const { group, elements: newElements } = groupAddFunction() || {};
240261
if (group) {
241262
uDataList.push({
242263
model: { id: group.id },
243264
isGroup: true,
244265
} as IGroupCommandElementObject);
245266
const rDataList = await CommandHelper.createGroupAddedDataList(group, newElements);
246-
const command = new GroupAddedCommand(
247-
nanoid(),
248-
{
249-
type: ElementCommandTypes.GroupAdded,
250-
uDataList,
251-
rDataList,
252-
},
253-
store,
254-
);
255-
return command;
267+
return CommandHelper.createGroupAddedCommand(uDataList, rDataList, store);
256268
}
257269
}
258270

271+
/**
272+
* 创建组件组合移除命令
273+
*
274+
* @param elements 组件列表
275+
* @param store 画板
276+
*/
277+
static createGroupRemovedCommand(uDataList: Array<ICommandElementObject>, store: IStageStore): ICommand<IElementCommandPayload> {
278+
const command = new GroupRemovedCommand(
279+
nanoid(),
280+
{
281+
type: ElementCommandTypes.GroupRemoved,
282+
uDataList,
283+
},
284+
store,
285+
);
286+
return command;
287+
}
288+
259289
/**
260290
* 创建组件组合取消命令
261291
*
262292
* @param groups 组合列表
263293
*/
264-
static async createGroupRemovedCommand(groups: IElementGroup[], store: IStageStore): Promise<ICommand<IElementCommandPayload>> {
294+
static async createGroupRemovedCommandBy(groups: IElementGroup[], store: IStageStore): Promise<ICommand<IElementCommandPayload>> {
265295
const uDataList: Array<IGroupCommandElementObject> = [];
266296
await Promise.all(
267297
groups.map(async group => {
@@ -278,15 +308,7 @@ export default class CommandHelper {
278308
});
279309
}),
280310
);
281-
const command = new GroupRemovedCommand(
282-
nanoid(),
283-
{
284-
type: ElementCommandTypes.GroupRemoved,
285-
uDataList,
286-
},
287-
store,
288-
);
289-
return command;
311+
return CommandHelper.createGroupRemovedCommand(uDataList, store);
290312
}
291313

292314
/**

src/modules/stage/StageShield.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,16 +2273,20 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
22732273
async _handleSelectGroup(): Promise<void> {
22742274
if (this.isElementsBusy) return;
22752275
if (this.store.isSelectedEmpty) return;
2276-
const command = await CommandHelper.createGroupAddedCommand(
2276+
const command = await CommandHelper.createGroupAddedCommandBy(
22772277
this.store.selectedElements,
22782278
(): { group: IElementGroup; elements: IElement[] } => {
2279-
const group = this.store.selectToGroup();
2280-
this.store.selectGroup(group);
2281-
return { group, elements: this.store.selectedElements };
2279+
const group = this.store.createGroup(this.store.selectedElements);
2280+
if (group) {
2281+
this.store.selectGroup(group);
2282+
return { group, elements: this.store.selectedElements };
2283+
}
22822284
},
22832285
this.store,
22842286
);
2285-
this.undoRedo.add(command);
2287+
if (command) {
2288+
this.undoRedo.add(command);
2289+
}
22862290
}
22872291

22882292
/**
@@ -2292,7 +2296,7 @@ export default class StageShield extends DrawerBase implements IStageShield, ISt
22922296
if (this.isElementsBusy) return;
22932297
const groups = this.store.getSelectedAncestorElementGroups();
22942298
if (groups.length === 0) return;
2295-
const command = await CommandHelper.createGroupRemovedCommand(groups, this.store);
2299+
const command = await CommandHelper.createGroupRemovedCommandBy(groups, this.store);
22962300
this.undoRedo.add(command);
22972301
this.store.cancelGroups(groups);
22982302
groups.forEach(group => {

src/modules/stage/StageStore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,9 +2497,10 @@ export default class StageStore implements IStageStore {
24972497

24982498
/**
24992499
* 将选中的组件转换为组合
2500+
*
2501+
* @param elements
25002502
*/
2501-
selectToGroup(): IElementGroup {
2502-
const elements = this.selectedElements;
2503+
createGroup(elements: IElement[]): IElementGroup {
25032504
if (elements.length < 2) {
25042505
return null;
25052506
}

src/types/IStageStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export default interface IStageStore extends IStageSetter {
175175
// 删除组合
176176
removeElementGroup(group: IElementGroup): void;
177177
// 将选中的组件转换为组合
178-
selectToGroup(): IElementGroup;
178+
createGroup(elements: IElement[]): IElementGroup;
179179
// 取消组合
180180
cancelGroups(groups: IElementGroup[]): void;
181181
// 获取选中的根组合

0 commit comments

Comments
 (0)