@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,365 +1,365 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
-
4
- type IPlugin = Pick<
5
- GroupAlignPlugin,
6
- | 'left'
7
- | 'right'
8
- | 'xcenter'
9
- | 'ycenter'
10
- | 'top'
11
- | 'bottom'
12
- | 'xequation'
13
- | 'yequation'
14
- >;
15
-
16
- declare module '@hprint/core' {
17
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
18
- interface IEditor extends IPlugin { }
19
- }
20
-
21
- class GroupAlignPlugin implements IPluginTempl {
22
- static pluginName = 'GroupAlignPlugin';
23
- static apis = [
24
- 'left',
25
- 'right',
26
- 'xcenter',
27
- 'ycenter',
28
- 'top',
29
- 'bottom',
30
- 'xequation',
31
- 'yequation',
32
- ];
33
- // public hotkeys: string[] = ['space'];
34
- constructor(
35
- public canvas: fabric.Canvas,
36
- public editor: IEditor
37
- ) { }
38
-
39
- left() {
40
- const { canvas } = this;
41
-
42
- const activeObject = canvas.getActiveObject();
43
- const selectObjects = canvas.getActiveObjects();
44
- if (activeObject) {
45
- const { left = 0 } = activeObject;
46
- canvas.discardActiveObject();
47
- selectObjects.forEach((item) => {
48
- const bounding = item.getBoundingRect(true);
49
- item.set({
50
- left: left - bounding.left + Number(item.left),
51
- });
52
- item.setCoords();
53
- });
54
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
55
- canvas: canvas,
56
- });
57
- canvas.setActiveObject(activeSelection);
58
- canvas.requestRenderAll();
59
- }
60
- }
61
-
62
- right() {
63
- const { canvas } = this;
64
-
65
- const activeObject = canvas.getActiveObject();
66
- const selectObjects = canvas.getActiveObjects();
67
- if (activeObject) {
68
- const { left = 0, width = 0 } = activeObject;
69
- canvas.discardActiveObject();
70
- selectObjects.forEach((item) => {
71
- const bounding = item.getBoundingRect(true);
72
- item.set({
73
- left:
74
- left +
75
- width -
76
- (bounding.left + bounding.width) +
77
- Number(item.left),
78
- });
79
- });
80
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
81
- canvas: canvas,
82
- });
83
- canvas.setActiveObject(activeSelection);
84
- canvas.requestRenderAll();
85
- }
86
- }
87
-
88
- xcenter() {
89
- const { canvas } = this;
90
-
91
- const activeObject = canvas.getActiveObject();
92
- const selectObjects = canvas.getActiveObjects();
93
- if (activeObject) {
94
- const { left = 0, width = 0 } = activeObject;
95
- canvas.discardActiveObject();
96
- selectObjects.forEach((item) => {
97
- const bounding = item.getBoundingRect(true);
98
- item.set({
99
- left:
100
- left +
101
- width / 2 -
102
- (bounding.left + bounding.width / 2) +
103
- Number(item.left),
104
- });
105
- });
106
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
107
- canvas: canvas,
108
- });
109
- canvas.setActiveObject(activeSelection);
110
- canvas.requestRenderAll();
111
- }
112
- }
113
-
114
- ycenter() {
115
- const { canvas } = this;
116
-
117
- const activeObject = canvas.getActiveObject();
118
- const selectObjects = canvas.getActiveObjects();
119
- if (activeObject) {
120
- const { top = 0, height = 0 } = activeObject;
121
- canvas.discardActiveObject();
122
- selectObjects.forEach((item) => {
123
- const bounding = item.getBoundingRect(true);
124
- item.set({
125
- top:
126
- top +
127
- height / 2 -
128
- (bounding.top + bounding.height / 2) +
129
- Number(item.top),
130
- });
131
- });
132
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
133
- canvas: canvas,
134
- });
135
- canvas.setActiveObject(activeSelection);
136
- canvas.requestRenderAll();
137
- }
138
- }
139
-
140
- top() {
141
- const { canvas } = this;
142
-
143
- const activeObject = canvas.getActiveObject();
144
- const selectObjects = canvas.getActiveObjects();
145
- if (activeObject) {
146
- const { top = 0 } = activeObject;
147
- canvas.discardActiveObject();
148
- selectObjects.forEach((item) => {
149
- const bounding = item.getBoundingRect(true);
150
- item.set({
151
- top: top - bounding.top + Number(item.top),
152
- });
153
- });
154
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
155
- canvas: canvas,
156
- });
157
- canvas.setActiveObject(activeSelection);
158
- canvas.requestRenderAll();
159
- }
160
- }
161
-
162
- bottom() {
163
- const { canvas } = this;
164
-
165
- const activeObject = canvas.getActiveObject();
166
- const selectObjects = canvas.getActiveObjects();
167
- if (activeObject) {
168
- const { top = 0, height = 0 } = activeObject;
169
- canvas.discardActiveObject();
170
- selectObjects.forEach((item) => {
171
- const bounding = item.getBoundingRect(true);
172
- item.set({
173
- top:
174
- top +
175
- height -
176
- (bounding.top + bounding.height) +
177
- Number(item.top),
178
- });
179
- });
180
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
181
- canvas: canvas,
182
- });
183
- canvas.setActiveObject(activeSelection);
184
- canvas.requestRenderAll();
185
- }
186
- }
187
-
188
- xequation() {
189
- const { canvas } = this;
190
- const activeObject = canvas.getActiveObject() as fabric.ActiveSelection;
191
- // width属性不准确,需要坐标换算
192
- function getItemWidth(item: fabric.Object) {
193
- let x1 = Infinity,
194
- x2 = -Infinity;
195
- for (const key in item.aCoords) {
196
- if (item.aCoords[key].x < x1) {
197
- x1 = item.aCoords[key].x;
198
- }
199
- if (item.aCoords[key].x > x2) {
200
- x2 = item.aCoords[key].x;
201
- }
202
- }
203
- return x2 - x1;
204
- }
205
-
206
- // 获取所有元素高度
207
- function getAllItemHeight() {
208
- let count = 0;
209
- if (activeObject) {
210
- activeObject.forEachObject((item: fabric.Object) => {
211
- count += getItemWidth(item);
212
- });
213
- }
214
-
215
- return count;
216
- }
217
- // 获取平均间距
218
- function spacWidth() {
219
- const count = getAllItemHeight();
220
- if (activeObject) {
221
- const allSpac = Number(activeObject.width) - count;
222
- return allSpac / (activeObject._objects.length - 1);
223
- }
224
- }
225
-
226
- // 获取当前元素之前所有元素的高度
227
- function getItemLeft(i: number) {
228
- if (i === 0) return 0;
229
- let width = 0;
230
- if (activeObject) {
231
- for (let index = 0; index < i; index++) {
232
- width += getItemWidth(activeObject._objects[index]);
233
- }
234
- }
235
-
236
- return width;
237
- }
238
- if (activeObject && activeObject.type === 'activeSelection') {
239
- const activeSelection = activeObject;
240
- // 排序
241
- activeSelection._objects.sort((a, b) => a.left - b.left);
242
-
243
- // 平均间距计算
244
- const itemSpac = spacWidth() as number;
245
- // 组原点高度
246
- const yHeight = Number(activeObject.width) / 2;
247
-
248
- activeObject.forEachObject((item: fabric.Object, i: number) => {
249
- // 获取当前元素之前所有元素的高度
250
- const preHeight = getItemLeft(i);
251
- // 顶部距离 间距 * 索引 + 之前元素高度 - 原点高度
252
- const top = itemSpac * i + preHeight - yHeight;
253
- item.set('left', top);
254
- });
255
- }
256
-
257
- const objecs = canvas.getActiveObjects();
258
- canvas.discardActiveObject();
259
- objecs.forEach((item: fabric.Object) => {
260
- let x = Infinity;
261
- for (const key in item.aCoords) {
262
- if (item.aCoords[key].x < x) {
263
- x = item.aCoords[key].x;
264
- }
265
- }
266
- item.set('left', 2 * item.left - x);
267
- });
268
-
269
- const sel = new fabric.ActiveSelection(objecs, {
270
- canvas: canvas,
271
- });
272
- canvas.setActiveObject(sel);
273
- canvas.requestRenderAll();
274
- }
275
-
276
- yequation() {
277
- const { canvas } = this;
278
- const activeObject =
279
- (canvas.getActiveObject() as fabric.ActiveSelection) || {
280
- top: 0,
281
- height: 0,
282
- };
283
- // width属性不准确,需要坐标换算
284
- function getItemHeight(item: fabric.Object) {
285
- let y1 = Infinity,
286
- y2 = -Infinity;
287
- for (const key in item.aCoords) {
288
- if (item.aCoords[key].y < y1) {
289
- y1 = item.aCoords[key].y;
290
- }
291
- if (item.aCoords[key].y > y2) {
292
- y2 = item.aCoords[key].y;
293
- }
294
- }
295
- return y2 - y1;
296
- }
297
- // 获取所有元素高度
298
- function getAllItemHeight() {
299
- let count = 0;
300
- activeObject.forEachObject((item: fabric.Object) => {
301
- count += getItemHeight(item);
302
- });
303
- return count;
304
- }
305
- // 获取平均间距
306
- function spacHeight() {
307
- const count = getAllItemHeight();
308
- const allSpac = activeObject.height - count;
309
- return allSpac / (activeObject._objects.length - 1);
310
- }
311
-
312
- // 获取当前元素之前所有元素的高度
313
- function getItemTop(i: number) {
314
- if (i === 0) return 0;
315
- let height = 0;
316
- for (let index = 0; index < i; index++) {
317
- height += getItemHeight(activeObject._objects[index]);
318
- }
319
- return height;
320
- }
321
-
322
- if (activeObject && activeObject.type === 'activeSelection') {
323
- const activeSelection = activeObject;
324
- // 排序
325
- activeSelection._objects.sort((a, b) => a.top - b.top);
326
-
327
- // 平均间距计算
328
- const itemSpac = spacHeight();
329
- // 组原点高度
330
- const yHeight = Number(activeObject.height) / 2;
331
-
332
- activeObject.forEachObject((item: fabric.Object, i: number) => {
333
- // 获取当前元素之前所有元素的高度
334
- const preHeight = getItemTop(i);
335
- // 顶部距离 间距 * 索引 + 之前元素高度 - 原点高度
336
- const top = itemSpac * i + preHeight - yHeight;
337
- item.set('top', top);
338
- });
339
- }
340
-
341
- const objecs = canvas.getActiveObjects();
342
- canvas.discardActiveObject();
343
- objecs.forEach((item) => {
344
- let y = Infinity;
345
- for (const key in item.aCoords) {
346
- if (item.aCoords[key].y < y) {
347
- y = item.aCoords[key].y;
348
- }
349
- }
350
- item.set('top', 2 * item.top - y);
351
- });
352
-
353
- const sel = new fabric.ActiveSelection(objecs, {
354
- canvas: canvas,
355
- });
356
- canvas.setActiveObject(sel);
357
- canvas.requestRenderAll();
358
- }
359
-
360
- destroy() {
361
- console.log('pluginDestroy');
362
- }
363
- }
364
-
365
- export default GroupAlignPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ type IPlugin = Pick<
5
+ GroupAlignPlugin,
6
+ | 'left'
7
+ | 'right'
8
+ | 'xcenter'
9
+ | 'ycenter'
10
+ | 'top'
11
+ | 'bottom'
12
+ | 'xequation'
13
+ | 'yequation'
14
+ >;
15
+
16
+ declare module '@hprint/core' {
17
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
18
+ interface IEditor extends IPlugin { }
19
+ }
20
+
21
+ class GroupAlignPlugin implements IPluginTempl {
22
+ static pluginName = 'GroupAlignPlugin';
23
+ static apis = [
24
+ 'left',
25
+ 'right',
26
+ 'xcenter',
27
+ 'ycenter',
28
+ 'top',
29
+ 'bottom',
30
+ 'xequation',
31
+ 'yequation',
32
+ ];
33
+ // public hotkeys: string[] = ['space'];
34
+ constructor(
35
+ public canvas: fabric.Canvas,
36
+ public editor: IEditor
37
+ ) { }
38
+
39
+ left() {
40
+ const { canvas } = this;
41
+
42
+ const activeObject = canvas.getActiveObject();
43
+ const selectObjects = canvas.getActiveObjects();
44
+ if (activeObject) {
45
+ const { left = 0 } = activeObject;
46
+ canvas.discardActiveObject();
47
+ selectObjects.forEach((item) => {
48
+ const bounding = item.getBoundingRect(true);
49
+ item.set({
50
+ left: left - bounding.left + Number(item.left),
51
+ });
52
+ item.setCoords();
53
+ });
54
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
55
+ canvas: canvas,
56
+ });
57
+ canvas.setActiveObject(activeSelection);
58
+ canvas.requestRenderAll();
59
+ }
60
+ }
61
+
62
+ right() {
63
+ const { canvas } = this;
64
+
65
+ const activeObject = canvas.getActiveObject();
66
+ const selectObjects = canvas.getActiveObjects();
67
+ if (activeObject) {
68
+ const { left = 0, width = 0 } = activeObject;
69
+ canvas.discardActiveObject();
70
+ selectObjects.forEach((item) => {
71
+ const bounding = item.getBoundingRect(true);
72
+ item.set({
73
+ left:
74
+ left +
75
+ width -
76
+ (bounding.left + bounding.width) +
77
+ Number(item.left),
78
+ });
79
+ });
80
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
81
+ canvas: canvas,
82
+ });
83
+ canvas.setActiveObject(activeSelection);
84
+ canvas.requestRenderAll();
85
+ }
86
+ }
87
+
88
+ xcenter() {
89
+ const { canvas } = this;
90
+
91
+ const activeObject = canvas.getActiveObject();
92
+ const selectObjects = canvas.getActiveObjects();
93
+ if (activeObject) {
94
+ const { left = 0, width = 0 } = activeObject;
95
+ canvas.discardActiveObject();
96
+ selectObjects.forEach((item) => {
97
+ const bounding = item.getBoundingRect(true);
98
+ item.set({
99
+ left:
100
+ left +
101
+ width / 2 -
102
+ (bounding.left + bounding.width / 2) +
103
+ Number(item.left),
104
+ });
105
+ });
106
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
107
+ canvas: canvas,
108
+ });
109
+ canvas.setActiveObject(activeSelection);
110
+ canvas.requestRenderAll();
111
+ }
112
+ }
113
+
114
+ ycenter() {
115
+ const { canvas } = this;
116
+
117
+ const activeObject = canvas.getActiveObject();
118
+ const selectObjects = canvas.getActiveObjects();
119
+ if (activeObject) {
120
+ const { top = 0, height = 0 } = activeObject;
121
+ canvas.discardActiveObject();
122
+ selectObjects.forEach((item) => {
123
+ const bounding = item.getBoundingRect(true);
124
+ item.set({
125
+ top:
126
+ top +
127
+ height / 2 -
128
+ (bounding.top + bounding.height / 2) +
129
+ Number(item.top),
130
+ });
131
+ });
132
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
133
+ canvas: canvas,
134
+ });
135
+ canvas.setActiveObject(activeSelection);
136
+ canvas.requestRenderAll();
137
+ }
138
+ }
139
+
140
+ top() {
141
+ const { canvas } = this;
142
+
143
+ const activeObject = canvas.getActiveObject();
144
+ const selectObjects = canvas.getActiveObjects();
145
+ if (activeObject) {
146
+ const { top = 0 } = activeObject;
147
+ canvas.discardActiveObject();
148
+ selectObjects.forEach((item) => {
149
+ const bounding = item.getBoundingRect(true);
150
+ item.set({
151
+ top: top - bounding.top + Number(item.top),
152
+ });
153
+ });
154
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
155
+ canvas: canvas,
156
+ });
157
+ canvas.setActiveObject(activeSelection);
158
+ canvas.requestRenderAll();
159
+ }
160
+ }
161
+
162
+ bottom() {
163
+ const { canvas } = this;
164
+
165
+ const activeObject = canvas.getActiveObject();
166
+ const selectObjects = canvas.getActiveObjects();
167
+ if (activeObject) {
168
+ const { top = 0, height = 0 } = activeObject;
169
+ canvas.discardActiveObject();
170
+ selectObjects.forEach((item) => {
171
+ const bounding = item.getBoundingRect(true);
172
+ item.set({
173
+ top:
174
+ top +
175
+ height -
176
+ (bounding.top + bounding.height) +
177
+ Number(item.top),
178
+ });
179
+ });
180
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
181
+ canvas: canvas,
182
+ });
183
+ canvas.setActiveObject(activeSelection);
184
+ canvas.requestRenderAll();
185
+ }
186
+ }
187
+
188
+ xequation() {
189
+ const { canvas } = this;
190
+ const activeObject = canvas.getActiveObject() as fabric.ActiveSelection;
191
+ // width属性不准确,需要坐标换算
192
+ function getItemWidth(item: fabric.Object) {
193
+ let x1 = Infinity,
194
+ x2 = -Infinity;
195
+ for (const key in item.aCoords) {
196
+ if (item.aCoords[key].x < x1) {
197
+ x1 = item.aCoords[key].x;
198
+ }
199
+ if (item.aCoords[key].x > x2) {
200
+ x2 = item.aCoords[key].x;
201
+ }
202
+ }
203
+ return x2 - x1;
204
+ }
205
+
206
+ // 获取所有元素高度
207
+ function getAllItemHeight() {
208
+ let count = 0;
209
+ if (activeObject) {
210
+ activeObject.forEachObject((item: fabric.Object) => {
211
+ count += getItemWidth(item);
212
+ });
213
+ }
214
+
215
+ return count;
216
+ }
217
+ // 获取平均间距
218
+ function spacWidth() {
219
+ const count = getAllItemHeight();
220
+ if (activeObject) {
221
+ const allSpac = Number(activeObject.width) - count;
222
+ return allSpac / (activeObject._objects.length - 1);
223
+ }
224
+ }
225
+
226
+ // 获取当前元素之前所有元素的高度
227
+ function getItemLeft(i: number) {
228
+ if (i === 0) return 0;
229
+ let width = 0;
230
+ if (activeObject) {
231
+ for (let index = 0; index < i; index++) {
232
+ width += getItemWidth(activeObject._objects[index]);
233
+ }
234
+ }
235
+
236
+ return width;
237
+ }
238
+ if (activeObject && activeObject.type === 'activeSelection') {
239
+ const activeSelection = activeObject;
240
+ // 排序
241
+ activeSelection._objects.sort((a, b) => a.left - b.left);
242
+
243
+ // 平均间距计算
244
+ const itemSpac = spacWidth() as number;
245
+ // 组原点高度
246
+ const yHeight = Number(activeObject.width) / 2;
247
+
248
+ activeObject.forEachObject((item: fabric.Object, i: number) => {
249
+ // 获取当前元素之前所有元素的高度
250
+ const preHeight = getItemLeft(i);
251
+ // 顶部距离 间距 * 索引 + 之前元素高度 - 原点高度
252
+ const top = itemSpac * i + preHeight - yHeight;
253
+ item.set('left', top);
254
+ });
255
+ }
256
+
257
+ const objecs = canvas.getActiveObjects();
258
+ canvas.discardActiveObject();
259
+ objecs.forEach((item: fabric.Object) => {
260
+ let x = Infinity;
261
+ for (const key in item.aCoords) {
262
+ if (item.aCoords[key].x < x) {
263
+ x = item.aCoords[key].x;
264
+ }
265
+ }
266
+ item.set('left', 2 * item.left - x);
267
+ });
268
+
269
+ const sel = new fabric.ActiveSelection(objecs, {
270
+ canvas: canvas,
271
+ });
272
+ canvas.setActiveObject(sel);
273
+ canvas.requestRenderAll();
274
+ }
275
+
276
+ yequation() {
277
+ const { canvas } = this;
278
+ const activeObject =
279
+ (canvas.getActiveObject() as fabric.ActiveSelection) || {
280
+ top: 0,
281
+ height: 0,
282
+ };
283
+ // width属性不准确,需要坐标换算
284
+ function getItemHeight(item: fabric.Object) {
285
+ let y1 = Infinity,
286
+ y2 = -Infinity;
287
+ for (const key in item.aCoords) {
288
+ if (item.aCoords[key].y < y1) {
289
+ y1 = item.aCoords[key].y;
290
+ }
291
+ if (item.aCoords[key].y > y2) {
292
+ y2 = item.aCoords[key].y;
293
+ }
294
+ }
295
+ return y2 - y1;
296
+ }
297
+ // 获取所有元素高度
298
+ function getAllItemHeight() {
299
+ let count = 0;
300
+ activeObject.forEachObject((item: fabric.Object) => {
301
+ count += getItemHeight(item);
302
+ });
303
+ return count;
304
+ }
305
+ // 获取平均间距
306
+ function spacHeight() {
307
+ const count = getAllItemHeight();
308
+ const allSpac = activeObject.height - count;
309
+ return allSpac / (activeObject._objects.length - 1);
310
+ }
311
+
312
+ // 获取当前元素之前所有元素的高度
313
+ function getItemTop(i: number) {
314
+ if (i === 0) return 0;
315
+ let height = 0;
316
+ for (let index = 0; index < i; index++) {
317
+ height += getItemHeight(activeObject._objects[index]);
318
+ }
319
+ return height;
320
+ }
321
+
322
+ if (activeObject && activeObject.type === 'activeSelection') {
323
+ const activeSelection = activeObject;
324
+ // 排序
325
+ activeSelection._objects.sort((a, b) => a.top - b.top);
326
+
327
+ // 平均间距计算
328
+ const itemSpac = spacHeight();
329
+ // 组原点高度
330
+ const yHeight = Number(activeObject.height) / 2;
331
+
332
+ activeObject.forEachObject((item: fabric.Object, i: number) => {
333
+ // 获取当前元素之前所有元素的高度
334
+ const preHeight = getItemTop(i);
335
+ // 顶部距离 间距 * 索引 + 之前元素高度 - 原点高度
336
+ const top = itemSpac * i + preHeight - yHeight;
337
+ item.set('top', top);
338
+ });
339
+ }
340
+
341
+ const objecs = canvas.getActiveObjects();
342
+ canvas.discardActiveObject();
343
+ objecs.forEach((item) => {
344
+ let y = Infinity;
345
+ for (const key in item.aCoords) {
346
+ if (item.aCoords[key].y < y) {
347
+ y = item.aCoords[key].y;
348
+ }
349
+ }
350
+ item.set('top', 2 * item.top - y);
351
+ });
352
+
353
+ const sel = new fabric.ActiveSelection(objecs, {
354
+ canvas: canvas,
355
+ });
356
+ canvas.setActiveObject(sel);
357
+ canvas.requestRenderAll();
358
+ }
359
+
360
+ destroy() {
361
+ console.log('pluginDestroy');
362
+ }
363
+ }
364
+
365
+ export default GroupAlignPlugin;