@hprint/plugins 0.0.1-alpha.2 → 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 (53) hide show
  1. package/dist/index.js +17 -17
  2. package/dist/index.mjs +1071 -1057
  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/GroupAlignPlugin.d.ts.map +1 -1
  6. package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
  7. package/dist/src/plugins/QrCodePlugin.d.ts +5 -0
  8. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  9. package/package.json +3 -3
  10. package/src/assets/style/resizePlugin.css +27 -27
  11. package/src/objects/Arrow.js +47 -47
  12. package/src/objects/ThinTailArrow.js +50 -50
  13. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
  14. package/src/plugins/BarCodePlugin.ts +2 -2
  15. package/src/plugins/ControlsPlugin.ts +251 -251
  16. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  17. package/src/plugins/CopyPlugin.ts +255 -255
  18. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  19. package/src/plugins/DrawLinePlugin.ts +162 -162
  20. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  21. package/src/plugins/DringPlugin.ts +125 -125
  22. package/src/plugins/FlipPlugin.ts +59 -59
  23. package/src/plugins/FontPlugin.ts +165 -165
  24. package/src/plugins/FreeDrawPlugin.ts +49 -49
  25. package/src/plugins/GroupAlignPlugin.ts +365 -365
  26. package/src/plugins/GroupPlugin.ts +82 -82
  27. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  28. package/src/plugins/HistoryPlugin.ts +181 -181
  29. package/src/plugins/ImageStroke.ts +121 -121
  30. package/src/plugins/LayerPlugin.ts +108 -108
  31. package/src/plugins/LockPlugin.ts +242 -240
  32. package/src/plugins/MaskPlugin.ts +155 -155
  33. package/src/plugins/MaterialPlugin.ts +224 -224
  34. package/src/plugins/MiddleMousePlugin.ts +45 -45
  35. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  36. package/src/plugins/PathTextPlugin.ts +89 -89
  37. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  38. package/src/plugins/PrintPlugin.ts +81 -81
  39. package/src/plugins/PsdPlugin.ts +52 -52
  40. package/src/plugins/QrCodePlugin.ts +322 -329
  41. package/src/plugins/ResizePlugin.ts +278 -278
  42. package/src/plugins/RulerPlugin.ts +78 -78
  43. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  44. package/src/plugins/UnitPlugin.ts +326 -326
  45. package/src/plugins/WaterMarkPlugin.ts +257 -257
  46. package/src/types/eventType.ts +11 -11
  47. package/src/utils/psd.js +432 -432
  48. package/src/utils/ruler/guideline.ts +145 -145
  49. package/src/utils/ruler/index.ts +91 -91
  50. package/src/utils/ruler/ruler.ts +924 -924
  51. package/src/utils/ruler/utils.ts +162 -162
  52. package/tsconfig.json +10 -10
  53. package/vite.config.ts +29 -29
