react-design-editor 0.0.32 → 0.0.36

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 (101) hide show
  1. package/README.md +37 -33
  2. package/dist/react-design-editor.js +33723 -13736
  3. package/dist/react-design-editor.min.js +1 -1
  4. package/dist/react-design-editor.min.js.LICENSE.txt +13 -2
  5. package/lib/Canvas.d.ts +18 -32
  6. package/lib/Canvas.js +172 -245
  7. package/lib/CanvasObject.d.ts +10 -10
  8. package/lib/CanvasObject.js +96 -89
  9. package/lib/constants/code.d.ts +19 -0
  10. package/lib/constants/code.js +22 -0
  11. package/lib/constants/defaults.d.ts +38 -0
  12. package/lib/constants/defaults.js +69 -0
  13. package/lib/constants/index.d.ts +3 -0
  14. package/lib/constants/index.js +26 -0
  15. package/lib/handlers/AlignmentHandler.d.ts +18 -18
  16. package/lib/handlers/AlignmentHandler.js +58 -58
  17. package/lib/handlers/AnimationHandler.d.ts +50 -48
  18. package/lib/handlers/AnimationHandler.js +323 -298
  19. package/lib/handlers/ChartHandler.d.ts +8 -8
  20. package/lib/handlers/ChartHandler.js +8 -8
  21. package/lib/handlers/ContextmenuHandler.d.ts +28 -23
  22. package/lib/handlers/ContextmenuHandler.js +65 -56
  23. package/lib/handlers/CropHandler.d.ts +43 -37
  24. package/lib/handlers/CropHandler.js +261 -243
  25. package/lib/handlers/CustomHandler.d.ts +7 -0
  26. package/lib/handlers/CustomHandler.js +10 -0
  27. package/lib/handlers/DrawingHandler.d.ts +28 -28
  28. package/lib/handlers/DrawingHandler.js +318 -318
  29. package/lib/handlers/ElementHandler.d.ts +80 -80
  30. package/lib/handlers/ElementHandler.js +154 -154
  31. package/lib/handlers/EventHandler.d.ts +170 -182
  32. package/lib/handlers/EventHandler.js +880 -904
  33. package/lib/handlers/FiberHandler.d.ts +6 -0
  34. package/lib/handlers/FiberHandler.js +23 -0
  35. package/lib/handlers/GridHandler.d.ts +19 -18
  36. package/lib/handlers/GridHandler.js +77 -76
  37. package/lib/handlers/GuidelineHandler.d.ts +61 -39
  38. package/lib/handlers/GuidelineHandler.js +315 -266
  39. package/lib/handlers/Handler.d.ts +622 -543
  40. package/lib/handlers/Handler.js +1638 -1510
  41. package/lib/handlers/ImageHandler.d.ts +307 -307
  42. package/lib/handlers/ImageHandler.js +529 -484
  43. package/lib/handlers/InteractionHandler.d.ts +45 -45
  44. package/lib/handlers/InteractionHandler.js +164 -159
  45. package/lib/handlers/LinkHandler.d.ts +115 -116
  46. package/lib/handlers/LinkHandler.js +247 -246
  47. package/lib/handlers/NodeHandler.d.ts +50 -50
  48. package/lib/handlers/NodeHandler.js +274 -274
  49. package/lib/handlers/PortHandler.d.ts +22 -22
  50. package/lib/handlers/PortHandler.js +179 -179
  51. package/lib/handlers/ShortcutHandler.d.ts +119 -119
  52. package/lib/handlers/ShortcutHandler.js +151 -150
  53. package/lib/handlers/TooltipHandler.d.ts +33 -24
  54. package/lib/handlers/TooltipHandler.js +91 -78
  55. package/lib/handlers/TransactionHandler.d.ts +59 -54
  56. package/lib/handlers/TransactionHandler.js +137 -132
  57. package/lib/handlers/WorkareaHandler.d.ts +43 -41
  58. package/lib/handlers/WorkareaHandler.js +354 -352
  59. package/lib/handlers/ZoomHandler.d.ts +48 -48
  60. package/lib/handlers/ZoomHandler.js +143 -143
  61. package/lib/handlers/index.d.ts +23 -21
  62. package/lib/handlers/index.js +48 -44
  63. package/lib/index.d.ts +6 -5
  64. package/lib/index.js +20 -19
  65. package/lib/objects/Arrow.d.ts +2 -2
  66. package/lib/objects/Arrow.js +40 -39
  67. package/lib/objects/Chart.d.ts +10 -10
  68. package/lib/objects/Chart.js +117 -116
  69. package/lib/objects/CirclePort.d.ts +2 -2
  70. package/lib/objects/CirclePort.js +28 -28
  71. package/lib/objects/Cube.d.ts +5 -5
  72. package/lib/objects/Cube.js +71 -71
  73. package/lib/objects/CurvedLink.d.ts +2 -2
  74. package/lib/objects/CurvedLink.js +51 -49
  75. package/lib/objects/Element.d.ts +13 -13
  76. package/lib/objects/Element.js +77 -76
  77. package/lib/objects/Gif.d.ts +3 -3
  78. package/lib/objects/Gif.js +41 -38
  79. package/lib/objects/Iframe.d.ts +9 -9
  80. package/lib/objects/Iframe.js +63 -62
  81. package/lib/objects/Line.d.ts +2 -2
  82. package/lib/objects/Line.js +24 -24
  83. package/lib/objects/Link.d.ts +15 -15
  84. package/lib/objects/Link.js +106 -104
  85. package/lib/objects/Node.d.ts +59 -59
  86. package/lib/objects/Node.js +271 -268
  87. package/lib/objects/OrthogonalLink.d.ts +2 -2
  88. package/lib/objects/OrthogonalLink.js +54 -52
  89. package/lib/objects/Port.d.ts +12 -12
  90. package/lib/objects/Port.js +28 -28
  91. package/lib/objects/Svg.d.ts +8 -0
  92. package/lib/objects/Svg.js +59 -0
  93. package/lib/objects/Video.d.ts +14 -14
  94. package/lib/objects/Video.js +113 -112
  95. package/lib/objects/index.d.ts +15 -14
  96. package/lib/objects/index.js +32 -30
  97. package/lib/utils/ObjectUtil.d.ts +407 -413
  98. package/lib/utils/ObjectUtil.js +13 -13
  99. package/lib/utils/index.d.ts +1 -1
  100. package/lib/utils/index.js +13 -13
  101. package/package.json +20 -17
