@hprint/plugins 0.0.1-alpha.2 → 0.0.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/index.js +17 -17
  2. package/dist/index.mjs +1071 -1057
  3. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
  4. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
  6. package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
  7. package/dist/src/plugins/QrCodePlugin.d.ts +5 -0
  8. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  9. package/package.json +3 -3
  10. package/src/assets/style/resizePlugin.css +27 -27
  11. package/src/objects/Arrow.js +47 -47
  12. package/src/objects/ThinTailArrow.js +50 -50
  13. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
  14. package/src/plugins/BarCodePlugin.ts +2 -2
  15. package/src/plugins/ControlsPlugin.ts +251 -251
  16. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  17. package/src/plugins/CopyPlugin.ts +255 -255
  18. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  19. package/src/plugins/DrawLinePlugin.ts +162 -162
  20. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  21. package/src/plugins/DringPlugin.ts +125 -125
  22. package/src/plugins/FlipPlugin.ts +59 -59
  23. package/src/plugins/FontPlugin.ts +165 -165
  24. package/src/plugins/FreeDrawPlugin.ts +49 -49
  25. package/src/plugins/GroupAlignPlugin.ts +365 -365
  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/LockPlugin.ts +242 -240
  32. package/src/plugins/MaskPlugin.ts +155 -155
  33. package/src/plugins/MaterialPlugin.ts +224 -224
  34. package/src/plugins/MiddleMousePlugin.ts +45 -45
  35. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  36. package/src/plugins/PathTextPlugin.ts +89 -89
  37. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  38. package/src/plugins/PrintPlugin.ts +81 -81
  39. package/src/plugins/PsdPlugin.ts +52 -52
  40. package/src/plugins/QrCodePlugin.ts +322 -329
  41. package/src/plugins/ResizePlugin.ts +278 -278
  42. package/src/plugins/RulerPlugin.ts +78 -78
  43. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  44. package/src/plugins/UnitPlugin.ts +326 -326
  45. package/src/plugins/WaterMarkPlugin.ts +257 -257
  46. package/src/types/eventType.ts +11 -11
  47. package/src/utils/psd.js +432 -432
  48. package/src/utils/ruler/guideline.ts +145 -145
  49. package/src/utils/ruler/index.ts +91 -91
  50. package/src/utils/ruler/ruler.ts +924 -924
  51. package/src/utils/ruler/utils.ts +162 -162
  52. package/tsconfig.json +10 -10
  53. package/vite.config.ts +29 -29
