@hprint/plugins 0.0.1-alpha.0
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.css +1 -0
- package/dist/index.js +478 -0
- package/dist/index.mjs +41731 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/objects/Arrow.d.ts +2 -0
- package/dist/src/objects/Arrow.d.ts.map +1 -0
- package/dist/src/objects/ThinTailArrow.d.ts +2 -0
- package/dist/src/objects/ThinTailArrow.d.ts.map +1 -0
- package/dist/src/plugins/AddBaseTypePlugin.d.ts +26 -0
- package/dist/src/plugins/AddBaseTypePlugin.d.ts.map +1 -0
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts +16 -0
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -0
- package/dist/src/plugins/BarCodePlugin.d.ts +68 -0
- package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -0
- package/dist/src/plugins/CenterAlignPlugin.d.ts +29 -0
- package/dist/src/plugins/CenterAlignPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ControlsPlugin.d.ts +11 -0
- package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ControlsRotatePlugin.d.ts +11 -0
- package/dist/src/plugins/ControlsRotatePlugin.d.ts.map +1 -0
- package/dist/src/plugins/CopyPlugin.d.ts +30 -0
- package/dist/src/plugins/CopyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/CreateElementPlugin.d.ts +121 -0
- package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DeleteHotKeyPlugin.d.ts +25 -0
- package/dist/src/plugins/DeleteHotKeyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DrawLinePlugin.d.ts +26 -0
- package/dist/src/plugins/DrawLinePlugin.d.ts.map +1 -0
- package/dist/src/plugins/DrawPolygonPlugin.d.ts +41 -0
- package/dist/src/plugins/DrawPolygonPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DringPlugin.d.ts +33 -0
- package/dist/src/plugins/DringPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FlipPlugin.d.ts +26 -0
- package/dist/src/plugins/FlipPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FontPlugin.d.ts +33 -0
- package/dist/src/plugins/FontPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FreeDrawPlugin.d.ts +23 -0
- package/dist/src/plugins/FreeDrawPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupAlignPlugin.d.ts +24 -0
- package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupPlugin.d.ts +24 -0
- package/dist/src/plugins/GroupPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupTextEditorPlugin.d.ts +18 -0
- package/dist/src/plugins/GroupTextEditorPlugin.d.ts.map +1 -0
- package/dist/src/plugins/HistoryPlugin.d.ts +30 -0
- package/dist/src/plugins/HistoryPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ImageStroke.d.ts +18 -0
- package/dist/src/plugins/ImageStroke.d.ts.map +1 -0
- package/dist/src/plugins/LayerPlugin.d.ts +31 -0
- package/dist/src/plugins/LayerPlugin.d.ts.map +1 -0
- package/dist/src/plugins/LockPlugin.d.ts +27 -0
- package/dist/src/plugins/LockPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MaskPlugin.d.ts +38 -0
- package/dist/src/plugins/MaskPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MaterialPlugin.d.ts +45 -0
- package/dist/src/plugins/MaterialPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MiddleMousePlugin.d.ts +18 -0
- package/dist/src/plugins/MiddleMousePlugin.d.ts.map +1 -0
- package/dist/src/plugins/MoveHotKeyPlugin.d.ts +12 -0
- package/dist/src/plugins/MoveHotKeyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PathTextPlugin.d.ts +30 -0
- package/dist/src/plugins/PathTextPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PolygonModifyPlugin.d.ts +28 -0
- package/dist/src/plugins/PolygonModifyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PrintPlugin.d.ts +39 -0
- package/dist/src/plugins/PrintPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PsdPlugin.d.ts +17 -0
- package/dist/src/plugins/PsdPlugin.d.ts.map +1 -0
- package/dist/src/plugins/QrCodePlugin.d.ts +137 -0
- package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -0
- package/dist/src/plugins/ResizePlugin.d.ts +44 -0
- package/dist/src/plugins/ResizePlugin.d.ts.map +1 -0
- package/dist/src/plugins/RulerPlugin.d.ts +24 -0
- package/dist/src/plugins/RulerPlugin.d.ts.map +1 -0
- package/dist/src/plugins/SimpleClipImagePlugin.d.ts +18 -0
- package/dist/src/plugins/SimpleClipImagePlugin.d.ts.map +1 -0
- package/dist/src/plugins/UnitPlugin.d.ts +84 -0
- package/dist/src/plugins/UnitPlugin.d.ts.map +1 -0
- package/dist/src/plugins/WaterMarkPlugin.d.ts +40 -0
- package/dist/src/plugins/WaterMarkPlugin.d.ts.map +1 -0
- package/dist/src/plugins/WorkspacePlugin.d.ts +57 -0
- package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -0
- package/dist/src/types/eventType.d.ts +11 -0
- package/dist/src/types/eventType.d.ts.map +1 -0
- package/dist/src/utils/psd.d.ts +3 -0
- package/dist/src/utils/psd.d.ts.map +1 -0
- package/dist/src/utils/ruler/guideline.d.ts +4 -0
- package/dist/src/utils/ruler/guideline.d.ts.map +1 -0
- package/dist/src/utils/ruler/index.d.ts +5 -0
- package/dist/src/utils/ruler/index.d.ts.map +1 -0
- package/dist/src/utils/ruler/ruler.d.ts +147 -0
- package/dist/src/utils/ruler/ruler.d.ts.map +1 -0
- package/dist/src/utils/ruler/utils.d.ts +50 -0
- package/dist/src/utils/ruler/utils.d.ts.map +1 -0
- package/dist/src/utils/units.d.ts +22 -0
- package/dist/src/utils/units.d.ts.map +1 -0
- package/package.json +51 -0
- package/src/assets/edgecontrol.svg +17 -0
- package/src/assets/lock.svg +7 -0
- package/src/assets/middlecontrol.svg +17 -0
- package/src/assets/middlecontrolhoz.svg +17 -0
- package/src/assets/rotateicon.svg +20 -0
- package/src/assets/style/resizePlugin.css +27 -0
- package/src/index.ts +121 -0
- package/src/objects/Arrow.js +47 -0
- package/src/objects/ThinTailArrow.js +50 -0
- package/src/plugins/AddBaseTypePlugin.ts +107 -0
- package/src/plugins/AlignGuidLinePlugin.ts +1141 -0
- package/src/plugins/BarCodePlugin.ts +860 -0
- package/src/plugins/CenterAlignPlugin.ts +133 -0
- package/src/plugins/ControlsPlugin.ts +251 -0
- package/src/plugins/ControlsRotatePlugin.ts +111 -0
- package/src/plugins/CopyPlugin.ts +255 -0
- package/src/plugins/CreateElementPlugin.ts +548 -0
- package/src/plugins/DeleteHotKeyPlugin.ts +57 -0
- package/src/plugins/DrawLinePlugin.ts +162 -0
- package/src/plugins/DrawPolygonPlugin.ts +205 -0
- package/src/plugins/DringPlugin.ts +125 -0
- package/src/plugins/FlipPlugin.ts +59 -0
- package/src/plugins/FontPlugin.ts +165 -0
- package/src/plugins/FreeDrawPlugin.ts +49 -0
- package/src/plugins/GroupAlignPlugin.ts +365 -0
- package/src/plugins/GroupPlugin.ts +82 -0
- package/src/plugins/GroupTextEditorPlugin.ts +198 -0
- package/src/plugins/HistoryPlugin.ts +181 -0
- package/src/plugins/ImageStroke.ts +121 -0
- package/src/plugins/LayerPlugin.ts +108 -0
- package/src/plugins/LockPlugin.ts +240 -0
- package/src/plugins/MaskPlugin.ts +155 -0
- package/src/plugins/MaterialPlugin.ts +224 -0
- package/src/plugins/MiddleMousePlugin.ts +45 -0
- package/src/plugins/MoveHotKeyPlugin.ts +46 -0
- package/src/plugins/PathTextPlugin.ts +89 -0
- package/src/plugins/PolygonModifyPlugin.ts +224 -0
- package/src/plugins/PrintPlugin.ts +81 -0
- package/src/plugins/PsdPlugin.ts +52 -0
- package/src/plugins/QrCodePlugin.ts +393 -0
- package/src/plugins/ResizePlugin.ts +274 -0
- package/src/plugins/RulerPlugin.ts +78 -0
- package/src/plugins/SimpleClipImagePlugin.ts +244 -0
- package/src/plugins/UnitPlugin.ts +327 -0
- package/src/plugins/WaterMarkPlugin.ts +257 -0
- package/src/plugins/WorkspacePlugin.ts +307 -0
- package/src/types/eventType.ts +11 -0
- package/src/utils/psd.js +432 -0
- package/src/utils/ruler/guideline.ts +145 -0
- package/src/utils/ruler/index.ts +91 -0
- package/src/utils/ruler/ruler.ts +924 -0
- package/src/utils/ruler/utils.ts +162 -0
- package/src/utils/units.ts +133 -0
- package/tsconfig.json +10 -0
- package/vite.config.ts +29 -0
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import QRCodeStyling from 'qr-code-styling';
|
|
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' | 'getQrCodeTypes'>;
|
|
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
|
+
enum DotsType {
|
|
17
|
+
rounded = 'rounded',
|
|
18
|
+
dots = 'dots',
|
|
19
|
+
classy = 'classy',
|
|
20
|
+
classy_rounded = 'classy-rounded',
|
|
21
|
+
square = 'square',
|
|
22
|
+
extra_rounded = 'extra-rounded',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
enum CornersType {
|
|
26
|
+
dot = 'dot',
|
|
27
|
+
square = 'square',
|
|
28
|
+
extra_rounded = 'extra-rounded',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
enum cornersDotType {
|
|
32
|
+
dot = 'dot',
|
|
33
|
+
square = 'square',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
enum errorCorrectionLevelType {
|
|
37
|
+
L = 'L',
|
|
38
|
+
M = 'M',
|
|
39
|
+
Q = 'Q',
|
|
40
|
+
H = 'H',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class QrParamsDefaults {
|
|
44
|
+
width = 300;
|
|
45
|
+
height = 300;
|
|
46
|
+
type = 'canvas' as const;
|
|
47
|
+
data = ' ';
|
|
48
|
+
margin = 10;
|
|
49
|
+
qrOptions = {
|
|
50
|
+
errorCorrectionLevel: 'M' as const,
|
|
51
|
+
};
|
|
52
|
+
dotsOptions = {
|
|
53
|
+
color: '#000000',
|
|
54
|
+
type: 'square' as const,
|
|
55
|
+
};
|
|
56
|
+
cornersSquareOptions = {
|
|
57
|
+
color: '#000000',
|
|
58
|
+
type: 'square' as const,
|
|
59
|
+
};
|
|
60
|
+
cornersDotOptions = {
|
|
61
|
+
color: '#000000',
|
|
62
|
+
type: 'square' as const,
|
|
63
|
+
};
|
|
64
|
+
backgroundOptions = {
|
|
65
|
+
color: '#ffffff',
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class QrCodePlugin implements IPluginTempl {
|
|
70
|
+
static pluginName = 'QrCodePlugin';
|
|
71
|
+
static apis = ['addQrCode', 'setQrCode', 'getQrCodeTypes'];
|
|
72
|
+
constructor(
|
|
73
|
+
public canvas: fabric.Canvas,
|
|
74
|
+
public editor: IEditor
|
|
75
|
+
) { }
|
|
76
|
+
|
|
77
|
+
async hookTransform(object: any) {
|
|
78
|
+
if (object.extensionType === 'qrcode') {
|
|
79
|
+
const paramsOption = this._paramsToOption(object.extension);
|
|
80
|
+
const url = await this._getBase64Str(paramsOption);
|
|
81
|
+
object.src = url;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async _getBase64Str(options: any): Promise<string> {
|
|
86
|
+
const zoom = this.canvas.getZoom() || 1;
|
|
87
|
+
const dpr = (window && (window as any).devicePixelRatio) || 1;
|
|
88
|
+
let scale = zoom * dpr;
|
|
89
|
+
const maxScale = 4;
|
|
90
|
+
if (!Number.isFinite(scale) || scale <= 0) scale = 1;
|
|
91
|
+
scale = Math.min(scale, maxScale);
|
|
92
|
+
|
|
93
|
+
const scaledOptions = {
|
|
94
|
+
...options,
|
|
95
|
+
width: Math.round((options.width || 300) * scale),
|
|
96
|
+
height: Math.round((options.height || options.width || 300) * scale),
|
|
97
|
+
margin: Math.round((options.margin || 0) * scale),
|
|
98
|
+
};
|
|
99
|
+
const qrCode = new QRCodeStyling(scaledOptions);
|
|
100
|
+
const blob = await qrCode.getRawData('png');
|
|
101
|
+
if (!blob) return '';
|
|
102
|
+
const base64Str = (await utils.blobToBase64(blob)) as string;
|
|
103
|
+
return base64Str || '';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_defaultBarcodeOption() {
|
|
107
|
+
return {
|
|
108
|
+
value: 'https://kuaitu.cc',
|
|
109
|
+
width: 300,
|
|
110
|
+
margin: 10,
|
|
111
|
+
errorCorrectionLevel: 'M',
|
|
112
|
+
dotsColor: '#000000',
|
|
113
|
+
dotsType: 'square',
|
|
114
|
+
cornersSquareColor: '#000000',
|
|
115
|
+
cornersSquareType: 'square',
|
|
116
|
+
cornersDotColor: '#000000',
|
|
117
|
+
cornersDotType: 'square',
|
|
118
|
+
background: '#ffffff',
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 将内部参数转换为二维码库需要的参数
|
|
124
|
+
*/
|
|
125
|
+
_paramsToOption(option: any) {
|
|
126
|
+
const hasW = Number.isFinite(option.width);
|
|
127
|
+
const hasH = Number.isFinite(option.height);
|
|
128
|
+
const size = hasW && hasH
|
|
129
|
+
? Math.max(option.width, option.height)
|
|
130
|
+
: (hasW ? option.width : (hasH ? option.height : undefined));
|
|
131
|
+
const base = {
|
|
132
|
+
width: size,
|
|
133
|
+
height: size ?? option.width,
|
|
134
|
+
type: 'canvas',
|
|
135
|
+
data: option.value != null ? String(option.value) : undefined,
|
|
136
|
+
margin: option.margin,
|
|
137
|
+
qrOptions: {
|
|
138
|
+
errorCorrectionLevel: option.errorCorrectionLevel,
|
|
139
|
+
},
|
|
140
|
+
dotsOptions: {
|
|
141
|
+
color: option.dotsColor,
|
|
142
|
+
type: option.dotsType,
|
|
143
|
+
},
|
|
144
|
+
cornersSquareOptions: {
|
|
145
|
+
color: option.cornersSquareColor,
|
|
146
|
+
type: option.cornersSquareType,
|
|
147
|
+
},
|
|
148
|
+
cornersDotOptions: {
|
|
149
|
+
color: option.cornersDotColor,
|
|
150
|
+
type: option.cornersDotType,
|
|
151
|
+
},
|
|
152
|
+
backgroundOptions: {
|
|
153
|
+
color: option.background,
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
const defaultParams = new QrParamsDefaults();
|
|
157
|
+
const merged = Object.assign({}, defaultParams, base);
|
|
158
|
+
merged.qrOptions = Object.assign({}, defaultParams.qrOptions, base.qrOptions);
|
|
159
|
+
merged.dotsOptions = Object.assign({}, defaultParams.dotsOptions, base.dotsOptions);
|
|
160
|
+
merged.cornersSquareOptions = Object.assign({}, defaultParams.cornersSquareOptions, base.cornersSquareOptions);
|
|
161
|
+
merged.cornersDotOptions = Object.assign({}, defaultParams.cornersDotOptions, base.cornersDotOptions);
|
|
162
|
+
merged.backgroundOptions = Object.assign({}, defaultParams.backgroundOptions, base.backgroundOptions);
|
|
163
|
+
|
|
164
|
+
if (!merged.data || (typeof merged.data === 'string' && merged.data.trim() === '')) {
|
|
165
|
+
merged.data = defaultParams.data;
|
|
166
|
+
}
|
|
167
|
+
return merged;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private async _updateQrCodeImage(imgEl: fabric.Image, immediate = false) {
|
|
171
|
+
const extension = imgEl.get('extension');
|
|
172
|
+
if (!extension) return;
|
|
173
|
+
const updateFn = async () => {
|
|
174
|
+
const currentWidth = imgEl.getScaledWidth();
|
|
175
|
+
const currentHeight = imgEl.getScaledHeight();
|
|
176
|
+
const size = Math.max(currentWidth, currentHeight);
|
|
177
|
+
const options = {
|
|
178
|
+
...extension,
|
|
179
|
+
width: size,
|
|
180
|
+
height: size,
|
|
181
|
+
};
|
|
182
|
+
const paramsOption = this._paramsToOption(options);
|
|
183
|
+
try {
|
|
184
|
+
const url = await this._getBase64Str(paramsOption);
|
|
185
|
+
await new Promise<void>((resolve) => {
|
|
186
|
+
imgEl.setSrc(url, () => {
|
|
187
|
+
this._setImageScale(imgEl, currentWidth, currentHeight);
|
|
188
|
+
imgEl.set('extension', options);
|
|
189
|
+
this.canvas.renderAll();
|
|
190
|
+
resolve();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error(error);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
if (immediate) {
|
|
198
|
+
await updateFn();
|
|
199
|
+
} else {
|
|
200
|
+
setTimeout(updateFn, 300);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 设置图片缩放到目标宽高
|
|
206
|
+
*/
|
|
207
|
+
private _setImageScale(
|
|
208
|
+
imgEl: fabric.Image,
|
|
209
|
+
targetWidth: number,
|
|
210
|
+
targetHeight: number
|
|
211
|
+
) {
|
|
212
|
+
const imgWidth = imgEl.width || 0;
|
|
213
|
+
const imgHeight = imgEl.height || 0;
|
|
214
|
+
if (imgWidth > 0 && imgHeight > 0) {
|
|
215
|
+
const scaleX = targetWidth / imgWidth;
|
|
216
|
+
const scaleY = targetHeight / imgHeight;
|
|
217
|
+
imgEl.set({
|
|
218
|
+
scaleX,
|
|
219
|
+
scaleY,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 创建二维码,支持传入内容与样式,进行单位转换并存储原始尺寸
|
|
226
|
+
*/
|
|
227
|
+
async addQrCode(
|
|
228
|
+
data?: string,
|
|
229
|
+
opts?: {
|
|
230
|
+
left?: number;
|
|
231
|
+
top?: number;
|
|
232
|
+
width?: number;
|
|
233
|
+
height?: number;
|
|
234
|
+
margin?: number;
|
|
235
|
+
errorCorrectionLevel?: 'L' | 'M' | 'Q' | 'H';
|
|
236
|
+
dotsColor?: string;
|
|
237
|
+
dotsType?: string;
|
|
238
|
+
cornersSquareColor?: string;
|
|
239
|
+
cornersSquareType?: string;
|
|
240
|
+
cornersDotColor?: string;
|
|
241
|
+
cornersDotType?: string;
|
|
242
|
+
background?: string;
|
|
243
|
+
},
|
|
244
|
+
dpi?: number
|
|
245
|
+
): Promise<fabric.Image> {
|
|
246
|
+
const option = {
|
|
247
|
+
...this._defaultBarcodeOption(),
|
|
248
|
+
...(opts || {}),
|
|
249
|
+
...(data ? { value: data } : {}),
|
|
250
|
+
};
|
|
251
|
+
const unit = getUnit(this.editor);
|
|
252
|
+
const { processed, originByUnit } = processOptions(option, unit, dpi, ['left', 'top', 'width', 'height', 'margin']);
|
|
253
|
+
const finalOptionBase = { ...option, ...processed };
|
|
254
|
+
const finalOption = {
|
|
255
|
+
...finalOptionBase,
|
|
256
|
+
width: Number.isFinite(finalOptionBase.width) && finalOptionBase.width > 0
|
|
257
|
+
? finalOptionBase.width
|
|
258
|
+
: this._defaultBarcodeOption().width,
|
|
259
|
+
height: Number.isFinite(finalOptionBase.height) && finalOptionBase.height > 0
|
|
260
|
+
? finalOptionBase.height
|
|
261
|
+
: finalOptionBase.width,
|
|
262
|
+
margin: Number.isFinite(finalOptionBase.margin) && finalOptionBase.margin >= 0
|
|
263
|
+
? finalOptionBase.margin
|
|
264
|
+
: this._defaultBarcodeOption().margin,
|
|
265
|
+
};
|
|
266
|
+
const paramsOption = this._paramsToOption(finalOption);
|
|
267
|
+
const url = await this._getBase64Str(paramsOption);
|
|
268
|
+
return new Promise<fabric.Image>((resolve) => {
|
|
269
|
+
fabric.Image.fromURL(
|
|
270
|
+
url,
|
|
271
|
+
(imgEl) => {
|
|
272
|
+
const safeLeft = Number.isFinite(processed.left)
|
|
273
|
+
? processed.left
|
|
274
|
+
: 0;
|
|
275
|
+
const safeTop = Number.isFinite(processed.top)
|
|
276
|
+
? processed.top
|
|
277
|
+
: 0;
|
|
278
|
+
imgEl.set({
|
|
279
|
+
left: safeLeft,
|
|
280
|
+
top: safeTop,
|
|
281
|
+
extensionType: 'qrcode',
|
|
282
|
+
extension: finalOption,
|
|
283
|
+
imageSmoothing: false,
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const targetWidth =
|
|
287
|
+
typeof finalOption.width === 'number'
|
|
288
|
+
? finalOption.width
|
|
289
|
+
: imgEl.width ?? 0;
|
|
290
|
+
const targetHeight =
|
|
291
|
+
typeof finalOption.height === 'number'
|
|
292
|
+
? finalOption.height
|
|
293
|
+
: targetWidth;
|
|
294
|
+
this._setImageScale(imgEl, targetWidth, targetHeight);
|
|
295
|
+
|
|
296
|
+
const origin = originByUnit[unit] || {};
|
|
297
|
+
const originMapped: Record<string, any> = { ...origin };
|
|
298
|
+
if (
|
|
299
|
+
originMapped.height === undefined &&
|
|
300
|
+
originMapped.width !== undefined
|
|
301
|
+
) {
|
|
302
|
+
originMapped.height = originMapped.width;
|
|
303
|
+
}
|
|
304
|
+
(imgEl as any)._originSize = { [unit]: originMapped };
|
|
305
|
+
(imgEl as any).setExtension = async (fields: Record<string, any>) => {
|
|
306
|
+
const currentExt = (imgEl.get('extension') as any) || {};
|
|
307
|
+
const merged = { ...currentExt, ...(fields || {}) };
|
|
308
|
+
imgEl.set('extension', merged);
|
|
309
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
310
|
+
};
|
|
311
|
+
(imgEl as any).setExtensionByUnit = async (
|
|
312
|
+
fields: Record<string, any>,
|
|
313
|
+
dpi?: number
|
|
314
|
+
) => {
|
|
315
|
+
const curUnit = getUnit(this.editor);
|
|
316
|
+
const { processed, originByUnit } = processOptions(fields || {}, curUnit, dpi);
|
|
317
|
+
const precision = (this.editor as any).getPrecision?.();
|
|
318
|
+
const formattedOrigin = formatOriginValues(originByUnit[curUnit] || {}, precision);
|
|
319
|
+
const originSize = (imgEl as any)._originSize || {};
|
|
320
|
+
const unitOrigin = originSize[curUnit] || {};
|
|
321
|
+
unitOrigin.extension = { ...(unitOrigin.extension || {}), ...formattedOrigin };
|
|
322
|
+
(imgEl as any)._originSize = { ...originSize, [curUnit]: unitOrigin };
|
|
323
|
+
const currentExt = (imgEl.get('extension') as any) || {};
|
|
324
|
+
const merged = { ...currentExt, ...processed };
|
|
325
|
+
imgEl.set('extension', merged);
|
|
326
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
327
|
+
};
|
|
328
|
+
(imgEl as any).off?.('modified');
|
|
329
|
+
(imgEl as any).off?.('scaled');
|
|
330
|
+
imgEl.on('modified', async (event: any) => {
|
|
331
|
+
const target = (event?.target as fabric.Image) || imgEl;
|
|
332
|
+
await this._updateQrCodeImage(target, true);
|
|
333
|
+
});
|
|
334
|
+
imgEl.on('scaled', async () => {
|
|
335
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
336
|
+
});
|
|
337
|
+
resolve(imgEl);
|
|
338
|
+
},
|
|
339
|
+
{ crossOrigin: 'anonymous' }
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async setQrCode(option: any) {
|
|
345
|
+
try {
|
|
346
|
+
const paramsOption = this._paramsToOption(option);
|
|
347
|
+
const url = await this._getBase64Str(paramsOption);
|
|
348
|
+
const activeObject = this.canvas.getActiveObjects()[0];
|
|
349
|
+
fabric.Image.fromURL(
|
|
350
|
+
url,
|
|
351
|
+
(imgEl) => {
|
|
352
|
+
imgEl.set({
|
|
353
|
+
left: activeObject.left,
|
|
354
|
+
top: activeObject.top,
|
|
355
|
+
extensionType: 'qrcode',
|
|
356
|
+
extension: { ...option },
|
|
357
|
+
});
|
|
358
|
+
imgEl.scaleToWidth(activeObject.getScaledWidth());
|
|
359
|
+
imgEl.off('modified');
|
|
360
|
+
imgEl.off('scaled');
|
|
361
|
+
imgEl.on('modified', async (event: any) => {
|
|
362
|
+
const target = (event?.target as fabric.Image) || imgEl;
|
|
363
|
+
await this._updateQrCodeImage(target, true);
|
|
364
|
+
});
|
|
365
|
+
imgEl.on('scaled', async () => {
|
|
366
|
+
await this._updateQrCodeImage(imgEl, true);
|
|
367
|
+
});
|
|
368
|
+
this.editor.del();
|
|
369
|
+
this.canvas.add(imgEl);
|
|
370
|
+
this.canvas.setActiveObject(imgEl);
|
|
371
|
+
},
|
|
372
|
+
{ crossOrigin: 'anonymous' }
|
|
373
|
+
);
|
|
374
|
+
} catch (error) {
|
|
375
|
+
console.log(error);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
getQrCodeTypes() {
|
|
380
|
+
return {
|
|
381
|
+
DotsType: Object.values(DotsType),
|
|
382
|
+
CornersType: Object.values(CornersType),
|
|
383
|
+
cornersDotType: Object.values(cornersDotType),
|
|
384
|
+
errorCorrectionLevelType: Object.values(errorCorrectionLevelType),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
destroy() {
|
|
389
|
+
console.log('pluginDestroy');
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export default QrCodePlugin;
|
|
@@ -0,0 +1,274 @@
|
|
|
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;
|