From 2bdbcae2b503e8370ad6cead037fe525ef720d85 Mon Sep 17 00:00:00 2001 From: Yan Heng Date: Mon, 31 Jul 2023 10:58:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20app=E6=96=B0=E5=A2=9EselectByEvent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/app.ts | 10 +++++++++- packages/core/src/tools/select-tool.ts | 14 ++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/core/src/app.ts b/packages/core/src/app.ts index 8acef987..f8ecc94d 100644 --- a/packages/core/src/app.ts +++ b/packages/core/src/app.ts @@ -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 { @@ -112,6 +112,14 @@ export class App extends BaseService { 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) diff --git a/packages/core/src/tools/select-tool.ts b/packages/core/src/tools/select-tool.ts index 55b55f39..67f049db 100644 --- a/packages/core/src/tools/select-tool.ts +++ b/packages/core/src/tools/select-tool.ts @@ -1,5 +1,3 @@ -import Konva from 'konva'; - import { Rect } from '../customs/rect'; import { AppMouseEvent, ChildType, Tool } from '../types'; import { Point } from '../utils'; @@ -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) { @@ -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); }, @@ -57,7 +47,7 @@ export const selectTool = (...shapes: ChildType[]): Tool => { rubberRect.visible(false); }, onMouseClick({ app, event }: AppMouseEvent) { - selectByEvent({ app, event }); + app.selectByEvent(event); }, }; };