Skip to content

Commit

Permalink
feat: app新增selectByEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 31, 2023
1 parent 9b70f19 commit 2bdbcae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseService } from '@pictode/utils';
import Konva from 'konva';

import { MouseService } from './services/mouse';
import { ChildType, EventArgs, Plugin, Tool } from './types';
import { ChildType, EventArgs, KonvaMouseEvent, Plugin, Tool } from './types';
import { Point } from './utils';

export class App extends BaseService<EventArgs> {
Expand Down Expand Up @@ -112,6 +112,14 @@ export class App extends BaseService<EventArgs> {
this.selector.moveToTop();
}

public selectByEvent(event: KonvaMouseEvent): void {
if (event.target instanceof Konva.Stage) {
this.select();
} else {
this.select(event.target);
}
}

public async getShapesInArea(shape: Konva.Shape): Promise<(Konva.Shape | Konva.Group)[]> {
return await this.mainLayer.getChildren(
(node) => node instanceof Konva.Shape && node.visible() && node !== shape && this.haveIntersection(shape, node)
Expand Down
14 changes: 2 additions & 12 deletions packages/core/src/tools/select-tool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Konva from 'konva';

import { Rect } from '../customs/rect';
import { AppMouseEvent, ChildType, Tool } from '../types';
import { Point } from '../utils';
Expand All @@ -17,14 +15,6 @@ export const selectTool = (...shapes: ChildType[]): Tool => {
});
let isRubberSelector: boolean = false;

const selectByEvent = ({ app, event }: AppMouseEvent) => {
if (event.target instanceof Konva.Stage) {
app.select();
} else {
app.select(event.target);
}
};

return {
name: 'selectTool',
onActive(app) {
Expand All @@ -36,7 +26,7 @@ export const selectTool = (...shapes: ChildType[]): Tool => {
startPointer.setXY(0, 0);
},
onMouseDown({ app, event }) {
selectByEvent({ app, event });
app.selectByEvent(event);
isRubberSelector = true;
startPointer.setXY(app.pointer.x, app.pointer.y);
},
Expand All @@ -57,7 +47,7 @@ export const selectTool = (...shapes: ChildType[]): Tool => {
rubberRect.visible(false);
},
onMouseClick({ app, event }: AppMouseEvent) {
selectByEvent({ app, event });
app.selectByEvent(event);
},
};
};
Expand Down

0 comments on commit 2bdbcae

Please sign in to comment.