@@ -1,904 +1,880 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fabric_1 = require("fabric");
7
- const animejs_1 = __importDefault(require("animejs"));
8
- /**
9
- * Event Handler Class
10
- * @author salgum1114
11
- * @class EventHandler
12
- */
13
- class EventHandler {
14
- constructor(handler) {
15
- /**
16
- * Attch event on document
17
- *
18
- */
19
- this.attachEventListener = () => {
20
- if (this.handler.editable) {
21
- this.handler.canvas.on({
22
- 'object:modified': this.modified,
23
- 'object:scaling': this.scaling,
24
- 'object:scaled': this.scaled,
25
- 'object:moving': this.moving,
26
- 'object:moved': this.moved,
27
- 'object:rotating': this.rotating,
28
- 'object:rotated': this.rotated,
29
- 'mouse:wheel': this.mousewheel,
30
- 'mouse:down': this.mousedown,
31
- 'mouse:move': this.mousemove,
32
- 'mouse:up': this.mouseup,
33
- 'selection:cleared': this.selection,
34
- 'selection:created': this.selection,
35
- 'selection:updated': this.selection,
36
- 'before:render': this.handler.guidelineOption.enabled ? this.beforeRender : null,
37
- 'after:render': this.handler.guidelineOption.enabled ? this.afterRender : null,
38
- });
39
- }
40
- else {
41
- this.handler.canvas.on({
42
- 'mouse:down': this.mousedown,
43
- 'mouse:move': this.mousemove,
44
- 'mouse:out': this.mouseout,
45
- 'mouse:up': this.mouseup,
46
- 'mouse:wheel': this.mousewheel,
47
- });
48
- }
49
- this.handler.canvas.wrapperEl.tabIndex = 1000;
50
- this.handler.canvas.wrapperEl.addEventListener('keydown', this.keydown, false);
51
- this.handler.canvas.wrapperEl.addEventListener('keyup', this.keyup, false);
52
- this.handler.canvas.wrapperEl.addEventListener('mousedown', this.onmousedown, false);
53
- this.handler.canvas.wrapperEl.addEventListener('contextmenu', this.contextmenu, false);
54
- if (this.handler.keyEvent.clipboard) {
55
- document.addEventListener('paste', this.paste, false);
56
- }
57
- };
58
- /**
59
- * Detach event on document
60
- *
61
- */
62
- this.detachEventListener = () => {
63
- if (this.handler.editable) {
64
- this.handler.canvas.off({
65
- 'object:modified': this.modified,
66
- 'object:scaling': this.scaling,
67
- 'object:moving': this.moving,
68
- 'object:moved': this.moved,
69
- 'object:rotating': this.rotating,
70
- 'mouse:wheel': this.mousewheel,
71
- 'mouse:down': this.mousedown,
72
- 'mouse:move': this.mousemove,
73
- 'mouse:up': this.mouseup,
74
- 'selection:cleared': this.selection,
75
- 'selection:created': this.selection,
76
- 'selection:updated': this.selection,
77
- 'before:render': this.beforeRender,
78
- 'after:render': this.afterRender,
79
- });
80
- }
81
- else {
82
- this.handler.canvas.off({
83
- 'mouse:down': this.mousedown,
84
- 'mouse:move': this.mousemove,
85
- 'mouse:out': this.mouseout,
86
- 'mouse:up': this.mouseup,
87
- 'mouse:wheel': this.mousewheel,
88
- });
89
- this.handler.getObjects().forEach(object => {
90
- object.off('mousedown', this.handler.eventHandler.object.mousedown);
91
- if (object.anime) {
92
- animejs_1.default.remove(object);
93
- }
94
- });
95
- }
96
- this.handler.canvas.wrapperEl.removeEventListener('keydown', this.keydown);
97
- this.handler.canvas.wrapperEl.removeEventListener('keyup', this.keyup);
98
- this.handler.canvas.wrapperEl.removeEventListener('mousedown', this.onmousedown);
99
- this.handler.canvas.wrapperEl.removeEventListener('contextmenu', this.contextmenu);
100
- if (this.handler.keyEvent.clipboard) {
101
- this.handler.canvas.wrapperEl.removeEventListener('paste', this.paste);
102
- }
103
- };
104
- /**
105
- * Individual object event
106
- *
107
- */
108
- this.object = {
109
- /**
110
- * Mouse down event on object
111
- * @param {FabricEvent} opt
112
- */
113
- mousedown: (opt) => {
114
- const { target } = opt;
115
- if (target && target.link && target.link.enabled) {
116
- const { onClick } = this.handler;
117
- if (onClick) {
118
- onClick(this.handler.canvas, target);
119
- }
120
- }
121
- },
122
- /**
123
- * Mouse double click event on object
124
- * @param {FabricEvent} opt
125
- */
126
- mousedblclick: (opt) => {
127
- const { target } = opt;
128
- if (target) {
129
- const { onDblClick } = this.handler;
130
- if (onDblClick) {
131
- onDblClick(this.handler.canvas, target);
132
- }
133
- }
134
- },
135
- };
136
- /**
137
- * Modified event object
138
- *
139
- * @param {FabricEvent} opt
140
- * @returns
141
- */
142
- this.modified = (opt) => {
143
- const { target } = opt;
144
- if (!target) {
145
- return;
146
- }
147
- if (target.type === 'circle' && target.parentId) {
148
- return;
149
- }
150
- const { onModified } = this.handler;
151
- if (onModified) {
152
- onModified(target);
153
- }
154
- };
155
- /**
156
- * Moving event object
157
- *
158
- * @param {FabricEvent} opt
159
- * @returns
160
- */
161
- this.moving = (opt) => {
162
- const { target } = opt;
163
- if (this.handler.interactionMode === 'crop') {
164
- this.handler.cropHandler.moving(opt);
165
- }
166
- else {
167
- if (this.handler.editable && this.handler.guidelineOption.enabled) {
168
- this.handler.guidelineHandler.movingGuidelines(target);
169
- }
170
- if (target.type === 'activeSelection') {
171
- const activeSelection = target;
172
- activeSelection.getObjects().forEach((obj) => {
173
- const left = target.left + obj.left + target.width / 2;
174
- const top = target.top + obj.top + target.height / 2;
175
- if (obj.superType === 'node') {
176
- this.handler.portHandler.setCoords({ ...obj, left, top });
177
- }
178
- else if (obj.superType === 'element') {
179
- const { id } = obj;
180
- const el = this.handler.elementHandler.findById(id);
181
- // TODO... Element object incorrect position
182
- this.handler.elementHandler.setPositionByOrigin(el, obj, left, top);
183
- }
184
- });
185
- return;
186
- }
187
- if (target.superType === 'node') {
188
- this.handler.portHandler.setCoords(target);
189
- }
190
- else if (target.superType === 'element') {
191
- const { id } = target;
192
- const el = this.handler.elementHandler.findById(id);
193
- this.handler.elementHandler.setPosition(el, target);
194
- }
195
- }
196
- };
197
- /**
198
- * Moved event object
199
- *
200
- * @param {FabricEvent} opt
201
- */
202
- this.moved = (opt) => {
203
- const { target } = opt;
204
- this.handler.gridHandler.setCoords(target);
205
- if (!this.handler.transactionHandler.active) {
206
- this.handler.transactionHandler.save('moved');
207
- }
208
- if (target.superType === 'element') {
209
- const { id } = target;
210
- const el = this.handler.elementHandler.findById(id);
211
- this.handler.elementHandler.setPosition(el, target);
212
- }
213
- };
214
- /**
215
- * Scaling event object
216
- *
217
- * @param {FabricEvent} opt
218
- */
219
- this.scaling = (opt) => {
220
- const { target } = opt;
221
- if (this.handler.interactionMode === 'crop') {
222
- this.handler.cropHandler.resize(opt);
223
- }
224
- // TODO...this.handler.guidelineHandler.scalingGuidelines(target);
225
- if (target.superType === 'element') {
226
- const { id, width, height } = target;
227
- const el = this.handler.elementHandler.findById(id);
228
- // update the element
229
- this.handler.elementHandler.setScaleOrAngle(el, target);
230
- this.handler.elementHandler.setSize(el, target);
231
- this.handler.elementHandler.setPosition(el, target);
232
- const video = target;
233
- if (video.type === 'video' && video.player) {
234
- video.player.setPlayerSize(width, height);
235
- }
236
- }
237
- };
238
- /**
239
- * Scaled event object
240
- *
241
- * @param {FabricEvent} opt
242
- */
243
- this.scaled = (_opt) => {
244
- if (!this.handler.transactionHandler.active) {
245
- this.handler.transactionHandler.save('scaled');
246
- }
247
- };
248
- /**
249
- * Rotating event object
250
- *
251
- * @param {FabricEvent} opt
252
- */
253
- this.rotating = (opt) => {
254
- const { target } = opt;
255
- if (target.superType === 'element') {
256
- const { id } = target;
257
- const el = this.handler.elementHandler.findById(id);
258
- // update the element
259
- this.handler.elementHandler.setScaleOrAngle(el, target);
260
- }
261
- };
262
- /**
263
- * Rotated event object
264
- *
265
- * @param {FabricEvent} opt
266
- */
267
- this.rotated = (_opt) => {
268
- if (!this.handler.transactionHandler.active) {
269
- this.handler.transactionHandler.save('rotated');
270
- }
271
- };
272
- /**
273
- * Moing object at keyboard arrow key down event
274
- *
275
- * @param {KeyboardEvent} e
276
- * @returns
277
- */
278
- this.arrowmoving = (e) => {
279
- const activeObject = this.handler.canvas.getActiveObject();
280
- if (!activeObject) {
281
- return false;
282
- }
283
- if (activeObject.id === 'workarea') {
284
- return false;
285
- }
286
- if (e.keyCode === 38) {
287
- activeObject.set('top', activeObject.top - 2);
288
- activeObject.setCoords();
289
- this.handler.canvas.renderAll();
290
- return true;
291
- }
292
- else if (e.keyCode === 40) {
293
- activeObject.set('top', activeObject.top + 2);
294
- activeObject.setCoords();
295
- this.handler.canvas.renderAll();
296
- return true;
297
- }
298
- else if (e.keyCode === 37) {
299
- activeObject.set('left', activeObject.left - 2);
300
- activeObject.setCoords();
301
- this.handler.canvas.renderAll();
302
- return true;
303
- }
304
- else if (e.keyCode === 39) {
305
- activeObject.set('left', activeObject.left + 2);
306
- activeObject.setCoords();
307
- this.handler.canvas.renderAll();
308
- return true;
309
- }
310
- if (this.handler.onModified) {
311
- this.handler.onModified(activeObject);
312
- }
313
- return true;
314
- };
315
- /**
316
- * Zoom at mouse wheel event
317
- *
318
- * @param {FabricEvent<WheelEvent>} opt
319
- * @returns
320
- */
321
- this.mousewheel = (opt) => {
322
- const event = opt;
323
- const { zoomEnabled } = this.handler;
324
- if (!zoomEnabled) {
325
- return;
326
- }
327
- const delta = event.e.deltaY;
328
- let zoomRatio = this.handler.canvas.getZoom();
329
- if (delta > 0) {
330
- zoomRatio -= 0.05;
331
- }
332
- else {
333
- zoomRatio += 0.05;
334
- }
335
- this.handler.zoomHandler.zoomToPoint(new fabric_1.fabric.Point(this.handler.canvas.getWidth() / 2, this.handler.canvas.getHeight() / 2), zoomRatio);
336
- event.e.preventDefault();
337
- event.e.stopPropagation();
338
- };
339
- /**
340
- * Mouse down event on object
341
- *
342
- * @param {FabricEvent<MouseEvent>} opt
343
- * @returns
344
- */
345
- this.mousedown = (opt) => {
346
- const event = opt;
347
- const { editable } = this.handler;
348
- if (event.e.altKey && editable && !this.handler.interactionHandler.isDrawingMode()) {
349
- this.handler.interactionHandler.grab();
350
- this.panning = true;
351
- return;
352
- }
353
- if (this.handler.interactionMode === 'grab') {
354
- this.panning = true;
355
- return;
356
- }
357
- const { target } = event;
358
- if (editable) {
359
- if (this.handler.prevTarget && this.handler.prevTarget.superType === 'link') {
360
- this.handler.prevTarget.set({
361
- stroke: this.handler.prevTarget.originStroke,
362
- });
363
- }
364
- if (target && target.type === 'fromPort') {
365
- this.handler.linkHandler.init(target);
366
- return;
367
- }
368
- if (target &&
369
- this.handler.interactionMode === 'link' &&
370
- (target.type === 'toPort' || target.superType === 'node')) {
371
- let toPort;
372
- if (target.superType === 'node') {
373
- toPort = target.toPort;
374
- }
375
- else {
376
- toPort = target;
377
- }
378
- this.handler.linkHandler.generate(toPort);
379
- return;
380
- }
381
- this.handler.guidelineHandler.viewportTransform = this.handler.canvas.viewportTransform;
382
- this.handler.guidelineHandler.zoom = this.handler.canvas.getZoom();
383
- if (this.handler.interactionMode === 'selection') {
384
- if (target && target.superType === 'link') {
385
- target.set({
386
- stroke: target.selectFill || 'green',
387
- });
388
- }
389
- this.handler.prevTarget = target;
390
- return;
391
- }
392
- if (this.handler.interactionMode === 'polygon') {
393
- if (target && this.handler.pointArray.length && target.id === this.handler.pointArray[0].id) {
394
- this.handler.drawingHandler.polygon.generate(this.handler.pointArray);
395
- }
396
- else {
397
- this.handler.drawingHandler.polygon.addPoint(event);
398
- }
399
- }
400
- else if (this.handler.interactionMode === 'line') {
401
- if (this.handler.pointArray.length && this.handler.activeLine) {
402
- this.handler.drawingHandler.line.generate(event);
403
- }
404
- else {
405
- this.handler.drawingHandler.line.addPoint(event);
406
- }
407
- }
408
- else if (this.handler.interactionMode === 'arrow') {
409
- if (this.handler.pointArray.length && this.handler.activeLine) {
410
- this.handler.drawingHandler.arrow.generate(event);
411
- }
412
- else {
413
- this.handler.drawingHandler.arrow.addPoint(event);
414
- }
415
- }
416
- }
417
- };
418
- /**
419
- * Mouse move event on canvas
420
- *
421
- * @param {FabricEvent<MouseEvent>} opt
422
- * @returns
423
- */
424
- this.mousemove = (opt) => {
425
- const event = opt;
426
- if (this.handler.interactionMode === 'grab' && this.panning) {
427
- this.handler.interactionHandler.moving(event.e);
428
- this.handler.canvas.requestRenderAll();
429
- }
430
- if (!this.handler.editable && event.target) {
431
- if (event.target.superType === 'element') {
432
- return;
433
- }
434
- if (event.target.id !== 'workarea') {
435
- if (event.target !== this.handler.target) {
436
- this.handler.tooltipHandler.show(event.target);
437
- }
438
- }
439
- else {
440
- this.handler.tooltipHandler.hide(event.target);
441
- }
442
- }
443
- if (this.handler.interactionMode === 'polygon') {
444
- if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
445
- const pointer = this.handler.canvas.getPointer(event.e);
446
- this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
447
- const points = this.handler.activeShape.get('points');
448
- points[this.handler.pointArray.length] = {
449
- x: pointer.x,
450
- y: pointer.y,
451
- };
452
- this.handler.activeShape.set({
453
- points,
454
- });
455
- this.handler.canvas.requestRenderAll();
456
- }
457
- }
458
- else if (this.handler.interactionMode === 'line') {
459
- if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
460
- const pointer = this.handler.canvas.getPointer(event.e);
461
- this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
462
- }
463
- this.handler.canvas.requestRenderAll();
464
- }
465
- else if (this.handler.interactionMode === 'arrow') {
466
- if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
467
- const pointer = this.handler.canvas.getPointer(event.e);
468
- this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
469
- }
470
- this.handler.canvas.requestRenderAll();
471
- }
472
- else if (this.handler.interactionMode === 'link') {
473
- if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
474
- const pointer = this.handler.canvas.getPointer(event.e);
475
- this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
476
- }
477
- this.handler.canvas.requestRenderAll();
478
- }
479
- return;
480
- };
481
- /**
482
- * Mouse up event on canvas
483
- *
484
- * @param {FabricEvent<MouseEvent>} opt
485
- * @returns
486
- */
487
- this.mouseup = (opt) => {
488
- const event = opt;
489
- if (this.handler.interactionMode === 'grab') {
490
- this.panning = false;
491
- return;
492
- }
493
- const { target, e } = event;
494
- if (this.handler.interactionMode === 'selection') {
495
- if (target && e.shiftKey && target.superType === 'node') {
496
- const node = target;
497
- this.handler.canvas.discardActiveObject();
498
- const nodes = [];
499
- this.handler.nodeHandler.getNodePath(node, nodes);
500
- const activeSelection = new fabric_1.fabric.ActiveSelection(nodes, {
501
- canvas: this.handler.canvas,
502
- ...this.handler.activeSelection,
503
- });
504
- this.handler.canvas.setActiveObject(activeSelection);
505
- this.handler.canvas.requestRenderAll();
506
- }
507
- }
508
- if (this.handler.editable && this.handler.guidelineOption.enabled) {
509
- this.handler.guidelineHandler.verticalLines.length = 0;
510
- this.handler.guidelineHandler.horizontalLines.length = 0;
511
- }
512
- this.handler.canvas.renderAll();
513
- };
514
- /**
515
- * Mouse out event on canvas
516
- *
517
- * @param {FabricEvent<MouseEvent>} opt
518
- */
519
- this.mouseout = (opt) => {
520
- const event = opt;
521
- if (!event.target) {
522
- this.handler.tooltipHandler.hide();
523
- }
524
- };
525
- /**
526
- * Selection event event on canvas
527
- *
528
- * @param {FabricEvent} opt
529
- */
530
- this.selection = (opt) => {
531
- const { onSelect, activeSelection } = this.handler;
532
- const target = opt.target;
533
- if (target && target.type === 'activeSelection') {
534
- target.set({
535
- ...activeSelection,
536
- });
537
- }
538
- if (onSelect) {
539
- onSelect(target);
540
- }
541
- };
542
- /**
543
- * Before the render
544
- *
545
- * @param {FabricEvent} _opt
546
- */
547
- this.beforeRender = (_opt) => {
548
- this.handler.canvas.clearContext(this.handler.guidelineHandler.ctx);
549
- };
550
- /**
551
- * After the render
552
- *
553
- * @param {FabricEvent} _opt
554
- */
555
- this.afterRender = (_opt) => {
556
- for (let i = this.handler.guidelineHandler.verticalLines.length; i--;) {
557
- this.handler.guidelineHandler.drawVerticalLine(this.handler.guidelineHandler.verticalLines[i]);
558
- }
559
- for (let i = this.handler.guidelineHandler.horizontalLines.length; i--;) {
560
- this.handler.guidelineHandler.drawHorizontalLine(this.handler.guidelineHandler.horizontalLines[i]);
561
- }
562
- this.handler.guidelineHandler.verticalLines.length = 0;
563
- this.handler.guidelineHandler.horizontalLines.length = 0;
564
- };
565
- /**
566
- * Called resize event on canvas
567
- *
568
- * @param {number} nextWidth
569
- * @param {number} nextHeight
570
- * @returns
571
- */
572
- this.resize = (nextWidth, nextHeight) => {
573
- this.handler.canvas.setWidth(nextWidth).setHeight(nextHeight);
574
- this.handler.canvas.setBackgroundColor(this.handler.canvasOption.backgroundColor, this.handler.canvas.renderAll.bind(this.handler.canvas));
575
- if (!this.handler.workarea) {
576
- return;
577
- }
578
- const diffWidth = nextWidth / 2 - this.handler.width / 2;
579
- const diffHeight = nextHeight / 2 - this.handler.height / 2;
580
- this.handler.width = nextWidth;
581
- this.handler.height = nextHeight;
582
- if (this.handler.workarea.layout === 'fixed') {
583
- this.handler.canvas.centerObject(this.handler.workarea);
584
- this.handler.workarea.setCoords();
585
- if (this.handler.gridOption.enabled) {
586
- return;
587
- }
588
- this.handler.canvas.getObjects().forEach((obj) => {
589
- if (obj.id !== 'workarea') {
590
- const left = obj.left + diffWidth;
591
- const top = obj.top + diffHeight;
592
- obj.set({
593
- left,
594
- top,
595
- });
596
- obj.setCoords();
597
- if (obj.superType === 'element') {
598
- const { id } = obj;
599
- const el = this.handler.elementHandler.findById(id);
600
- // update the element
601
- this.handler.elementHandler.setPosition(el, obj);
602
- }
603
- }
604
- });
605
- this.handler.canvas.requestRenderAll();
606
- return;
607
- }
608
- if (this.handler.workarea.layout === 'responsive') {
609
- const { scaleX } = this.handler.workareaHandler.calculateScale();
610
- const center = this.handler.canvas.getCenter();
611
- const deltaPoint = new fabric_1.fabric.Point(diffWidth, diffHeight);
612
- this.handler.canvas.relativePan(deltaPoint);
613
- this.handler.zoomHandler.zoomToPoint(new fabric_1.fabric.Point(center.left, center.top), scaleX);
614
- return;
615
- }
616
- const scaleX = nextWidth / this.handler.workarea.width;
617
- const scaleY = nextHeight / this.handler.workarea.height;
618
- const diffScaleX = nextWidth / (this.handler.workarea.width * this.handler.workarea.scaleX);
619
- const diffScaleY = nextHeight / (this.handler.workarea.height * this.handler.workarea.scaleY);
620
- this.handler.workarea.set({
621
- scaleX,
622
- scaleY,
623
- });
624
- this.handler.canvas.getObjects().forEach((obj) => {
625
- const { id } = obj;
626
- if (obj.id !== 'workarea') {
627
- const left = obj.left * diffScaleX;
628
- const top = obj.top * diffScaleY;
629
- const newScaleX = obj.scaleX * diffScaleX;
630
- const newScaleY = obj.scaleY * diffScaleY;
631
- obj.set({
632
- scaleX: newScaleX,
633
- scaleY: newScaleY,
634
- left,
635
- top,
636
- });
637
- obj.setCoords();
638
- if (obj.superType === 'element') {
639
- const video = obj;
640
- const { width, height } = obj;
641
- const el = this.handler.elementHandler.findById(id);
642
- this.handler.elementHandler.setSize(el, obj);
643
- if (video.player) {
644
- video.player.setPlayerSize(width, height);
645
- }
646
- this.handler.elementHandler.setPosition(el, obj);
647
- }
648
- }
649
- });
650
- this.handler.canvas.renderAll();
651
- };
652
- /**
653
- * Paste event on canvas
654
- *
655
- * @param {ClipboardEvent} e
656
- * @returns
657
- */
658
- this.paste = (e) => {
659
- if (this.handler.canvas.wrapperEl !== document.activeElement) {
660
- return false;
661
- }
662
- if (e.preventDefault) {
663
- e.preventDefault();
664
- }
665
- if (e.stopPropagation) {
666
- e.stopPropagation();
667
- }
668
- const clipboardData = e.clipboardData;
669
- if (clipboardData.types.length) {
670
- clipboardData.types.forEach((clipboardType) => {
671
- if (clipboardType === 'text/plain') {
672
- const textPlain = clipboardData.getData('text/plain');
673
- try {
674
- const objects = JSON.parse(textPlain);
675
- const { gridOption: { grid = 10 }, isCut, } = this.handler;
676
- const padding = isCut ? 0 : grid;
677
- if (objects && Array.isArray(objects)) {
678
- const filteredObjects = objects.filter(obj => obj !== null);
679
- if (filteredObjects.length === 1) {
680
- const obj = filteredObjects[0];
681
- if (typeof obj.cloneable !== 'undefined' && !obj.cloneable) {
682
- return;
683
- }
684
- obj.left = obj.properties.left + padding;
685
- obj.top = obj.properties.top + padding;
686
- const createdObj = this.handler.add(obj, false, true, false);
687
- this.handler.canvas.setActiveObject(createdObj);
688
- this.handler.canvas.requestRenderAll();
689
- }
690
- else {
691
- const nodes = [];
692
- const targets = [];
693
- objects.forEach(obj => {
694
- if (!obj) {
695
- return;
696
- }
697
- if (obj.superType === 'link') {
698
- obj.fromNodeId = nodes[obj.fromNodeIndex].id;
699
- obj.toNodeId = nodes[obj.toNodeIndex].id;
700
- }
701
- else {
702
- obj.left = obj.properties.left + padding;
703
- obj.top = obj.properties.top + padding;
704
- }
705
- const createdObj = this.handler.add(obj, false, true, false);
706
- if (obj.superType === 'node') {
707
- nodes.push(createdObj);
708
- }
709
- else {
710
- targets.push(createdObj);
711
- }
712
- });
713
- const activeSelection = new fabric_1.fabric.ActiveSelection(nodes.length ? nodes : targets, {
714
- canvas: this.handler.canvas,
715
- ...this.handler.activeSelection,
716
- });
717
- this.handler.canvas.setActiveObject(activeSelection);
718
- this.handler.canvas.requestRenderAll();
719
- }
720
- if (!this.handler.transactionHandler.active) {
721
- this.handler.transactionHandler.save('paste');
722
- }
723
- this.handler.isCut = false;
724
- this.handler.copy();
725
- }
726
- }
727
- catch (error) {
728
- console.error(error);
729
- // const item = {
730
- // id: uuv4id(),
731
- // type: 'textbox',
732
- // text: textPlain,
733
- // };
734
- // this.handler.add(item, true);
735
- }
736
- }
737
- else if (clipboardType === 'text/html') {
738
- // Todo ...
739
- // const textHtml = clipboardData.getData('text/html');
740
- // console.log(textHtml);
741
- }
742
- else if (clipboardType === 'Files') {
743
- // Array.from(clipboardData.files).forEach((file) => {
744
- // const { type } = file;
745
- // if (type === 'image/png' || type === 'image/jpeg' || type === 'image/jpg') {
746
- // const item = {
747
- // id: v4(),
748
- // type: 'image',
749
- // file,
750
- // superType: 'image',
751
- // };
752
- // this.handler.add(item, true);
753
- // } else {
754
- // console.error('Not supported file type');
755
- // }
756
- // });
757
- }
758
- });
759
- }
760
- return true;
761
- };
762
- /**
763
- * Keydown event on document
764
- *
765
- * @param {KeyboardEvent} e
766
- */
767
- this.keydown = (e) => {
768
- const { keyEvent, editable } = this.handler;
769
- if (!Object.keys(keyEvent).length) {
770
- return;
771
- }
772
- const { clipboard } = keyEvent;
773
- if (this.handler.interactionHandler.isDrawingMode()) {
774
- if (this.handler.shortcutHandler.isEscape(e)) {
775
- if (this.handler.interactionMode === 'polygon') {
776
- this.handler.drawingHandler.polygon.finish();
777
- }
778
- else if (this.handler.interactionMode === 'line') {
779
- this.handler.drawingHandler.line.finish();
780
- }
781
- else if (this.handler.interactionMode === 'arrow') {
782
- this.handler.drawingHandler.arrow.finish();
783
- }
784
- else if (this.handler.interactionMode === 'link') {
785
- this.handler.linkHandler.finish();
786
- }
787
- }
788
- return;
789
- }
790
- if (this.handler.shortcutHandler.isW(e)) {
791
- this.keyCode = e.keyCode;
792
- this.handler.interactionHandler.grab();
793
- return;
794
- }
795
- if (e.altKey && editable) {
796
- this.handler.interactionHandler.grab();
797
- return;
798
- }
799
- if (this.handler.shortcutHandler.isEscape(e)) {
800
- if (this.handler.interactionMode === 'selection') {
801
- this.handler.canvas.discardActiveObject();
802
- this.handler.canvas.renderAll();
803
- }
804
- this.handler.tooltipHandler.hide();
805
- }
806
- if (this.handler.canvas.wrapperEl !== document.activeElement) {
807
- return;
808
- }
809
- if (editable) {
810
- if (this.handler.shortcutHandler.isQ(e)) {
811
- this.keyCode = e.keyCode;
812
- }
813
- else if (this.handler.shortcutHandler.isDelete(e)) {
814
- this.handler.remove();
815
- }
816
- else if (this.handler.shortcutHandler.isArrow(e)) {
817
- this.arrowmoving(e);
818
- }
819
- else if (this.handler.shortcutHandler.isCtrlA(e)) {
820
- e.preventDefault();
821
- this.handler.selectAll();
822
- }
823
- else if (this.handler.shortcutHandler.isCtrlC(e)) {
824
- e.preventDefault();
825
- this.handler.copy();
826
- }
827
- else if (this.handler.shortcutHandler.isCtrlV(e) && !clipboard) {
828
- e.preventDefault();
829
- this.handler.paste();
830
- }
831
- else if (this.handler.shortcutHandler.isCtrlX(e)) {
832
- e.preventDefault();
833
- this.handler.cut();
834
- }
835
- else if (this.handler.shortcutHandler.isCtrlZ(e)) {
836
- e.preventDefault();
837
- this.handler.transactionHandler.undo();
838
- }
839
- else if (this.handler.shortcutHandler.isCtrlY(e)) {
840
- e.preventDefault();
841
- this.handler.transactionHandler.redo();
842
- }
843
- else if (this.handler.shortcutHandler.isPlus(e)) {
844
- e.preventDefault();
845
- this.handler.zoomHandler.zoomIn();
846
- }
847
- else if (this.handler.shortcutHandler.isMinus(e)) {
848
- e.preventDefault();
849
- this.handler.zoomHandler.zoomOut();
850
- }
851
- else if (this.handler.shortcutHandler.isO(e)) {
852
- e.preventDefault();
853
- this.handler.zoomHandler.zoomOneToOne();
854
- }
855
- else if (this.handler.shortcutHandler.isP(e)) {
856
- e.preventDefault();
857
- this.handler.zoomHandler.zoomToFit();
858
- }
859
- return;
860
- }
861
- return;
862
- };
863
- /**
864
- * Key up event on canvas
865
- *
866
- * @param {KeyboardEvent} _e
867
- */
868
- this.keyup = (e) => {
869
- if (this.handler.interactionHandler.isDrawingMode()) {
870
- return;
871
- }
872
- if (!this.handler.shortcutHandler.isW(e)) {
873
- this.handler.interactionHandler.selection();
874
- }
875
- };
876
- /**
877
- * Context menu event on canvas
878
- *
879
- * @param {MouseEvent} e
880
- */
881
- this.contextmenu = (e) => {
882
- e.preventDefault();
883
- const { editable, onContext } = this.handler;
884
- if (editable && onContext) {
885
- const target = this.handler.canvas.findTarget(e, false);
886
- if (target && target.type !== 'activeSelection') {
887
- this.handler.select(target);
888
- }
889
- this.handler.contextmenuHandler.show(e, target);
890
- }
891
- };
892
- /**
893
- * Mouse down event on canvas
894
- *
895
- * @param {MouseEvent} _e
896
- */
897
- this.onmousedown = (_e) => {
898
- this.handler.contextmenuHandler.hide();
899
- };
900
- this.handler = handler;
901
- this.attachEventListener();
902
- }
903
- }
904
- exports.default = EventHandler;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const animejs_1 = __importDefault(require("animejs"));
7
+ const fabric_1 = require("fabric");
8
+ const constants_1 = require("../constants");
9
+ /**
10
+ * Event Handler Class
11
+ * @author salgum1114
12
+ * @class EventHandler
13
+ */
14
+ class EventHandler {
15
+ constructor(handler) {
16
+ /**
17
+ * Detach event on document
18
+ *
19
+ */
20
+ this.destroy = () => {
21
+ if (this.handler.editable) {
22
+ this.handler.canvas.off({
23
+ 'object:modified': this.modified,
24
+ 'object:scaling': this.scaling,
25
+ 'object:moving': this.moving,
26
+ 'object:moved': this.moved,
27
+ 'object:rotating': this.rotating,
28
+ 'mouse:wheel': this.mousewheel,
29
+ 'mouse:down': this.mousedown,
30
+ 'mouse:move': this.mousemove,
31
+ 'mouse:up': this.mouseup,
32
+ 'selection:cleared': this.selection,
33
+ 'selection:created': this.selection,
34
+ 'selection:updated': this.selection,
35
+ });
36
+ }
37
+ else {
38
+ this.handler.canvas.off({
39
+ 'mouse:down': this.mousedown,
40
+ 'mouse:move': this.mousemove,
41
+ 'mouse:out': this.mouseout,
42
+ 'mouse:up': this.mouseup,
43
+ 'mouse:wheel': this.mousewheel,
44
+ });
45
+ this.handler.getObjects().forEach(object => {
46
+ object.off('mousedown', this.handler.eventHandler.object.mousedown);
47
+ if (object.anime) {
48
+ animejs_1.default.remove(object);
49
+ }
50
+ });
51
+ }
52
+ this.handler.canvas.wrapperEl.removeEventListener('keydown', this.keydown);
53
+ this.handler.canvas.wrapperEl.removeEventListener('keyup', this.keyup);
54
+ this.handler.canvas.wrapperEl.removeEventListener('mousedown', this.onmousedown);
55
+ this.handler.canvas.wrapperEl.removeEventListener('contextmenu', this.contextmenu);
56
+ if (this.handler.keyEvent.clipboard) {
57
+ this.handler.canvas.wrapperEl.removeEventListener('paste', this.paste);
58
+ }
59
+ };
60
+ /**
61
+ * Individual object event
62
+ *
63
+ */
64
+ this.object = {
65
+ /**
66
+ * Mouse down event on object
67
+ * @param {FabricEvent} opt
68
+ */
69
+ mousedown: (opt) => {
70
+ const { target } = opt;
71
+ if (target && target.link && target.link.enabled) {
72
+ const { onClick } = this.handler;
73
+ if (onClick) {
74
+ onClick(this.handler.canvas, target);
75
+ }
76
+ }
77
+ },
78
+ /**
79
+ * Mouse double click event on object
80
+ * @param {FabricEvent} opt
81
+ */
82
+ mousedblclick: (opt) => {
83
+ const { target } = opt;
84
+ if (target) {
85
+ const { onDblClick } = this.handler;
86
+ if (onDblClick) {
87
+ onDblClick(this.handler.canvas, target);
88
+ }
89
+ }
90
+ },
91
+ };
92
+ /**
93
+ * Modified event object
94
+ *
95
+ * @param {FabricEvent} opt
96
+ * @returns
97
+ */
98
+ this.modified = (opt) => {
99
+ const { target } = opt;
100
+ if (!target) {
101
+ return;
102
+ }
103
+ if (target.type === 'circle' && target.parentId) {
104
+ return;
105
+ }
106
+ const { onModified } = this.handler;
107
+ if (onModified) {
108
+ onModified(target);
109
+ }
110
+ };
111
+ /**
112
+ * Moving event object
113
+ *
114
+ * @param {FabricEvent} opt
115
+ * @returns
116
+ */
117
+ this.moving = (opt) => {
118
+ const { target } = opt;
119
+ if (this.handler.interactionMode === 'crop') {
120
+ this.handler.cropHandler.moving(opt);
121
+ }
122
+ else {
123
+ if (this.handler.editable && this.handler.guidelineOption.enabled) {
124
+ this.handler.guidelineHandler.movingGuidelines(target);
125
+ }
126
+ if (target.type === 'activeSelection') {
127
+ const activeSelection = target;
128
+ activeSelection.getObjects().forEach((obj) => {
129
+ const left = target.left + obj.left + target.width / 2;
130
+ const top = target.top + obj.top + target.height / 2;
131
+ if (obj.superType === 'node') {
132
+ this.handler.portHandler.setCoords({ ...obj, left, top });
133
+ }
134
+ else if (obj.superType === 'element') {
135
+ const { id } = obj;
136
+ const el = this.handler.elementHandler.findById(id);
137
+ // TODO... Element object incorrect position
138
+ this.handler.elementHandler.setPositionByOrigin(el, obj, left, top);
139
+ }
140
+ });
141
+ return;
142
+ }
143
+ if (target.superType === 'node') {
144
+ this.handler.portHandler.setCoords(target);
145
+ }
146
+ else if (target.superType === 'element') {
147
+ const { id } = target;
148
+ const el = this.handler.elementHandler.findById(id);
149
+ this.handler.elementHandler.setPosition(el, target);
150
+ }
151
+ }
152
+ };
153
+ /**
154
+ * Moved event object
155
+ *
156
+ * @param {FabricEvent} opt
157
+ */
158
+ this.moved = (opt) => {
159
+ const { target } = opt;
160
+ this.handler.gridHandler.setCoords(target);
161
+ if (!this.handler.transactionHandler.active) {
162
+ this.handler.transactionHandler.save('moved');
163
+ }
164
+ if (target.superType === 'element') {
165
+ const { id } = target;
166
+ const el = this.handler.elementHandler.findById(id);
167
+ this.handler.elementHandler.setPosition(el, target);
168
+ }
169
+ };
170
+ /**
171
+ * Scaling event object
172
+ *
173
+ * @param {FabricEvent} opt
174
+ */
175
+ this.scaling = (opt) => {
176
+ const { target } = opt;
177
+ if (this.handler.interactionMode === 'crop') {
178
+ this.handler.cropHandler.resize(opt);
179
+ }
180
+ // TODO...this.handler.guidelineHandler.scalingGuidelines(target);
181
+ if (target.superType === 'element') {
182
+ const { id, width, height } = target;
183
+ const el = this.handler.elementHandler.findById(id);
184
+ // update the element
185
+ this.handler.elementHandler.setScaleOrAngle(el, target);
186
+ this.handler.elementHandler.setSize(el, target);
187
+ this.handler.elementHandler.setPosition(el, target);
188
+ const video = target;
189
+ if (video.type === 'video' && video.player) {
190
+ video.player.setPlayerSize(width, height);
191
+ }
192
+ }
193
+ };
194
+ /**
195
+ * Scaled event object
196
+ *
197
+ * @param {FabricEvent} opt
198
+ */
199
+ this.scaled = (_opt) => {
200
+ if (!this.handler.transactionHandler.active) {
201
+ this.handler.transactionHandler.save('scaled');
202
+ }
203
+ };
204
+ /**
205
+ * Rotating event object
206
+ *
207
+ * @param {FabricEvent} opt
208
+ */
209
+ this.rotating = (opt) => {
210
+ const { target } = opt;
211
+ if (target.superType === 'element') {
212
+ const { id } = target;
213
+ const el = this.handler.elementHandler.findById(id);
214
+ // update the element
215
+ this.handler.elementHandler.setScaleOrAngle(el, target);
216
+ }
217
+ };
218
+ /**
219
+ * Rotated event object
220
+ *
221
+ * @param {FabricEvent} opt
222
+ */
223
+ this.rotated = (_opt) => {
224
+ if (!this.handler.transactionHandler.active) {
225
+ this.handler.transactionHandler.save('rotated');
226
+ }
227
+ };
228
+ /**
229
+ * Moing object at keyboard arrow key down event
230
+ *
231
+ * @param {KeyboardEvent} e
232
+ * @returns
233
+ */
234
+ this.arrowmoving = (e) => {
235
+ const activeObject = this.handler.canvas.getActiveObject();
236
+ if (!activeObject) {
237
+ return false;
238
+ }
239
+ if (activeObject.id === 'workarea') {
240
+ return false;
241
+ }
242
+ if (e.code === constants_1.code.ARROW_UP) {
243
+ activeObject.set('top', activeObject.top - 2);
244
+ activeObject.setCoords();
245
+ this.handler.canvas.renderAll();
246
+ return true;
247
+ }
248
+ else if (e.code === constants_1.code.ARROW_DOWN) {
249
+ activeObject.set('top', activeObject.top + 2);
250
+ activeObject.setCoords();
251
+ this.handler.canvas.renderAll();
252
+ return true;
253
+ }
254
+ else if (e.code === constants_1.code.ARROW_LEFT) {
255
+ activeObject.set('left', activeObject.left - 2);
256
+ activeObject.setCoords();
257
+ this.handler.canvas.renderAll();
258
+ return true;
259
+ }
260
+ else if (e.code === constants_1.code.ARROW_RIGHT) {
261
+ activeObject.set('left', activeObject.left + 2);
262
+ activeObject.setCoords();
263
+ this.handler.canvas.renderAll();
264
+ return true;
265
+ }
266
+ if (this.handler.onModified) {
267
+ this.handler.onModified(activeObject);
268
+ }
269
+ return true;
270
+ };
271
+ /**
272
+ * Zoom at mouse wheel event
273
+ *
274
+ * @param {FabricEvent<WheelEvent>} opt
275
+ * @returns
276
+ */
277
+ this.mousewheel = (opt) => {
278
+ const event = opt;
279
+ const { zoomEnabled } = this.handler;
280
+ if (!zoomEnabled) {
281
+ return;
282
+ }
283
+ const delta = event.e.deltaY;
284
+ let zoomRatio = this.handler.canvas.getZoom();
285
+ if (delta > 0) {
286
+ zoomRatio -= 0.05;
287
+ }
288
+ else {
289
+ zoomRatio += 0.05;
290
+ }
291
+ this.handler.zoomHandler.zoomToPoint(new fabric_1.fabric.Point(this.handler.canvas.getWidth() / 2, this.handler.canvas.getHeight() / 2), zoomRatio);
292
+ event.e.preventDefault();
293
+ event.e.stopPropagation();
294
+ };
295
+ /**
296
+ * Mouse down event on object
297
+ *
298
+ * @param {FabricEvent<MouseEvent>} opt
299
+ * @returns
300
+ */
301
+ this.mousedown = (opt) => {
302
+ const event = opt;
303
+ const { editable } = this.handler;
304
+ if (event.e.altKey && editable && !this.handler.interactionHandler.isDrawingMode()) {
305
+ this.handler.interactionHandler.grab();
306
+ this.panning = true;
307
+ return;
308
+ }
309
+ if (this.handler.interactionMode === 'grab') {
310
+ this.panning = true;
311
+ return;
312
+ }
313
+ const { target } = event;
314
+ if (editable) {
315
+ if (this.handler.prevTarget && this.handler.prevTarget.superType === 'link') {
316
+ this.handler.prevTarget.set({
317
+ stroke: this.handler.prevTarget.originStroke,
318
+ });
319
+ }
320
+ if (target && target.type === 'fromPort') {
321
+ this.handler.linkHandler.init(target);
322
+ return;
323
+ }
324
+ if (target &&
325
+ this.handler.interactionMode === 'link' &&
326
+ (target.type === 'toPort' || target.superType === 'node')) {
327
+ let toPort;
328
+ if (target.superType === 'node') {
329
+ toPort = target.toPort;
330
+ }
331
+ else {
332
+ toPort = target;
333
+ }
334
+ this.handler.linkHandler.generate(toPort);
335
+ return;
336
+ }
337
+ this.handler.guidelineHandler.viewportTransform = this.handler.canvas.viewportTransform;
338
+ this.handler.guidelineHandler.zoom = this.handler.canvas.getZoom();
339
+ if (this.handler.interactionMode === 'selection') {
340
+ if (target && target.superType === 'link') {
341
+ target.set({
342
+ stroke: target.selectFill || 'green',
343
+ });
344
+ }
345
+ this.handler.prevTarget = target;
346
+ return;
347
+ }
348
+ if (this.handler.interactionMode === 'polygon') {
349
+ if (target && this.handler.pointArray.length && target.id === this.handler.pointArray[0].id) {
350
+ this.handler.drawingHandler.polygon.generate(this.handler.pointArray);
351
+ }
352
+ else {
353
+ this.handler.drawingHandler.polygon.addPoint(event);
354
+ }
355
+ }
356
+ else if (this.handler.interactionMode === 'line') {
357
+ if (this.handler.pointArray.length && this.handler.activeLine) {
358
+ this.handler.drawingHandler.line.generate(event);
359
+ }
360
+ else {
361
+ this.handler.drawingHandler.line.addPoint(event);
362
+ }
363
+ }
364
+ else if (this.handler.interactionMode === 'arrow') {
365
+ if (this.handler.pointArray.length && this.handler.activeLine) {
366
+ this.handler.drawingHandler.arrow.generate(event);
367
+ }
368
+ else {
369
+ this.handler.drawingHandler.arrow.addPoint(event);
370
+ }
371
+ }
372
+ }
373
+ };
374
+ /**
375
+ * Mouse move event on canvas
376
+ *
377
+ * @param {FabricEvent<MouseEvent>} opt
378
+ * @returns
379
+ */
380
+ this.mousemove = (opt) => {
381
+ const event = opt;
382
+ if (this.handler.interactionMode === 'grab' && this.panning) {
383
+ this.handler.interactionHandler.moving(event.e);
384
+ this.handler.canvas.requestRenderAll();
385
+ }
386
+ if (!this.handler.editable && event.target) {
387
+ if (event.target.superType === 'element') {
388
+ return;
389
+ }
390
+ if (event.target.id !== 'workarea') {
391
+ if (event.target !== this.handler.target) {
392
+ this.handler.tooltipHandler.show(event.target);
393
+ }
394
+ }
395
+ else {
396
+ this.handler.tooltipHandler.hide(event.target);
397
+ }
398
+ }
399
+ if (this.handler.interactionMode === 'polygon') {
400
+ if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
401
+ const pointer = this.handler.canvas.getPointer(event.e);
402
+ this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
403
+ const points = this.handler.activeShape.get('points');
404
+ points[this.handler.pointArray.length] = {
405
+ x: pointer.x,
406
+ y: pointer.y,
407
+ };
408
+ this.handler.activeShape.set({
409
+ points,
410
+ });
411
+ this.handler.canvas.requestRenderAll();
412
+ }
413
+ }
414
+ else if (this.handler.interactionMode === 'line') {
415
+ if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
416
+ const pointer = this.handler.canvas.getPointer(event.e);
417
+ this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
418
+ }
419
+ this.handler.canvas.requestRenderAll();
420
+ }
421
+ else if (this.handler.interactionMode === 'arrow') {
422
+ if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
423
+ const pointer = this.handler.canvas.getPointer(event.e);
424
+ this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
425
+ }
426
+ this.handler.canvas.requestRenderAll();
427
+ }
428
+ else if (this.handler.interactionMode === 'link') {
429
+ if (this.handler.activeLine && this.handler.activeLine.class === 'line') {
430
+ const pointer = this.handler.canvas.getPointer(event.e);
431
+ this.handler.activeLine.set({ x2: pointer.x, y2: pointer.y });
432
+ }
433
+ this.handler.canvas.requestRenderAll();
434
+ }
435
+ return;
436
+ };
437
+ /**
438
+ * Mouse up event on canvas
439
+ *
440
+ * @param {FabricEvent<MouseEvent>} opt
441
+ * @returns
442
+ */
443
+ this.mouseup = (opt) => {
444
+ const event = opt;
445
+ if (this.handler.interactionMode === 'grab') {
446
+ this.panning = false;
447
+ return;
448
+ }
449
+ const { target, e } = event;
450
+ if (this.handler.interactionMode === 'selection') {
451
+ if (target && e.shiftKey && target.superType === 'node') {
452
+ const node = target;
453
+ this.handler.canvas.discardActiveObject();
454
+ const nodes = [];
455
+ this.handler.nodeHandler.getNodePath(node, nodes);
456
+ const activeSelection = new fabric_1.fabric.ActiveSelection(nodes, {
457
+ canvas: this.handler.canvas,
458
+ ...this.handler.activeSelectionOption,
459
+ });
460
+ this.handler.canvas.setActiveObject(activeSelection);
461
+ this.handler.canvas.requestRenderAll();
462
+ }
463
+ }
464
+ if (this.handler.editable && this.handler.guidelineOption.enabled) {
465
+ this.handler.guidelineHandler.verticalLines.length = 0;
466
+ this.handler.guidelineHandler.horizontalLines.length = 0;
467
+ }
468
+ this.handler.canvas.renderAll();
469
+ };
470
+ /**
471
+ * Mouse out event on canvas
472
+ *
473
+ * @param {FabricEvent<MouseEvent>} opt
474
+ */
475
+ this.mouseout = (opt) => {
476
+ const event = opt;
477
+ if (!event.target) {
478
+ this.handler.tooltipHandler.hide();
479
+ }
480
+ };
481
+ /**
482
+ * Selection event event on canvas
483
+ *
484
+ * @param {FabricEvent} opt
485
+ */
486
+ this.selection = (opt) => {
487
+ const { onSelect, activeSelectionOption } = this.handler;
488
+ const target = opt.target;
489
+ if (target && target.type === 'activeSelection') {
490
+ target.set({
491
+ ...activeSelectionOption,
492
+ });
493
+ }
494
+ if (onSelect) {
495
+ onSelect(target);
496
+ }
497
+ };
498
+ /**
499
+ * Called resize event on canvas
500
+ *
501
+ * @param {number} nextWidth
502
+ * @param {number} nextHeight
503
+ * @returns
504
+ */
505
+ this.resize = (nextWidth, nextHeight) => {
506
+ this.handler.canvas.setWidth(nextWidth).setHeight(nextHeight);
507
+ this.handler.canvas.setBackgroundColor(this.handler.canvasOption.backgroundColor, this.handler.canvas.renderAll.bind(this.handler.canvas));
508
+ if (!this.handler.workarea) {
509
+ return;
510
+ }
511
+ const diffWidth = nextWidth / 2 - this.handler.width / 2;
512
+ const diffHeight = nextHeight / 2 - this.handler.height / 2;
513
+ this.handler.width = nextWidth;
514
+ this.handler.height = nextHeight;
515
+ if (this.handler.workarea.layout === 'fixed') {
516
+ this.handler.canvas.centerObject(this.handler.workarea);
517
+ this.handler.workarea.setCoords();
518
+ if (this.handler.gridOption.enabled) {
519
+ return;
520
+ }
521
+ this.handler.canvas.getObjects().forEach((obj) => {
522
+ if (obj.id !== 'workarea') {
523
+ const left = obj.left + diffWidth;
524
+ const top = obj.top + diffHeight;
525
+ obj.set({
526
+ left,
527
+ top,
528
+ });
529
+ obj.setCoords();
530
+ if (obj.superType === 'element') {
531
+ const { id } = obj;
532
+ const el = this.handler.elementHandler.findById(id);
533
+ // update the element
534
+ this.handler.elementHandler.setPosition(el, obj);
535
+ }
536
+ }
537
+ });
538
+ this.handler.canvas.requestRenderAll();
539
+ return;
540
+ }
541
+ if (this.handler.workarea.layout === 'responsive') {
542
+ const { scaleX } = this.handler.workareaHandler.calculateScale();
543
+ const center = this.handler.canvas.getCenter();
544
+ const deltaPoint = new fabric_1.fabric.Point(diffWidth, diffHeight);
545
+ this.handler.canvas.relativePan(deltaPoint);
546
+ this.handler.zoomHandler.zoomToPoint(new fabric_1.fabric.Point(center.left, center.top), scaleX);
547
+ return;
548
+ }
549
+ const scaleX = nextWidth / this.handler.workarea.width;
550
+ const scaleY = nextHeight / this.handler.workarea.height;
551
+ const diffScaleX = nextWidth / (this.handler.workarea.width * this.handler.workarea.scaleX);
552
+ const diffScaleY = nextHeight / (this.handler.workarea.height * this.handler.workarea.scaleY);
553
+ this.handler.workarea.set({
554
+ scaleX,
555
+ scaleY,
556
+ });
557
+ this.handler.canvas.getObjects().forEach((obj) => {
558
+ const { id } = obj;
559
+ if (obj.id !== 'workarea') {
560
+ const left = obj.left * diffScaleX;
561
+ const top = obj.top * diffScaleY;
562
+ const newScaleX = obj.scaleX * diffScaleX;
563
+ const newScaleY = obj.scaleY * diffScaleY;
564
+ obj.set({
565
+ scaleX: newScaleX,
566
+ scaleY: newScaleY,
567
+ left,
568
+ top,
569
+ });
570
+ obj.setCoords();
571
+ if (obj.superType === 'element') {
572
+ const video = obj;
573
+ const { width, height } = obj;
574
+ const el = this.handler.elementHandler.findById(id);
575
+ this.handler.elementHandler.setSize(el, obj);
576
+ if (video.player) {
577
+ video.player.setPlayerSize(width, height);
578
+ }
579
+ this.handler.elementHandler.setPosition(el, obj);
580
+ }
581
+ }
582
+ });
583
+ this.handler.canvas.renderAll();
584
+ };
585
+ /**
586
+ * Paste event on canvas
587
+ *
588
+ * @param {ClipboardEvent} e
589
+ * @returns
590
+ */
591
+ this.paste = async (e) => {
592
+ if (this.handler.canvas.wrapperEl !== document.activeElement) {
593
+ return false;
594
+ }
595
+ if (e.preventDefault) {
596
+ e.preventDefault();
597
+ }
598
+ if (e.stopPropagation) {
599
+ e.stopPropagation();
600
+ }
601
+ const clipboardData = e.clipboardData;
602
+ if (clipboardData.types.length) {
603
+ clipboardData.types.forEach((clipboardType) => {
604
+ if (clipboardType === 'text/plain') {
605
+ const textPlain = clipboardData.getData('text/plain');
606
+ try {
607
+ const objects = JSON.parse(textPlain);
608
+ const { gridOption: { grid = 10 }, isCut, } = this.handler;
609
+ const padding = isCut ? 0 : grid;
610
+ if (objects && Array.isArray(objects)) {
611
+ const filteredObjects = objects.filter(obj => obj !== null);
612
+ if (filteredObjects.length === 1) {
613
+ const obj = filteredObjects[0];
614
+ if (typeof obj.cloneable !== 'undefined' && !obj.cloneable) {
615
+ return;
616
+ }
617
+ obj.left = obj.properties.left + padding;
618
+ obj.top = obj.properties.top + padding;
619
+ const createdObj = this.handler.add(obj, false, true);
620
+ this.handler.canvas.setActiveObject(createdObj);
621
+ this.handler.canvas.requestRenderAll();
622
+ }
623
+ else {
624
+ const nodes = [];
625
+ const targets = [];
626
+ objects.forEach(obj => {
627
+ if (!obj) {
628
+ return;
629
+ }
630
+ if (obj.superType === 'link') {
631
+ obj.fromNodeId = nodes[obj.fromNodeIndex].id;
632
+ obj.toNodeId = nodes[obj.toNodeIndex].id;
633
+ }
634
+ else {
635
+ obj.left = obj.properties.left + padding;
636
+ obj.top = obj.properties.top + padding;
637
+ }
638
+ const createdObj = this.handler.add(obj, false, true);
639
+ if (obj.superType === 'node') {
640
+ nodes.push(createdObj);
641
+ }
642
+ else {
643
+ targets.push(createdObj);
644
+ }
645
+ });
646
+ const activeSelection = new fabric_1.fabric.ActiveSelection(nodes.length ? nodes : targets, {
647
+ canvas: this.handler.canvas,
648
+ ...this.handler.activeSelectionOption,
649
+ });
650
+ this.handler.canvas.setActiveObject(activeSelection);
651
+ this.handler.canvas.requestRenderAll();
652
+ }
653
+ if (!this.handler.transactionHandler.active) {
654
+ this.handler.transactionHandler.save('paste');
655
+ }
656
+ this.handler.isCut = false;
657
+ this.handler.copy();
658
+ }
659
+ }
660
+ catch (error) {
661
+ console.error(error);
662
+ // const item = {
663
+ // id: uuv4id(),
664
+ // type: 'textbox',
665
+ // text: textPlain,
666
+ // };
667
+ // this.handler.add(item, true);
668
+ }
669
+ }
670
+ else if (clipboardType === 'text/html') {
671
+ // Todo ...
672
+ // const textHtml = clipboardData.getData('text/html');
673
+ // console.log(textHtml);
674
+ }
675
+ else if (clipboardType === 'Files') {
676
+ // Array.from(clipboardData.files).forEach((file) => {
677
+ // const { type } = file;
678
+ // if (type === 'image/png' || type === 'image/jpeg' || type === 'image/jpg') {
679
+ // const item = {
680
+ // id: uuid(),
681
+ // type: 'image',
682
+ // file,
683
+ // superType: 'image',
684
+ // };
685
+ // this.handler.add(item, true);
686
+ // } else {
687
+ // console.error('Not supported file type');
688
+ // }
689
+ // });
690
+ }
691
+ });
692
+ }
693
+ return true;
694
+ };
695
+ /**
696
+ * Keydown event on document
697
+ *
698
+ * @param {KeyboardEvent} e
699
+ */
700
+ this.keydown = (e) => {
701
+ const { keyEvent, editable } = this.handler;
702
+ if (!Object.keys(keyEvent).length) {
703
+ return;
704
+ }
705
+ const { clipboard, grab } = keyEvent;
706
+ if (this.handler.interactionHandler.isDrawingMode()) {
707
+ if (this.handler.shortcutHandler.isEscape(e)) {
708
+ if (this.handler.interactionMode === 'polygon') {
709
+ this.handler.drawingHandler.polygon.finish();
710
+ }
711
+ else if (this.handler.interactionMode === 'line') {
712
+ this.handler.drawingHandler.line.finish();
713
+ }
714
+ else if (this.handler.interactionMode === 'arrow') {
715
+ this.handler.drawingHandler.arrow.finish();
716
+ }
717
+ else if (this.handler.interactionMode === 'link') {
718
+ this.handler.linkHandler.finish();
719
+ }
720
+ }
721
+ return;
722
+ }
723
+ if (this.handler.shortcutHandler.isW(e) && grab) {
724
+ this.code = e.code;
725
+ this.handler.interactionHandler.grab();
726
+ return;
727
+ }
728
+ if (e.altKey && editable && grab) {
729
+ this.handler.interactionHandler.grab();
730
+ return;
731
+ }
732
+ if (this.handler.shortcutHandler.isEscape(e)) {
733
+ if (this.handler.interactionMode === 'selection') {
734
+ this.handler.canvas.discardActiveObject();
735
+ this.handler.canvas.renderAll();
736
+ }
737
+ this.handler.tooltipHandler.hide();
738
+ }
739
+ if (this.handler.canvas.wrapperEl !== document.activeElement) {
740
+ return;
741
+ }
742
+ if (editable) {
743
+ if (this.handler.shortcutHandler.isQ(e)) {
744
+ this.code = e.code;
745
+ }
746
+ else if (this.handler.shortcutHandler.isDelete(e)) {
747
+ this.handler.remove();
748
+ }
749
+ else if (this.handler.shortcutHandler.isArrow(e)) {
750
+ this.arrowmoving(e);
751
+ }
752
+ else if (this.handler.shortcutHandler.isCtrlA(e)) {
753
+ e.preventDefault();
754
+ this.handler.selectAll();
755
+ }
756
+ else if (this.handler.shortcutHandler.isCtrlC(e)) {
757
+ e.preventDefault();
758
+ this.handler.copy();
759
+ }
760
+ else if (this.handler.shortcutHandler.isCtrlV(e) && !clipboard) {
761
+ e.preventDefault();
762
+ this.handler.paste();
763
+ }
764
+ else if (this.handler.shortcutHandler.isCtrlX(e)) {
765
+ e.preventDefault();
766
+ this.handler.cut();
767
+ }
768
+ else if (this.handler.shortcutHandler.isCtrlZ(e)) {
769
+ e.preventDefault();
770
+ this.handler.transactionHandler.undo();
771
+ }
772
+ else if (this.handler.shortcutHandler.isCtrlY(e)) {
773
+ e.preventDefault();
774
+ this.handler.transactionHandler.redo();
775
+ }
776
+ else if (this.handler.shortcutHandler.isPlus(e)) {
777
+ e.preventDefault();
778
+ this.handler.zoomHandler.zoomIn();
779
+ }
780
+ else if (this.handler.shortcutHandler.isMinus(e)) {
781
+ e.preventDefault();
782
+ this.handler.zoomHandler.zoomOut();
783
+ }
784
+ else if (this.handler.shortcutHandler.isO(e)) {
785
+ e.preventDefault();
786
+ this.handler.zoomHandler.zoomOneToOne();
787
+ }
788
+ else if (this.handler.shortcutHandler.isP(e)) {
789
+ e.preventDefault();
790
+ this.handler.zoomHandler.zoomToFit();
791
+ }
792
+ return;
793
+ }
794
+ return;
795
+ };
796
+ /**
797
+ * Key up event on canvas
798
+ *
799
+ * @param {KeyboardEvent} _e
800
+ */
801
+ this.keyup = (e) => {
802
+ if (this.handler.interactionHandler.isDrawingMode()) {
803
+ return;
804
+ }
805
+ if (!this.handler.shortcutHandler.isW(e)) {
806
+ this.handler.interactionHandler.selection();
807
+ }
808
+ };
809
+ /**
810
+ * Context menu event on canvas
811
+ *
812
+ * @param {MouseEvent} e
813
+ */
814
+ this.contextmenu = (e) => {
815
+ e.preventDefault();
816
+ const { editable, onContext } = this.handler;
817
+ if (editable && onContext) {
818
+ const target = this.handler.canvas.findTarget(e, false);
819
+ if (target && target.type !== 'activeSelection') {
820
+ this.handler.select(target);
821
+ }
822
+ this.handler.contextmenuHandler.show(e, target);
823
+ }
824
+ };
825
+ /**
826
+ * Mouse down event on canvas
827
+ *
828
+ * @param {MouseEvent} _e
829
+ */
830
+ this.onmousedown = (_e) => {
831
+ this.handler.contextmenuHandler.hide();
832
+ };
833
+ this.handler = handler;
834
+ this.initialize();
835
+ }
836
+ /**
837
+ * Attch event on document
838
+ *
839
+ */
840
+ initialize() {
841
+ if (this.handler.editable) {
842
+ // @ts-ignore
843
+ this.handler.canvas.on({
844
+ 'object:modified': this.modified,
845
+ 'object:scaling': this.scaling,
846
+ 'object:scaled': this.scaled,
847
+ 'object:moving': this.moving,
848
+ 'object:moved': this.moved,
849
+ 'object:rotating': this.rotating,
850
+ 'object:rotated': this.rotated,
851
+ 'mouse:wheel': this.mousewheel,
852
+ 'mouse:down': this.mousedown,
853
+ 'mouse:move': this.mousemove,
854
+ 'mouse:up': this.mouseup,
855
+ 'selection:cleared': this.selection,
856
+ 'selection:created': this.selection,
857
+ 'selection:updated': this.selection,
858
+ });
859
+ }
860
+ else {
861
+ // @ts-ignore
862
+ this.handler.canvas.on({
863
+ 'mouse:down': this.mousedown,
864
+ 'mouse:move': this.mousemove,
865
+ 'mouse:out': this.mouseout,
866
+ 'mouse:up': this.mouseup,
867
+ 'mouse:wheel': this.mousewheel,
868
+ });
869
+ }
870
+ this.handler.canvas.wrapperEl.tabIndex = 1000;
871
+ this.handler.canvas.wrapperEl.addEventListener('keydown', this.keydown, false);
872
+ this.handler.canvas.wrapperEl.addEventListener('keyup', this.keyup, false);
873
+ this.handler.canvas.wrapperEl.addEventListener('mousedown', this.onmousedown, false);
874
+ this.handler.canvas.wrapperEl.addEventListener('contextmenu', this.contextmenu, false);
875
+ if (this.handler.keyEvent.clipboard) {
876
+ document.addEventListener('paste', this.paste, false);
877
+ }
878
+ }
879
+ }
880
+ exports.default = EventHandler;