@hprint/plugins 0.0.6 → 0.0.8

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 (47) hide show
  1. package/dist/index.js +15 -15
  2. package/dist/index.mjs +1864 -1862
  3. package/dist/src/plugins/BarCodePlugin.d.ts +2 -2
  4. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/CopyPlugin.d.ts.map +1 -1
  6. package/dist/src/plugins/CreateElementPlugin.d.ts +2 -2
  7. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  8. package/dist/src/plugins/QrCodePlugin.d.ts +2 -2
  9. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  10. package/package.json +3 -3
  11. package/src/assets/style/resizePlugin.css +27 -27
  12. package/src/objects/Arrow.js +47 -47
  13. package/src/objects/ThinTailArrow.js +50 -50
  14. package/src/plugins/BarCodePlugin.ts +7 -7
  15. package/src/plugins/ControlsPlugin.ts +413 -413
  16. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  17. package/src/plugins/CopyPlugin.ts +261 -255
  18. package/src/plugins/CreateElementPlugin.ts +3 -1
  19. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  20. package/src/plugins/DrawLinePlugin.ts +162 -162
  21. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  22. package/src/plugins/DringPlugin.ts +125 -125
  23. package/src/plugins/FlipPlugin.ts +59 -59
  24. package/src/plugins/FontPlugin.ts +165 -165
  25. package/src/plugins/FreeDrawPlugin.ts +49 -49
  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/MaskPlugin.ts +155 -155
  32. package/src/plugins/MaterialPlugin.ts +224 -224
  33. package/src/plugins/MiddleMousePlugin.ts +45 -45
  34. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  35. package/src/plugins/PathTextPlugin.ts +89 -89
  36. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  37. package/src/plugins/PrintPlugin.ts +81 -81
  38. package/src/plugins/PsdPlugin.ts +52 -52
  39. package/src/plugins/QrCodePlugin.ts +6 -6
  40. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  41. package/src/types/eventType.ts +11 -11
  42. package/src/utils/psd.js +432 -432
  43. package/src/utils/ruler/guideline.ts +145 -145
  44. package/src/utils/ruler/index.ts +91 -91
  45. package/src/utils/ruler/utils.ts +162 -162
  46. package/tsconfig.json +10 -10
  47. package/vite.config.ts +29 -29
