From 2a9f07a0ee3e5fa0c6b79389820911eb22aaa37b Mon Sep 17 00:00:00 2001 From: Yan Heng Date: Tue, 25 Jul 2023 09:12:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B7=A5=E5=85=B7=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E4=B8=AD=E9=BC=A0=E6=A0=87=E6=96=B9=E6=B3=95=E4=B8=BA=E5=8F=AF?= =?UTF-8?q?=E9=80=89=E6=96=B9=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=8A=A0name?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/services/mouse.ts | 12 +++++++++--- packages/core/src/tools/ellipse-tool.ts | 1 + packages/core/src/tools/polyline-tool.ts | 1 + packages/core/src/tools/rect-tool.ts | 1 + packages/core/src/tools/select-tool.ts | 6 ++---- packages/core/src/tools/triangle-tool.ts | 1 + packages/core/src/types.ts | 7 ++++--- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/core/src/services/mouse.ts b/packages/core/src/services/mouse.ts index 694ee248..0713a9f5 100644 --- a/packages/core/src/services/mouse.ts +++ b/packages/core/src/services/mouse.ts @@ -63,7 +63,9 @@ export class MouseService extends Service { this.targets.push(event.target); event.target.set({ evented: false }); } - this.app.currentTool.onMouseDown({ event, app: this.app }); + if (typeof this.app.currentTool.onMouseDown === 'function') { + this.app.currentTool.onMouseDown({ event, app: this.app }); + } } private onMouseUp(event: IMouseEvent): void { @@ -75,7 +77,9 @@ export class MouseService extends Service { obj.set({ evented: true }); }); this.targets = []; - this.app.currentTool.onMouseUp({ event, app: this.app }); + if (typeof this.app.currentTool.onMouseUp === 'function') { + this.app.currentTool.onMouseUp({ event, app: this.app }); + } } private onMouseMove(event: IMouseEvent): void { @@ -87,7 +91,9 @@ export class MouseService extends Service { this.targets.push(event.target); event.target.set({ evented: false }); } - this.app.currentTool.onMouseMove({ event, app: this.app }); + if (typeof this.app.currentTool.onMouseMove === 'function') { + this.app.currentTool.onMouseMove({ event, app: this.app }); + } } private onMouseDoubleClick(event: IMouseEvent): void { diff --git a/packages/core/src/tools/ellipse-tool.ts b/packages/core/src/tools/ellipse-tool.ts index 9cc2d18f..0a2c5f27 100644 --- a/packages/core/src/tools/ellipse-tool.ts +++ b/packages/core/src/tools/ellipse-tool.ts @@ -6,6 +6,7 @@ import { AppMouseEvent, ToolStrategy } from '../types'; import { selectTool } from './select-tool'; class EllipseTool implements ToolStrategy { + public name: string = 'ellipseTool'; public drawable: boolean = true; private startPointer: fabric.Point = new fabric.Point(0, 0); private ellipse: Ellipse | null = null; diff --git a/packages/core/src/tools/polyline-tool.ts b/packages/core/src/tools/polyline-tool.ts index 9b1674dd..b86cd959 100644 --- a/packages/core/src/tools/polyline-tool.ts +++ b/packages/core/src/tools/polyline-tool.ts @@ -6,6 +6,7 @@ import { AppMouseEvent, ToolStrategy } from '../types'; import { selectTool } from './select-tool'; class PolylineTool implements ToolStrategy { + public name: string = 'polylineTool'; public drawable: boolean = true; private points: fabric.Point[] = []; private polyline: Polyline | null = null; diff --git a/packages/core/src/tools/rect-tool.ts b/packages/core/src/tools/rect-tool.ts index 7faa5871..2fc1ca6a 100644 --- a/packages/core/src/tools/rect-tool.ts +++ b/packages/core/src/tools/rect-tool.ts @@ -6,6 +6,7 @@ import { AppMouseEvent, ToolStrategy } from '../types'; import { selectTool } from './select-tool'; class RectTool implements ToolStrategy { + public name: string = 'rectTool'; public drawable: boolean = true; private startPointer: fabric.Point = new fabric.Point(0, 0); private rectangle: Rect | null = null; diff --git a/packages/core/src/tools/select-tool.ts b/packages/core/src/tools/select-tool.ts index 7ffe0924..3fd544ef 100644 --- a/packages/core/src/tools/select-tool.ts +++ b/packages/core/src/tools/select-tool.ts @@ -1,7 +1,9 @@ import { AppMouseEvent, ToolStrategy } from '../types'; class SelectTool implements ToolStrategy { + public name: string = 'selectTool'; public drawable: boolean = false; + public onMouseDown({ app }: AppMouseEvent): void { app.canvas.selection = true; app.canvas.isDrawingMode = false; @@ -9,10 +11,6 @@ class SelectTool implements ToolStrategy { app.canvas.selectionBorderColor = 'rgb(157, 157, 231)'; app.canvas.selectionLineWidth = 2; } - - public onMouseMove(): void {} - - public onMouseUp(): void {} } export const selectTool = new SelectTool(); diff --git a/packages/core/src/tools/triangle-tool.ts b/packages/core/src/tools/triangle-tool.ts index 14180e48..d33a0865 100644 --- a/packages/core/src/tools/triangle-tool.ts +++ b/packages/core/src/tools/triangle-tool.ts @@ -6,6 +6,7 @@ import { AppMouseEvent, ToolStrategy } from '../types'; import { selectTool } from './select-tool'; class TriangleTool implements ToolStrategy { + public name: string = 'triangleTool'; public drawable: boolean = true; private startPointer: fabric.Point = new fabric.Point(0, 0); private triangle: Triangle | null = null; diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index f67b51bc..47b94414 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -17,10 +17,11 @@ export interface AppMouseEvent { } export interface ToolStrategy { + name: string; drawable: boolean; - onMouseDown(event: AppMouseEvent): void; - onMouseMove(event: AppMouseEvent): void; - onMouseUp(event: AppMouseEvent): void; + onMouseDown?: (event: AppMouseEvent) => void; + onMouseMove?: (event: AppMouseEvent) => void; + onMouseUp?: (event: AppMouseEvent) => void; } export abstract class Service {