@hprint/plugins 0.0.9-alpha.1 → 0.0.11-alpha.1

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.
@@ -314,7 +314,7 @@ class CreateElementPlugin implements IPluginTempl {
314
314
  }
315
315
  return originalSet(key, value);
316
316
  };
317
- (obj as any).syncOriginSizeByUnit = function (fieldsOrDpi?: any, dpi?: number) {
317
+ (obj as any).syncOriginSizeByUnit = function (fieldsOrDpi?: any, dpi?: number) {
318
318
  const unit = getUnit(editorRef);
319
319
  if (unit === 'px') return;
320
320
  const hasFieldsArray = Array.isArray(fieldsOrDpi);
@@ -325,10 +325,10 @@ class CreateElementPlugin implements IPluginTempl {
325
325
  const isLineObject = (this as any).type === 'line' || (this as any).type === 'arrow' || (this as any).type === 'thinTailArrow';
326
326
  const targetFields: string[] =
327
327
  hasFieldsArray && (fieldsOrDpi as string[])?.length ? (fieldsOrDpi as string[]) : singleFields;
328
- targetFields.forEach((field) => {
329
- if (field === 'points') return;
330
- let currentVal: number | undefined;
331
- if (isImageObject && (field === 'width' || field === 'height')) {
328
+ targetFields.forEach((field) => {
329
+ if (field === 'points') return;
330
+ let currentVal: number | undefined;
331
+ if (isImageObject && (field === 'width' || field === 'height')) {
332
332
  if (field === 'width') {
333
333
  currentVal = (this as any).getScaledWidth?.();
334
334
  } else {
@@ -1,5 +1,5 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
3
 
4
4
  type IPlugin = Pick<
5
5
  GroupAlignPlugin,
@@ -31,10 +31,22 @@ class GroupAlignPlugin implements IPluginTempl {
31
31
  'yequation',
32
32
  ];
33
33
  // public hotkeys: string[] = ['space'];
34
- constructor(
35
- public canvas: fabric.Canvas,
36
- public editor: IEditor
37
- ) { }
34
+ constructor(
35
+ public canvas: fabric.Canvas,
36
+ public editor: IEditor
37
+ ) { }
38
+
39
+ _refreshObjectCoordinates(objects: fabric.Object[]) {
40
+ objects.forEach((item) => {
41
+ item.setCoords();
42
+ (item as any).syncOriginSizeByUnit?.(['left', 'top']);
43
+ });
44
+ }
45
+
46
+ _notifySelectionModified(selection: fabric.ActiveSelection) {
47
+ this.canvas.fire('object:modified', { target: selection });
48
+ this.canvas.requestRenderAll();
49
+ }
38
50
 
39
51
  left() {
40
52
  const { canvas } = this;
@@ -51,15 +63,16 @@ class GroupAlignPlugin implements IPluginTempl {
51
63
  });
52
64
  item.setCoords();
53
65
  });
54
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
66
+ this._refreshObjectCoordinates(selectObjects);
67
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
55
68
  canvas: canvas,
56
69
  });
57
70
  canvas.setActiveObject(activeSelection);
58
- canvas.requestRenderAll();
71
+ this._notifySelectionModified(activeSelection);
59
72
  }
60
73
  }
61
74
 
62
- right() {
75
+ right() {
63
76
  const { canvas } = this;
64
77
 
65
78
  const activeObject = canvas.getActiveObject();
@@ -77,15 +90,16 @@ class GroupAlignPlugin implements IPluginTempl {
77
90
  Number(item.left),
78
91
  });
79
92
  });
80
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
93
+ this._refreshObjectCoordinates(selectObjects);
94
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
81
95
  canvas: canvas,
82
96
  });
83
97
  canvas.setActiveObject(activeSelection);
84
- canvas.requestRenderAll();
98
+ this._notifySelectionModified(activeSelection);
85
99
  }
86
100
  }
87
101
 
88
- xcenter() {
102
+ xcenter() {
89
103
  const { canvas } = this;
90
104
 
91
105
  const activeObject = canvas.getActiveObject();
@@ -103,15 +117,16 @@ class GroupAlignPlugin implements IPluginTempl {
103
117
  Number(item.left),
104
118
  });
105
119
  });
106
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
120
+ this._refreshObjectCoordinates(selectObjects);
121
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
107
122
  canvas: canvas,
108
123
  });
109
124
  canvas.setActiveObject(activeSelection);
110
- canvas.requestRenderAll();
125
+ this._notifySelectionModified(activeSelection);
111
126
  }
