@hprint/plugins 0.0.1-alpha.1 → 0.0.1-alpha.3

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 (64) hide show
  1. package/dist/index.js +122 -117
  2. package/dist/index.mjs +41257 -21115
  3. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
  4. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/BarCodePlugin.d.ts +4 -0
  6. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
  7. package/dist/src/plugins/CreateElementPlugin.d.ts +3 -9
  8. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  9. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
  10. package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
  11. package/dist/src/plugins/QrCodePlugin.d.ts +19 -97
  12. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  13. package/dist/src/plugins/ResizePlugin.d.ts.map +1 -1
  14. package/dist/src/plugins/UnitPlugin.d.ts.map +1 -1
  15. package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -1
  16. package/dist/src/utils/units.d.ts.map +1 -1
  17. package/package.json +5 -4
  18. package/src/assets/style/resizePlugin.css +27 -27
  19. package/src/objects/Arrow.js +47 -47
  20. package/src/objects/ThinTailArrow.js +50 -50
  21. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
  22. package/src/plugins/BarCodePlugin.ts +33 -27
  23. package/src/plugins/ControlsPlugin.ts +251 -251
  24. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  25. package/src/plugins/CopyPlugin.ts +255 -255
  26. package/src/plugins/CreateElementPlugin.ts +14 -10
  27. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  28. package/src/plugins/DrawLinePlugin.ts +162 -162
  29. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  30. package/src/plugins/DringPlugin.ts +125 -125
  31. package/src/plugins/FlipPlugin.ts +59 -59
  32. package/src/plugins/FontPlugin.ts +165 -165
  33. package/src/plugins/FreeDrawPlugin.ts +49 -49
  34. package/src/plugins/GroupAlignPlugin.ts +365 -365
  35. package/src/plugins/GroupPlugin.ts +82 -82
  36. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  37. package/src/plugins/HistoryPlugin.ts +181 -181
  38. package/src/plugins/ImageStroke.ts +121 -121
  39. package/src/plugins/LayerPlugin.ts +108 -108
  40. package/src/plugins/LockPlugin.ts +242 -240
  41. package/src/plugins/MaskPlugin.ts +155 -155
  42. package/src/plugins/MaterialPlugin.ts +224 -224
  43. package/src/plugins/MiddleMousePlugin.ts +45 -45
  44. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  45. package/src/plugins/PathTextPlugin.ts +89 -89
  46. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  47. package/src/plugins/PrintPlugin.ts +81 -81
  48. package/src/plugins/PsdPlugin.ts +52 -52
  49. package/src/plugins/QrCodePlugin.ts +322 -393
  50. package/src/plugins/ResizePlugin.ts +278 -274
  51. package/src/plugins/RulerPlugin.ts +78 -78
  52. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  53. package/src/plugins/UnitPlugin.ts +326 -327
  54. package/src/plugins/WaterMarkPlugin.ts +257 -257
  55. package/src/plugins/WorkspacePlugin.ts +10 -6
  56. package/src/types/eventType.ts +11 -11
  57. package/src/utils/psd.js +432 -432
  58. package/src/utils/ruler/guideline.ts +145 -145
  59. package/src/utils/ruler/index.ts +91 -91
  60. package/src/utils/ruler/ruler.ts +924 -924
  61. package/src/utils/ruler/utils.ts +162 -162
  62. package/src/utils/units.ts +4 -2
  63. package/tsconfig.json +10 -10
  64. package/vite.config.ts +29 -29
