@hprint/core 0.0.2 → 0.0.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/package.json +2 -2
- package/src/ServersPlugin.ts +411 -411
- package/src/interface/Editor.ts +58 -58
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hprint/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"hotkeys-js": "~3.8.9",
|
|
34
34
|
"tapable": "~2.3.0",
|
|
35
35
|
"uuid": "~8.3.2",
|
|
36
|
-
"@hprint/shared": "0.0.
|
|
36
|
+
"@hprint/shared": "0.0.3"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "vite build",
|
package/src/ServersPlugin.ts
CHANGED
|
@@ -1,411 +1,411 @@
|
|
|
1
|
-
import { v4 as uuid } from 'uuid';
|
|
2
|
-
import { selectFiles, clipboardText, downFile } from './utils/utils';
|
|
3
|
-
import { fabric, StaticCanvas } from 'fabric';
|
|
4
|
-
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
5
|
-
import { SelectEvent, SelectMode } from '../../plugins/src/types/eventType';
|
|
6
|
-
|
|
7
|
-
type IPlugin = Pick<
|
|
8
|
-
ServersPlugin,
|
|
9
|
-
| 'insert'
|
|
10
|
-
| 'loadJSON'
|
|
11
|
-
| 'getJson'
|
|
12
|
-
| 'dragAddItem'
|
|
13
|
-
| 'clipboard'
|
|
14
|
-
| 'clipboardBase64'
|
|
15
|
-
| 'saveJson'
|
|
16
|
-
| 'saveSvg'
|
|
17
|
-
| 'getBase64'
|
|
18
|
-
| 'saveImg'
|
|
19
|
-
| 'clear'
|
|
20
|
-
| 'preview'
|
|
21
|
-
| 'staticPreview'
|
|
22
|
-
| 'getSelectMode'
|
|
23
|
-
| 'getExtensionKey'
|
|
24
|
-
>;
|
|
25
|
-
|
|
26
|
-
declare module '@hprint/core' {
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
28
|
-
interface IEditor extends IPlugin { }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function transformText(objects: any) {
|
|
32
|
-
if (!objects) return;
|
|
33
|
-
objects.forEach((item: any) => {
|
|
34
|
-
if (item.objects) {
|
|
35
|
-
transformText(item.objects);
|
|
36
|
-
} else {
|
|
37
|
-
item.type === 'text' && (item.type = 'textbox');
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
class ServersPlugin implements IPluginTempl {
|
|
43
|
-
public selectedMode: SelectMode;
|
|
44
|
-
static pluginName = 'ServersPlugin';
|
|
45
|
-
static apis = [
|
|
46
|
-
'insert',
|
|
47
|
-
'loadJSON',
|
|
48
|
-
'getJson',
|
|
49
|
-
'dragAddItem',
|
|
50
|
-
'clipboard',
|
|
51
|
-
'clipboardBase64',
|
|
52
|
-
'saveJson',
|
|
53
|
-
'saveSvg',
|
|
54
|
-
'saveImg',
|
|
55
|
-
'getBase64',
|
|
56
|
-
'clear',
|
|
57
|
-
'preview',
|
|
58
|
-
'staticPreview',
|
|
59
|
-
'getSelectMode',
|
|
60
|
-
'getExtensionKey',
|
|
61
|
-
];
|
|
62
|
-
static events = [SelectMode.ONE, SelectMode.MULTI, SelectEvent.CANCEL];
|
|
63
|
-
// public hotkeys: string[] = ['left', 'right', 'down', 'up'];
|
|
64
|
-
constructor(
|
|
65
|
-
public canvas: fabric.Canvas,
|
|
66
|
-
public editor: IEditor
|
|
67
|
-
) {
|
|
68
|
-
this.selectedMode = SelectMode.EMPTY;
|
|
69
|
-
this._initSelectEvent();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private _initSelectEvent() {
|
|
73
|
-
this.canvas.on('selection:created', () => this._emitSelectEvent());
|
|
74
|
-
this.canvas.on('selection:updated', () => this._emitSelectEvent());
|
|
75
|
-
this.canvas.on('selection:cleared', () => this._emitSelectEvent());
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private _emitSelectEvent() {
|
|
79
|
-
if (!this.canvas) {
|
|
80
|
-
throw TypeError('还未初始化');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const actives = this.canvas
|
|
84
|
-
.getActiveObjects()
|
|
85
|
-
.filter((item) => !(item instanceof fabric.GuideLine)); // 过滤掉辅助线
|
|
86
|
-
if (actives && actives.length === 1) {
|
|
87
|
-
this.selectedMode = SelectMode.ONE;
|
|
88
|
-
this.editor.emit(SelectEvent.ONE, actives);
|
|
89
|
-
} else if (actives && actives.length > 1) {
|
|
90
|
-
this.selectedMode = SelectMode.MULTI;
|
|
91
|
-
this.editor.emit(SelectEvent.MULTI, actives);
|
|
92
|
-
} else {
|
|
93
|
-
this.editor.emit(SelectEvent.CANCEL);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getSelectMode() {
|
|
98
|
-
return String(this.selectedMode);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
insert(callback?: () => void) {
|
|
102
|
-
selectFiles({ accept: '.json' }).then((files) => {
|
|
103
|
-
if (files && files.length > 0) {
|
|
104
|
-
const file = files[0];
|
|
105
|
-
const reader = new FileReader();
|
|
106
|
-
reader.readAsText(file, 'UTF-8');
|
|
107
|
-
reader.onload = () => {
|
|
108
|
-
this.loadJSON(reader.result as string, callback);
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 设置path属性
|
|
115
|
-
renderITextPath(textPaths: Record<'id' | 'path', any>[]) {
|
|
116
|
-
textPaths.forEach((item) => {
|
|
117
|
-
const object = this.canvas
|
|
118
|
-
.getObjects()
|
|
119
|
-
.find((o) => o.id === item.id);
|
|
120
|
-
if (object) {
|
|
121
|
-
fabric.Path.fromObject(item.path, (e) => {
|
|
122
|
-
object.set('path', e);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async loadJSON(jsonFile: string | object, callback?: () => void) {
|
|
129
|
-
// 确保元素存在id
|
|
130
|
-
const temp =
|
|
131
|
-
typeof jsonFile === 'string' ? JSON.parse(jsonFile) : jsonFile;
|
|
132
|
-
const textPaths: Record<'id' | 'path', any>[] = [];
|
|
133
|
-
temp.objects.forEach((item: any) => {
|
|
134
|
-
!item.id && (item.id = uuid());
|
|
135
|
-
// 收集所有路径文本元素i-text,并设置path为null
|
|
136
|
-
if (item.type === 'i-text' && item.path) {
|
|
137
|
-
textPaths.push({ id: item.id, path: item.path });
|
|
138
|
-
item.path = null;
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// hookTransform遍历
|
|
143
|
-
const tempTransform = await this._transform(temp);
|
|
144
|
-
|
|
145
|
-
jsonFile = JSON.stringify(tempTransform);
|
|
146
|
-
// 加载前钩子
|
|
147
|
-
this.editor.hooksEntity.hookImportBefore.callAsync(jsonFile, () => {
|
|
148
|
-
this.canvas.loadFromJSON(
|
|
149
|
-
jsonFile,
|
|
150
|
-
() => {
|
|
151
|
-
// 把i-text对应的path加上
|
|
152
|
-
this.renderITextPath(textPaths);
|
|
153
|
-
this.canvas.renderAll();
|
|
154
|
-
// 加载后钩子
|
|
155
|
-
this.editor.hooksEntity.hookImportAfter.callAsync(
|
|
156
|
-
jsonFile,
|
|
157
|
-
() => {
|
|
158
|
-
// 修复导入带水印的json无法清除问题 #359
|
|
159
|
-
this.editor?.updateDrawStatus &&
|
|
160
|
-
typeof this.editor.updateDrawStatus ===
|
|
161
|
-
'function' &&
|
|
162
|
-
this.editor.updateDrawStatus(
|
|
163
|
-
!!temp['overlayImage']
|
|
164
|
-
);
|
|
165
|
-
this.canvas.renderAll();
|
|
166
|
-
callback && callback();
|
|
167
|
-
this.editor.emit('loadJson');
|
|
168
|
-
}
|
|
169
|
-
);
|
|
170
|
-
},
|
|
171
|
-
(originObject: any, fabricObject: fabric.Object) => {
|
|
172
|
-
this.editor.hooksEntity.hookTransformObjectEnd.callAsync(
|
|
173
|
-
{ originObject, fabricObject },
|
|
174
|
-
() => {
|
|
175
|
-
this.canvas.renderAll();
|
|
176
|
-
}
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
);
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async _transform(json: any) {
|
|
184
|
-
await this.promiseCallAsync(json);
|
|
185
|
-
if (json.objects) {
|
|
186
|
-
const all = json.objects.map((item: any) => {
|
|
187
|
-
return this._transform(item);
|
|
188
|
-
});
|
|
189
|
-
await Promise.all(all);
|
|
190
|
-
}
|
|
191
|
-
return json;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
promiseCallAsync(item: any) {
|
|
195
|
-
return new Promise((resolve) => {
|
|
196
|
-
this.editor.hooksEntity.hookTransform.callAsync(item, () => {
|
|
197
|
-
resolve(item);
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
getJson(options?: {
|
|
203
|
-
clearSrc?: boolean
|
|
204
|
-
}) {
|
|
205
|
-
const keys = this.getExtensionKey();
|
|
206
|
-
const jsonObject = this.canvas.toJSON(keys);
|
|
207
|
-
if (options?.clearSrc) {
|
|
208
|
-
jsonObject.objects.forEach((item: any) => {
|
|
209
|
-
if (['qrcode', 'barcode'].includes(item.extensionType)) {
|
|
210
|
-
item.src = '';
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
return jsonObject;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
getExtensionKey() {
|
|
218
|
-
return [
|
|
219
|
-
'id',
|
|
220
|
-
'gradientAngle',
|
|
221
|
-
'selectable',
|
|
222
|
-
'hasControls',
|
|
223
|
-
'linkData',
|
|
224
|
-
'editable',
|
|
225
|
-
'extensionType',
|
|
226
|
-
'extension',
|
|
227
|
-
'verticalAlign',
|
|
228
|
-
'roundValue',
|
|
229
|
-
'getBase64',
|
|
230
|
-
'lockScalingX',
|
|
231
|
-
'lockScalingY',
|
|
232
|
-
'_originSize',
|
|
233
|
-
];
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* @description: 拖拽添加到画布
|
|
238
|
-
* @param {Event} event
|
|
239
|
-
* @param {Object} item
|
|
240
|
-
*/
|
|
241
|
-
dragAddItem(item: fabric.Object, event?: DragEvent) {
|
|
242
|
-
if (event) {
|
|
243
|
-
const { left, top } = this.canvas
|
|
244
|
-
.getSelectionElement()
|
|
245
|
-
.getBoundingClientRect();
|
|
246
|
-
if (event.x < left || event.y < top || item.width === undefined)
|
|
247
|
-
return;
|
|
248
|
-
|
|
249
|
-
const point = {
|
|
250
|
-
x: event.x - left,
|
|
251
|
-
y: event.y - top,
|
|
252
|
-
};
|
|
253
|
-
const pointerVpt = this.canvas.restorePointerVpt(point);
|
|
254
|
-
item.left = pointerVpt.x - item.width / 2;
|
|
255
|
-
item.top = pointerVpt.y;
|
|
256
|
-
}
|
|
257
|
-
const { width } = this._getSaveOption();
|
|
258
|
-
width && item.scaleToWidth(width / 2);
|
|
259
|
-
this.canvas.add(item);
|
|
260
|
-
this.canvas.setActiveObject(item);
|
|
261
|
-
|
|
262
|
-
!event && this.editor.position('center');
|
|
263
|
-
this.canvas.requestRenderAll();
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
clipboard() {
|
|
267
|
-
const jsonStr = this.getJson();
|
|
268
|
-
return clipboardText(JSON.stringify(jsonStr, null, '\t'));
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async clipboardBase64() {
|
|
272
|
-
const dataUrl = await this.preview();
|
|
273
|
-
return clipboardText(dataUrl);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async saveJson() {
|
|
277
|
-
const dataUrl = this.getJson();
|
|
278
|
-
// 把文本text转为textgroup,让导入可以编辑
|
|
279
|
-
await transformText(dataUrl.objects);
|
|
280
|
-
const fileStr = `data:text/json;charset=utf-8,${encodeURIComponent(
|
|
281
|
-
JSON.stringify(dataUrl, null, '\t')
|
|
282
|
-
)}`;
|
|
283
|
-
downFile(fileStr, 'json');
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
saveSvg() {
|
|
287
|
-
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
288
|
-
const { fontOption, svgOption } = this._getSaveSvgOption();
|
|
289
|
-
fabric.fontPaths = {
|
|
290
|
-
...fontOption,
|
|
291
|
-
};
|
|
292
|
-
const dataUrl = this.canvas.toSVG(svgOption);
|
|
293
|
-
const fileStr = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(dataUrl)}`;
|
|
294
|
-
this.editor.hooksEntity.hookSaveAfter.callAsync(fileStr, () => {
|
|
295
|
-
downFile(fileStr, 'svg');
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
saveImg() {
|
|
301
|
-
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
302
|
-
const option = this._getSaveOption();
|
|
303
|
-
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
304
|
-
const dataUrl = this.canvas.toDataURL(option);
|
|
305
|
-
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
306
|
-
downFile(dataUrl, 'png');
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
getBase64() {
|
|
312
|
-
return new Promise<string>((resolve) => {
|
|
313
|
-
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
314
|
-
const option = this._getSaveOption();
|
|
315
|
-
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
316
|
-
const dataUrl = this.canvas.toDataURL(option);
|
|
317
|
-
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () =>
|
|
318
|
-
resolve(dataUrl)
|
|
319
|
-
);
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
preview() {
|
|
325
|
-
return new Promise<string>((resolve) => {
|
|
326
|
-
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
327
|
-
const option = this._getSaveOption();
|
|
328
|
-
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
329
|
-
this.canvas.renderAll();
|
|
330
|
-
const dataUrl = this.canvas.toDataURL(option);
|
|
331
|
-
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
332
|
-
resolve(dataUrl);
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
staticPreview() {
|
|
339
|
-
return new Promise<string>((resolve) => { });
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
_getSaveSvgOption() {
|
|
343
|
-
const workspace = this.canvas
|
|
344
|
-
.getObjects()
|
|
345
|
-
.find((item) => item.id === 'workspace');
|
|
346
|
-
let fontFamilyArry = this.canvas
|
|
347
|
-
.getObjects()
|
|
348
|
-
.filter((item) => item.type == 'textbox')
|
|
349
|
-
.map((item) => item.fontFamily);
|
|
350
|
-
fontFamilyArry = Array.from(new Set(fontFamilyArry));
|
|
351
|
-
|
|
352
|
-
const fontList = this.editor.getPlugin('FontPlugin').cacheList;
|
|
353
|
-
|
|
354
|
-
const fontEntry = {};
|
|
355
|
-
for (const font of fontFamilyArry) {
|
|
356
|
-
const item = fontList.find((item) => item.name === font);
|
|
357
|
-
fontEntry[font] = item.file;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
console.log('_getSaveSvgOption', fontEntry);
|
|
361
|
-
const { left, top, width, height } = workspace as fabric.Object;
|
|
362
|
-
return {
|
|
363
|
-
fontOption: fontEntry,
|
|
364
|
-
svgOption: {
|
|
365
|
-
width,
|
|
366
|
-
height,
|
|
367
|
-
viewBox: {
|
|
368
|
-
x: left,
|
|
369
|
-
y: top,
|
|
370
|
-
width,
|
|
371
|
-
height,
|
|
372
|
-
},
|
|
373
|
-
},
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
_getSaveOption() {
|
|
378
|
-
const workspace = this.canvas
|
|
379
|
-
.getObjects()
|
|
380
|
-
.find((item: fabric.Object) => item.id === 'workspace');
|
|
381
|
-
const { left, top, width, height } = workspace as fabric.Object;
|
|
382
|
-
const option = {
|
|
383
|
-
name: 'New Image',
|
|
384
|
-
format: 'jpeg',
|
|
385
|
-
quality: 1,
|
|
386
|
-
multiplier: 5,
|
|
387
|
-
width,
|
|
388
|
-
height,
|
|
389
|
-
left,
|
|
390
|
-
top,
|
|
391
|
-
};
|
|
392
|
-
return option;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
clear() {
|
|
396
|
-
this.canvas.getObjects().forEach((obj) => {
|
|
397
|
-
if (obj.id !== 'workspace') {
|
|
398
|
-
this.canvas.remove(obj);
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
this.editor?.setWorkspaseBg('#fff');
|
|
402
|
-
this.canvas.discardActiveObject();
|
|
403
|
-
this.canvas.renderAll();
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
destroy() {
|
|
407
|
-
console.log('pluginDestroy');
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
export default ServersPlugin;
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
import { selectFiles, clipboardText, downFile } from './utils/utils';
|
|
3
|
+
import { fabric, StaticCanvas } from 'fabric';
|
|
4
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
5
|
+
import { SelectEvent, SelectMode } from '../../plugins/src/types/eventType';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<
|
|
8
|
+
ServersPlugin,
|
|
9
|
+
| 'insert'
|
|
10
|
+
| 'loadJSON'
|
|
11
|
+
| 'getJson'
|
|
12
|
+
| 'dragAddItem'
|
|
13
|
+
| 'clipboard'
|
|
14
|
+
| 'clipboardBase64'
|
|
15
|
+
| 'saveJson'
|
|
16
|
+
| 'saveSvg'
|
|
17
|
+
| 'getBase64'
|
|
18
|
+
| 'saveImg'
|
|
19
|
+
| 'clear'
|
|
20
|
+
| 'preview'
|
|
21
|
+
| 'staticPreview'
|
|
22
|
+
| 'getSelectMode'
|
|
23
|
+
| 'getExtensionKey'
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
declare module '@hprint/core' {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
28
|
+
interface IEditor extends IPlugin { }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function transformText(objects: any) {
|
|
32
|
+
if (!objects) return;
|
|
33
|
+
objects.forEach((item: any) => {
|
|
34
|
+
if (item.objects) {
|
|
35
|
+
transformText(item.objects);
|
|
36
|
+
} else {
|
|
37
|
+
item.type === 'text' && (item.type = 'textbox');
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class ServersPlugin implements IPluginTempl {
|
|
43
|
+
public selectedMode: SelectMode;
|
|
44
|
+
static pluginName = 'ServersPlugin';
|
|
45
|
+
static apis = [
|
|
46
|
+
'insert',
|
|
47
|
+
'loadJSON',
|
|
48
|
+
'getJson',
|
|
49
|
+
'dragAddItem',
|
|
50
|
+
'clipboard',
|
|
51
|
+
'clipboardBase64',
|
|
52
|
+
'saveJson',
|
|
53
|
+
'saveSvg',
|
|
54
|
+
'saveImg',
|
|
55
|
+
'getBase64',
|
|
56
|
+
'clear',
|
|
57
|
+
'preview',
|
|
58
|
+
'staticPreview',
|
|
59
|
+
'getSelectMode',
|
|
60
|
+
'getExtensionKey',
|
|
61
|
+
];
|
|
62
|
+
static events = [SelectMode.ONE, SelectMode.MULTI, SelectEvent.CANCEL];
|
|
63
|
+
// public hotkeys: string[] = ['left', 'right', 'down', 'up'];
|
|
64
|
+
constructor(
|
|
65
|
+
public canvas: fabric.Canvas,
|
|
66
|
+
public editor: IEditor
|
|
67
|
+
) {
|
|
68
|
+
this.selectedMode = SelectMode.EMPTY;
|
|
69
|
+
this._initSelectEvent();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private _initSelectEvent() {
|
|
73
|
+
this.canvas.on('selection:created', () => this._emitSelectEvent());
|
|
74
|
+
this.canvas.on('selection:updated', () => this._emitSelectEvent());
|
|
75
|
+
this.canvas.on('selection:cleared', () => this._emitSelectEvent());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private _emitSelectEvent() {
|
|
79
|
+
if (!this.canvas) {
|
|
80
|
+
throw TypeError('还未初始化');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const actives = this.canvas
|
|
84
|
+
.getActiveObjects()
|
|
85
|
+
.filter((item) => !(item instanceof fabric.GuideLine)); // 过滤掉辅助线
|
|
86
|
+
if (actives && actives.length === 1) {
|
|
87
|
+
this.selectedMode = SelectMode.ONE;
|
|
88
|
+
this.editor.emit(SelectEvent.ONE, actives);
|
|
89
|
+
} else if (actives && actives.length > 1) {
|
|
90
|
+
this.selectedMode = SelectMode.MULTI;
|
|
91
|
+
this.editor.emit(SelectEvent.MULTI, actives);
|
|
92
|
+
} else {
|
|
93
|
+
this.editor.emit(SelectEvent.CANCEL);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getSelectMode() {
|
|
98
|
+
return String(this.selectedMode);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
insert(callback?: () => void) {
|
|
102
|
+
selectFiles({ accept: '.json' }).then((files) => {
|
|
103
|
+
if (files && files.length > 0) {
|
|
104
|
+
const file = files[0];
|
|
105
|
+
const reader = new FileReader();
|
|
106
|
+
reader.readAsText(file, 'UTF-8');
|
|
107
|
+
reader.onload = () => {
|
|
108
|
+
this.loadJSON(reader.result as string, callback);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 设置path属性
|
|
115
|
+
renderITextPath(textPaths: Record<'id' | 'path', any>[]) {
|
|
116
|
+
textPaths.forEach((item) => {
|
|
117
|
+
const object = this.canvas
|
|
118
|
+
.getObjects()
|
|
119
|
+
.find((o) => o.id === item.id);
|
|
120
|
+
if (object) {
|
|
121
|
+
fabric.Path.fromObject(item.path, (e) => {
|
|
122
|
+
object.set('path', e);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async loadJSON(jsonFile: string | object, callback?: () => void) {
|
|
129
|
+
// 确保元素存在id
|
|
130
|
+
const temp =
|
|
131
|
+
typeof jsonFile === 'string' ? JSON.parse(jsonFile) : jsonFile;
|
|
132
|
+
const textPaths: Record<'id' | 'path', any>[] = [];
|
|
133
|
+
temp.objects.forEach((item: any) => {
|
|
134
|
+
!item.id && (item.id = uuid());
|
|
135
|
+
// 收集所有路径文本元素i-text,并设置path为null
|
|
136
|
+
if (item.type === 'i-text' && item.path) {
|
|
137
|
+
textPaths.push({ id: item.id, path: item.path });
|
|
138
|
+
item.path = null;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// hookTransform遍历
|
|
143
|
+
const tempTransform = await this._transform(temp);
|
|
144
|
+
|
|
145
|
+
jsonFile = JSON.stringify(tempTransform);
|
|
146
|
+
// 加载前钩子
|
|
147
|
+
this.editor.hooksEntity.hookImportBefore.callAsync(jsonFile, () => {
|
|
148
|
+
this.canvas.loadFromJSON(
|
|
149
|
+
jsonFile,
|
|
150
|
+
() => {
|
|
151
|
+
// 把i-text对应的path加上
|
|
152
|
+
this.renderITextPath(textPaths);
|
|
153
|
+
this.canvas.renderAll();
|
|
154
|
+
// 加载后钩子
|
|
155
|
+
this.editor.hooksEntity.hookImportAfter.callAsync(
|
|
156
|
+
jsonFile,
|
|
157
|
+
() => {
|
|
158
|
+
// 修复导入带水印的json无法清除问题 #359
|
|
159
|
+
this.editor?.updateDrawStatus &&
|
|
160
|
+
typeof this.editor.updateDrawStatus ===
|
|
161
|
+
'function' &&
|
|
162
|
+
this.editor.updateDrawStatus(
|
|
163
|
+
!!temp['overlayImage']
|
|
164
|
+
);
|
|
165
|
+
this.canvas.renderAll();
|
|
166
|
+
callback && callback();
|
|
167
|
+
this.editor.emit('loadJson');
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
},
|
|
171
|
+
(originObject: any, fabricObject: fabric.Object) => {
|
|
172
|
+
this.editor.hooksEntity.hookTransformObjectEnd.callAsync(
|
|
173
|
+
{ originObject, fabricObject },
|
|
174
|
+
() => {
|
|
175
|
+
this.canvas.renderAll();
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async _transform(json: any) {
|
|
184
|
+
await this.promiseCallAsync(json);
|
|
185
|
+
if (json.objects) {
|
|
186
|
+
const all = json.objects.map((item: any) => {
|
|
187
|
+
return this._transform(item);
|
|
188
|
+
});
|
|
189
|
+
await Promise.all(all);
|
|
190
|
+
}
|
|
191
|
+
return json;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
promiseCallAsync(item: any) {
|
|
195
|
+
return new Promise((resolve) => {
|
|
196
|
+
this.editor.hooksEntity.hookTransform.callAsync(item, () => {
|
|
197
|
+
resolve(item);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
getJson(options?: {
|
|
203
|
+
clearSrc?: boolean
|
|
204
|
+
}) {
|
|
205
|
+
const keys = this.getExtensionKey();
|
|
206
|
+
const jsonObject = this.canvas.toJSON(keys);
|
|
207
|
+
if (options?.clearSrc) {
|
|
208
|
+
jsonObject.objects.forEach((item: any) => {
|
|
209
|
+
if (['qrcode', 'barcode'].includes(item.extensionType)) {
|
|
210
|
+
item.src = '';
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return jsonObject;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
getExtensionKey() {
|
|
218
|
+
return [
|
|
219
|
+
'id',
|
|
220
|
+
'gradientAngle',
|
|
221
|
+
'selectable',
|
|
222
|
+
'hasControls',
|
|
223
|
+
'linkData',
|
|
224
|
+
'editable',
|
|
225
|
+
'extensionType',
|
|
226
|
+
'extension',
|
|
227
|
+
'verticalAlign',
|
|
228
|
+
'roundValue',
|
|
229
|
+
'getBase64',
|
|
230
|
+
'lockScalingX',
|
|
231
|
+
'lockScalingY',
|
|
232
|
+
'_originSize',
|
|
233
|
+
];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @description: 拖拽添加到画布
|
|
238
|
+
* @param {Event} event
|
|
239
|
+
* @param {Object} item
|
|
240
|
+
*/
|
|
241
|
+
dragAddItem(item: fabric.Object, event?: DragEvent) {
|
|
242
|
+
if (event) {
|
|
243
|
+
const { left, top } = this.canvas
|
|
244
|
+
.getSelectionElement()
|
|
245
|
+
.getBoundingClientRect();
|
|
246
|
+
if (event.x < left || event.y < top || item.width === undefined)
|
|
247
|
+
return;
|
|
248
|
+
|
|
249
|
+
const point = {
|
|
250
|
+
x: event.x - left,
|
|
251
|
+
y: event.y - top,
|
|
252
|
+
};
|
|
253
|
+
const pointerVpt = this.canvas.restorePointerVpt(point);
|
|
254
|
+
item.left = pointerVpt.x - item.width / 2;
|
|
255
|
+
item.top = pointerVpt.y;
|
|
256
|
+
}
|
|
257
|
+
const { width } = this._getSaveOption();
|
|
258
|
+
width && item.scaleToWidth(width / 2);
|
|
259
|
+
this.canvas.add(item);
|
|
260
|
+
this.canvas.setActiveObject(item);
|
|
261
|
+
|
|
262
|
+
!event && this.editor.position('center');
|
|
263
|
+
this.canvas.requestRenderAll();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
clipboard() {
|
|
267
|
+
const jsonStr = this.getJson();
|
|
268
|
+
return clipboardText(JSON.stringify(jsonStr, null, '\t'));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async clipboardBase64() {
|
|
272
|
+
const dataUrl = await this.preview();
|
|
273
|
+
return clipboardText(dataUrl);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async saveJson() {
|
|
277
|
+
const dataUrl = this.getJson();
|
|
278
|
+
// 把文本text转为textgroup,让导入可以编辑
|
|
279
|
+
await transformText(dataUrl.objects);
|
|
280
|
+
const fileStr = `data:text/json;charset=utf-8,${encodeURIComponent(
|
|
281
|
+
JSON.stringify(dataUrl, null, '\t')
|
|
282
|
+
)}`;
|
|
283
|
+
downFile(fileStr, 'json');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
saveSvg() {
|
|
287
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
288
|
+
const { fontOption, svgOption } = this._getSaveSvgOption();
|
|
289
|
+
fabric.fontPaths = {
|
|
290
|
+
...fontOption,
|
|
291
|
+
};
|
|
292
|
+
const dataUrl = this.canvas.toSVG(svgOption);
|
|
293
|
+
const fileStr = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(dataUrl)}`;
|
|
294
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(fileStr, () => {
|
|
295
|
+
downFile(fileStr, 'svg');
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
saveImg() {
|
|
301
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
302
|
+
const option = this._getSaveOption();
|
|
303
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
304
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
305
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
306
|
+
downFile(dataUrl, 'png');
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
getBase64() {
|
|
312
|
+
return new Promise<string>((resolve) => {
|
|
313
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
314
|
+
const option = this._getSaveOption();
|
|
315
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
316
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
317
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () =>
|
|
318
|
+
resolve(dataUrl)
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
preview() {
|
|
325
|
+
return new Promise<string>((resolve) => {
|
|
326
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
327
|
+
const option = this._getSaveOption();
|
|
328
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
329
|
+
this.canvas.renderAll();
|
|
330
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
331
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
332
|
+
resolve(dataUrl);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
staticPreview() {
|
|
339
|
+
return new Promise<string>((resolve) => { });
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
_getSaveSvgOption() {
|
|
343
|
+
const workspace = this.canvas
|
|
344
|
+
.getObjects()
|
|
345
|
+
.find((item) => item.id === 'workspace');
|
|
346
|
+
let fontFamilyArry = this.canvas
|
|
347
|
+
.getObjects()
|
|
348
|
+
.filter((item) => item.type == 'textbox')
|
|
349
|
+
.map((item) => item.fontFamily);
|
|
350
|
+
fontFamilyArry = Array.from(new Set(fontFamilyArry));
|
|
351
|
+
|
|
352
|
+
const fontList = this.editor.getPlugin('FontPlugin').cacheList;
|
|
353
|
+
|
|
354
|
+
const fontEntry = {};
|
|
355
|
+
for (const font of fontFamilyArry) {
|
|
356
|
+
const item = fontList.find((item) => item.name === font);
|
|
357
|
+
fontEntry[font] = item.file;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
console.log('_getSaveSvgOption', fontEntry);
|
|
361
|
+
const { left, top, width, height } = workspace as fabric.Object;
|
|
362
|
+
return {
|
|
363
|
+
fontOption: fontEntry,
|
|
364
|
+
svgOption: {
|
|
365
|
+
width,
|
|
366
|
+
height,
|
|
367
|
+
viewBox: {
|
|
368
|
+
x: left,
|
|
369
|
+
y: top,
|
|
370
|
+
width,
|
|
371
|
+
height,
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
_getSaveOption() {
|
|
378
|
+
const workspace = this.canvas
|
|
379
|
+
.getObjects()
|
|
380
|
+
.find((item: fabric.Object) => item.id === 'workspace');
|
|
381
|
+
const { left, top, width, height } = workspace as fabric.Object;
|
|
382
|
+
const option = {
|
|
383
|
+
name: 'New Image',
|
|
384
|
+
format: 'jpeg',
|
|
385
|
+
quality: 1,
|
|
386
|
+
multiplier: 5,
|
|
387
|
+
width,
|
|
388
|
+
height,
|
|
389
|
+
left,
|
|
390
|
+
top,
|
|
391
|
+
};
|
|
392
|
+
return option;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
clear() {
|
|
396
|
+
this.canvas.getObjects().forEach((obj) => {
|
|
397
|
+
if (obj.id !== 'workspace') {
|
|
398
|
+
this.canvas.remove(obj);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
this.editor?.setWorkspaseBg('#fff');
|
|
402
|
+
this.canvas.discardActiveObject();
|
|
403
|
+
this.canvas.renderAll();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
destroy() {
|
|
407
|
+
console.log('pluginDestroy');
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export default ServersPlugin;
|
package/src/interface/Editor.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import type Editor from '@hprint/core';
|
|
2
|
-
|
|
3
|
-
// IEditor类型包含插件实例,Editor不包含插件实例
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
5
|
-
export interface IEditor extends Editor { }
|
|
6
|
-
|
|
7
|
-
// 生命周期事件类型
|
|
8
|
-
export type IEditorHooksType =
|
|
9
|
-
| 'hookImportBefore'
|
|
10
|
-
| 'hookImportAfter'
|
|
11
|
-
| 'hookSaveBefore'
|
|
12
|
-
| 'hookSaveAfter'
|
|
13
|
-
| 'hookTransform'
|
|
14
|
-
| 'hookTransformObjectEnd';
|
|
15
|
-
|
|
16
|
-
// 插件实例
|
|
17
|
-
export declare class IPluginTempl {
|
|
18
|
-
constructor(
|
|
19
|
-
canvas: fabric.Canvas,
|
|
20
|
-
editor: IEditor,
|
|
21
|
-
options?: IPluginOption
|
|
22
|
-
);
|
|
23
|
-
static pluginName: string;
|
|
24
|
-
static events: string[];
|
|
25
|
-
static apis: string[];
|
|
26
|
-
hotkeyEvent?: (name: string, e: KeyboardEvent) => void;
|
|
27
|
-
hookImportBefore?: (...args: unknown[]) => Promise<unknown>;
|
|
28
|
-
hookImportAfter?: (...args: unknown[]) => Promise<unknown>;
|
|
29
|
-
hookSaveBefore?: (...args: unknown[]) => Promise<unknown>;
|
|
30
|
-
hookSaveAfter?: (...args: unknown[]) => Promise<unknown>;
|
|
31
|
-
hookTransform?: (...args: unknown[]) => Promise<unknown>;
|
|
32
|
-
hookTransformObjectEnd?: (...args: unknown[]) => Promise<unknown>;
|
|
33
|
-
[propName: string]: any;
|
|
34
|
-
canvas?: fabric.Canvas;
|
|
35
|
-
editor?: IEditor;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export declare interface IPluginOption {
|
|
39
|
-
[propName: string]: unknown | undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
declare class IPluginClass2 extends IPluginTempl {
|
|
43
|
-
constructor();
|
|
44
|
-
}
|
|
45
|
-
// 插件class
|
|
46
|
-
export declare interface IPluginClass {
|
|
47
|
-
new(
|
|
48
|
-
canvas: fabric.Canvas,
|
|
49
|
-
editor: Editor,
|
|
50
|
-
options?: IPluginOption
|
|
51
|
-
): IPluginClass2;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export declare interface IPluginMenu {
|
|
55
|
-
text: string;
|
|
56
|
-
command?: () => void;
|
|
57
|
-
child?: IPluginMenu[];
|
|
58
|
-
}
|
|
1
|
+
import type Editor from '@hprint/core';
|
|
2
|
+
|
|
3
|
+
// IEditor类型包含插件实例,Editor不包含插件实例
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
5
|
+
export interface IEditor extends Editor { }
|
|
6
|
+
|
|
7
|
+
// 生命周期事件类型
|
|
8
|
+
export type IEditorHooksType =
|
|
9
|
+
| 'hookImportBefore'
|
|
10
|
+
| 'hookImportAfter'
|
|
11
|
+
| 'hookSaveBefore'
|
|
12
|
+
| 'hookSaveAfter'
|
|
13
|
+
| 'hookTransform'
|
|
14
|
+
| 'hookTransformObjectEnd';
|
|
15
|
+
|
|
16
|
+
// 插件实例
|
|
17
|
+
export declare class IPluginTempl {
|
|
18
|
+
constructor(
|
|
19
|
+
canvas: fabric.Canvas,
|
|
20
|
+
editor: IEditor,
|
|
21
|
+
options?: IPluginOption
|
|
22
|
+
);
|
|
23
|
+
static pluginName: string;
|
|
24
|
+
static events: string[];
|
|
25
|
+
static apis: string[];
|
|
26
|
+
hotkeyEvent?: (name: string, e: KeyboardEvent) => void;
|
|
27
|
+
hookImportBefore?: (...args: unknown[]) => Promise<unknown>;
|
|
28
|
+
hookImportAfter?: (...args: unknown[]) => Promise<unknown>;
|
|
29
|
+
hookSaveBefore?: (...args: unknown[]) => Promise<unknown>;
|
|
30
|
+
hookSaveAfter?: (...args: unknown[]) => Promise<unknown>;
|
|
31
|
+
hookTransform?: (...args: unknown[]) => Promise<unknown>;
|
|
32
|
+
hookTransformObjectEnd?: (...args: unknown[]) => Promise<unknown>;
|
|
33
|
+
[propName: string]: any;
|
|
34
|
+
canvas?: fabric.Canvas;
|
|
35
|
+
editor?: IEditor;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare interface IPluginOption {
|
|
39
|
+
[propName: string]: unknown | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
declare class IPluginClass2 extends IPluginTempl {
|
|
43
|
+
constructor();
|
|
44
|
+
}
|
|
45
|
+
// 插件class
|
|
46
|
+
export declare interface IPluginClass {
|
|
47
|
+
new(
|
|
48
|
+
canvas: fabric.Canvas,
|
|
49
|
+
editor: Editor,
|
|
50
|
+
options?: IPluginOption
|
|
51
|
+
): IPluginClass2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export declare interface IPluginMenu {
|
|
55
|
+
text: string;
|
|
56
|
+
command?: () => void;
|
|
57
|
+
child?: IPluginMenu[];
|
|
58
|
+
}
|