@hprint/plugins 0.0.7 → 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 (37) hide show
  1. package/dist/index.js +1 -1
  2. package/dist/index.mjs +1 -1
  3. package/package.json +3 -3
  4. package/src/assets/style/resizePlugin.css +27 -27
  5. package/src/objects/Arrow.js +47 -47
  6. package/src/objects/ThinTailArrow.js +50 -50
  7. package/src/plugins/ControlsPlugin.ts +413 -413
  8. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  9. package/src/plugins/CopyPlugin.ts +261 -261
  10. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  11. package/src/plugins/DrawLinePlugin.ts +162 -162
  12. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  13. package/src/plugins/DringPlugin.ts +125 -125
  14. package/src/plugins/FlipPlugin.ts +59 -59
  15. package/src/plugins/FontPlugin.ts +165 -165
  16. package/src/plugins/FreeDrawPlugin.ts +49 -49
  17. package/src/plugins/GroupPlugin.ts +82 -82
  18. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  19. package/src/plugins/HistoryPlugin.ts +181 -181
  20. package/src/plugins/ImageStroke.ts +121 -121
  21. package/src/plugins/LayerPlugin.ts +108 -108
  22. package/src/plugins/MaskPlugin.ts +155 -155
  23. package/src/plugins/MaterialPlugin.ts +224 -224
  24. package/src/plugins/MiddleMousePlugin.ts +45 -45
  25. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  26. package/src/plugins/PathTextPlugin.ts +89 -89
  27. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  28. package/src/plugins/PrintPlugin.ts +81 -81
  29. package/src/plugins/PsdPlugin.ts +52 -52
  30. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  31. package/src/types/eventType.ts +11 -11
  32. package/src/utils/psd.js +432 -432
  33. package/src/utils/ruler/guideline.ts +145 -145
  34. package/src/utils/ruler/index.ts +91 -91
  35. package/src/utils/ruler/utils.ts +162 -162
  36. package/tsconfig.json +10 -10
  37. package/vite.config.ts +29 -29
@@ -1,261 +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
- 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;
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;
@@ -1,57 +1,57 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
-
4
- type IPlugin = Pick<DeleteHotKeyPlugin, 'del'>;
5
-
6
- declare module '@hprint/core' {
7
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
8
- interface IEditor extends IPlugin {}
9
- }
10
-
11
- class DeleteHotKeyPlugin implements IPluginTempl {
12
- static pluginName = 'DeleteHotKeyPlugin';
13
- static apis = ['del'];
14
- hotkeys: string[] = ['backspace'];
15
- constructor(
16
- public canvas: fabric.Canvas,
17
- public editor: IEditor
18
- ) {}
19
-
20
- // 快捷键扩展回调
21
- hotkeyEvent(eventName: string, e: KeyboardEvent) {
22
- if (e.type === 'keydown' && eventName === 'backspace') {
23
- this.del();
24
- }
25
- }
26
-
27
- del() {
28
- const { canvas } = this;
29
- const activeObject = canvas.getActiveObjects();
30
- if (activeObject) {
31
- activeObject.map((item) => canvas.remove(item));
32
- canvas.requestRenderAll();
33
- canvas.discardActiveObject();
34
- }
35
- }
36
-
37
- contextMenu() {
38
- const activeObject = this.canvas.getActiveObject();
39
- if (activeObject) {
40
- return [
41
- null,
42
- {
43
- text: '删除',
44
- hotkey: 'Backspace',
45
- disabled: false,
46
- onclick: () => this.del(),
47
- },
48
- ];
49
- }
50
- }
51
-
52
- destroy() {
53
- console.log('pluginDestroy');
54
- }
55
- }
56
-
57
- export default DeleteHotKeyPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ type IPlugin = Pick<DeleteHotKeyPlugin, 'del'>;
5
+
6
+ declare module '@hprint/core' {
7
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
8
+ interface IEditor extends IPlugin {}
9
+ }
10
+
11
+ class DeleteHotKeyPlugin implements IPluginTempl {
12
+ static pluginName = 'DeleteHotKeyPlugin';
13
+ static apis = ['del'];
14
+ hotkeys: string[] = ['backspace'];
15
+ constructor(
16
+ public canvas: fabric.Canvas,
17
+ public editor: IEditor
18
+ ) {}
19
+
20
+ // 快捷键扩展回调
21
+ hotkeyEvent(eventName: string, e: KeyboardEvent) {
22
+ if (e.type === 'keydown' && eventName === 'backspace') {
23
+ this.del();
24
+ }
25
+ }
26
+
27
+ del() {
28
+ const { canvas } = this;
29
+ const activeObject = canvas.getActiveObjects();
30
+ if (activeObject) {
31
+ activeObject.map((item) => canvas.remove(item));
32
+ canvas.requestRenderAll();
33
+ canvas.discardActiveObject();
34
+ }
35
+ }
36
+
37
+ contextMenu() {
38
+ const activeObject = this.canvas.getActiveObject();
39
+ if (activeObject) {
40
+ return [
41
+ null,
42
+ {
43
+ text: '删除',
44
+ hotkey: 'Backspace',
45
+ disabled: false,
46
+ onclick: () => this.del(),
47
+ },
48
+ ];
49
+ }
50
+ }
51
+
52
+ destroy() {
53
+ console.log('pluginDestroy');
54
+ }
55
+ }
56
+
57
+ export default DeleteHotKeyPlugin;