@hprint/plugins 0.0.1-alpha.3 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/index.js +17 -17
  2. package/dist/index.mjs +1282 -1277
  3. package/dist/src/plugins/CreateElementPlugin.d.ts +4 -0
  4. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  5. package/dist/src/utils/ruler/ruler.d.ts +1 -0
  6. package/dist/src/utils/ruler/ruler.d.ts.map +1 -1
  7. package/package.json +3 -3
  8. package/src/assets/style/resizePlugin.css +27 -27
  9. package/src/objects/Arrow.js +47 -47
  10. package/src/objects/ThinTailArrow.js +50 -50
  11. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1152
  12. package/src/plugins/ControlsPlugin.ts +251 -251
  13. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  14. package/src/plugins/CopyPlugin.ts +255 -255
  15. package/src/plugins/CreateElementPlugin.ts +23 -2
  16. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  17. package/src/plugins/DrawLinePlugin.ts +162 -162
  18. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  19. package/src/plugins/DringPlugin.ts +125 -125
  20. package/src/plugins/FlipPlugin.ts +59 -59
  21. package/src/plugins/FontPlugin.ts +165 -165
  22. package/src/plugins/FreeDrawPlugin.ts +49 -49
  23. package/src/plugins/GroupAlignPlugin.ts +365 -365
  24. package/src/plugins/GroupPlugin.ts +82 -82
  25. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  26. package/src/plugins/HistoryPlugin.ts +181 -181
  27. package/src/plugins/ImageStroke.ts +121 -121
  28. package/src/plugins/LayerPlugin.ts +108 -108
  29. package/src/plugins/LockPlugin.ts +242 -242
  30. package/src/plugins/MaskPlugin.ts +155 -155
  31. package/src/plugins/MaterialPlugin.ts +224 -224
  32. package/src/plugins/MiddleMousePlugin.ts +45 -45
  33. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  34. package/src/plugins/PathTextPlugin.ts +89 -89
  35. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  36. package/src/plugins/PrintPlugin.ts +81 -81
  37. package/src/plugins/PsdPlugin.ts +52 -52
  38. package/src/plugins/ResizePlugin.ts +278 -278
  39. package/src/plugins/RulerPlugin.ts +78 -78
  40. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  41. package/src/plugins/UnitPlugin.ts +326 -326
  42. package/src/plugins/WaterMarkPlugin.ts +257 -257
  43. package/src/types/eventType.ts +11 -11
  44. package/src/utils/psd.js +432 -432
  45. package/src/utils/ruler/guideline.ts +145 -145
  46. package/src/utils/ruler/index.ts +91 -91
  47. package/src/utils/ruler/ruler.ts +936 -924
  48. package/src/utils/ruler/utils.ts +162 -162
  49. package/tsconfig.json +10 -10
  50. package/vite.config.ts +29 -29
