react-design-editor 0.0.50 → 0.0.51
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.
- package/dist/react-design-editor.js +7739 -7114
- package/dist/react-design-editor.min.js +1 -1
- package/dist/react-design-editor.min.js.LICENSE.txt +2 -0
- package/lib/Canvas.d.ts +18 -18
- package/lib/Canvas.js +172 -172
- package/lib/CanvasObject.d.ts +10 -10
- package/lib/CanvasObject.js +96 -96
- package/lib/constants/code.d.ts +19 -19
- package/lib/constants/code.js +22 -22
- package/lib/constants/defaults.d.ts +38 -38
- package/lib/constants/defaults.js +69 -69
- package/lib/constants/index.d.ts +3 -3
- package/lib/constants/index.js +26 -26
- package/lib/handlers/AlignmentHandler.d.ts +18 -18
- package/lib/handlers/AlignmentHandler.js +58 -58
- package/lib/handlers/AnimationHandler.d.ts +50 -50
- package/lib/handlers/AnimationHandler.js +346 -346
- package/lib/handlers/ChartHandler.d.ts +8 -8
- package/lib/handlers/ChartHandler.js +8 -8
- package/lib/handlers/ContextmenuHandler.d.ts +28 -28
- package/lib/handlers/ContextmenuHandler.js +65 -65
- package/lib/handlers/CropHandler.d.ts +43 -43
- package/lib/handlers/CropHandler.js +261 -261
- package/lib/handlers/CustomHandler.d.ts +7 -7
- package/lib/handlers/CustomHandler.js +10 -10
- package/lib/handlers/DrawingHandler.d.ts +28 -28
- package/lib/handlers/DrawingHandler.js +318 -318
- package/lib/handlers/ElementHandler.d.ts +80 -80
- package/lib/handlers/ElementHandler.js +154 -154
- package/lib/handlers/EventHandler.d.ts +170 -170
- package/lib/handlers/EventHandler.js +880 -880
- package/lib/handlers/FiberHandler.d.ts +6 -6
- package/lib/handlers/FiberHandler.js +23 -23
- package/lib/handlers/GridHandler.d.ts +19 -19
- package/lib/handlers/GridHandler.js +77 -77
- package/lib/handlers/GuidelineHandler.d.ts +61 -61
- package/lib/handlers/GuidelineHandler.js +315 -315
- package/lib/handlers/Handler.d.ts +618 -618
- package/lib/handlers/Handler.js +1645 -1645
- package/lib/handlers/ImageHandler.d.ts +307 -307
- package/lib/handlers/ImageHandler.js +528 -528
- package/lib/handlers/InteractionHandler.d.ts +45 -45
- package/lib/handlers/InteractionHandler.js +168 -164
- package/lib/handlers/LinkHandler.d.ts +115 -115
- package/lib/handlers/LinkHandler.js +247 -247
- package/lib/handlers/NodeHandler.d.ts +50 -50
- package/lib/handlers/NodeHandler.js +274 -274
- package/lib/handlers/PortHandler.d.ts +22 -22
- package/lib/handlers/PortHandler.js +179 -179
- package/lib/handlers/ShortcutHandler.d.ts +119 -119
- package/lib/handlers/ShortcutHandler.js +151 -151
- package/lib/handlers/TooltipHandler.d.ts +33 -33
- package/lib/handlers/TooltipHandler.js +91 -91
- package/lib/handlers/TransactionHandler.d.ts +59 -59
- package/lib/handlers/TransactionHandler.js +137 -137
- package/lib/handlers/WorkareaHandler.d.ts +43 -43
- package/lib/handlers/WorkareaHandler.js +354 -354
- package/lib/handlers/ZoomHandler.d.ts +48 -48
- package/lib/handlers/ZoomHandler.js +143 -143
- package/lib/handlers/index.d.ts +23 -23
- package/lib/handlers/index.js +48 -48
- package/lib/index.d.ts +6 -6
- package/lib/index.js +20 -20
- package/lib/objects/Arrow.d.ts +2 -2
- package/lib/objects/Arrow.js +40 -40
- package/lib/objects/Chart.d.ts +10 -10
- package/lib/objects/Chart.js +117 -117
- package/lib/objects/CirclePort.d.ts +2 -2
- package/lib/objects/CirclePort.js +28 -28
- package/lib/objects/Cube.d.ts +5 -5
- package/lib/objects/Cube.js +71 -71
- package/lib/objects/CurvedLink.d.ts +2 -2
- package/lib/objects/CurvedLink.js +51 -51
- package/lib/objects/Element.d.ts +13 -13
- package/lib/objects/Element.js +77 -77
- package/lib/objects/Gif.d.ts +3 -3
- package/lib/objects/Gif.js +41 -41
- package/lib/objects/Iframe.d.ts +9 -9
- package/lib/objects/Iframe.js +63 -63
- package/lib/objects/Line.d.ts +2 -2
- package/lib/objects/Line.js +24 -24
- package/lib/objects/Link.d.ts +15 -15
- package/lib/objects/Link.js +107 -107
- package/lib/objects/Node.d.ts +59 -59
- package/lib/objects/Node.js +271 -271
- package/lib/objects/OrthogonalLink.d.ts +2 -2
- package/lib/objects/OrthogonalLink.js +54 -54
- package/lib/objects/Port.d.ts +12 -12
- package/lib/objects/Port.js +28 -28
- package/lib/objects/Svg.d.ts +12 -12
- package/lib/objects/Svg.js +93 -93
- package/lib/objects/Video.d.ts +14 -14
- package/lib/objects/Video.js +113 -113
- package/lib/objects/index.d.ts +15 -15
- package/lib/objects/index.js +32 -32
- package/lib/utils/ObjectUtil.d.ts +408 -408
- package/lib/utils/ObjectUtil.js +13 -13
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +13 -13
- package/package.json +1 -1
|
@@ -1,315 +1,315 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const fabric_1 = require("fabric");
|
|
4
|
-
class GuidelineHandler {
|
|
5
|
-
constructor(handler) {
|
|
6
|
-
this.aligningLineOffset = 5;
|
|
7
|
-
this.aligningLineMargin = 4;
|
|
8
|
-
this.aligningLineWidth = 1;
|
|
9
|
-
this.aligningLineColor = 'rgb(255, 0, 0)';
|
|
10
|
-
this.zoom = 1;
|
|
11
|
-
/**
|
|
12
|
-
* Before the render
|
|
13
|
-
*
|
|
14
|
-
* @param {FabricEvent} _opt
|
|
15
|
-
*/
|
|
16
|
-
this.beforeRender = (_opt) => {
|
|
17
|
-
this.handler.canvas.clearContext(this.handler.guidelineHandler.ctx);
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* After the render
|
|
21
|
-
*
|
|
22
|
-
* @param {FabricEvent} _opt
|
|
23
|
-
*/
|
|
24
|
-
this.afterRender = (_opt) => {
|
|
25
|
-
for (let i = this.handler.guidelineHandler.verticalLines.length; i--;) {
|
|
26
|
-
this.handler.guidelineHandler.drawVerticalLine(this.handler.guidelineHandler.verticalLines[i]);
|
|
27
|
-
}
|
|
28
|
-
for (let i = this.handler.guidelineHandler.horizontalLines.length; i--;) {
|
|
29
|
-
this.handler.guidelineHandler.drawHorizontalLine(this.handler.guidelineHandler.horizontalLines[i]);
|
|
30
|
-
}
|
|
31
|
-
this.handler.guidelineHandler.verticalLines.length = 0;
|
|
32
|
-
this.handler.guidelineHandler.horizontalLines.length = 0;
|
|
33
|
-
};
|
|
34
|
-
this.drawVerticalLine = (coords) => {
|
|
35
|
-
this.drawLine(coords.x + 0.5, coords.y1 > coords.y2 ? coords.y2 : coords.y1, coords.x + 0.5, coords.y2 > coords.y1 ? coords.y2 : coords.y1);
|
|
36
|
-
};
|
|
37
|
-
this.drawHorizontalLine = (coords) => {
|
|
38
|
-
this.drawLine(coords.x1 > coords.x2 ? coords.x2 : coords.x1, coords.y + 0.5, coords.x2 > coords.x1 ? coords.x2 : coords.x1, coords.y + 0.5);
|
|
39
|
-
};
|
|
40
|
-
this.drawLine = (x1, y1, x2, y2) => {
|
|
41
|
-
const { ctx, aligningLineWidth, aligningLineColor, viewportTransform, zoom } = this;
|
|
42
|
-
ctx.save();
|
|
43
|
-
ctx.lineWidth = aligningLineWidth;
|
|
44
|
-
ctx.strokeStyle = aligningLineColor;
|
|
45
|
-
ctx.beginPath();
|
|
46
|
-
ctx.moveTo(x1 * zoom + viewportTransform[4], y1 * zoom + viewportTransform[5]);
|
|
47
|
-
ctx.lineTo(x2 * zoom + viewportTransform[4], y2 * zoom + viewportTransform[5]);
|
|
48
|
-
ctx.stroke();
|
|
49
|
-
ctx.restore();
|
|
50
|
-
};
|
|
51
|
-
this.isInRange = (v1, v2) => {
|
|
52
|
-
const { aligningLineMargin } = this;
|
|
53
|
-
v1 = Math.round(v1);
|
|
54
|
-
v2 = Math.round(v2);
|
|
55
|
-
for (let i = v1 - aligningLineMargin, len = v1 + aligningLineMargin; i <= len; i++) {
|
|
56
|
-
if (i === v2) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return false;
|
|
61
|
-
};
|
|
62
|
-
this.movingGuidelines = (target) => {
|
|
63
|
-
const canvasObjects = this.handler.canvas.getObjects();
|
|
64
|
-
const activeObjectCenter = target.getCenterPoint();
|
|
65
|
-
const activeObjectLeft = activeObjectCenter.x;
|
|
66
|
-
const activeObjectTop = activeObjectCenter.y;
|
|
67
|
-
const activeObjectBoundingRect = target.getBoundingRect();
|
|
68
|
-
const activeObjectHeight = activeObjectBoundingRect.height / this.viewportTransform[3];
|
|
69
|
-
const activeObjectWidth = activeObjectBoundingRect.width / this.viewportTransform[0];
|
|
70
|
-
let horizontalInTheRange = false;
|
|
71
|
-
let verticalInTheRange = false;
|
|
72
|
-
const { _currentTransform: transform } = this.handler.canvas;
|
|
73
|
-
if (!transform) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
// It should be trivial to DRY this up by encapsulating (repeating) creation of x1, x2, y1, and y2 into functions,
|
|
77
|
-
// but we're not doing it here for perf. reasons -- as this a function that's invoked on every mouse move
|
|
78
|
-
for (let i = canvasObjects.length; i--;) {
|
|
79
|
-
if (canvasObjects[i] === target ||
|
|
80
|
-
canvasObjects[i].superType === 'port' ||
|
|
81
|
-
canvasObjects[i].superType === 'link' ||
|
|
82
|
-
!canvasObjects[i].evented) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
const objectCenter = canvasObjects[i].getCenterPoint();
|
|
86
|
-
const objectLeft = objectCenter.x;
|
|
87
|
-
const objectTop = objectCenter.y;
|
|
88
|
-
const objectBoundingRect = canvasObjects[i].getBoundingRect();
|
|
89
|
-
const objectHeight = objectBoundingRect.height / this.viewportTransform[3];
|
|
90
|
-
const objectWidth = objectBoundingRect.width / this.viewportTransform[0];
|
|
91
|
-
// snap by the horizontal center line
|
|
92
|
-
if (this.isInRange(objectLeft, activeObjectLeft)) {
|
|
93
|
-
verticalInTheRange = true;
|
|
94
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
95
|
-
const y1 = -5000;
|
|
96
|
-
const y2 = 5000;
|
|
97
|
-
this.verticalLines.push({
|
|
98
|
-
x: objectLeft,
|
|
99
|
-
y1,
|
|
100
|
-
y2,
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
this.verticalLines.push({
|
|
105
|
-
x: objectLeft,
|
|
106
|
-
y1: objectTop < activeObjectTop
|
|
107
|
-
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
108
|
-
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
109
|
-
y2: activeObjectTop > objectTop
|
|
110
|
-
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
111
|
-
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft, activeObjectTop), 'center', 'center');
|
|
115
|
-
}
|
|
116
|
-
// snap by the left edge
|
|
117
|
-
if (this.isInRange(objectLeft - objectWidth / 2, activeObjectLeft - activeObjectWidth / 2)) {
|
|
118
|
-
verticalInTheRange = true;
|
|
119
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
120
|
-
const workarea = canvasObjects[i];
|
|
121
|
-
const y1 = -5000;
|
|
122
|
-
const y2 = 5000;
|
|
123
|
-
let x = objectLeft - objectWidth / 2;
|
|
124
|
-
if (workarea.layout === 'fullscreen') {
|
|
125
|
-
x = 0;
|
|
126
|
-
}
|
|
127
|
-
this.verticalLines.push({
|
|
128
|
-
x,
|
|
129
|
-
y1,
|
|
130
|
-
y2,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
this.verticalLines.push({
|
|
135
|
-
x: objectLeft - objectWidth / 2,
|
|
136
|
-
y1: objectTop < activeObjectTop
|
|
137
|
-
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
138
|
-
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
139
|
-
y2: activeObjectTop > objectTop
|
|
140
|
-
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
141
|
-
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft - objectWidth / 2 + activeObjectWidth / 2, activeObjectTop), 'center', 'center');
|
|
145
|
-
}
|
|
146
|
-
// snap by the right edge
|
|
147
|
-
if (this.isInRange(objectLeft + objectWidth / 2, activeObjectLeft + activeObjectWidth / 2)) {
|
|
148
|
-
verticalInTheRange = true;
|
|
149
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
150
|
-
const workarea = canvasObjects[i];
|
|
151
|
-
const y1 = -5000;
|
|
152
|
-
const y2 = 5000;
|
|
153
|
-
let x = objectLeft + objectWidth / 2;
|
|
154
|
-
if (workarea.layout === 'fullscreen') {
|
|
155
|
-
x = this.handler.canvas.getWidth();
|
|
156
|
-
}
|
|
157
|
-
this.verticalLines.push({
|
|
158
|
-
x,
|
|
159
|
-
y1,
|
|
160
|
-
y2,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
this.verticalLines.push({
|
|
165
|
-
x: objectLeft + objectWidth / 2,
|
|
166
|
-
y1: objectTop < activeObjectTop
|
|
167
|
-
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
168
|
-
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
169
|
-
y2: activeObjectTop > objectTop
|
|
170
|
-
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
171
|
-
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft + objectWidth / 2 - activeObjectWidth / 2, activeObjectTop), 'center', 'center');
|
|
175
|
-
}
|
|
176
|
-
// snap by the vertical center line
|
|
177
|
-
if (this.isInRange(objectTop, activeObjectTop)) {
|
|
178
|
-
horizontalInTheRange = true;
|
|
179
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
180
|
-
const x1 = -5000;
|
|
181
|
-
const x2 = 5000;
|
|
182
|
-
this.horizontalLines.push({
|
|
183
|
-
y: objectTop,
|
|
184
|
-
x1,
|
|
185
|
-
x2,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
this.horizontalLines.push({
|
|
190
|
-
y: objectTop,
|
|
191
|
-
x1: objectLeft < activeObjectLeft
|
|
192
|
-
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
193
|
-
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
194
|
-
x2: activeObjectLeft > objectLeft
|
|
195
|
-
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
196
|
-
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop), 'center', 'center');
|
|
200
|
-
}
|
|
201
|
-
// snap by the top edge
|
|
202
|
-
if (this.isInRange(objectTop - objectHeight / 2, activeObjectTop - activeObjectHeight / 2)) {
|
|
203
|
-
horizontalInTheRange = true;
|
|
204
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
205
|
-
const workarea = canvasObjects[i];
|
|
206
|
-
const x1 = -5000;
|
|
207
|
-
const x2 = 5000;
|
|
208
|
-
let y = objectTop - objectHeight / 2;
|
|
209
|
-
if (workarea.layout === 'fullscreen') {
|
|
210
|
-
y = 0;
|
|
211
|
-
}
|
|
212
|
-
this.horizontalLines.push({
|
|
213
|
-
y,
|
|
214
|
-
x1,
|
|
215
|
-
x2,
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
this.horizontalLines.push({
|
|
220
|
-
y: objectTop - objectHeight / 2,
|
|
221
|
-
x1: objectLeft < activeObjectLeft
|
|
222
|
-
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
223
|
-
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
224
|
-
x2: activeObjectLeft > objectLeft
|
|
225
|
-
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
226
|
-
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop - objectHeight / 2 + activeObjectHeight / 2), 'center', 'center');
|
|
230
|
-
}
|
|
231
|
-
// snap by the bottom edge
|
|
232
|
-
if (this.isInRange(objectTop + objectHeight / 2, activeObjectTop + activeObjectHeight / 2)) {
|
|
233
|
-
horizontalInTheRange = true;
|
|
234
|
-
if (canvasObjects[i].id === 'workarea') {
|
|
235
|
-
const workarea = canvasObjects[i];
|
|
236
|
-
const x1 = -5000;
|
|
237
|
-
const x2 = 5000;
|
|
238
|
-
let y = objectTop + objectHeight / 2;
|
|
239
|
-
if (workarea.layout === 'fullscreen') {
|
|
240
|
-
y = this.handler.canvas.getHeight();
|
|
241
|
-
}
|
|
242
|
-
this.horizontalLines.push({
|
|
243
|
-
y,
|
|
244
|
-
x1,
|
|
245
|
-
x2,
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
this.horizontalLines.push({
|
|
250
|
-
y: objectTop + objectHeight / 2,
|
|
251
|
-
x1: objectLeft < activeObjectLeft
|
|
252
|
-
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
253
|
-
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
254
|
-
x2: activeObjectLeft > objectLeft
|
|
255
|
-
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
256
|
-
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop + objectHeight / 2 - activeObjectHeight / 2), 'center', 'center');
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (!horizontalInTheRange) {
|
|
263
|
-
this.horizontalLines.length = 0;
|
|
264
|
-
}
|
|
265
|
-
if (!verticalInTheRange) {
|
|
266
|
-
this.verticalLines.length = 0;
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
this.scalingGuidelines = (_target) => {
|
|
270
|
-
// TODO... object scaling guideline
|
|
271
|
-
};
|
|
272
|
-
this.handler = handler;
|
|
273
|
-
this.initialize();
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Initialize guideline handler
|
|
277
|
-
*
|
|
278
|
-
*/
|
|
279
|
-
initialize() {
|
|
280
|
-
if (this.handler.guidelineOption.enabled) {
|
|
281
|
-
// @ts-ignore
|
|
282
|
-
this.handler.canvas.on({
|
|
283
|
-
'before:render': this.beforeRender,
|
|
284
|
-
'after:render': this.afterRender,
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
this.handler.canvas.off({
|
|
289
|
-
'before:render': this.beforeRender,
|
|
290
|
-
'after:render': this.afterRender,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
this.ctx = this.handler.canvas.getSelectionContext();
|
|
294
|
-
this.aligningLineOffset = 5;
|
|
295
|
-
this.aligningLineMargin = 4;
|
|
296
|
-
this.aligningLineWidth = 1;
|
|
297
|
-
this.aligningLineColor = 'rgb(255, 0, 0)';
|
|
298
|
-
this.viewportTransform = this.handler.canvas.viewportTransform;
|
|
299
|
-
this.zoom = 1;
|
|
300
|
-
this.verticalLines = [];
|
|
301
|
-
this.horizontalLines = [];
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Destroy guideline handler
|
|
305
|
-
*
|
|
306
|
-
* @author salgum1114
|
|
307
|
-
*/
|
|
308
|
-
destroy() {
|
|
309
|
-
this.handler.canvas.off({
|
|
310
|
-
'before:render': this.beforeRender,
|
|
311
|
-
'after:render': this.afterRender,
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
exports.default = GuidelineHandler;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fabric_1 = require("fabric");
|
|
4
|
+
class GuidelineHandler {
|
|
5
|
+
constructor(handler) {
|
|
6
|
+
this.aligningLineOffset = 5;
|
|
7
|
+
this.aligningLineMargin = 4;
|
|
8
|
+
this.aligningLineWidth = 1;
|
|
9
|
+
this.aligningLineColor = 'rgb(255, 0, 0)';
|
|
10
|
+
this.zoom = 1;
|
|
11
|
+
/**
|
|
12
|
+
* Before the render
|
|
13
|
+
*
|
|
14
|
+
* @param {FabricEvent} _opt
|
|
15
|
+
*/
|
|
16
|
+
this.beforeRender = (_opt) => {
|
|
17
|
+
this.handler.canvas.clearContext(this.handler.guidelineHandler.ctx);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* After the render
|
|
21
|
+
*
|
|
22
|
+
* @param {FabricEvent} _opt
|
|
23
|
+
*/
|
|
24
|
+
this.afterRender = (_opt) => {
|
|
25
|
+
for (let i = this.handler.guidelineHandler.verticalLines.length; i--;) {
|
|
26
|
+
this.handler.guidelineHandler.drawVerticalLine(this.handler.guidelineHandler.verticalLines[i]);
|
|
27
|
+
}
|
|
28
|
+
for (let i = this.handler.guidelineHandler.horizontalLines.length; i--;) {
|
|
29
|
+
this.handler.guidelineHandler.drawHorizontalLine(this.handler.guidelineHandler.horizontalLines[i]);
|
|
30
|
+
}
|
|
31
|
+
this.handler.guidelineHandler.verticalLines.length = 0;
|
|
32
|
+
this.handler.guidelineHandler.horizontalLines.length = 0;
|
|
33
|
+
};
|
|
34
|
+
this.drawVerticalLine = (coords) => {
|
|
35
|
+
this.drawLine(coords.x + 0.5, coords.y1 > coords.y2 ? coords.y2 : coords.y1, coords.x + 0.5, coords.y2 > coords.y1 ? coords.y2 : coords.y1);
|
|
36
|
+
};
|
|
37
|
+
this.drawHorizontalLine = (coords) => {
|
|
38
|
+
this.drawLine(coords.x1 > coords.x2 ? coords.x2 : coords.x1, coords.y + 0.5, coords.x2 > coords.x1 ? coords.x2 : coords.x1, coords.y + 0.5);
|
|
39
|
+
};
|
|
40
|
+
this.drawLine = (x1, y1, x2, y2) => {
|
|
41
|
+
const { ctx, aligningLineWidth, aligningLineColor, viewportTransform, zoom } = this;
|
|
42
|
+
ctx.save();
|
|
43
|
+
ctx.lineWidth = aligningLineWidth;
|
|
44
|
+
ctx.strokeStyle = aligningLineColor;
|
|
45
|
+
ctx.beginPath();
|
|
46
|
+
ctx.moveTo(x1 * zoom + viewportTransform[4], y1 * zoom + viewportTransform[5]);
|
|
47
|
+
ctx.lineTo(x2 * zoom + viewportTransform[4], y2 * zoom + viewportTransform[5]);
|
|
48
|
+
ctx.stroke();
|
|
49
|
+
ctx.restore();
|
|
50
|
+
};
|
|
51
|
+
this.isInRange = (v1, v2) => {
|
|
52
|
+
const { aligningLineMargin } = this;
|
|
53
|
+
v1 = Math.round(v1);
|
|
54
|
+
v2 = Math.round(v2);
|
|
55
|
+
for (let i = v1 - aligningLineMargin, len = v1 + aligningLineMargin; i <= len; i++) {
|
|
56
|
+
if (i === v2) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
};
|
|
62
|
+
this.movingGuidelines = (target) => {
|
|
63
|
+
const canvasObjects = this.handler.canvas.getObjects();
|
|
64
|
+
const activeObjectCenter = target.getCenterPoint();
|
|
65
|
+
const activeObjectLeft = activeObjectCenter.x;
|
|
66
|
+
const activeObjectTop = activeObjectCenter.y;
|
|
67
|
+
const activeObjectBoundingRect = target.getBoundingRect();
|
|
68
|
+
const activeObjectHeight = activeObjectBoundingRect.height / this.viewportTransform[3];
|
|
69
|
+
const activeObjectWidth = activeObjectBoundingRect.width / this.viewportTransform[0];
|
|
70
|
+
let horizontalInTheRange = false;
|
|
71
|
+
let verticalInTheRange = false;
|
|
72
|
+
const { _currentTransform: transform } = this.handler.canvas;
|
|
73
|
+
if (!transform) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// It should be trivial to DRY this up by encapsulating (repeating) creation of x1, x2, y1, and y2 into functions,
|
|
77
|
+
// but we're not doing it here for perf. reasons -- as this a function that's invoked on every mouse move
|
|
78
|
+
for (let i = canvasObjects.length; i--;) {
|
|
79
|
+
if (canvasObjects[i] === target ||
|
|
80
|
+
canvasObjects[i].superType === 'port' ||
|
|
81
|
+
canvasObjects[i].superType === 'link' ||
|
|
82
|
+
!canvasObjects[i].evented) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const objectCenter = canvasObjects[i].getCenterPoint();
|
|
86
|
+
const objectLeft = objectCenter.x;
|
|
87
|
+
const objectTop = objectCenter.y;
|
|
88
|
+
const objectBoundingRect = canvasObjects[i].getBoundingRect();
|
|
89
|
+
const objectHeight = objectBoundingRect.height / this.viewportTransform[3];
|
|
90
|
+
const objectWidth = objectBoundingRect.width / this.viewportTransform[0];
|
|
91
|
+
// snap by the horizontal center line
|
|
92
|
+
if (this.isInRange(objectLeft, activeObjectLeft)) {
|
|
93
|
+
verticalInTheRange = true;
|
|
94
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
95
|
+
const y1 = -5000;
|
|
96
|
+
const y2 = 5000;
|
|
97
|
+
this.verticalLines.push({
|
|
98
|
+
x: objectLeft,
|
|
99
|
+
y1,
|
|
100
|
+
y2,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
this.verticalLines.push({
|
|
105
|
+
x: objectLeft,
|
|
106
|
+
y1: objectTop < activeObjectTop
|
|
107
|
+
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
108
|
+
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
109
|
+
y2: activeObjectTop > objectTop
|
|
110
|
+
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
111
|
+
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft, activeObjectTop), 'center', 'center');
|
|
115
|
+
}
|
|
116
|
+
// snap by the left edge
|
|
117
|
+
if (this.isInRange(objectLeft - objectWidth / 2, activeObjectLeft - activeObjectWidth / 2)) {
|
|
118
|
+
verticalInTheRange = true;
|
|
119
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
120
|
+
const workarea = canvasObjects[i];
|
|
121
|
+
const y1 = -5000;
|
|
122
|
+
const y2 = 5000;
|
|
123
|
+
let x = objectLeft - objectWidth / 2;
|
|
124
|
+
if (workarea.layout === 'fullscreen') {
|
|
125
|
+
x = 0;
|
|
126
|
+
}
|
|
127
|
+
this.verticalLines.push({
|
|
128
|
+
x,
|
|
129
|
+
y1,
|
|
130
|
+
y2,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
this.verticalLines.push({
|
|
135
|
+
x: objectLeft - objectWidth / 2,
|
|
136
|
+
y1: objectTop < activeObjectTop
|
|
137
|
+
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
138
|
+
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
139
|
+
y2: activeObjectTop > objectTop
|
|
140
|
+
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
141
|
+
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft - objectWidth / 2 + activeObjectWidth / 2, activeObjectTop), 'center', 'center');
|
|
145
|
+
}
|
|
146
|
+
// snap by the right edge
|
|
147
|
+
if (this.isInRange(objectLeft + objectWidth / 2, activeObjectLeft + activeObjectWidth / 2)) {
|
|
148
|
+
verticalInTheRange = true;
|
|
149
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
150
|
+
const workarea = canvasObjects[i];
|
|
151
|
+
const y1 = -5000;
|
|
152
|
+
const y2 = 5000;
|
|
153
|
+
let x = objectLeft + objectWidth / 2;
|
|
154
|
+
if (workarea.layout === 'fullscreen') {
|
|
155
|
+
x = this.handler.canvas.getWidth();
|
|
156
|
+
}
|
|
157
|
+
this.verticalLines.push({
|
|
158
|
+
x,
|
|
159
|
+
y1,
|
|
160
|
+
y2,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
this.verticalLines.push({
|
|
165
|
+
x: objectLeft + objectWidth / 2,
|
|
166
|
+
y1: objectTop < activeObjectTop
|
|
167
|
+
? objectTop - objectHeight / 2 - this.aligningLineOffset
|
|
168
|
+
: objectTop + objectHeight / 2 + this.aligningLineOffset,
|
|
169
|
+
y2: activeObjectTop > objectTop
|
|
170
|
+
? activeObjectTop + activeObjectHeight / 2 + this.aligningLineOffset
|
|
171
|
+
: activeObjectTop - activeObjectHeight / 2 - this.aligningLineOffset,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(objectLeft + objectWidth / 2 - activeObjectWidth / 2, activeObjectTop), 'center', 'center');
|
|
175
|
+
}
|
|
176
|
+
// snap by the vertical center line
|
|
177
|
+
if (this.isInRange(objectTop, activeObjectTop)) {
|
|
178
|
+
horizontalInTheRange = true;
|
|
179
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
180
|
+
const x1 = -5000;
|
|
181
|
+
const x2 = 5000;
|
|
182
|
+
this.horizontalLines.push({
|
|
183
|
+
y: objectTop,
|
|
184
|
+
x1,
|
|
185
|
+
x2,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.horizontalLines.push({
|
|
190
|
+
y: objectTop,
|
|
191
|
+
x1: objectLeft < activeObjectLeft
|
|
192
|
+
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
193
|
+
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
194
|
+
x2: activeObjectLeft > objectLeft
|
|
195
|
+
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
196
|
+
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop), 'center', 'center');
|
|
200
|
+
}
|
|
201
|
+
// snap by the top edge
|
|
202
|
+
if (this.isInRange(objectTop - objectHeight / 2, activeObjectTop - activeObjectHeight / 2)) {
|
|
203
|
+
horizontalInTheRange = true;
|
|
204
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
205
|
+
const workarea = canvasObjects[i];
|
|
206
|
+
const x1 = -5000;
|
|
207
|
+
const x2 = 5000;
|
|
208
|
+
let y = objectTop - objectHeight / 2;
|
|
209
|
+
if (workarea.layout === 'fullscreen') {
|
|
210
|
+
y = 0;
|
|
211
|
+
}
|
|
212
|
+
this.horizontalLines.push({
|
|
213
|
+
y,
|
|
214
|
+
x1,
|
|
215
|
+
x2,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this.horizontalLines.push({
|
|
220
|
+
y: objectTop - objectHeight / 2,
|
|
221
|
+
x1: objectLeft < activeObjectLeft
|
|
222
|
+
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
223
|
+
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
224
|
+
x2: activeObjectLeft > objectLeft
|
|
225
|
+
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
226
|
+
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop - objectHeight / 2 + activeObjectHeight / 2), 'center', 'center');
|
|
230
|
+
}
|
|
231
|
+
// snap by the bottom edge
|
|
232
|
+
if (this.isInRange(objectTop + objectHeight / 2, activeObjectTop + activeObjectHeight / 2)) {
|
|
233
|
+
horizontalInTheRange = true;
|
|
234
|
+
if (canvasObjects[i].id === 'workarea') {
|
|
235
|
+
const workarea = canvasObjects[i];
|
|
236
|
+
const x1 = -5000;
|
|
237
|
+
const x2 = 5000;
|
|
238
|
+
let y = objectTop + objectHeight / 2;
|
|
239
|
+
if (workarea.layout === 'fullscreen') {
|
|
240
|
+
y = this.handler.canvas.getHeight();
|
|
241
|
+
}
|
|
242
|
+
this.horizontalLines.push({
|
|
243
|
+
y,
|
|
244
|
+
x1,
|
|
245
|
+
x2,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
this.horizontalLines.push({
|
|
250
|
+
y: objectTop + objectHeight / 2,
|
|
251
|
+
x1: objectLeft < activeObjectLeft
|
|
252
|
+
? objectLeft - objectWidth / 2 - this.aligningLineOffset
|
|
253
|
+
: objectLeft + objectWidth / 2 + this.aligningLineOffset,
|
|
254
|
+
x2: activeObjectLeft > objectLeft
|
|
255
|
+
? activeObjectLeft + activeObjectWidth / 2 + this.aligningLineOffset
|
|
256
|
+
: activeObjectLeft - activeObjectWidth / 2 - this.aligningLineOffset,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
target.setPositionByOrigin(new fabric_1.fabric.Point(activeObjectLeft, objectTop + objectHeight / 2 - activeObjectHeight / 2), 'center', 'center');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (!horizontalInTheRange) {
|
|
263
|
+
this.horizontalLines.length = 0;
|
|
264
|
+
}
|
|
265
|
+
if (!verticalInTheRange) {
|
|
266
|
+
this.verticalLines.length = 0;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
this.scalingGuidelines = (_target) => {
|
|
270
|
+
// TODO... object scaling guideline
|
|
271
|
+
};
|
|
272
|
+
this.handler = handler;
|
|
273
|
+
this.initialize();
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Initialize guideline handler
|
|
277
|
+
*
|
|
278
|
+
*/
|
|
279
|
+
initialize() {
|
|
280
|
+
if (this.handler.guidelineOption.enabled) {
|
|
281
|
+
// @ts-ignore
|
|
282
|
+
this.handler.canvas.on({
|
|
283
|
+
'before:render': this.beforeRender,
|
|
284
|
+
'after:render': this.afterRender,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
this.handler.canvas.off({
|
|
289
|
+
'before:render': this.beforeRender,
|
|
290
|
+
'after:render': this.afterRender,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
this.ctx = this.handler.canvas.getSelectionContext();
|
|
294
|
+
this.aligningLineOffset = 5;
|
|
295
|
+
this.aligningLineMargin = 4;
|
|
296
|
+
this.aligningLineWidth = 1;
|
|
297
|
+
this.aligningLineColor = 'rgb(255, 0, 0)';
|
|
298
|
+
this.viewportTransform = this.handler.canvas.viewportTransform;
|
|
299
|
+
this.zoom = 1;
|
|
300
|
+
this.verticalLines = [];
|
|
301
|
+
this.horizontalLines = [];
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Destroy guideline handler
|
|
305
|
+
*
|
|
306
|
+
* @author salgum1114
|
|
307
|
+
*/
|
|
308
|
+
destroy() {
|
|
309
|
+
this.handler.canvas.off({
|
|
310
|
+
'before:render': this.beforeRender,
|
|
311
|
+
'after:render': this.afterRender,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
exports.default = GuidelineHandler;
|