Skip to content

Commit

Permalink
feat: 工具协议中鼠标方法为可选方法,增加name属性
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 25, 2023
1 parent e3e8c58 commit 2a9f07a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/services/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tools/ellipse-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tools/polyline-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tools/rect-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/tools/select-tool.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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;
app.canvas.selectionColor = 'rgba(157, 157, 231, 0.5)';
app.canvas.selectionBorderColor = 'rgb(157, 157, 231)';
app.canvas.selectionLineWidth = 2;
}

public onMouseMove(): void {}

public onMouseUp(): void {}
}

export const selectTool = new SelectTool();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tools/triangle-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2a9f07a

Please sign in to comment.