@hprint/plugins 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.
@@ -1,242 +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
- // 避免导入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
- }
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
+ }