@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.
- package/dist/index.js +17 -17
- package/dist/index.mjs +1071 -1057
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
- package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
- package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
- package/dist/src/plugins/QrCodePlugin.d.ts +5 -0
- package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/assets/style/resizePlugin.css +27 -27
- package/src/objects/Arrow.js +47 -47
- package/src/objects/ThinTailArrow.js +50 -50
- package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
- package/src/plugins/BarCodePlugin.ts +2 -2
- package/src/plugins/ControlsPlugin.ts +251 -251
- package/src/plugins/ControlsRotatePlugin.ts +111 -111
- package/src/plugins/CopyPlugin.ts +255 -255
- package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
- package/src/plugins/DrawLinePlugin.ts +162 -162
- package/src/plugins/DrawPolygonPlugin.ts +205 -205
- package/src/plugins/DringPlugin.ts +125 -125
- package/src/plugins/FlipPlugin.ts +59 -59
- package/src/plugins/FontPlugin.ts +165 -165
- package/src/plugins/FreeDrawPlugin.ts +49 -49
- package/src/plugins/GroupAlignPlugin.ts +365 -365
- package/src/plugins/GroupPlugin.ts +82 -82
- package/src/plugins/GroupTextEditorPlugin.ts +198 -198
- package/src/plugins/HistoryPlugin.ts +181 -181
- package/src/plugins/ImageStroke.ts +121 -121
- package/src/plugins/LayerPlugin.ts +108 -108
- package/src/plugins/LockPlugin.ts +242 -240
- package/src/plugins/MaskPlugin.ts +155 -155
- package/src/plugins/MaterialPlugin.ts +224 -224
- package/src/plugins/MiddleMousePlugin.ts +45 -45
- package/src/plugins/MoveHotKeyPlugin.ts +46 -46
- package/src/plugins/PathTextPlugin.ts +89 -89
- package/src/plugins/PolygonModifyPlugin.ts +224 -224
- package/src/plugins/PrintPlugin.ts +81 -81
- package/src/plugins/PsdPlugin.ts +52 -52
- package/src/plugins/QrCodePlugin.ts +322 -329
- package/src/plugins/ResizePlugin.ts +278 -278
- package/src/plugins/RulerPlugin.ts +78 -78
- package/src/plugins/SimpleClipImagePlugin.ts +244 -244
- package/src/plugins/UnitPlugin.ts +326 -326
- package/src/plugins/WaterMarkPlugin.ts +257 -257
- package/src/types/eventType.ts +11 -11
- package/src/utils/psd.js +432 -432
- package/src/utils/ruler/guideline.ts +145 -145
- package/src/utils/ruler/index.ts +91 -91
- package/src/utils/ruler/ruler.ts +924 -924
- package/src/utils/ruler/utils.ts +162 -162
- package/tsconfig.json +10 -10
- package/vite.config.ts +29 -29
|
@@ -1,329 +1,322 @@
|
|
|
1
|
-
import { fabric } from '@hprint/core';
|
|
2
|
-
import bwipjs from 'bwip-js';
|
|
3
|
-
import { utils } from '@hprint/shared';
|
|
4
|
-
import { getUnit, processOptions, formatOriginValues } from '../utils/units';
|
|
5
|
-
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
6
|
-
|
|
7
|
-
type IPlugin = Pick<QrCodePlugin, 'addQrCode' | 'setQrCode'>;
|
|
8
|
-
|
|
9
|
-
declare module '@hprint/core' {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
11
|
-
interface IEditor extends IPlugin { }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
:
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
destroy() {
|
|
325
|
-
console.log('pluginDestroy');
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
export default QrCodePlugin;
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import bwipjs from 'bwip-js';
|
|
3
|
+
import { utils } from '@hprint/shared';
|
|
4
|
+
import { getUnit, processOptions, formatOriginValues } from '../utils/units';
|
|
5
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<QrCodePlugin, 'addQrCode' | 'setQrCode'>;
|
|
8
|
+
|
|
9
|
+
declare module '@hprint/core' {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
11
|
+
interface IEditor extends IPlugin { }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class QrCodePlugin implements IPluginTempl {
|
|
15
|
+
static pluginName = 'QrCodePlugin';
|
|
16
|
+
static apis = ['addQrCode', 'setQrCode'];
|
|
17
|
+
constructor(
|
|
18
|
+
public canvas: fabric.Canvas,
|
|
19
|
+
public editor: IEditor
|
|
20
|
+
) { }
|
|
21
|
+
|
|
22
|
+
async hookTransform(object: any) {
|
|
23
|
+
if (object.extensionType === 'qrcode') {
|
|
24
|
+
const paramsOption = this._paramsToOption(object.extension);
|
|
25
|
+
const { url, width, height } = await this._getQrCodeResult(paramsOption);
|
|
26
|
+
object.src = url;
|
|
27
|
+
|
|
28
|
+
// 修复 base64 生成后,没有拉伸到容器大小的问题
|
|
29
|
+
if (width > 0 && height > 0) {
|
|
30
|
+
const oldWidth = object.width || 0;
|
|
31
|
+
const oldHeight = object.height || 0;
|
|
32
|
+
const oldScaleX = object.scaleX || 1;
|
|
33
|
+
const oldScaleY = object.scaleY || 1;
|
|
34
|
+
|
|
35
|
+
const displayWidth = oldWidth * oldScaleX;
|
|
36
|
+
const displayHeight = oldHeight * oldScaleY;
|
|
37
|
+
|
|
38
|
+
if (displayWidth > 0 && displayHeight > 0) {
|
|
39
|
+
object.width = width;
|
|
40
|
+
object.height = height;
|
|
41
|
+
object.scaleX = displayWidth / width;
|
|
42
|
+
object.scaleY = displayHeight / height;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async hookTransformObjectEnd({ originObject, fabricObject }: { originObject: any, fabricObject: any }) {
|
|
49
|
+
if (originObject.extensionType === 'qrcode') {
|
|
50
|
+
this._bindQrCodeEvents(fabricObject);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async _getQrCodeResult(options: any): Promise<{ url: string; width: number; height: number }> {
|
|
55
|
+
const zoom = this.canvas.getZoom() || 1;
|
|
56
|
+
const dpr = (window && (window as any).devicePixelRatio) || 1;
|
|
57
|
+
|
|
58
|
+
const targetWidth = (options.width || 300) * zoom * dpr;
|
|
59
|
+
// 估算 module 数量,QR Code 通常在 21-177 之间,取一个中间值作为估算基础
|
|
60
|
+
const estimatedModules = 35;
|
|
61
|
+
// 计算需要的缩放比例,确保生成的图片足够大
|
|
62
|
+
let bwipScale = Math.ceil(targetWidth / estimatedModules);
|
|
63
|
+
// 保证最小缩放比例,避免过小
|
|
64
|
+
if (bwipScale < 2) bwipScale = 2;
|
|
65
|
+
|
|
66
|
+
const canvas = document.createElement('canvas');
|
|
67
|
+
|
|
68
|
+
const barColor = options.color?.replace('#', '') || '000000';
|
|
69
|
+
const bgColor = options.bgColor?.replace('#', '') || 'ffffff';
|
|
70
|
+
const ecLevel = options.ecLevel || 'M';
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
bwipjs.toCanvas(canvas, {
|
|
74
|
+
bcid: 'qrcode',
|
|
75
|
+
text: options.data || ' ',
|
|
76
|
+
scale: bwipScale,
|
|
77
|
+
eclevel: ecLevel,
|
|
78
|
+
barcolor: barColor,
|
|
79
|
+
backgroundcolor: bgColor,
|
|
80
|
+
} as any);
|
|
81
|
+
return {
|
|
82
|
+
url: canvas.toDataURL('image/png'),
|
|
83
|
+
width: canvas.width,
|
|
84
|
+
height: canvas.height
|
|
85
|
+
};
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('QR Code generation failed:', error);
|
|
88
|
+
return { url: '', width: 0, height: 0 };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _getBase64Str(options: any): Promise<string> {
|
|
93
|
+
const { url } = await this._getQrCodeResult(options);
|
|
94
|
+
return url;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_defaultBarcodeOption() {
|
|
98
|
+
return {
|
|
99
|
+
value: '@hprint/print',
|
|
100
|
+
width: 300,
|
|
101
|
+
margin: 10,
|
|
102
|
+
ecLevel: 'M',
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 将内部参数转换为二维码库需要的参数
|
|
108
|
+
*/
|
|
109
|
+
_paramsToOption(option: any) {
|
|
110
|
+
const hasW = Number.isFinite(option.width);
|
|
111
|
+
const hasH = Number.isFinite(option.height);
|
|
112
|
+
const size = hasW && hasH
|
|
113
|
+
? Math.max(option.width, option.height)
|
|
114
|
+
: (hasW ? option.width : (hasH ? option.height : undefined));
|
|
115
|
+
const options = {
|
|
116
|
+
...option,
|
|
117
|
+
width: size,
|
|
118
|
+
height: size ?? option.width,
|
|
119
|
+
type: 'canvas',
|
|
120
|
+
data: option.value != null ? String(option.value) : undefined,
|
|
121
|
+
margin: option.margin,
|
|
122
|
+
};
|
|
123
|
+
return options;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private async _updateQrCodeImage(imgEl: fabric.Image, immediate = false) {
|
|
127
|
+
const extension = imgEl.get('extension');
|
|
128
|
+
if (!extension) return;
|
|
129
|
+
const updateFn = async () => {
|
|
130
|
+
const currentWidth = imgEl.getScaledWidth();
|
|
131
|
+
const currentHeight = imgEl.getScaledHeight();
|
|
132
|
+
const size = Math.max(currentWidth, currentHeight);
|
|
133
|
+
const options = {
|
|
134
|
+
...extension,
|
|
135
|
+
width: size,
|
|
136
|
+
height: size,
|
|
137
|
+
};
|
|
138
|
+
const paramsOption = this._paramsToOption(options);
|
|
139
|
+
try {
|
|
140
|
+
const url = await this._getBase64Str(paramsOption);
|
|
141
|
+
await new Promise<void>((resolve) => {
|
|
142
|
+
imgEl.setSrc(url, () => {
|
|
143
|
+
this._setImageScale(imgEl, currentWidth, currentHeight);
|
|
144
|
+
imgEl.set('extension', options);
|
|
145
|
+
this.canvas.renderAll();
|
|
146
|
+
resolve();
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error(error);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
if (immediate) {
|
|
154
|
+
await updateFn();
|
|
155
|
+
} else {
|
|
156
|
+
setTimeout(updateFn, 300);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 设置图片缩放到目标宽高
|
|
162
|
+
*/
|
|
163
|
+
private _setImageScale(
|
|
164
|
+
imgEl: fabric.Image,
|
|
165
|
+
targetWidth: number,
|
|
166
|
+
targetHeight: number
|
|
167
|
+
) {
|
|
168
|
+
const imgWidth = imgEl.width || 0;
|
|
169
|
+
const imgHeight = imgEl.height || 0;
|
|
170
|
+
if (imgWidth > 0 && imgHeight > 0) {
|
|
171
|
+
const scaleX = targetWidth / imgWidth;
|
|
172
|
+
const scaleY = targetHeight / imgHeight;
|
|
173
|
+
imgEl.set({
|
|
174
|
+
scaleX,
|
|
175
|
+
scaleY,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 绑定二维码相关事件与方法
|
|
182
|
+
*/
|
|
183
|
+
private _bindQrCodeEvents(imgEl: fabric.Image) {
|
|
184
|
+
(imgEl as any).setExtension = async (fields: Record<string, any>) => {
|
|
185
|
+
const currentExt = (imgEl.get('extension') as any) || {};
|
|
186
|
+
const merged = { ...currentExt, ...(fields || {}) };
|
|
187
|
+
imgEl.set('extension', merged);
|
|
188
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
189
|
+
};
|
|
190
|
+
(imgEl as any).setExtensionByUnit = async (
|
|
191
|
+
fields: Record<string, any>,
|
|
192
|
+
dpi?: number
|
|
193
|
+
) => {
|
|
194
|
+
const curUnit = getUnit(this.editor);
|
|
195
|
+
const { processed, originByUnit } = processOptions(fields || {}, curUnit, dpi);
|
|
196
|
+
const precision = (this.editor as any).getPrecision?.();
|
|
197
|
+
const formattedOrigin = formatOriginValues(originByUnit[curUnit] || {}, precision);
|
|
198
|
+
const originSize = (imgEl as any)._originSize || {};
|
|
199
|
+
const unitOrigin = originSize[curUnit] || {};
|
|
200
|
+
unitOrigin.extension = { ...(unitOrigin.extension || {}), ...formattedOrigin };
|
|
201
|
+
(imgEl as any)._originSize = { ...originSize, [curUnit]: unitOrigin };
|
|
202
|
+
const currentExt = (imgEl.get('extension') as any) || {};
|
|
203
|
+
const merged = { ...currentExt, ...processed };
|
|
204
|
+
imgEl.set('extension', merged);
|
|
205
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
206
|
+
};
|
|
207
|
+
(imgEl as any).off?.('modified');
|
|
208
|
+
(imgEl as any).off?.('scaled');
|
|
209
|
+
imgEl.on('modified', async (event: any) => {
|
|
210
|
+
const target = (event?.target as fabric.Image) || imgEl;
|
|
211
|
+
await this._updateQrCodeImage(target, true);
|
|
212
|
+
});
|
|
213
|
+
imgEl.on('scaled', async () => {
|
|
214
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 创建二维码,支持传入内容与样式,进行单位转换并存储原始尺寸
|
|
221
|
+
*/
|
|
222
|
+
async addQrCode(
|
|
223
|
+
data?: string,
|
|
224
|
+
opts?: {
|
|
225
|
+
left?: number;
|
|
226
|
+
top?: number;
|
|
227
|
+
width?: number;
|
|
228
|
+
height?: number;
|
|
229
|
+
ecLevel?: 'L' | 'M' | 'Q' | 'H';
|
|
230
|
+
color: string;
|
|
231
|
+
bgColor: string;
|
|
232
|
+
},
|
|
233
|
+
dpi?: number
|
|
234
|
+
): Promise<fabric.Image> {
|
|
235
|
+
const option = {
|
|
236
|
+
...this._defaultBarcodeOption(),
|
|
237
|
+
...(opts || {}),
|
|
238
|
+
...(data ? { value: data } : {}),
|
|
239
|
+
};
|
|
240
|
+
const unit = getUnit(this.editor);
|
|
241
|
+
const { processed, originByUnit } = processOptions(option, unit, dpi, ['left', 'top', 'width', 'height', 'margin']);
|
|
242
|
+
const finalOption = { ...option, ...processed };
|
|
243
|
+
const paramsOption = this._paramsToOption(finalOption);
|
|
244
|
+
const url = await this._getBase64Str(paramsOption);
|
|
245
|
+
return new Promise<fabric.Image>((resolve) => {
|
|
246
|
+
fabric.Image.fromURL(
|
|
247
|
+
url,
|
|
248
|
+
(imgEl) => {
|
|
249
|
+
const safeLeft = Number.isFinite(processed.left)
|
|
250
|
+
? processed.left
|
|
251
|
+
: 0;
|
|
252
|
+
const safeTop = Number.isFinite(processed.top)
|
|
253
|
+
? processed.top
|
|
254
|
+
: 0;
|
|
255
|
+
imgEl.set({
|
|
256
|
+
left: safeLeft,
|
|
257
|
+
top: safeTop,
|
|
258
|
+
extensionType: 'qrcode',
|
|
259
|
+
extension: finalOption,
|
|
260
|
+
imageSmoothing: false,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const targetWidth =
|
|
264
|
+
typeof finalOption.width === 'number'
|
|
265
|
+
? finalOption.width
|
|
266
|
+
: imgEl.width ?? 0;
|
|
267
|
+
const targetHeight =
|
|
268
|
+
typeof finalOption.height === 'number'
|
|
269
|
+
? finalOption.height
|
|
270
|
+
: targetWidth;
|
|
271
|
+
this._setImageScale(imgEl, targetWidth, targetHeight);
|
|
272
|
+
|
|
273
|
+
const origin = originByUnit[unit] || {};
|
|
274
|
+
const originMapped: Record<string, any> = { ...origin };
|
|
275
|
+
if (
|
|
276
|
+
originMapped.height === undefined &&
|
|
277
|
+
originMapped.width !== undefined
|
|
278
|
+
) {
|
|
279
|
+
originMapped.height = originMapped.width;
|
|
280
|
+
}
|
|
281
|
+
(imgEl as any)._originSize = { [unit]: originMapped };
|
|
282
|
+
this._bindQrCodeEvents(imgEl);
|
|
283
|
+
resolve(imgEl);
|
|
284
|
+
},
|
|
285
|
+
{ crossOrigin: 'anonymous' }
|
|
286
|
+
);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async setQrCode(option: any) {
|
|
291
|
+
try {
|
|
292
|
+
const paramsOption = this._paramsToOption(option);
|
|
293
|
+
const url = await this._getBase64Str(paramsOption);
|
|
294
|
+
const activeObject = this.canvas.getActiveObjects()[0];
|
|
295
|
+
fabric.Image.fromURL(
|
|
296
|
+
url,
|
|
297
|
+
(imgEl) => {
|
|
298
|
+
imgEl.set({
|
|
299
|
+
left: activeObject.left,
|
|
300
|
+
top: activeObject.top,
|
|
301
|
+
extensionType: 'qrcode',
|
|
302
|
+
extension: { ...option },
|
|
303
|
+
});
|
|
304
|
+
imgEl.scaleToWidth(activeObject.getScaledWidth());
|
|
305
|
+
this._bindQrCodeEvents(imgEl);
|
|
306
|
+
this.editor.del();
|
|
307
|
+
this.canvas.add(imgEl);
|
|
308
|
+
this.canvas.setActiveObject(imgEl);
|
|
309
|
+
},
|
|
310
|
+
{ crossOrigin: 'anonymous' }
|
|
311
|
+
);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
console.log(error);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
destroy() {
|
|
318
|
+
console.log('pluginDestroy');
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export default QrCodePlugin;
|