112
127
  }
113
128
 
114
- ycenter() {
129
+ ycenter() {
115
130
  const { canvas } = this;
116
131
 
117
132
  const activeObject = canvas.getActiveObject();
@@ -129,15 +144,16 @@ class GroupAlignPlugin implements IPluginTempl {
129
144
  Number(item.top),
130
145
  });
131
146
  });
132
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
147
+ this._refreshObjectCoordinates(selectObjects);
148
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
133
149
  canvas: canvas,
134
150
  });
135
151
  canvas.setActiveObject(activeSelection);
136
- canvas.requestRenderAll();
152
+ this._notifySelectionModified(activeSelection);
137
153
  }
138
154
  }
139
155
 
140
- top() {
156
+ top() {
141
157
  const { canvas } = this;
142
158
 
143
159
  const activeObject = canvas.getActiveObject();
@@ -151,15 +167,16 @@ class GroupAlignPlugin implements IPluginTempl {
151
167
  top: top - bounding.top + Number(item.top),
152
168
  });
153
169
  });
154
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
170
+ this._refreshObjectCoordinates(selectObjects);
171
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
155
172
  canvas: canvas,
156
173
  });
157
174
  canvas.setActiveObject(activeSelection);
158
- canvas.requestRenderAll();
175
+ this._notifySelectionModified(activeSelection);
159
176
  }
160
177
  }
161
178
 
162
- bottom() {
179
+ bottom() {
163
180
  const { canvas } = this;
164
181
 
165
182
  const activeObject = canvas.getActiveObject();
@@ -177,11 +194,12 @@ class GroupAlignPlugin implements IPluginTempl {
177
194
  Number(item.top),
178
195
  });
179
196
  });
180
- const activeSelection = new fabric.ActiveSelection(selectObjects, {
197
+ this._refreshObjectCoordinates(selectObjects);
198
+ const activeSelection = new fabric.ActiveSelection(selectObjects, {
181
199
  canvas: canvas,
182
200
  });
183
201
  canvas.setActiveObject(activeSelection);
184
- canvas.requestRenderAll();
202
+ this._notifySelectionModified(activeSelection);
185
203
  }
186
204
  }
187
205
 
@@ -256,21 +274,23 @@ class GroupAlignPlugin implements IPluginTempl {
256
274
 
257
275
  const objecs = canvas.getActiveObjects();
258
276
  canvas.discardActiveObject();
259
- objecs.forEach((item: fabric.Object) => {
260
- let x = Infinity;
277
+ objecs.forEach((item: fabric.Object) => {
278
+ let x = Infinity;
261
279
  for (const key in item.aCoords) {
262
280
  if (item.aCoords[key].x < x) {
263
281
  x = item.aCoords[key].x;
264
282
  }
265
283
  }
266
- item.set('left', 2 * item.left - x);
267
- });
268
-
269
- const sel = new fabric.ActiveSelection(objecs, {
284
+ item.set('left', 2 * item.left - x);
285
+ });
286
+
287
+ this._refreshObjectCoordinates(objecs);
288
+
289
+ const sel = new fabric.ActiveSelection(objecs, {
270
290
  canvas: canvas,
271
291
  });
272
292
  canvas.setActiveObject(sel);
273
- canvas.requestRenderAll();
293
+ this._notifySelectionModified(sel);
274
294
  }
275
295
 
276
296
  yequation() {
@@ -340,21 +360,23 @@ class GroupAlignPlugin implements IPluginTempl {
340
360
 
341
361
  const objecs = canvas.getActiveObjects();
342
362
  canvas.discardActiveObject();
343
- objecs.forEach((item) => {
344
- let y = Infinity;
363
+ objecs.forEach((item) => {
364
+ let y = Infinity;
345
365
  for (const key in item.aCoords) {
346
366
  if (item.aCoords[key].y < y) {
347
367
  y = item.aCoords[key].y;
348
368
  }
349
369
  }
350
- item.set('top', 2 * item.top - y);
351
- });
352
-
353
- const sel = new fabric.ActiveSelection(objecs, {
370
+ item.set('top', 2 * item.top - y);
371
+ });
372
+
373
+ this._refreshObjectCoordinates(objecs);
374
+
375
+ const sel = new fabric.ActiveSelection(objecs, {
354
376
  canvas: canvas,
355
377
  });
356
378
  canvas.setActiveObject(sel);
357
- canvas.requestRenderAll();
379
+ this._notifySelectionModified(sel);
358
380
  }
359
381
 
360
382
  destroy() {