@@ -1,326 +1,326 @@
1
- import type { IEditor, IPluginTempl } from '@hprint/core';
2
- import { LengthConvert } from '@hprint/shared';
3
- import { syncMmFromObject } from '../utils/units';
4
- import { fabric } from '@hprint/core';
5
- import { throttle } from 'lodash-es';
6
-
7
- type IPlugin = Pick<
8
- UnitPlugin,
9
- | 'getUnit'
10
- | 'setUnit'
11
- | 'getSizeByUnit'
12
- | 'setSizeByUnit'
13
- | 'getOriginSize'
14
- | 'syncOriginSizeByUnit'
15
- | 'applyObjectPx'
16
- | 'applyObjectMm'
17
- | 'applyObjectInch'
18
- | 'applyObjectByUnit'
19
- >;
20
-
21
- type TUnit = 'px' | 'mm' | 'inch';
22
-
23
- declare module '@hprint/core' {
24
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
25
- interface IEditor extends IPlugin { }
26
- }
27
-
28
- class UnitPlugin implements IPluginTempl {
29
- static pluginName = 'UnitPlugin';
30
- // static events = ['sizeChange'];
31
- static apis = [
32
- 'getUnit',
33
- 'setUnit',
34
- 'getSizeByUnit',
35
- 'setSizeByUnit',
36
- 'getOriginSize',
37
- 'getPrecision',
38
- 'syncOriginSizeByUnit',
39
- 'applyObjectPx',
40
- 'applyObjectMm',
41
- 'applyObjectInch',
42
- 'applyObjectByUnit',
43
- ];
44
- unit: TUnit = 'px';
45
- precision?: number;
46
- _originSize: Record<string, { width: number, height: number }> = { mm: { width: 0, height: 0 } };
47
- constructor(
48
- public canvas: fabric.Canvas,
49
- public editor: IEditor,
50
- options?: {
51
- unit?: TUnit;
52
- precision?: number;
53
- }
54
- ) {
55
- this.init(options);
56
- this._bindEvents();
57
- }
58
-
59
- hookSaveBefore() {
60
- return new Promise((resolve) => {
61
- resolve(true);
62
- });
63
- }
64
-
65
- hookSaveAfter() {
66
- return new Promise((resolve) => {
67
- resolve(true);
68
- });
69
- }
70
-
71
- init(options?: { unit?: TUnit; precision?: number }) {
72
- if (options?.unit) this.unit = options?.unit;
73
- if (options?.precision) this.precision = options?.precision;
74
- }
75
-
76
- setUnit(unit: TUnit) {
77
- if (this.unit === unit) return;
78
- this.unit = unit;
79
- this.editor.emit('unitChange', unit);
80
- }
81
-
82
- getUnit() {
83
- return this.unit;
84
- }
85
-
86
- getPrecision() {
87
- return this.precision;
88
- }
89
-
90
- getSizeByUnit(pxValue: number, unit?: TUnit, dpi?: number) {
91
- const targetUnit = unit ?? this.editor.getUnit();
92
- if (targetUnit === 'px') return pxValue;
93
- if (targetUnit === 'mm') return LengthConvert.pxToMm(pxValue, dpi, { direct: true });
94
- if (targetUnit === 'inch') {
95
- const mm = LengthConvert.pxToMm(pxValue, dpi, { direct: true });
96
- return mm / LengthConvert.CONSTANTS.INCH_TO_MM;
97
- }
98
- }
99
-
100
- _toPrecisionValue(value: number) {
101
- if (this.precision === undefined) return value;
102
- const factor = Math.pow(10, this.precision);
103
- return Math.round(value * factor) / factor;
104
- }
105
-
106
- _formatOriginUnitValues<T extends { [k: string]: number | undefined }>(vals: T): T {
107
- const result: Record<string, number | undefined> = {};
108
- for (const key in vals) {
109
- const v = vals[key];
110
- result[key] = v === undefined ? undefined : this._toPrecisionValue(v);
111
- }
112
- return result as T;
113
- }
114
-
115
- applyObjectPx(
116
- obj: fabric.Object,
117
- opts: {
118
- left?: number;
119
- top?: number;
120
- width?: number;
121
- height?: number;
122
- strokeWidth?: number;
123
- fontSize?: number;
124
- }
125
- ) {
126
- const { left, top, width, height, strokeWidth, fontSize } = opts;
127
- // fontSize必须在width之前,否则可能宽度设置不正确
128
- if (fontSize !== undefined) obj.set('fontSize', fontSize);
129
- if (strokeWidth !== undefined) obj.set('strokeWidth', strokeWidth);
130
- if (width !== undefined) obj.set('width', width);
131
- if (height !== undefined) obj.set('height', height);
132
- if (left !== undefined) obj.set('left', left);
133
- if (top !== undefined) obj.set('top', top);
134
- }
135
-
136
- applyObjectMm(
137
- obj: fabric.Object,
138
- mm: {
139
- left?: number;
140
- top?: number;
141
- width?: number;
142
- height?: number;
143
- strokeWidth?: number;
144
- fontSize?: number;
145
- },
146
- dpi?: number
147
- ) {
148
- const toPx = (v: number | undefined) =>
149
- v === undefined ? undefined : LengthConvert.mmToPx(v, dpi, { direct: true });
150
- this.applyObjectPx(obj, {
151
- left: toPx(mm.left),
152
- top: toPx(mm.top),
153
- width: toPx(mm.width),
154
- height: toPx(mm.height),
155
- strokeWidth: toPx(mm.strokeWidth),
156
- fontSize: toPx(mm.fontSize),
157
- });
158
- const formatted = this._formatOriginUnitValues(mm);
159
- (obj as any)._originSize = { ...(obj as any)._originSize, mm: { ...formatted } };
160
- }
161
-
162
- applyObjectInch(
163
- obj: fabric.Object,
164
- inch: {
165
- left?: number;
166
- top?: number;
167
- width?: number;
168
- height?: number;
169
- strokeWidth?: number;
170
- fontSize?: number;
171
- },
172
- dpi?: number
173
- ) {
174
- const toMm = (v: number | undefined) =>
175
- v === undefined ? undefined : v * LengthConvert.CONSTANTS.INCH_TO_MM;
176
- this.applyObjectMm(
177
- obj,
178
- {
179
- left: toMm(inch.left),
180
- top: toMm(inch.top),
181
- width: toMm(inch.width),
182
- height: toMm(inch.height),
183
- strokeWidth: toMm(inch.strokeWidth),
184
- fontSize: toMm(inch.fontSize),
185
- },
186
- dpi
187
- );
188
- const formatted = this._formatOriginUnitValues(inch);
189
- (obj as any)._originSize = { ...(obj as any)._originSize, inch: { ...formatted } };
190
- }
191
-
192
- applyObjectByUnit(
193
- obj: fabric.Object,
194
- opts: {
195
- left?: number;
196
- top?: number;
197
- width?: number;
198
- height?: number;
199
- strokeWidth?: number;
200
- fontSize?: number;
201
- },
202
- dpi?: number
203
- ) {
204
- const unit = this.getUnit();
205
- if (unit === 'mm') return this.applyObjectMm(obj, opts, dpi);
206
- if (unit === 'inch') return this.applyObjectInch(obj, opts, dpi);
207
- return this.applyObjectPx(obj, opts);
208
- }
209
-
210
- // mm
211
- setSizeMm(widthMm: number, heightMm: number, dpi?: number) {
212
- const width = LengthConvert.mmToPx(widthMm, dpi, { direct: true });
213
- const height = LengthConvert.mmToPx(heightMm, dpi, { direct: true });
214
- this.editor.setSize(width, height);
215
- this._syncOriginSize(widthMm, heightMm);
216
- }
217
-
218
- setSizeByUnit(width: number, height: number, options: { dpi: number, slient?: boolean }) {
219
- const unit = (this.editor as any).getUnit?.() || 'px';
220
- if (unit === 'mm') {
221
- return this.setSizeMm(width, height, options?.dpi);
222
- }
223
- if (unit === 'inch') {
224
- this._syncOriginSize(width, height);
225
- const wmm = width * LengthConvert.CONSTANTS.INCH_TO_MM;
226
- const hmm = height * LengthConvert.CONSTANTS.INCH_TO_MM;
227
- return this.setSizeMm(wmm, hmm, options?.dpi);
228
- }
229
- this.editor.setSize(width, height, { slient: options.slient });
230
- }
231
-
232
- getOriginSize(dpi?: number) {
233
- const unit = this.getUnit();
234
- const origin = (this.canvas as any)._originSize || {};
235
- const originMm: { width?: number; height?: number } = origin.mm || {};
236
-
237
- if (unit === 'px') {
238
- return {
239
- width: this.canvas.getWidth(),
240
- height: this.canvas.getHeight(),
241
- };
242
- }
243
- const ensureMmWidth = originMm.width !== undefined
244
- ? originMm.width
245
- : LengthConvert.pxToMm(this.canvas.getWidth(), dpi, { direct: true });
246
- const ensureMmHeight = originMm.height !== undefined
247
- ? originMm.height
248
- : LengthConvert.pxToMm(this.canvas.getHeight(), dpi, { direct: true });
249
-
250
- if (unit === 'mm') {
251
- return this._originSize.mm ?? {
252
- width: ensureMmWidth,
253
- height: ensureMmHeight,
254
- };
255
- }
256
-
257
- // inch
258
- return this._originSize.inch ?? {
259
- width: ensureMmWidth / LengthConvert.CONSTANTS.INCH_TO_MM,
260
- height: ensureMmHeight / LengthConvert.CONSTANTS.INCH_TO_MM,
261
- };
262
- }
263
-
264
- _syncOriginSize(width?: number, height?: number) {
265
- const unit = this.getUnit();
266
- if (unit === 'px') return;
267
- this._originSize[unit] = this._originSize[unit] || { width: 0, height: 0 };
268
- if (width !== undefined) this._originSize[unit].width = this._toPrecisionValue(width);
269
- if (height !== undefined) this._originSize[unit].height = this._toPrecisionValue(height);
270
- }
271
-
272
- syncOriginSizeByUnit(width?: number, height?: number) {
273
- const unit = this.getUnit();
274
- if (unit === 'mm') {
275
- if (width !== undefined) {
276
- const v = LengthConvert.pxToMm(width, undefined, { direct: true });
277
- this._originSize[unit].width = this._toPrecisionValue(v);
278
- }
279
- if (height !== undefined) {
280
- const v = LengthConvert.pxToMm(height, undefined, { direct: true });
281
- this._originSize[unit].height = this._toPrecisionValue(v);
282
- }
283
- }
284
- }
285
-
286
- _bindEvents() {
287
- const throttledSync = throttle((obj: fabric.Object) => {
288
- syncMmFromObject(obj, undefined, this.precision);
289
- }, 30);
290
-
291
- this.canvas.on('object:modified', (e: any) => {
292
- const target = e.target as fabric.Object | undefined;
293
- if (target) syncMmFromObject(target, undefined, this.precision);
294
- });
295
-
296
- this.canvas.on('object:moving', (e: any) => {
297
- const target = e.target as fabric.Object | undefined;
298
- if (target) throttledSync(target);
299
- });
300
-
301
- this.canvas.on('object:scaling', (e: any) => {
302
- const target = e.target as fabric.Object | undefined;
303
- if (target) throttledSync(target);
304
- });
305
-
306
- this.canvas.on('object:rotating', (e: any) => {
307
- const target = e.target as fabric.Object | undefined;
308
- if (target) throttledSync(target);
309
- });
310
-
311
- this.editor.on?.('sizeChange', (event: { width: number, height: number }) => {
312
- const unit = this.getUnit();
313
- if (unit === 'px') return;
314
- if (unit === 'mm') {
315
- this._syncOriginSize(event.width, event.height);
316
- return
317
- }
318
- });
319
- }
320
-
321
- destroy() {
322
- console.log('pluginDestroy');
323
- }
324
- }
325
-
326
- export default UnitPlugin;
1
+ import type { IEditor, IPluginTempl } from '@hprint/core';
2
+ import { LengthConvert } from '@hprint/shared';
3
+ import { syncMmFromObject } from '../utils/units';
4
+ import { fabric } from '@hprint/core';
5
+ import { throttle } from 'lodash-es';
6
+
7
+ type IPlugin = Pick<
8
+ UnitPlugin,
9
+ | 'getUnit'
10
+ | 'setUnit'
11
+ | 'getSizeByUnit'
12
+ | 'setSizeByUnit'
13
+ | 'getOriginSize'
14
+ | 'syncOriginSizeByUnit'
15
+ | 'applyObjectPx'
16
+ | 'applyObjectMm'
17
+ | 'applyObjectInch'
18
+ | 'applyObjectByUnit'
19
+ >;
20
+
21
+ type TUnit = 'px' | 'mm' | 'inch';
22
+
23
+ declare module '@hprint/core' {
24
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
25
+ interface IEditor extends IPlugin { }
26
+ }
27
+
28
+ class UnitPlugin implements IPluginTempl {
29
+ static pluginName = 'UnitPlugin';
30
+ // static events = ['sizeChange'];
31
+ static apis = [
32
+ 'getUnit',
33
+ 'setUnit',
34
+ 'getSizeByUnit',
35
+ 'setSizeByUnit',
36
+ 'getOriginSize',
37
+ 'getPrecision',
38
+ 'syncOriginSizeByUnit',
39
+ 'applyObjectPx',
40
+ 'applyObjectMm',
41
+ 'applyObjectInch',
42
+ 'applyObjectByUnit',
43
+ ];
44
+ unit: TUnit = 'px';
45
+ precision?: number;
46
+ _originSize: Record<string, { width: number, height: number }> = { mm: { width: 0, height: 0 } };
47
+ constructor(
48
+ public canvas: fabric.Canvas,
49
+ public editor: IEditor,
50
+ options?: {
51
+ unit?: TUnit;
52
+ precision?: number;
53
+ }
54
+ ) {
55
+ this.init(options);
56
+ this._bindEvents();
57
+ }
58
+
59
+ hookSaveBefore() {
60
+ return new Promise((resolve) => {
61
+ resolve(true);
62
+ });
63
+ }
64
+
65
+ hookSaveAfter() {
66
+ return new Promise((resolve) => {
67
+ resolve(true);
68
+ });
69
+ }
70
+
71
+ init(options?: { unit?: TUnit; precision?: number }) {
72
+ if (options?.unit) this.unit = options?.unit;
73
+ if (options?.precision) this.precision = options?.precision;
74
+ }
75
+
76
+ setUnit(unit: TUnit) {
77
+ if (this.unit === unit) return;
78
+ this.unit = unit;
79
+ this.editor.emit('unitChange', unit);
80
+ }
81
+
82
+ getUnit() {
83
+ return this.unit;
84
+ }
85
+
86
+ getPrecision() {
87
+ return this.precision;
88
+ }
89
+
90
+ getSizeByUnit(pxValue: number, unit?: TUnit, dpi?: number) {
91
+ const targetUnit = unit ?? this.editor.getUnit();
92
+ if (targetUnit === 'px') return pxValue;
93
+ if (targetUnit === 'mm') return LengthConvert.pxToMm(pxValue, dpi, { direct: true });
94
+ if (targetUnit === 'inch') {
95
+ const mm = LengthConvert.pxToMm(pxValue, dpi, { direct: true });
96
+ return mm / LengthConvert.CONSTANTS.INCH_TO_MM;
97
+ }
98
+ }
99
+
100
+ _toPrecisionValue(value: number) {
101
+ if (this.precision === undefined) return value;
102
+ const factor = Math.pow(10, this.precision);
103
+ return Math.round(value * factor) / factor;
104
+ }
105
+
106
+ _formatOriginUnitValues<T extends { [k: string]: number | undefined }>(vals: T): T {
107
+ const result: Record<string, number | undefined> = {};
108
+ for (const key in vals) {
109
+ const v = vals[key];
110
+ result[key] = v === undefined ? undefined : this._toPrecisionValue(v);
111
+ }
112
+ return result as T;
113
+ }
114
+
115
+ applyObjectPx(
116
+ obj: fabric.Object,
117
+ opts: {
118
+ left?: number;
119
+ top?: number;
120
+ width?: number;
121
+ height?: number;
122
+ strokeWidth?: number;
123
+ fontSize?: number;
124
+ }
125
+ ) {
126
+ const { left, top, width, height, strokeWidth, fontSize } = opts;
127
+ // fontSize必须在width之前,否则可能宽度设置不正确
128
+ if (fontSize !== undefined) obj.set('fontSize', fontSize);
129
+ if (strokeWidth !== undefined) obj.set('strokeWidth', strokeWidth);
130
+ if (width !== undefined) obj.set('width', width);
131
+ if (height !== undefined) obj.set('height', height);
132
+ if (left !== undefined) obj.set('left', left);
133
+ if (top !== undefined) obj.set('top', top);
134
+ }
135
+
136
+ applyObjectMm(
137
+ obj: fabric.Object,
138
+ mm: {
139
+ left?: number;
140
+ top?: number;
141
+ width?: number;
142
+ height?: number;
143
+ strokeWidth?: number;
144
+ fontSize?: number;
145
+ },
146
+ dpi?: number
147
+ ) {
148
+ const toPx = (v: number | undefined) =>
149
+ v === undefined ? undefined : LengthConvert.mmToPx(v, dpi, { direct: true });
150
+ this.applyObjectPx(obj, {
151
+ left: toPx(mm.left),
152
+ top: toPx(mm.top),
153
+ width: toPx(mm.width),
154
+ height: toPx(mm.height),
155
+ strokeWidth: toPx(mm.strokeWidth),
156
+ fontSize: toPx(mm.fontSize),
157
+ });
158
+ const formatted = this._formatOriginUnitValues(mm);
159
+ (obj as any)._originSize = { ...(obj as any)._originSize, mm: { ...formatted } };
160
+ }
161
+
162
+ applyObjectInch(
163
+ obj: fabric.Object,
164
+ inch: {
165
+ left?: number;
166
+ top?: number;
167
+ width?: number;
168
+ height?: number;
169
+ strokeWidth?: number;
170
+ fontSize?: number;
171
+ },
172
+ dpi?: number
173
+ ) {
174
+ const toMm = (v: number | undefined) =>
175
+ v === undefined ? undefined : v * LengthConvert.CONSTANTS.INCH_TO_MM;
176
+ this.applyObjectMm(
177
+ obj,
178
+ {
179
+ left: toMm(inch.left),
180
+ top: toMm(inch.top),
181
+ width: toMm(inch.width),
182
+ height: toMm(inch.height),
183
+ strokeWidth: toMm(inch.strokeWidth),
184
+ fontSize: toMm(inch.fontSize),
185
+ },
186
+ dpi
187
+ );
188
+ const formatted = this._formatOriginUnitValues(inch);
189
+ (obj as any)._originSize = { ...(obj as any)._originSize, inch: { ...formatted } };
190
+ }
191
+
192
+ applyObjectByUnit(
193
+ obj: fabric.Object,
194
+ opts: {
195
+ left?: number;
196
+ top?: number;
197
+ width?: number;
198
+ height?: number;
199
+ strokeWidth?: number;
200
+ fontSize?: number;
201
+ },
202
+ dpi?: number
203
+ ) {
204
+ const unit = this.getUnit();
205
+ if (unit === 'mm') return this.applyObjectMm(obj, opts, dpi);
206
+ if (unit === 'inch') return this.applyObjectInch(obj, opts, dpi);
207
+ return this.applyObjectPx(obj, opts);
208
+ }
209
+
210
+ // mm
211
+ setSizeMm(widthMm: number, heightMm: number, dpi?: number) {
212
+ const width = LengthConvert.mmToPx(widthMm, dpi, { direct: true });
213
+ const height = LengthConvert.mmToPx(heightMm, dpi, { direct: true });
214
+ this.editor.setSize(width, height);
215
+ this._syncOriginSize(widthMm, heightMm);
216
+ }
217
+
218
+ setSizeByUnit(width: number, height: number, options: { dpi: number, slient?: boolean }) {
219
+ const unit = (this.editor as any).getUnit?.() || 'px';
220
+ if (unit === 'mm') {
221
+ return this.setSizeMm(width, height, options?.dpi);
222
+ }
223
+ if (unit === 'inch') {
224
+ this._syncOriginSize(width, height);
225
+ const wmm = width * LengthConvert.CONSTANTS.INCH_TO_MM;
226
+ const hmm = height * LengthConvert.CONSTANTS.INCH_TO_MM;
227
+ return this.setSizeMm(wmm, hmm, options?.dpi);
228
+ }
229
+ this.editor.setSize(width, height, { slient: options.slient });
230
+ }
231
+
232
+ getOriginSize(dpi?: number) {
233
+ const unit = this.getUnit();
234
+ const origin = (this.canvas as any)._originSize || {};
235
+ const originMm: { width?: number; height?: number } = origin.mm || {};
236
+
237
+ if (unit === 'px') {
238
+ return {
239
+ width: this.canvas.getWidth(),
240
+ height: this.canvas.getHeight(),
241
+ };
242
+ }
243
+ const ensureMmWidth = originMm.width !== undefined
244
+ ? originMm.width
245
+ : LengthConvert.pxToMm(this.canvas.getWidth(), dpi, { direct: true });
246
+ const ensureMmHeight = originMm.height !== undefined
247
+ ? originMm.height
248
+ : LengthConvert.pxToMm(this.canvas.getHeight(), dpi, { direct: true });
249
+
250
+ if (unit === 'mm') {
251
+ return this._originSize.mm ?? {
252
+ width: ensureMmWidth,
253
+ height: ensureMmHeight,
254
+ };
255
+ }
256
+
257
+ // inch
258
+ return this._originSize.inch ?? {
259
+ width: ensureMmWidth / LengthConvert.CONSTANTS.INCH_TO_MM,
260
+ height: ensureMmHeight / LengthConvert.CONSTANTS.INCH_TO_MM,
261
+ };
262
+ }
263
+
264
+ _syncOriginSize(width?: number, height?: number) {
265
+ const unit = this.getUnit();
266
+ if (unit === 'px') return;
267
+ this._originSize[unit] = this._originSize[unit] || { width: 0, height: 0 };
268
+ if (width !== undefined) this._originSize[unit].width = this._toPrecisionValue(width);
269
+ if (height !== undefined) this._originSize[unit].height = this._toPrecisionValue(height);
270
+ }
271
+
272
+ syncOriginSizeByUnit(width?: number, height?: number) {
273
+ const unit = this.getUnit();
274
+ if (unit === 'mm') {
275
+ if (width !== undefined) {
276
+ const v = LengthConvert.pxToMm(width, undefined, { direct: true });
277
+ this._originSize[unit].width = this._toPrecisionValue(v);
278
+ }
279
+ if (height !== undefined) {
280
+ const v = LengthConvert.pxToMm(height, undefined, { direct: true });
281
+ this._originSize[unit].height = this._toPrecisionValue(v);
282
+ }
283
+ }
284
+ }
285
+
286
+ _bindEvents() {
287
+ const throttledSync = throttle((obj: fabric.Object) => {
288
+ syncMmFromObject(obj, undefined, this.precision);
289
+ }, 30);
290
+
291
+ this.canvas.on('object:modified', (e: any) => {
292
+ const target = e.target as fabric.Object | undefined;
293
+ if (target) syncMmFromObject(target, undefined, this.precision);
294
+ });
295
+
296
+ this.canvas.on('object:moving', (e: any) => {
297
+ const target = e.target as fabric.Object | undefined;
298
+ if (target) throttledSync(target);
299
+ });
300
+
301
+ this.canvas.on('object:scaling', (e: any) => {
302
+ const target = e.target as fabric.Object | undefined;
303
+ if (target) throttledSync(target);
304
+ });
305
+
306
+ this.canvas.on('object:rotating', (e: any) => {
307
+ const target = e.target as fabric.Object | undefined;
308
+ if (target) throttledSync(target);
309
+ });
310
+
311
+ this.editor.on?.('sizeChange', (event: { width: number, height: number }) => {
312
+ const unit = this.getUnit();
313
+ if (unit === 'px') return;
314
+ if (unit === 'mm') {
315
+ this._syncOriginSize(event.width, event.height);
316
+ return
317
+ }
318
+ });
319
+ }
320
+
321
+ destroy() {
322
+ console.log('pluginDestroy');
323
+ }
324
+ }
325
+
326
+ export default UnitPlugin;