@@ -1,89 +1,89 @@
1
- import { fabric } from '@hprint/core';
2
- import { v4 as uuid } from 'uuid';
3
- import type { IEditor, IPluginTempl } from '@hprint/core';
4
-
5
- type IPlugin = Pick<PathTextPlugin, 'startTextPathDraw' | 'endTextPathDraw'>;
6
-
7
- declare module '@hprint/core' {
8
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
9
- interface IEditor extends IPlugin {}
10
- }
11
-
12
- type DrawOptions = {
13
- decimate: number;
14
- width: number;
15
- defaultText: string;
16
- color: string;
17
- lineColor: string;
18
- defaultFontSize: number;
19
- };
20
-
21
- export default class PathTextPlugin implements IPluginTempl {
22
- static pluginName = 'PathTextPlugin';
23
- static apis = ['startTextPathDraw', 'endTextPathDraw'];
24
- private options?: DrawOptions;
25
- constructor(
26
- public canvas: fabric.Canvas,
27
- public editor: IEditor
28
- ) {}
29
-
30
- _beforeHandler = (opt: any) => {
31
- if (this.options == null) return;
32
- const path = opt.path as any;
33
- const getPathSegmentsInfo = (fabric.util as any).getPathSegmentsInfo;
34
- path.segmentsInfo = getPathSegmentsInfo(path.path);
35
- path.set({ stroke: this.options.lineColor });
36
- const text = this.options.defaultText;
37
- const fontSize = this.options.defaultFontSize;
38
- const textObject = new fabric.IText(text, {
39
- shadow: '',
40
- fontFamily: 'arial',
41
- fontSize: fontSize,
42
- top: path.top,
43
- left: path.left,
44
- fill: this.options.color,
45
- path: path,
46
- id: uuid(),
47
- // 路径文字元素禁止在画布上直接编辑
48
- editable: false,
49
- });
50
- this.canvas.add(textObject);
51
- };
52
- _createdHandler = (opt: any) => {
53
- this.canvas.remove(opt.path);
54
- };
55
- _bindEvent() {
56
- this.canvas.on('before:path:created', this._beforeHandler);
57
- this.canvas.on('path:created', this._createdHandler);
58
- }
59
- _unbindEvent() {
60
- this.canvas.off('before:path:created', this._beforeHandler);
61
- this.canvas.off('path:created', this._createdHandler);
62
- }
63
- startTextPathDraw(options: Partial<DrawOptions> = {}) {
64
- const defaultOptions = {
65
- decimate: 8,
66
- width: 2,
67
- defaultText: '诸事顺遂 万事大吉',
68
- color: '#000000',
69
- lineColor: '#000000',
70
- defaultFontSize: 20,
71
- };
72
- this.options = {
73
- ...defaultOptions,
74
- ...options,
75
- };
76
- this.canvas.isDrawingMode = true;
77
- const brush = (this.canvas.freeDrawingBrush = new fabric.PencilBrush(
78
- this.canvas
79
- ));
80
- brush.decimate = this.options.decimate;
81
- brush.width = this.options.width;
82
- brush.color = this.options.color;
83
- this._bindEvent();
84
- }
85
- endTextPathDraw() {
86
- this.canvas.isDrawingMode = false;
87
- this._unbindEvent();
88
- }
89
- }
1
+ import { fabric } from '@hprint/core';
2
+ import { v4 as uuid } from 'uuid';
3
+ import type { IEditor, IPluginTempl } from '@hprint/core';
4
+
5
+ type IPlugin = Pick<PathTextPlugin, 'startTextPathDraw' | 'endTextPathDraw'>;
6
+
7
+ declare module '@hprint/core' {
8
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
9
+ interface IEditor extends IPlugin {}
10
+ }
11
+
12
+ type DrawOptions = {
13
+ decimate: number;
14
+ width: number;
15
+ defaultText: string;
16
+ color: string;
17
+ lineColor: string;
18
+ defaultFontSize: number;
19
+ };
20
+
21
+ export default class PathTextPlugin implements IPluginTempl {
22
+ static pluginName = 'PathTextPlugin';
23
+ static apis = ['startTextPathDraw', 'endTextPathDraw'];
24
+ private options?: DrawOptions;
25
+ constructor(
26
+ public canvas: fabric.Canvas,
27
+ public editor: IEditor
28
+ ) {}
29
+
30
+ _beforeHandler = (opt: any) => {
31
+ if (this.options == null) return;
32
+ const path = opt.path as any;
33
+ const getPathSegmentsInfo = (fabric.util as any).getPathSegmentsInfo;
34
+ path.segmentsInfo = getPathSegmentsInfo(path.path);
35
+ path.set({ stroke: this.options.lineColor });
36
+ const text = this.options.defaultText;
37
+ const fontSize = this.options.defaultFontSize;
38
+ const textObject = new fabric.IText(text, {
39
+ shadow: '',
40
+ fontFamily: 'arial',
41
+ fontSize: fontSize,
42
+ top: path.top,
43
+ left: path.left,
44
+ fill: this.options.color,
45
+ path: path,
46
+ id: uuid(),
47
+ // 路径文字元素禁止在画布上直接编辑
48
+ editable: false,
49
+ });
50
+ this.canvas.add(textObject);
51
+ };
52
+ _createdHandler = (opt: any) => {
53
+ this.canvas.remove(opt.path);
54
+ };
55
+ _bindEvent() {
56
+ this.canvas.on('before:path:created', this._beforeHandler);
57
+ this.canvas.on('path:created', this._createdHandler);
58
+ }
59
+ _unbindEvent() {
60
+ this.canvas.off('before:path:created', this._beforeHandler);
61
+ this.canvas.off('path:created', this._createdHandler);
62
+ }
63
+ startTextPathDraw(options: Partial<DrawOptions> = {}) {
64
+ const defaultOptions = {
65
+ decimate: 8,
66
+ width: 2,
67
+ defaultText: '诸事顺遂 万事大吉',
68
+ color: '#000000',
69
+ lineColor: '#000000',
70
+ defaultFontSize: 20,
71
+ };
72
+ this.options = {
73
+ ...defaultOptions,
74
+ ...options,
75
+ };
76
+ this.canvas.isDrawingMode = true;
77
+ const brush = (this.canvas.freeDrawingBrush = new fabric.PencilBrush(
78
+ this.canvas
79
+ ));
80
+ brush.decimate = this.options.decimate;
81
+ brush.width = this.options.width;
82
+ brush.color = this.options.color;
83
+ this._bindEvent();
84
+ }
85
+ endTextPathDraw() {
86
+ this.canvas.isDrawingMode = false;
87
+ this._unbindEvent();
88
+ }
89
+ }
@@ -1,224 +1,224 @@
1
- import { fabric } from '@hprint/core';
2
- import { utils } from '@hprint/shared';
3
- import edgeImg from '../assets/edgecontrol.svg?url';
4
- import { noop } from 'lodash-es';
5
- import type { IEditor, IPluginTempl } from '@hprint/core';
6
-
7
- type IPlugin = Pick<
8
- PolygonModifyPlugin,
9
- 'toggleEdit' | 'activeEdit' | 'inActiveEdit'
10
- >;
11
-
12
- declare module '@hprint/core' {
13
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
14
- interface IEditor extends IPlugin {}
15
- }
16
-
17
- export type Options = {
18
- fill: string;
19
- style: fabric.IObjectOptions['cornerStyle'];
20
- };
21
-
22
- interface PointIndexPolygon extends fabric.Polygon {
23
- pointIndex: number;
24
- __corner: string;
25
- _setPositionDimensions: (...args: any[]) => any;
26
- }
27
-
28
- interface PointIndexControl extends fabric.Control {
29
- pointIndex: number;
30
- }
31
-
32
- const actionHandler: fabric.Control['actionHandler'] = function (
33
- eventData: MouseEvent,
34
- transform: fabric.Transform,
35
- x: number,
36
- y: number
37
- ) {
38
- const polygon = transform.target as PointIndexPolygon,
39
- currentControl = polygon.controls[
40
- polygon.__corner
41
- ] as PointIndexControl,
42
- mouseLocalPosition = polygon.toLocalPoint(
43
- new fabric.Point(x, y),
44
- 'center',
45
- 'center'
46
- ),
47
- polygonBaseSize = getObjectSizeWithStroke(polygon),
48
- size = polygon._getTransformedDimensions(0, 0);
49
- if (polygon.points == null) return false;
50
- polygon.points[currentControl.pointIndex] = new fabric.Point(
51
- (mouseLocalPosition.x * polygonBaseSize.x) / size.x +
52
- polygon.pathOffset.x,
53
- (mouseLocalPosition.y * polygonBaseSize.y) / size.y +
54
- polygon.pathOffset.y
55
- );
56
- return true;
57
- };
58
- const anchorWrapper = function (
59
- anchorIndex: number,
60
- fn: fabric.Control['actionHandler']
61
- ) {
62
- return function (
63
- eventData: MouseEvent,
64
- transform: fabric.Transform,
65
- x: number,
66
- y: number
67
- ) {
68
- const fabricObject = transform.target as PointIndexPolygon;
69
- if (fabricObject.points == null) return false;
70
- const absolutePoint = fabric.util.transformPoint(
71
- new fabric.Point(
72
- fabricObject.points[anchorIndex].x -
73
- fabricObject.pathOffset.x,
74
- fabricObject.points[anchorIndex].y -
75
- fabricObject.pathOffset.y
76
- ),
77
- fabricObject.calcTransformMatrix()
78
- ),
79
- actionPerformed = fn(eventData, transform, x, y),
80
- // newDim = fabricObject._setPositionDimensions({}),
81
- polygonBaseSize = getObjectSizeWithStroke(fabricObject),
82
- newX =
83
- (fabricObject.points[anchorIndex].x -
84
- fabricObject.pathOffset.x) /
85
- polygonBaseSize.x,
86
- newY =
87
- (fabricObject.points[anchorIndex].y -
88
- fabricObject.pathOffset.y) /
89
- polygonBaseSize.y;
90
- const originX = (newX + 0.5) as any;
91
- const originY = (newY + 0.5) as any;
92
- fabricObject.setPositionByOrigin(absolutePoint, originX, originY);
93
- return actionPerformed;
94
- };
95
- };
96
- const getObjectSizeWithStroke = function (object: fabric.Object) {
97
- const stroke = new fabric.Point(
98
- object.strokeUniform ? 1 / object.scaleX! : 1,
99
- object.strokeUniform ? 1 / object.scaleY! : 1
100
- ).multiply(object.strokeWidth!);
101
- return new fabric.Point(
102
- object.width! + stroke.x,
103
- object.height! + stroke.y
104
- );
105
- };
106
- const polygonPositionHandler = function (
107
- this: PointIndexControl,
108
- dim: any,
109
- finalMatrix: any,
110
- fabricObject: any
111
- ) {
112
- const x =
113
- fabricObject.points[this.pointIndex].x - fabricObject.pathOffset.x,
114
- y = fabricObject.points[this.pointIndex].y - fabricObject.pathOffset.y;
115
- // 求出在世界坐标系的位置
116
- return fabric.util.transformPoint(
117
- new fabric.Point(x, y), // 物体坐标系下的位置
118
- fabric.util.multiplyTransformMatrices(
119
- fabricObject.canvas.viewportTransform,
120
- fabricObject.calcTransformMatrix()
121
- )
122
- );
123
- };
124
- function renderIconEdge(
125
- ctx: CanvasRenderingContext2D,
126
- left: number,
127
- top: number,
128
- styleOverride: any,
129
- fabricObject: fabric.Object,
130
- img: HTMLImageElement
131
- ) {
132
- utils.drawImg(
133
- ctx,
134
- left,
135
- top,
136
- img,
137
- 25,
138
- 25,
139
- fabric.util.degreesToRadians(fabricObject.angle || 0)
140
- );
141
- }
142
-
143
- class PolygonModifyPlugin implements IPluginTempl {
144
- public isEdit: boolean;
145
- private img: HTMLImageElement;
146
- static pluginName = 'PolygonModifyPlugin';
147
- static events = [];
148
- static apis = ['toggleEdit', 'activeEdit', 'inActiveEdit'];
149
-
150
- constructor(
151
- public canvas: fabric.Canvas,
152
- public editor: IEditor
153
- ) {
154
- this.isEdit = false;
155
- const img = document.createElement('img');
156
- img.src = edgeImg;
157
- this.img = img;
158
- this.init();
159
- }
160
- init() {
161
- console.info('[PolygonModifyPlugin]: init');
162
- }
163
- _onDeselected: () => any = noop;
164
- _ensureEvent(poly: fabric.Object) {
165
- poly.off('deselected', this._onDeselected);
166
- }
167
- toggleEdit() {
168
- this.isEdit ? this.inActiveEdit() : this.activeEdit();
169
- }
170
- activeEdit() {
171
- this.isEdit = true;
172
- const poly = this.canvas.getActiveObject() as fabric.Polygon;
173
- if (poly && poly.type === 'polygon') {
174
- this._ensureEvent(poly);
175
- if (poly.points == null) return;
176
- const lastControl = poly.points.length - 1;
177
- const This = this;
178
- poly.controls = poly.points.reduce<
179
- Record<string, PointIndexControl>
180
- >(function (acc, point, index) {
181
- acc['p' + index] = <PointIndexControl>new fabric.Control({
182
- positionHandler: polygonPositionHandler,
183
- actionHandler: anchorWrapper(
184
- index > 0 ? index - 1 : lastControl,
185
- actionHandler
186
- ),
187
- actionName: 'modifyPolygon',
188
- render: (...args) => renderIconEdge(...args, This.img),
189
- });
190
- Object.defineProperty(acc['p' + index], 'pointIndex', {
191
- value: index,
192
- });
193
- return acc;
194
- }, {});
195
- poly.set({
196
- objectCaching: false,
197
- });
198
- poly.hasBorders = !this.isEdit;
199
- this.canvas.requestRenderAll();
200
- this._onDeselected = () => this.inActiveEdit(poly);
201
- poly.on('deselected', this._onDeselected);
202
- }
203
- }
204
- inActiveEdit(poly?: fabric.Polygon) {
205
- this.isEdit = false;
206
- poly = poly || (this.canvas.getActiveObject() as fabric.Polygon);
207
- if (poly && poly.type === 'polygon') {
208
- poly.cornerColor = 'blue';
209
- poly.cornerStyle = 'rect';
210
- poly.controls = fabric.Object.prototype.controls;
211
- poly.hasBorders = !this.isEdit;
212
- poly.set({
213
- objectCaching: true,
214
- });
215
- if (this._onDeselected) {
216
- poly.off('deselected', this._onDeselected);
217
- this._onDeselected = noop;
218
- }
219
- }
220
- this.canvas.requestRenderAll();
221
- }
222
- }
223
-
224
- export default PolygonModifyPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import { utils } from '@hprint/shared';
3
+ import edgeImg from '../assets/edgecontrol.svg?url';
4
+ import { noop } from 'lodash-es';
5
+ import type { IEditor, IPluginTempl } from '@hprint/core';
6
+
7
+ type IPlugin = Pick<
8
+ PolygonModifyPlugin,
9
+ 'toggleEdit' | 'activeEdit' | 'inActiveEdit'
10
+ >;
11
+
12
+ declare module '@hprint/core' {
13
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
14
+ interface IEditor extends IPlugin {}
15
+ }
16
+
17
+ export type Options = {
18
+ fill: string;
19
+ style: fabric.IObjectOptions['cornerStyle'];
20
+ };
21
+
22
+ interface PointIndexPolygon extends fabric.Polygon {
23
+ pointIndex: number;
24
+ __corner: string;
25
+ _setPositionDimensions: (...args: any[]) => any;
26
+ }
27
+
28
+ interface PointIndexControl extends fabric.Control {
29
+ pointIndex: number;
30
+ }
31
+
32
+ const actionHandler: fabric.Control['actionHandler'] = function (
33
+ eventData: MouseEvent,
34
+ transform: fabric.Transform,
35
+ x: number,
36
+ y: number
37
+ ) {
38
+ const polygon = transform.target as PointIndexPolygon,
39
+ currentControl = polygon.controls[
40
+ polygon.__corner
41
+ ] as PointIndexControl,
42
+ mouseLocalPosition = polygon.toLocalPoint(
43
+ new fabric.Point(x, y),
44
+ 'center',
45
+ 'center'
46
+ ),
47
+ polygonBaseSize = getObjectSizeWithStroke(polygon),
48
+ size = polygon._getTransformedDimensions(0, 0);
49
+ if (polygon.points == null) return false;
50
+ polygon.points[currentControl.pointIndex] = new fabric.Point(
51
+ (mouseLocalPosition.x * polygonBaseSize.x) / size.x +
52
+ polygon.pathOffset.x,
53
+ (mouseLocalPosition.y * polygonBaseSize.y) / size.y +
54
+ polygon.pathOffset.y
55
+ );
56
+ return true;
57
+ };
58
+ const anchorWrapper = function (
59
+ anchorIndex: number,
60
+ fn: fabric.Control['actionHandler']
61
+ ) {
62
+ return function (
63
+ eventData: MouseEvent,
64
+ transform: fabric.Transform,
65
+ x: number,
66
+ y: number
67
+ ) {
68
+ const fabricObject = transform.target as PointIndexPolygon;
69
+ if (fabricObject.points == null) return false;
70
+ const absolutePoint = fabric.util.transformPoint(
71
+ new fabric.Point(
72
+ fabricObject.points[anchorIndex].x -
73
+ fabricObject.pathOffset.x,
74
+ fabricObject.points[anchorIndex].y -
75
+ fabricObject.pathOffset.y
76
+ ),
77
+ fabricObject.calcTransformMatrix()
78
+ ),
79
+ actionPerformed = fn(eventData, transform, x, y),
80
+ // newDim = fabricObject._setPositionDimensions({}),
81
+ polygonBaseSize = getObjectSizeWithStroke(fabricObject),
82
+ newX =
83
+ (fabricObject.points[anchorIndex].x -
84
+ fabricObject.pathOffset.x) /
85
+ polygonBaseSize.x,
86
+ newY =
87
+ (fabricObject.points[anchorIndex].y -
88
+ fabricObject.pathOffset.y) /
89
+ polygonBaseSize.y;
90
+ const originX = (newX + 0.5) as any;
91
+ const originY = (newY + 0.5) as any;
92
+ fabricObject.setPositionByOrigin(absolutePoint, originX, originY);
93
+ return actionPerformed;
94
+ };
95
+ };
96
+ const getObjectSizeWithStroke = function (object: fabric.Object) {
97
+ const stroke = new fabric.Point(
98
+ object.strokeUniform ? 1 / object.scaleX! : 1,
99
+ object.strokeUniform ? 1 / object.scaleY! : 1
100
+ ).multiply(object.strokeWidth!);
101
+ return new fabric.Point(
102
+ object.width! + stroke.x,
103
+ object.height! + stroke.y
104
+ );
105
+ };
106
+ const polygonPositionHandler = function (
107
+ this: PointIndexControl,
108
+ dim: any,
109
+ finalMatrix: any,
110
+ fabricObject: any
111
+ ) {
112
+ const x =
113
+ fabricObject.points[this.pointIndex].x - fabricObject.pathOffset.x,
114
+ y = fabricObject.points[this.pointIndex].y - fabricObject.pathOffset.y;
115
+ // 求出在世界坐标系的位置
116
+ return fabric.util.transformPoint(
117
+ new fabric.Point(x, y), // 物体坐标系下的位置
118
+ fabric.util.multiplyTransformMatrices(
119
+ fabricObject.canvas.viewportTransform,
120
+ fabricObject.calcTransformMatrix()
121
+ )
122
+ );
123
+ };
124
+ function renderIconEdge(
125
+ ctx: CanvasRenderingContext2D,
126
+ left: number,
127
+ top: number,
128
+ styleOverride: any,
129
+ fabricObject: fabric.Object,
130
+ img: HTMLImageElement
131
+ ) {
132
+ utils.drawImg(
133
+ ctx,
134
+ left,
135
+ top,
136
+ img,
137
+ 25,
138
+ 25,
139
+ fabric.util.degreesToRadians(fabricObject.angle || 0)
140
+ );
141
+ }
142
+
143
+ class PolygonModifyPlugin implements IPluginTempl {
144
+ public isEdit: boolean;
145
+ private img: HTMLImageElement;
146
+ static pluginName = 'PolygonModifyPlugin';
147
+ static events = [];
148
+ static apis = ['toggleEdit', 'activeEdit', 'inActiveEdit'];
149
+
150
+ constructor(
151
+ public canvas: fabric.Canvas,
152
+ public editor: IEditor
153
+ ) {
154
+ this.isEdit = false;
155
+ const img = document.createElement('img');
156
+ img.src = edgeImg;
157
+ this.img = img;
158
+ this.init();
159
+ }
160
+ init() {
161
+ console.info('[PolygonModifyPlugin]: init');
162
+ }
163
+ _onDeselected: () => any = noop;
164
+ _ensureEvent(poly: fabric.Object) {
165
+ poly.off('deselected', this._onDeselected);
166
+ }
167
+ toggleEdit() {
168
+ this.isEdit ? this.inActiveEdit() : this.activeEdit();
169
+ }
170
+ activeEdit() {
171
+ this.isEdit = true;
172
+ const poly = this.canvas.getActiveObject() as fabric.Polygon;
173
+ if (poly && poly.type === 'polygon') {
174
+ this._ensureEvent(poly);
175
+ if (poly.points == null) return;
176
+ const lastControl = poly.points.length - 1;
177
+ const This = this;
178
+ poly.controls = poly.points.reduce<
179
+ Record<string, PointIndexControl>
180
+ >(function (acc, point, index) {
181
+ acc['p' + index] = <PointIndexControl>new fabric.Control({
182
+ positionHandler: polygonPositionHandler,
183
+ actionHandler: anchorWrapper(
184
+ index > 0 ? index - 1 : lastControl,
185
+ actionHandler
186
+ ),
187
+ actionName: 'modifyPolygon',
188
+ render: (...args) => renderIconEdge(...args, This.img),
189
+ });
190
+ Object.defineProperty(acc['p' + index], 'pointIndex', {
191
+ value: index,
192
+ });
193
+ return acc;
194
+ }, {});
195
+ poly.set({
196
+ objectCaching: false,
197
+ });
198
+ poly.hasBorders = !this.isEdit;
199
+ this.canvas.requestRenderAll();
200
+ this._onDeselected = () => this.inActiveEdit(poly);
201
+ poly.on('deselected', this._onDeselected);
202
+ }
203
+ }
204
+ inActiveEdit(poly?: fabric.Polygon) {
205
+ this.isEdit = false;
206
+ poly = poly || (this.canvas.getActiveObject() as fabric.Polygon);
207
+ if (poly && poly.type === 'polygon') {
208
+ poly.cornerColor = 'blue';
209
+ poly.cornerStyle = 'rect';
210
+ poly.controls = fabric.Object.prototype.controls;
211
+ poly.hasBorders = !this.isEdit;
212
+ poly.set({
213
+ objectCaching: true,
214
+ });
215
+ if (this._onDeselected) {
216
+ poly.off('deselected', this._onDeselected);
217
+ this._onDeselected = noop;
218
+ }
219
+ }
220
+ this.canvas.requestRenderAll();
221
+ }
222
+ }
223
+
224
+ export default PolygonModifyPlugin;