@@ -1,240 +1,242 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
- import lockImg from '../assets/lock.svg?url';
4
- import { SelectEvent, SelectMode } from '../types/eventType';
5
- // import lockImg from '../assets/rotateicon.svg?url';
6
- // import unlockImg from '../assets/unlock.svg?url'
7
-
8
- type IPlugin = Pick<LockPlugin, 'lock' | 'unLock'>;
9
-
10
- declare module '@hprint/core' {
11
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
12
- interface IEditor extends IPlugin {}
13
- }
14
-
15
- enum ItypeKey {
16
- lockMovementX = 'lockMovementX',
17
- lockMovementY = 'lockMovementY',
18
- lockRotation = 'lockRotation',
19
- lockScalingX = 'lockScalingX',
20
- lockScalingY = 'lockScalingY',
21
- }
22
-
23
- enum IControlKey {
24
- bl = 'bl',
25
- br = 'br',
26
- mb = 'mb',
27
- ml = 'ml',
28
- mr = 'mr',
29
- mt = 'mt',
30
- tl = 'tl',
31
- tr = 'tr',
32
- mtr = 'mtr',
33
- lock = 'lock',
34
- }
35
-
36
- export default class LockPlugin implements IPluginTempl {
37
- static pluginName = 'LockPlugin';
38
- static apis = ['lock', 'unLock'];
39
- constructor(
40
- public canvas: fabric.Canvas,
41
- public editor: IEditor
42
- ) {
43
- this.init();
44
- }
45
-
46
- init() {
47
- const imgEl = document.createElement('img');
48
- imgEl.src = lockImg;
49
- const that = this;
50
- function renderIcon(
51
- ctx: CanvasRenderingContext2D,
52
- left: number,
53
- top: number,
54
- styleOverride: any,
55
- fabricObject: fabric.Object
56
- ) {
57
- const iconWith = 25;
58
- ctx.save();
59
- ctx.translate(left, top);
60
- const angle = fabricObject.angle as number;
61
- ctx.rotate(fabric.util.degreesToRadians(angle));
62
- ctx.drawImage(
63
- imgEl,
64
- -iconWith / 2,
65
- -iconWith / 2,
66
- iconWith,
67
- iconWith
68
- );
69
- ctx.restore();
70
- }
71
-
72
- function unLockObject(eventData: any, transform: any): boolean {
73
- that.unLock();
74
- return true;
75
- }
76
-
77
- fabric.Object.prototype.controls.lock = new fabric.Control({
78
- x: 0.5,
79
- y: 0.5,
80
- offsetY: 0,
81
- cursorStyle: 'pointer',
82
- mouseUpHandler: unLockObject,
83
- render: renderIcon,
84
- });
85
-
86
- fabric.Textbox.prototype.controls.lock = new fabric.Control({
87
- x: 0.5,
88
- y: 0.5,
89
- offsetY: 0,
90
- cursorStyle: 'pointer',
91
- mouseUpHandler: unLockObject,
92
- render: renderIcon,
93
- });
94
- this.canvas.on('selection:created', () =>
95
- this.renderCornerByActiveObj()
96
- );
97
- this.canvas.on('selection:updated', () =>
98
- this.renderCornerByActiveObj()
99
- );
100
-
101
- // 鼠标框选不能多选锁定元素
102
- (fabric.Canvas.prototype as any)._groupSelectedObjects = function (
103
- e: any
104
- ) {
105
- const group = this._collectObjects(e);
106
- let aGroup;
107
-
108
- for (let i = group.length - 1; i >= 0; i--) {
109
- if (group[i].lockMovementX) {
110
- group.splice(i, 1);
111
- }
112
- }
113
-
114
- // do not create group for 1 element only
115
- if (group.length === 1) {
116
- this.setActiveObject(group[0], e);
117
- } else if (group.length > 1) {
118
- aGroup = new fabric.ActiveSelection(group.reverse(), {
119
- canvas: this,
120
- });
121
- this.setActiveObject(aGroup, e);
122
- }
123
- };
124
-
125
- // shift+左键点选不能多选锁定元素
126
- (fabric.Canvas.prototype as any)._handleGrouping = function (
127
- e: any,
128
- target: fabric.Object
129
- ) {
130
- const activeObject = this._activeObject;
131
- // avoid multi select when shift click on a corner
132
- if (activeObject.__corner) {
133
- return;
134
- }
135
-
136
- if (target.lockMovementX) return;
137
- if (activeObject.lockMovementX) return;
138
-
139
- if (target === activeObject) {
140
- // if it's a group, find target again, using activeGroup objects
141
- target = this.findTarget(e, true);
142
- // if even object is not found or we are on activeObjectCorner, bail out
143
- if (!target || !target.selectable) {
144
- return;
145
- }
146
- if (target.lockMovementX) return;
147
- }
148
- if (activeObject && activeObject.type === 'activeSelection') {
149
- this._updateActiveSelection(target, e);
150
- } else {
151
- this._createActiveSelection(target, e);
152
- }
153
- };
154
- }
155
-
156
- controlCornersVisible(obj: fabric.Object) {
157
- const isLocked = obj.lockMovementX;
158
- Object.values(IControlKey).forEach((key: IControlKey) => {
159
- if (key === IControlKey.lock) {
160
- obj.setControlVisible(key, isLocked);
161
- } else {
162
- obj.setControlVisible(key, !isLocked);
163
- }
164
- });
165
- }
166
-
167
- renderCornerByActiveObj() {
168
- const actives = this.canvas
169
- .getActiveObjects()
170
- .filter((item) => !(item instanceof fabric.GuideLine));
171
- if (actives && actives.length === 1) {
172
- const active = actives[0];
173
- this.controlCornersVisible(active);
174
- } else if (actives && actives.length > 1) {
175
- const active = this.canvas.getActiveObject();
176
- if (active) {
177
- this.controlCornersVisible(active);
178
- }
179
- }
180
- }
181
-
182
- hookImportAfter() {
183
- this.canvas.forEachObject((obj: fabric.Object) => {
184
- if (obj.hasControls === false && obj.selectable === false) {
185
- this.canvas.setActiveObject(obj);
186
- this.lock();
187
- }
188
- });
189
- return Promise.resolve();
190
- }
191
-
192
- lock() {
193
- const activeObject = this.canvas.getActiveObject() as fabric.Object;
194
- if (activeObject) {
195
- // 修改默认属性
196
- Object.values(ItypeKey).forEach((key: ItypeKey) => {
197
- activeObject[key] = true;
198
- });
199
- this.controlCornersVisible(activeObject);
200
- this.canvas.renderAll();
201
- this.editor.emit(SelectEvent.ONE, [activeObject]);
202
- }
203
- }
204
-
205
- unLock() {
206
- const activeObject = this.canvas.getActiveObject() as fabric.Object;
207
- if (activeObject) {
208
- activeObject.hasControls = true;
209
- activeObject.selectable = true;
210
- activeObject.evented = true;
211
- // 修改默认属性
212
- Object.values(ItypeKey).forEach((key: ItypeKey) => {
213
- activeObject[key] = false;
214
- });
215
- this.controlCornersVisible(activeObject);
216
- this.canvas.renderAll();
217
- this.editor.emit(SelectEvent.ONE, [activeObject]);
218
- }
219
- }
220
-
221
- contextMenu() {
222
- const selectedMode = this.editor.getSelectMode();
223
- const activeObject = this.canvas.getActiveObject();
224
- if (selectedMode === SelectMode.ONE && activeObject) {
225
- if (activeObject.selectable) {
226
- return [
227
- { text: '锁定', hotkey: '', onclick: () => this.lock() },
228
- ];
229
- } else {
230
- return [
231
- { text: '解锁', hotkey: '', onclick: () => this.unLock() },
232
- ];
233
- }
234
- }
235
- }
236
-
237
- destroy() {
238
- console.log('pluginDestroy');
239
- }
240
- }
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+ import lockImg from '../assets/lock.svg?url';
4
+ import { SelectEvent, SelectMode } from '../types/eventType';
5
+ // import lockImg from '../assets/rotateicon.svg?url';
6
+ // import unlockImg from '../assets/unlock.svg?url'
7
+
8
+ type IPlugin = Pick<LockPlugin, 'lock' | 'unLock'>;
9
+
10
+ declare module '@hprint/core' {
11
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
12
+ interface IEditor extends IPlugin { }
13
+ }
14
+
15
+ enum ItypeKey {
16
+ lockMovementX = 'lockMovementX',
17
+ lockMovementY = 'lockMovementY',
18
+ lockRotation = 'lockRotation',
19
+ lockScalingX = 'lockScalingX',
20
+ lockScalingY = 'lockScalingY',
21
+ }
22
+
23
+ enum IControlKey {
24
+ bl = 'bl',
25
+ br = 'br',
26
+ mb = 'mb',
27
+ ml = 'ml',
28
+ mr = 'mr',
29
+ mt = 'mt',
30
+ tl = 'tl',
31
+ tr = 'tr',
32
+ mtr = 'mtr',
33
+ lock = 'lock',
34
+ }
35
+
36
+ export default class LockPlugin implements IPluginTempl {
37
+ static pluginName = 'LockPlugin';
38
+ static apis = ['lock', 'unLock'];
39
+ constructor(
40
+ public canvas: fabric.Canvas,
41
+ public editor: IEditor
42
+ ) {
43
+ this.init();
44
+ }
45
+
46
+ init() {
47
+ const imgEl = document.createElement('img');
48
+ imgEl.src = lockImg;
49
+ const that = this;
50
+ function renderIcon(
51
+ ctx: CanvasRenderingContext2D,
52
+ left: number,
53
+ top: number,
54
+ styleOverride: any,
55
+ fabricObject: fabric.Object
56
+ ) {
57
+ const iconWith = 25;
58
+ ctx.save();
59
+ ctx.translate(left, top);
60
+ const angle = fabricObject.angle as number;
61
+ ctx.rotate(fabric.util.degreesToRadians(angle));
62
+ ctx.drawImage(
63
+ imgEl,
64
+ -iconWith / 2,
65
+ -iconWith / 2,
66
+ iconWith,
67
+ iconWith
68
+ );
69
+ ctx.restore();
70
+ }
71
+
72
+ function unLockObject(eventData: any, transform: any): boolean {
73
+ that.unLock();
74
+ return true;
75
+ }
76
+
77
+ fabric.Object.prototype.controls.lock = new fabric.Control({
78
+ x: 0.5,
79
+ y: 0.5,
80
+ offsetY: 0,
81
+ cursorStyle: 'pointer',
82
+ mouseUpHandler: unLockObject,
83
+ render: renderIcon,
84
+ });
85
+
86
+ fabric.Textbox.prototype.controls.lock = new fabric.Control({
87
+ x: 0.5,
88
+ y: 0.5,
89
+ offsetY: 0,
90
+ cursorStyle: 'pointer',
91
+ mouseUpHandler: unLockObject,
92
+ render: renderIcon,
93
+ });
94
+ this.canvas.on('selection:created', () =>
95
+ this.renderCornerByActiveObj()
96
+ );
97
+ this.canvas.on('selection:updated', () =>
98
+ this.renderCornerByActiveObj()
99
+ );
100
+
101
+ // 鼠标框选不能多选锁定元素
102
+ (fabric.Canvas.prototype as any)._groupSelectedObjects = function (
103
+ e: any
104
+ ) {
105
+ const group = this._collectObjects(e);
106
+ let aGroup;
107
+
108
+ for (let i = group.length - 1; i >= 0; i--) {
109
+ if (group[i].lockMovementX) {
110
+ group.splice(i, 1);
111
+ }
112
+ }
113
+
114
+ // do not create group for 1 element only
115
+ if (group.length === 1) {
116
+ this.setActiveObject(group[0], e);
117
+ } else if (group.length > 1) {
118
+ aGroup = new fabric.ActiveSelection(group.reverse(), {
119
+ canvas: this,
120
+ });
121
+ this.setActiveObject(aGroup, e);
122
+ }
123
+ };
124
+
125
+ // shift+左键点选不能多选锁定元素
126
+ (fabric.Canvas.prototype as any)._handleGrouping = function (
127
+ e: any,
128
+ target: fabric.Object
129
+ ) {
130
+ const activeObject = this._activeObject;
131
+ // avoid multi select when shift click on a corner
132
+ if (activeObject.__corner) {
133
+ return;
134
+ }
135
+
136
+ if (target.lockMovementX) return;
137
+ if (activeObject.lockMovementX) return;
138
+
139
+ if (target === activeObject) {
140
+ // if it's a group, find target again, using activeGroup objects
141
+ target = this.findTarget(e, true);
142
+ // if even object is not found or we are on activeObjectCorner, bail out
143
+ if (!target || !target.selectable) {
144
+ return;
145
+ }
146
+ if (target.lockMovementX) return;
147
+ }
148
+ if (activeObject && activeObject.type === 'activeSelection') {
149
+ this._updateActiveSelection(target, e);
150
+ } else {
151
+ this._createActiveSelection(target, e);
152
+ }
153
+ };
154
+ }
155
+
156
+ controlCornersVisible(obj: fabric.Object) {
157
+ const isLocked = obj.lockMovementX;
158
+ Object.values(IControlKey).forEach((key: IControlKey) => {
159
+ if (key === IControlKey.lock) {
160
+ obj.setControlVisible(key, isLocked);
161
+ } else {
162
+ obj.setControlVisible(key, !isLocked);
163
+ }
164
+ });
165
+ }
166
+
167
+ renderCornerByActiveObj() {
168
+ const actives = this.canvas
169
+ .getActiveObjects()
170
+ .filter((item) => !(item instanceof fabric.GuideLine));
171
+ if (actives && actives.length === 1) {
172
+ const active = actives[0];
173
+ this.controlCornersVisible(active);
174
+ } else if (actives && actives.length > 1) {
175
+ const active = this.canvas.getActiveObject();
176
+ if (active) {
177
+ this.controlCornersVisible(active);
178
+ }
179
+ }
180
+ }
181
+
182
+ hookImportAfter() {
183
+ this.canvas.forEachObject((obj: fabric.Object) => {
184
+ // 避免导入JSON后选中画布
185
+ if (obj.id === 'workspace') return;
186
+ if (obj.hasControls === false && obj.selectable === false) {
187
+ this.canvas.setActiveObject(obj);
188
+ this.lock();
189
+ }
190
+ });
191
+ return Promise.resolve();
192
+ }
193
+
194
+ lock() {
195
+ const activeObject = this.canvas.getActiveObject() as fabric.Object;
196
+ if (activeObject) {
197
+ // 修改默认属性
198
+ Object.values(ItypeKey).forEach((key: ItypeKey) => {
199
+ activeObject[key] = true;
200
+ });
201
+ this.controlCornersVisible(activeObject);
202
+ this.canvas.renderAll();
203
+ this.editor.emit(SelectEvent.ONE, [activeObject]);
204
+ }
205
+ }
206
+
207
+ unLock() {
208
+ const activeObject = this.canvas.getActiveObject() as fabric.Object;
209
+ if (activeObject) {
210
+ activeObject.hasControls = true;
211
+ activeObject.selectable = true;
212
+ activeObject.evented = true;
213
+ // 修改默认属性
214
+ Object.values(ItypeKey).forEach((key: ItypeKey) => {
215
+ activeObject[key] = false;
216
+ });
217
+ this.controlCornersVisible(activeObject);
218
+ this.canvas.renderAll();
219
+ this.editor.emit(SelectEvent.ONE, [activeObject]);
220
+ }
221
+ }
222
+
223
+ contextMenu() {
224
+ const selectedMode = this.editor.getSelectMode();
225
+ const activeObject = this.canvas.getActiveObject();
226
+ if (selectedMode === SelectMode.ONE && activeObject) {
227
+ if (activeObject.selectable) {
228
+ return [
229
+ { text: '锁定', hotkey: '', onclick: () => this.lock() },
230
+ ];
231
+ } else {
232
+ return [
233
+ { text: '解锁', hotkey: '', onclick: () => this.unLock() },
234
+ ];
235
+ }
236
+ }
237
+ }
238
+
239
+ destroy() {
240
+ console.log('pluginDestroy');
241
+ }
242
+ }