@@ -1,255 +1,261 @@
1
- import { fabric } from '@hprint/core';
2
- import { v4 as uuid } from 'uuid';
3
- import { utils } from '@hprint/shared';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPlugin = Pick<CopyPlugin, 'clone'>;
7
-
8
- declare module '@hprint/core' {
9
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
- interface IEditor extends IPlugin {}
11
- }
12
-
13
- class CopyPlugin implements IPluginTempl {
14
- static pluginName = 'CopyPlugin';
15
- static apis = ['clone'];
16
- hotkeys: string[] = ['ctrl+v', 'ctrl+c'];
17
- private cache: null | fabric.ActiveSelection | fabric.Object = null;
18
- constructor(
19
- public canvas: fabric.Canvas,
20
- public editor: IEditor
21
- ) {
22
- this.initPaste();
23
- }
24
-
25
- // 多选对象复制
26
- _copyActiveSelection(activeObject: fabric.Object) {
27
- // 间距设置
28
- const grid = 10;
29
- const canvas = this.canvas;
30
- const keys = this.editor.getExtensionKey();
31
- activeObject?.clone((cloned: fabric.Object) => {
32
- // 再次进行克隆,处理选择多个对象的情况
33
- cloned.clone((clonedObj: fabric.ActiveSelection) => {
34
- canvas.discardActiveObject();
35
- if (clonedObj.left === undefined || clonedObj.top === undefined)
36
- return;
37
- // 将克隆的画布重新赋值
38
- clonedObj.canvas = canvas;
39
- // 设置位置信息
40
- clonedObj.set({
41
- left: clonedObj.left + grid,
42
- top: clonedObj.top + grid,
43
- evented: true,
44
- id: uuid(),
45
- });
46
- clonedObj.forEachObject((obj: fabric.Object) => {
47
- obj.id = uuid();
48
- canvas.add(obj);
49
- });
50
- // 解决不可选择问题
51
- clonedObj.setCoords();
52
- canvas.setActiveObject(clonedObj);
53
- canvas.requestRenderAll();
54
- });
55
- }, keys);
56
- }
57
-
58
- // 单个对象复制
59
- _copyObject(activeObject: fabric.Object) {
60
- // 间距设置
61
- const grid = 10;
62
- const canvas = this.canvas;
63
- const keys = this.editor.getExtensionKey();
64
- activeObject?.clone((cloned: fabric.Object) => {
65
- if (cloned.left === undefined || cloned.top === undefined) return;
66
- canvas.discardActiveObject();
67
- // 设置位置信息
68
- cloned.set({
69
- left: cloned.left + grid,
70
- top: cloned.top + grid,
71
- evented: true,
72
- id: uuid(),
73
- });
74
- canvas.add(cloned);
75
- canvas.setActiveObject(cloned);
76
- canvas.requestRenderAll();
77
- }, keys);
78
- }
79
-
80
- // 复制元素
81
- clone(paramsActiveObeject?: fabric.ActiveSelection | fabric.Object) {
82
- const activeObject =
83
- paramsActiveObeject || this.canvas.getActiveObject();
84
- if (!activeObject) return;
85
- if (activeObject?.type === 'activeSelection') {
86
- this._copyActiveSelection(activeObject);
87
- } else {
88
- this._copyObject(activeObject);
89
- }
90
- }
91
-
92
- // 快捷键扩展回调
93
- hotkeyEvent(eventName: string, e: KeyboardEvent) {
94
- if (eventName === 'ctrl+c' && e.type === 'keydown') {
95
- const activeObject = this.canvas.getActiveObject();
96
- this.cache = activeObject;
97
- // 清空剪切板
98
- navigator.clipboard.writeText('');
99
- }
100
- if (eventName === 'ctrl+v' && e.type === 'keydown') {
101
- // 确保clone元素操作的执行晚于pasteListener
102
- setTimeout(() => {
103
- if (this.cache) {
104
- this.clone(this.cache);
105
- }
106
- }, 0);
107
- }
108
- }
109
-
110
- contextMenu() {
111
- const activeObject = this.canvas.getActiveObject();
112
- if (activeObject) {
113
- return [
114
- {
115
- text: '复制',
116
- hotkey: 'Ctrl+V',
117
- disabled: false,
118
- onclick: () => this.clone(),
119
- },
120
- ];
121
- }
122
- }
123
-
124
- destroy() {
125
- console.log('pluginDestroy');
126
- window.removeEventListener('paste', (e) => this.pasteListener(e));
127
- }
128
-
129
- initPaste() {
130
- window.addEventListener('paste', (e) => this.pasteListener(e));
131
- }
132
-
133
- async pasteListener(event: any) {
134
- const canvas = this.canvas;
135
- if (document.activeElement === document.body) {
136
- event.preventDefault(); // 阻止默认粘贴行为
137
- } else {
138
- return;
139
- }
140
-
141
- const items = (event.clipboardData || event.originalEvent.clipboardData)
142
- .items;
143
- const fileAccept =
144
- '.pdf,.psd,.cdr,.ai,.svg,.jpg,.jpeg,.png,.webp,.json';
145
- for (const item of items) {
146
- if (item.kind === 'file') {
147
- const file = item.getAsFile();
148
- const curFileSuffix: string | undefined = file.name
149
- .split('.')
150
- .pop();
151
- if (!fileAccept.split(',').includes(`.${curFileSuffix}`))
152
- return;
153
- if (curFileSuffix === 'svg') {
154
- const svgFile = await utils.getImgStr(file);
155
- if (!svgFile) throw new Error('file is undefined');
156
- fabric.loadSVGFromURL(
157
- svgFile as string,
158
- (objects, options) => {
159
- const item = fabric.util.groupSVGElements(objects, {
160
- ...options,
161
- name: 'defaultSVG',
162
- id: uuid(),
163
- });
164
- canvas.add(item).centerObject(item).renderAll();
165
- }
166
- );
167
- }
168
- // if (curFileSuffix === 'json') {
169
- // const dataText = await getImageText(file);
170
- // const template = JSON.parse(dataText);
171
- // addTemplate(template);
172
- // }
173
- if (item.type.indexOf('image/') === 0) {
174
- // 这是一个图片文件
175
- const imageUrl = URL.createObjectURL(file);
176
- const imgEl = document.createElement('img');
177
- imgEl.src = imageUrl;
178
- // 插入页面
179
- document.body.appendChild(imgEl);
180
- imgEl.onload = () => {
181
- // 创建图片对象
182
- const imgInstance = new fabric.Image(imgEl, {
183
- id: uuid(),
184
- name: '图片1',
185
- left: 100,
186
- top: 100,
187
- });
188
- // 设置缩放
189
- canvas.add(imgInstance);
190
- canvas.setActiveObject(imgInstance);
191
- canvas.renderAll();
192
- // 删除页面中的图片元素
193
- imgEl.remove();
194
- };
195
- }
196
- } else if (
197
- item.kind === 'string' &&
198
- item.type.indexOf('text/plain') === 0
199
- ) {
200
- // 文本数据
201
- item.getAsString((text: any) => {
202
- // 插入到文本框
203
- const activeObject =
204
- canvas.getActiveObject() as fabric.Textbox;
205
- // 如果是激活的文字把复制的内容插入到对应光标位置
206
- if (
207
- activeObject &&
208
- (activeObject.type === 'textbox' ||
209
- activeObject.type === 'i-text') &&
210
- activeObject.text
211
- ) {
212
- const cursorPosition = activeObject.selectionStart;
213
- const textBeforeCursorPosition =
214
- activeObject.text.substring(0, cursorPosition);
215
- const textAfterCursorPosition =
216
- activeObject.text.substring(
217
- cursorPosition as number
218
- );
219
-
220
- // 更新文本对象的文本
221
- activeObject.set(
222
- 'text',
223
- textBeforeCursorPosition +
224
- text +
225
- textAfterCursorPosition
226
- );
227
-
228
- // 重新设置光标的位置
229
- activeObject.selectionStart =
230
- cursorPosition + text.length;
231
- activeObject.selectionEnd =
232
- cursorPosition + text.length;
233
-
234
- // 重新渲染画布展示更新后的文本
235
- activeObject.dirty = true;
236
- canvas.renderAll();
237
- } else {
238
- const fabricText = new fabric.IText(text, {
239
- left: 100,
240
- top: 100,
241
- fontSize: 80,
242
- id: uuid(),
243
- });
244
- canvas.add(fabricText);
245
- canvas.setActiveObject(fabricText);
246
- }
247
- });
248
- }
249
- }
250
- // 复制浏览器外的元素时,清空暂存的画布内粘贴元素
251
- if (items.length) this.cache = null;
252
- }
253
- }
254
-
255
- export default CopyPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import { v4 as uuid } from 'uuid';
3
+ import { utils } from '@hprint/shared';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPlugin = Pick<CopyPlugin, 'clone'>;
7
+
8
+ declare module '@hprint/core' {
9
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
+ interface IEditor extends IPlugin {}
11
+ }
12
+
13
+ class CopyPlugin implements IPluginTempl {
14
+ static pluginName = 'CopyPlugin';
15
+ static apis = ['clone'];
16
+ hotkeys: string[] = ['ctrl+v', 'ctrl+c'];
17
+ private cache: null | fabric.ActiveSelection | fabric.Object = null;
18
+ constructor(
19
+ public canvas: fabric.Canvas,
20
+ public editor: IEditor
21
+ ) {
22
+ this.initPaste();
23
+ }
24
+
25
+ // 多选对象复制
26
+ _copyActiveSelection(activeObject: fabric.Object) {
27
+ // 间距设置
28
+ const grid = 10;
29
+ const canvas = this.canvas;
30
+ const keys = this.editor.getExtensionKey();
31
+ activeObject?.clone((cloned: fabric.Object) => {
32
+ // 再次进行克隆,处理选择多个对象的情况
33
+ cloned.clone((clonedObj: fabric.ActiveSelection) => {
34
+ canvas.discardActiveObject();
35
+ if (clonedObj.left === undefined || clonedObj.top === undefined)
36
+ return;
37
+ // 将克隆的画布重新赋值
38
+ clonedObj.canvas = canvas;
39
+ // 设置位置信息
40
+ clonedObj.set({
41
+ left: clonedObj.left + grid,
42
+ top: clonedObj.top + grid,
43
+ evented: true,
44
+ id: uuid(),
45
+ });
46
+ clonedObj.forEachObject((obj: fabric.Object) => {
47
+ obj.id = uuid();
48
+ canvas.add(obj);
49
+ });
50
+ // 解决不可选择问题
51
+ clonedObj.setCoords();
52
+ canvas.setActiveObject(clonedObj);
53
+ canvas.requestRenderAll();
54
+ });
55
+ }, keys);
56
+ }
57
+
58
+ // 单个对象复制
59
+ _copyObject(activeObject: fabric.Object) {
60
+ // 间距设置
61
+ const grid = 10;
62
+ const canvas = this.canvas;
63
+ const keys = this.editor.getExtensionKey();
64
+ activeObject?.clone((cloned: fabric.Object) => {
65
+ if (cloned.left === undefined || cloned.top === undefined) return;
66
+ canvas.discardActiveObject();
67
+ // 设置位置信息
68
+ cloned.set({
69
+ left: cloned.left + grid,
70
+ top: cloned.top + grid,
71
+ evented: true,
72
+ id: uuid(),
73
+ });
74
+ this.editor.addSetAndSyncByUnit(cloned);
75
+ if (cloned.extensionType === 'barcode') {
76
+ this.editor.initBarcodeEvents(cloned)
77
+ } else if (cloned.extensionType === 'qrcode') {
78
+ this.editor.initQrcodeEvents(cloned);
79
+ }
80
+ canvas.add(cloned);
81
+ canvas.setActiveObject(cloned);
82
+ canvas.requestRenderAll();
83
+ }, keys);
84
+ }
85
+
86
+ // 复制元素
87
+ clone(paramsActiveObeject?: fabric.ActiveSelection | fabric.Object) {
88
+ const activeObject =
89
+ paramsActiveObeject || this.canvas.getActiveObject();
90
+ if (!activeObject) return;
91
+ if (activeObject?.type === 'activeSelection') {
92
+ this._copyActiveSelection(activeObject);
93
+ } else {
94
+ this._copyObject(activeObject);
95
+ }
96
+ }
97
+
98
+ // 快捷键扩展回调
99
+ hotkeyEvent(eventName: string, e: KeyboardEvent) {
100
+ if (eventName === 'ctrl+c' && e.type === 'keydown') {
101
+ const activeObject = this.canvas.getActiveObject();
102
+ this.cache = activeObject;
103
+ // 清空剪切板
104
+ navigator.clipboard.writeText('');
105
+ }
106
+ if (eventName === 'ctrl+v' && e.type === 'keydown') {
107
+ // 确保clone元素操作的执行晚于pasteListener
108
+ setTimeout(() => {
109
+ if (this.cache) {
110
+ this.clone(this.cache);
111
+ }
112
+ }, 0);
113
+ }
114
+ }
115
+
116
+ contextMenu() {
117
+ const activeObject = this.canvas.getActiveObject();
118
+ if (activeObject) {
119
+ return [
120
+ {
121
+ text: '复制',
122
+ hotkey: 'Ctrl+V',
123
+ disabled: false,
124
+ onclick: () => this.clone(),
125
+ },
126
+ ];
127
+ }
128
+ }
129
+
130
+ destroy() {
131
+ console.log('pluginDestroy');
132
+ window.removeEventListener('paste', (e) => this.pasteListener(e));
133
+ }
134
+
135
+ initPaste() {
136
+ window.addEventListener('paste', (e) => this.pasteListener(e));
137
+ }
138
+
139
+ async pasteListener(event: any) {
140
+ const canvas = this.canvas;
141
+ if (document.activeElement === document.body) {
142
+ event.preventDefault(); // 阻止默认粘贴行为
143
+ } else {
144
+ return;
145
+ }
146
+
147
+ const items = (event.clipboardData || event.originalEvent.clipboardData)
148
+ .items;
149
+ const fileAccept =
150
+ '.pdf,.psd,.cdr,.ai,.svg,.jpg,.jpeg,.png,.webp,.json';
151
+ for (const item of items) {
152
+ if (item.kind === 'file') {
153
+ const file = item.getAsFile();
154
+ const curFileSuffix: string | undefined = file.name
155
+ .split('.')
156
+ .pop();
157
+ if (!fileAccept.split(',').includes(`.${curFileSuffix}`))
158
+ return;
159
+ if (curFileSuffix === 'svg') {
160
+ const svgFile = await utils.getImgStr(file);
161
+ if (!svgFile) throw new Error('file is undefined');
162
+ fabric.loadSVGFromURL(
163
+ svgFile as string,
164
+ (objects, options) => {
165
+ const item = fabric.util.groupSVGElements(objects, {
166
+ ...options,
167
+ name: 'defaultSVG',
168
+ id: uuid(),
169
+ });
170
+ canvas.add(item).centerObject(item).renderAll();
171
+ }
172
+ );
173
+ }
174
+ // if (curFileSuffix === 'json') {
175
+ // const dataText = await getImageText(file);
176
+ // const template = JSON.parse(dataText);
177
+ // addTemplate(template);
178
+ // }
179
+ if (item.type.indexOf('image/') === 0) {
180
+ // 这是一个图片文件
181
+ const imageUrl = URL.createObjectURL(file);
182
+ const imgEl = document.createElement('img');
183
+ imgEl.src = imageUrl;
184
+ // 插入页面
185
+ document.body.appendChild(imgEl);
186
+ imgEl.onload = () => {
187
+ // 创建图片对象
188
+ const imgInstance = new fabric.Image(imgEl, {
189
+ id: uuid(),
190
+ name: '图片1',
191
+ left: 100,
192
+ top: 100,
193
+ });
194
+ // 设置缩放
195
+ canvas.add(imgInstance);
196
+ canvas.setActiveObject(imgInstance);
197
+ canvas.renderAll();
198
+ // 删除页面中的图片元素
199
+ imgEl.remove();
200
+ };
201
+ }
202
+ } else if (
203
+ item.kind === 'string' &&
204
+ item.type.indexOf('text/plain') === 0
205
+ ) {
206
+ // 文本数据
207
+ item.getAsString((text: any) => {
208
+ // 插入到文本框
209
+ const activeObject =
210
+ canvas.getActiveObject() as fabric.Textbox;
211
+ // 如果是激活的文字把复制的内容插入到对应光标位置
212
+ if (
213
+ activeObject &&
214
+ (activeObject.type === 'textbox' ||
215
+ activeObject.type === 'i-text') &&
216
+ activeObject.text
217
+ ) {
218
+ const cursorPosition = activeObject.selectionStart;
219
+ const textBeforeCursorPosition =
220
+ activeObject.text.substring(0, cursorPosition);
221
+ const textAfterCursorPosition =
222
+ activeObject.text.substring(
223
+ cursorPosition as number
224
+ );
225
+
226
+ // 更新文本对象的文本
227
+ activeObject.set(
228
+ 'text',
229
+ textBeforeCursorPosition +
230
+ text +
231
+ textAfterCursorPosition
232
+ );
233
+
234
+ // 重新设置光标的位置
235
+ activeObject.selectionStart =
236
+ cursorPosition + text.length;
237
+ activeObject.selectionEnd =
238
+ cursorPosition + text.length;
239
+
240
+ // 重新渲染画布展示更新后的文本
241
+ activeObject.dirty = true;
242
+ canvas.renderAll();
243
+ } else {
244
+ const fabricText = new fabric.IText(text, {
245
+ left: 100,
246
+ top: 100,
247
+ fontSize: 80,
248
+ id: uuid(),
249
+ });
250
+ canvas.add(fabricText);
251
+ canvas.setActiveObject(fabricText);
252
+ }
253
+ });
254
+ }
255
+ }
256
+ // 复制浏览器外的元素时,清空暂存的画布内粘贴元素
257
+ if (items.length) this.cache = null;
258
+ }
259
+ }
260
+
261
+ export default CopyPlugin;
@@ -15,6 +15,7 @@ type IPlugin = Pick<
15
15
  | 'createImage'
16
16
  | 'createBarcode'
17
17
  | 'createQrcode'
18
+ | 'addSetAndSyncByUnit'
18
19
  >;
19
20
 
20
21
  declare module '@hprint/core' {
@@ -34,6 +35,7 @@ class CreateElementPlugin implements IPluginTempl {
34
35
  'createImage',
35
36
  'createBarcode',
36
37
  'createQrcode',
38
+ 'addSetAndSyncByUnit',
37
39
  ];
38
40
 
39
41
  static lengthFieldConfigs: Array<{ field: string; dealMethod: 'single' | 'points' }> = [
@@ -76,7 +78,7 @@ class CreateElementPlugin implements IPluginTempl {
76
78
  /**
77
79
  * 覆盖指定对象实例的 set 方法,仅在本插件创建的元素上生效
78
80
  */
79
- private addSetAndSyncByUnit(obj: fabric.Object) {
81
+ addSetAndSyncByUnit(obj: fabric.Object) {
80
82
  const originalSet = obj.set.bind(obj);
81
83
  const editorRef = this.editor;
82
84
  const singleFields = CreateElementPlugin.lengthFieldConfigs