@@ -1,274 +1,278 @@
1
- import { fabric } from '@hprint/core';
2
- import { throttle } from 'lodash-es';
3
- import '../assets/style/resizePlugin.css';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPosition = 'left' | 'right' | 'top' | 'bottom';
7
-
8
- class ResizePlugin implements IPluginTempl {
9
- static pluginName = 'ResizePlugin';
10
- static events = [];
11
- static apis = [];
12
- workspaceEl!: HTMLElement;
13
- // 最小画布尺寸
14
- minSize = { width: 1, height: 1 };
15
- // 控制条
16
- barOpts = {
17
- bWidth: 30, // 宽
18
- bHeight: 6, // 高
19
- bPadding: 10, // 离画布边缘的距离
20
- };
21
- hasCreatedBar = false;
22
- isDragging = false;
23
- dragEl: HTMLElement | null = null;
24
- startPoints = { x: 0, y: 0 };
25
- barOffset = { x: 0, y: 0 };
26
- wsOffset: Record<'left' | 'top' | 'width' | 'height', number> = {
27
- left: 0,
28
- top: 0,
29
- width: 0,
30
- height: 0,
31
- };
32
- constructor(
33
- public canvas: fabric.Canvas,
34
- public editor: IEditor
35
- ) {
36
- this._init();
37
- this._initResizeObserve();
38
- this._addListeners();
39
- }
40
-
41
- _init() {
42
- const workspaceEl = document.querySelector('#workspace') as HTMLElement;
43
- if (!workspaceEl) {
44
- throw new Error('element #workspace is missing, plz check!');
45
- }
46
- this.workspaceEl = workspaceEl;
47
- }
48
-
49
- // 初始化监听器
50
- _initResizeObserve() {
51
- const resizeObserver = new ResizeObserver(
52
- throttle(() => {
53
- this.renderBars();
54
- }, 50)
55
- );
56
- resizeObserver.observe(this.workspaceEl);
57
- }
58
-
59
- // 渲染控制条具体位置
60
- renderBars() {
61
- const viewportTransform = this.canvas.viewportTransform;
62
- const [scaleX, , , scaleY, offsetX, offsetY] = viewportTransform || [];
63
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
64
- const wsWidth = workspace.width * scaleX;
65
- const wsHeight = workspace.height * scaleY;
66
- const wsLeft = workspace.left * scaleX;
67
- const wsTop = workspace.top * scaleY;
68
- const { bWidth, bHeight, bPadding } = this.barOpts;
69
- if (!viewportTransform) return;
70
- // 左控制条
71
- const leftBar = this._getBarFromType('left');
72
- leftBar.style.left = `${offsetX + wsLeft - bHeight - bPadding}px`;
73
- leftBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
74
- // 右控制条
75
- const rightBar = this._getBarFromType('right');
76
- rightBar.style.left = `${offsetX + wsLeft + wsWidth + bPadding}px`;
77
- rightBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
78
- // 上控制条
79
- const topBar = this._getBarFromType('top');
80
- topBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
81
- topBar.style.top = `${offsetY + wsTop - bHeight - bPadding}px`;
82
- // 下控制条
83
- const bottomBar = this._getBarFromType('bottom');
84
- bottomBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
85
- bottomBar.style.top = `${offsetY + wsTop + wsHeight + bPadding}px`;
86
- // 监听
87
- if (!this.hasCreatedBar) {
88
- this.hasCreatedBar = true;
89
- this._watchDrag();
90
- }
91
- }
92
-
93
- // 获取或创建控制条
94
- _getBarFromType(type: IPosition) {
95
- let bar = document.querySelector(`#resize-${type}-bar`) as HTMLElement;
96
- if (bar) return bar;
97
- bar = document.createElement('div');
98
- bar.id = `resize-${type}-bar`;
99
- bar.className = 'resize-bar';
100
- if (['left', 'right'].includes(type)) {
101
- bar.classList.add('horizontal');
102
- } else {
103
- bar.classList.add('vertical');
104
- }
105
- this.workspaceEl.appendChild(bar);
106
- return bar;
107
- }
108
-
109
- // 监听拖拽相关事件
110
- _watchDrag() {
111
- const barList = Array.from(
112
- document.getElementsByClassName('resize-bar')
113
- ) as HTMLElement[];
114
- barList.forEach((bar) => {
115
- bar.addEventListener('mousedown', (e: MouseEvent) => {
116
- this.isDragging = true;
117
- this.dragEl = bar;
118
- this.dragEl.classList.add('active');
119
- this.startPoints = {
120
- x: e.clientX,
121
- y: e.clientY,
122
- };
123
- this.barOffset = {
124
- x: bar.offsetLeft,
125
- y: bar.offsetTop,
126
- };
127
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
128
- const { width, height, left, top } = workspace;
129
- this.wsOffset = { width, height, left, top };
130
- });
131
- });
132
- document.addEventListener('mousemove', this.eventHandler.onDragging);
133
- document.addEventListener('mouseup', () => {
134
- if (this.isDragging && this.dragEl) {
135
- this.isDragging = false;
136
- this.dragEl.classList.remove('active');
137
- this.dragEl = null;
138
- this.canvas.defaultCursor = 'default';
139
- }
140
- });
141
- }
142
-
143
- // 拖拽更新控制条及画布
144
- onDragging(e: MouseEvent) {
145
- if (this.isDragging && this.dragEl) {
146
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
147
- const viewportTransform = this.canvas.viewportTransform;
148
- const [scaleX, , , scaleY] = viewportTransform || [];
149
- const deltaX = e.clientX - this.startPoints.x;
150
- const deltaY = e.clientY - this.startPoints.y;
151
- const deltaViewX = deltaX / scaleX;
152
- const deltaViewY = deltaY / scaleY;
153
- const type = this.dragEl.id.split('-')[1];
154
- let tempLength = 0;
155
- switch (type) {
156
- case 'left':
157
- tempLength = Math.round(
158
- this.wsOffset.width - deltaViewX * 2
159
- );
160
- if (tempLength >= this.minSize.width) {
161
- this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
162
- workspace.set(
163
- 'left',
164
- this.wsOffset.left + deltaViewX * 2
165
- );
166
- workspace.set('width', tempLength);
167
- this.editor.syncOriginSizeByUnit(tempLength, undefined);
168
- } else {
169
- workspace.set(
170
- 'left',
171
- this.wsOffset.left +
172
- this.wsOffset.width -
173
- this.minSize.width
174
- );
175
- workspace.set('width', this.minSize.width);
176
- this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
177
- }
178
- break;
179
- case 'right':
180
- tempLength = Math.round(
181
- this.wsOffset.width + deltaViewX * 2
182
- );
183
- if (tempLength >= this.minSize.width) {
184
- this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
185
- workspace.set('width', tempLength);
186
- this.editor.syncOriginSizeByUnit(tempLength, undefined);
187
- } else {
188
- workspace.set('width', this.minSize.width);
189
- this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
190
- }
191
- break;
192
- case 'top':
193
- tempLength = Math.round(
194
- this.wsOffset.height - deltaViewY * 2
195
- );
196
- if (tempLength >= this.minSize.height) {
197
- this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
198
- workspace.set(
199
- 'top',
200
- this.wsOffset.top + deltaViewY * 2
201
- );
202
- workspace.set('height', tempLength);
203
- this.editor.syncOriginSizeByUnit(undefined, tempLength);
204
- } else {
205
- workspace.set(
206
- 'top',
207
- this.wsOffset.top +
208
- this.wsOffset.height -
209
- this.minSize.height
210
- );
211
- workspace.set('height', this.minSize.height);
212
- this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
213
- }
214
- break;
215
- case 'bottom':
216
- tempLength = Math.round(
217
- this.wsOffset.height + deltaViewY * 2
218
- );
219
- if (tempLength >= this.minSize.height) {
220
- this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
221
- workspace.set('height', tempLength);
222
- this.editor.syncOriginSizeByUnit(undefined, tempLength);
223
- } else {
224
- workspace.set('height', this.minSize.height);
225
- this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
226
- }
227
- break;
228
- default:
229
- break;
230
- }
231
-
232
- this.editor.setCenterFromObject(workspace);
233
- workspace.clone((cloned: fabric.Rect) => {
234
- this.canvas.clipPath = cloned;
235
- this.canvas.requestRenderAll();
236
- });
237
- if (['left', 'right'].includes(type)) {
238
- this.canvas.defaultCursor = 'ew-resize';
239
- } else {
240
- this.canvas.defaultCursor = 'ns-resize';
241
- }
242
- let curUnitWidth = workspace.width;
243
- let curUnitHeight = workspace.height;
244
- if (this.editor.getUnit() !== 'px') {
245
- ({ width: curUnitWidth, height: curUnitHeight } = this.editor.getOriginSize());
246
- }
247
- this.editor.emit('sizeChange', { width: curUnitWidth, height: curUnitHeight });
248
- }
249
- }
250
-
251
- // 事件句柄缓存
252
- private eventHandler: Record<string, (...args: any) => void> = {
253
- render: throttle(this.renderBars.bind(this), 50),
254
- onDragging: throttle(this.onDragging.bind(this), 30),
255
- };
256
-
257
- // 监听画布渲染
258
- _addListeners() {
259
- this.canvas.on('after:render', this.eventHandler.render);
260
- }
261
-
262
- // 返回workspace对象
263
- getWorkspase() {
264
- return this.canvas
265
- .getObjects()
266
- .find((item) => item.id === 'workspace') as fabric.Rect;
267
- }
268
-
269
- destroy() {
270
- console.log('pluginDestroy');
271
- }
272
- }
273
-
274
- export default ResizePlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import { throttle } from 'lodash-es';
3
+ import '../assets/style/resizePlugin.css';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPosition = 'left' | 'right' | 'top' | 'bottom';
7
+
8
+ class ResizePlugin implements IPluginTempl {
9
+ static pluginName = 'ResizePlugin';
10
+ static events = [];
11
+ static apis = [];
12
+ workspaceEl!: HTMLElement;
13
+ // 最小画布尺寸
14
+ minSize = { width: 1, height: 1 };
15
+ // 控制条
16
+ barOpts = {
17
+ bWidth: 30, // 宽
18
+ bHeight: 6, // 高
19
+ bPadding: 10, // 离画布边缘的距离
20
+ };
21
+ hasCreatedBar = false;
22
+ isDragging = false;
23
+ dragEl: HTMLElement | null = null;
24
+ startPoints = { x: 0, y: 0 };
25
+ barOffset = { x: 0, y: 0 };
26
+ wsOffset: Record<'left' | 'top' | 'width' | 'height', number> = {
27
+ left: 0,
28
+ top: 0,
29
+ width: 0,
30
+ height: 0,
31
+ };
32
+ constructor(
33
+ public canvas: fabric.Canvas,
34
+ public editor: IEditor
35
+ ) {
36
+ this._init();
37
+ this._initResizeObserve();
38
+ this._addListeners();
39
+ }
40
+
41
+ _init() {
42
+ const workspaceEl = document.querySelector('#workspace') as HTMLElement;
43
+ if (!workspaceEl) {
44
+ throw new Error('element #workspace is missing, plz check!');
45
+ }
46
+ this.workspaceEl = workspaceEl;
47
+ }
48
+
49
+ // 初始化监听器
50
+ _initResizeObserve() {
51
+ const resizeObserver = new ResizeObserver(
52
+ throttle(() => {
53
+ this.renderBars();
54
+ }, 50)
55
+ );
56
+ resizeObserver.observe(this.workspaceEl);
57
+ }
58
+
59
+ // 渲染控制条具体位置
60
+ renderBars() {
61
+ const viewportTransform = this.canvas.viewportTransform;
62
+ const [scaleX, , , scaleY, offsetX, offsetY] = viewportTransform || [];
63
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
64
+ const wsWidth = workspace.width * scaleX;
65
+ const wsHeight = workspace.height * scaleY;
66
+ const wsLeft = workspace.left * scaleX;
67
+ const wsTop = workspace.top * scaleY;
68
+ const { bWidth, bHeight, bPadding } = this.barOpts;
69
+ if (!viewportTransform) return;
70
+ // 左控制条
71
+ const leftBar = this._getBarFromType('left');
72
+ leftBar.style.left = `${offsetX + wsLeft - bHeight - bPadding}px`;
73
+ leftBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
74
+ // 右控制条
75
+ const rightBar = this._getBarFromType('right');
76
+ rightBar.style.left = `${offsetX + wsLeft + wsWidth + bPadding}px`;
77
+ rightBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
78
+ // 上控制条
79
+ const topBar = this._getBarFromType('top');
80
+ topBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
81
+ topBar.style.top = `${offsetY + wsTop - bHeight - bPadding}px`;
82
+ // 下控制条
83
+ const bottomBar = this._getBarFromType('bottom');
84
+ bottomBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
85
+ bottomBar.style.top = `${offsetY + wsTop + wsHeight + bPadding}px`;
86
+ // 监听
87
+ if (!this.hasCreatedBar) {
88
+ this.hasCreatedBar = true;
89
+ this._watchDrag();
90
+ }
91
+ }
92
+
93
+ // 获取或创建控制条
94
+ _getBarFromType(type: IPosition) {
95
+ let bar = document.querySelector(`#resize-${type}-bar`) as HTMLElement;
96
+ if (bar) return bar;
97
+ bar = document.createElement('div');
98
+ bar.id = `resize-${type}-bar`;
99
+ bar.className = 'resize-bar';
100
+ if (['left', 'right'].includes(type)) {
101
+ bar.classList.add('horizontal');
102
+ } else {
103
+ bar.classList.add('vertical');
104
+ }
105
+ this.workspaceEl.appendChild(bar);
106
+ return bar;
107
+ }
108
+
109
+ // 监听拖拽相关事件
110
+ _watchDrag() {
111
+ const barList = Array.from(
112
+ document.getElementsByClassName('resize-bar')
113
+ ) as HTMLElement[];
114
+ barList.forEach((bar) => {
115
+ bar.addEventListener('mousedown', (e: MouseEvent) => {
116
+ this.isDragging = true;
117
+ this.dragEl = bar;
118
+ this.dragEl.classList.add('active');
119
+ this.startPoints = {
120
+ x: e.clientX,
121
+ y: e.clientY,
122
+ };
123
+ this.barOffset = {
124
+ x: bar.offsetLeft,
125
+ y: bar.offsetTop,
126
+ };
127
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
128
+ const { width, height, left, top } = workspace;
129
+ this.wsOffset = { width, height, left, top };
130
+ });
131
+ });
132
+ document.addEventListener('mousemove', this.eventHandler.onDragging);
133
+ document.addEventListener('mouseup', () => {
134
+ if (this.isDragging && this.dragEl) {
135
+ this.isDragging = false;
136
+ this.dragEl.classList.remove('active');
137
+ this.dragEl = null;
138
+ this.canvas.defaultCursor = 'default';
139
+ }
140
+ });
141
+ }
142
+
143
+ // 拖拽更新控制条及画布
144
+ onDragging(e: MouseEvent) {
145
+ if (this.isDragging && this.dragEl) {
146
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
147
+ const viewportTransform = this.canvas.viewportTransform;
148
+ const [scaleX, , , scaleY] = viewportTransform || [];
149
+ const deltaX = e.clientX - this.startPoints.x;
150
+ const deltaY = e.clientY - this.startPoints.y;
151
+ const deltaViewX = deltaX / scaleX;
152
+ const deltaViewY = deltaY / scaleY;
153
+ const type = this.dragEl.id.split('-')[1];
154
+ let tempLength = 0;
155
+ switch (type) {
156
+ case 'left':
157
+ tempLength = Math.round(
158
+ this.wsOffset.width - deltaViewX * 2
159
+ );
160
+ if (tempLength >= this.minSize.width) {
161
+ this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
162
+ workspace.set(
163
+ 'left',
164
+ this.wsOffset.left + deltaViewX * 2
165
+ );
166
+ workspace.set('width', tempLength);
167
+ this.editor.syncOriginSizeByUnit(tempLength, undefined);
168
+ } else {
169
+ workspace.set(
170
+ 'left',
171
+ this.wsOffset.left +
172
+ this.wsOffset.width -
173
+ this.minSize.width
174
+ );
175
+ workspace.set('width', this.minSize.width);
176
+ this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
177
+ }
178
+ break;
179
+ case 'right':
180
+ tempLength = Math.round(
181
+ this.wsOffset.width + deltaViewX * 2
182
+ );
183
+ if (tempLength >= this.minSize.width) {
184
+ this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
185
+ workspace.set('width', tempLength);
186
+ this.editor.syncOriginSizeByUnit(tempLength, undefined);
187
+ } else {
188
+ workspace.set('width', this.minSize.width);
189
+ this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
190
+ }
191
+ break;
192
+ case 'top':
193
+ tempLength = Math.round(
194
+ this.wsOffset.height - deltaViewY * 2
195
+ );
196
+ if (tempLength >= this.minSize.height) {
197
+ this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
198
+ workspace.set(
199
+ 'top',
200
+ this.wsOffset.top + deltaViewY * 2
201
+ );
202
+ workspace.set('height', tempLength);
203
+ this.editor.syncOriginSizeByUnit(undefined, tempLength);
204
+ } else {
205
+ workspace.set(
206
+ 'top',
207
+ this.wsOffset.top +
208
+ this.wsOffset.height -
209
+ this.minSize.height
210
+ );
211
+ workspace.set('height', this.minSize.height);
212
+ this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
213
+ }
214
+ break;
215
+ case 'bottom':
216
+ tempLength = Math.round(
217
+ this.wsOffset.height + deltaViewY * 2
218
+ );
219
+ if (tempLength >= this.minSize.height) {
220
+ this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
221
+ workspace.set('height', tempLength);
222
+ this.editor.syncOriginSizeByUnit(undefined, tempLength);
223
+ } else {
224
+ workspace.set('height', this.minSize.height);
225
+ this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
226
+ }
227
+ break;
228
+ default:
229
+ break;
230
+ }
231
+
232
+ this.editor.setCenterFromObject(workspace);
233
+ workspace.clone((cloned: fabric.Rect) => {
234
+ this.canvas.clipPath = cloned;
235
+ this.canvas.requestRenderAll();
236
+ });
237
+ if (['left', 'right'].includes(type)) {
238
+ this.canvas.defaultCursor = 'ew-resize';
239
+ } else {
240
+ this.canvas.defaultCursor = 'ns-resize';
241
+ }
242
+ let curUnitWidth = workspace.width;
243
+ let curUnitHeight = workspace.height;
244
+ if (this.editor.getUnit() !== 'px') {
245
+ ({ width: curUnitWidth, height: curUnitHeight } = this.editor.getOriginSize());
246
+ }
247
+ this.editor.emit('sizeChange', {
248
+ width: this.editor.getSizeByUnit(curUnitWidth),
249
+ height: this.editor.getSizeByUnit(curUnitHeight),
250
+ unit: this.editor.getUnit()
251
+ });
252
+ }
253
+ }
254
+
255
+ // 事件句柄缓存
256
+ private eventHandler: Record<string, (...args: any) => void> = {
257
+ render: throttle(this.renderBars.bind(this), 50),
258
+ onDragging: throttle(this.onDragging.bind(this), 30),
259
+ };
260
+
261
+ // 监听画布渲染
262
+ _addListeners() {
263
+ this.canvas.on('after:render', this.eventHandler.render);
264
+ }
265
+
266
+ // 返回workspace对象
267
+ getWorkspase() {
268
+ return this.canvas
269
+ .getObjects()
270
+ .find((item) => item.id === 'workspace') as fabric.Rect;
271
+ }
272
+
273
+ destroy() {
274
+ console.log('pluginDestroy');
275
+ }
276
+ }
277
+
278
+ export default ResizePlugin;