Skip to content

Commit

Permalink
feat: object增加id作为唯一属性
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 28, 2023
1 parent a328dd3 commit b67234c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 4 deletions.
13 changes: 12 additions & 1 deletion packages/core/src/customs/ellipse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Ellipse } from 'fabric';
import { BaseFabricObject, Ellipse } from 'fabric';

export class PEllipse extends Ellipse {
public id: string;

constructor(options: Partial<Ellipse> & { id: string }) {
super(options);
this.id = options.id;
}

public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}

public toJSON(): BaseFabricObject {
return { ...super.toJSON(), id: this.id };
}
}

export default PEllipse;
13 changes: 12 additions & 1 deletion packages/core/src/customs/polyline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Control, Point, Polyline, TPointerEvent, util } from 'fabric';
import { BaseFabricObject, Control, Point, Polyline, TPointerEvent, util, XY } from 'fabric';

export class PPolyline extends Polyline {
public id?: string;

constructor(points?: XY[], options?: Partial<Polyline> & { id: string }) {
super(points, options);
this.id = options?.id;
}

public onSelect(options: { e?: TPointerEvent | undefined }): boolean {
const points = this.points ?? [];
this.controls = points.reduce<Record<string, Control>>(
Expand Down Expand Up @@ -43,6 +50,10 @@ export class PPolyline extends Polyline {
public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}

public toJSON(): BaseFabricObject {
return { ...super.toJSON(), id: this.id };
}
}

export default PPolyline;
13 changes: 12 additions & 1 deletion packages/core/src/customs/rect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Rect } from 'fabric';
import { BaseFabricObject, Rect } from 'fabric';

export class PRect extends Rect {
public id: string;

constructor(options: Partial<Rect> & { id: string }) {
super(options);
this.id = options.id;
}

public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}

public toJSON(): BaseFabricObject {
return { ...super.toJSON(), id: this.id };
}
}

export default PRect;
13 changes: 12 additions & 1 deletion packages/core/src/customs/triangle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Triangle } from 'fabric';
import { BaseFabricObject, Triangle } from 'fabric';

export class PTriangle extends Triangle {
public id: string;

constructor(options: Partial<Triangle> & { id: string }) {
super(options);
this.id = options.id;
}

public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}

public toJSON(): BaseFabricObject {
return { ...super.toJSON(), id: this.id };
}
}

export default PTriangle;
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 @@ -15,6 +15,7 @@ class EllipseTool implements Tool {
app.canvas.selection = false;
this.startPointer = app.pointer;
this.ellipse = new PEllipse({
id: Date.now().toString(),
left: this.startPointer.x,
top: this.startPointer.y,
rx: 0,
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 @@ -21,6 +21,7 @@ class PolylineTool implements Tool {
this.polyline.set({ points: this.points });
} else {
this.polyline = new PPolyline(this.points, {
id: Date.now().toString(),
fill: 'transparent',
stroke: 'black',
strokeWidth: 2,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/tools/rect-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RectTool implements Tool {
app.canvas.selection = false;
this.startPointer = app.pointer;
this.rectangle = new PRect({
id: Date.now().toString(),
left: this.startPointer.x,
top: this.startPointer.y,
width: 10,
Expand All @@ -23,6 +24,7 @@ class RectTool implements Tool {
stroke: 'black',
strokeWidth: 2,
});
this.rectangle.set({ id: Date.now() });
app.canvas.add(this.rectangle);
}

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 @@ -15,6 +15,7 @@ class TriangleTool implements Tool {
app.canvas.selection = false;
this.startPointer = app.pointer;
this.triangle = new PTriangle({
id: Date.now().toString(),
top: this.startPointer.y,
left: this.startPointer.x,
width: 0,
Expand Down

0 comments on commit b67234c

Please sign in to comment.