Skip to content

Commit

Permalink
feat: 新增删除方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Aug 1, 2023
1 parent 84dda9e commit d1295ed
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions main/src/view/canvas/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ onMounted(() => {

<template>
<div class="wrapper">
<div class="tools">
<button @click="app.remove(...app.selected)">删除</button>
<button @click="app.undo()">回退</button>
<button @click="app.redo()">恢复</button>
</div>
<div class="tools">
<button @click="app.setTool(selectTool())">选择🖱️</button>
<button @click="app.setTool(rectTool())">矩形⬜️</button>
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class App extends BaseService<EventArgs> {
return new Point(x, y);
}

public get selected(): ChildType[] {
return this.selector.selected;
}

public mount(element: HTMLElement) {
element.appendChild(this.containerElement);
this.stage.setSize({
Expand All @@ -71,11 +75,11 @@ export class App extends BaseService<EventArgs> {
}

public remove(...children: ChildType[]): void {
console.log('====>', children);

this.mainLayer.find((node: any) => {
console.log('=====>', node);
children.forEach((child) => {
child.remove();
});
this.render();
this.emit('shape:removed', { object: children });
}

public triggerSelector(enable?: boolean): void {
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/services/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@ export class Selector extends Service {
});
this.optionLayer.add(this.rubberRect);

(['onMouseDown', 'onMouseUp', 'onMouseMove', 'onMouseClick'] as (keyof this)[]).forEach((method) => {
method = method as keyof Selector;
this[method] = (this[method] as Function).bind(this);
});
(['onMouseDown', 'onMouseUp', 'onMouseMove', 'onMouseClick', 'onShapeRemoved'] as (keyof this)[]).forEach(
(method) => {
method = method as keyof Selector;
this[method] = (this[method] as Function).bind(this);
}
);

this.app.on('mouse:down', this.onMouseDown);
this.app.on('mouse:move', this.onMouseMove);
this.app.on('mouse:up', this.onMouseUp);
this.app.on('mouse:click', this.onMouseClick);
this.app.on('shape:removed', this.onShapeRemoved);
}

public select(...children: ChildType[]): void {
Expand Down Expand Up @@ -165,6 +168,12 @@ export class Selector extends Service {
this.select(event.target);
}

private onShapeRemoved({ object }: EventArgs['shape:removed']): void {
const removed = object.map((child) => child.id());
const result = this.selected.filter((child) => !removed.includes(child.id()));
this.select(...result);
}

public dispose(): void {
this.app.off('mouse:down', this.onMouseDown);
this.app.off('mouse:move', this.onMouseMove);
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-history/src/commands/add-object-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export class AddObjectCmd extends BaseCmd<Cmd.AddObjectOptions> {
}

public undo(): void {
// this.app.mainLayer.find((obj) => {})?.destroy();
this.app.render();
this.app.remove(...this.options.object);
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-history/src/commands/remove-object-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export class RemoveObjectCmd extends BaseCmd<Cmd.RemoveObjectOptions> {
}

public execute(): void {
// this.app.mainLayer.findOne(this.options.object.id())?.destroy();
this.app.render();
this.app.remove(...this.options.object);
}

public undo(): void {
Expand Down

0 comments on commit d1295ed

Please sign in to comment.