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.
Files changed (100) hide show
  1. package/dist/react-design-editor.js +7739 -7114
  2. package/dist/react-design-editor.min.js +1 -1
  3. package/dist/react-design-editor.min.js.LICENSE.txt +2 -0
  4. package/lib/Canvas.d.ts +18 -18
  5. package/lib/Canvas.js +172 -172
  6. package/lib/CanvasObject.d.ts +10 -10
  7. package/lib/CanvasObject.js +96 -96
  8. package/lib/constants/code.d.ts +19 -19
  9. package/lib/constants/code.js +22 -22
  10. package/lib/constants/defaults.d.ts +38 -38
  11. package/lib/constants/defaults.js +69 -69
  12. package/lib/constants/index.d.ts +3 -3
  13. package/lib/constants/index.js +26 -26
  14. package/lib/handlers/AlignmentHandler.d.ts +18 -18
  15. package/lib/handlers/AlignmentHandler.js +58 -58
  16. package/lib/handlers/AnimationHandler.d.ts +50 -50
  17. package/lib/handlers/AnimationHandler.js +346 -346
  18. package/lib/handlers/ChartHandler.d.ts +8 -8
  19. package/lib/handlers/ChartHandler.js +8 -8
  20. package/lib/handlers/ContextmenuHandler.d.ts +28 -28
  21. package/lib/handlers/ContextmenuHandler.js +65 -65
  22. package/lib/handlers/CropHandler.d.ts +43 -43
  23. package/lib/handlers/CropHandler.js +261 -261
  24. package/lib/handlers/CustomHandler.d.ts +7 -7
  25. package/lib/handlers/CustomHandler.js +10 -10
  26. package/lib/handlers/DrawingHandler.d.ts +28 -28
  27. package/lib/handlers/DrawingHandler.js +318 -318
  28. package/lib/handlers/ElementHandler.d.ts +80 -80
  29. package/lib/handlers/ElementHandler.js +154 -154
  30. package/lib/handlers/EventHandler.d.ts +170 -170
  31. package/lib/handlers/EventHandler.js +880 -880
  32. package/lib/handlers/FiberHandler.d.ts +6 -6
  33. package/lib/handlers/FiberHandler.js +23 -23
  34. package/lib/handlers/GridHandler.d.ts +19 -19
  35. package/lib/handlers/GridHandler.js +77 -77
  36. package/lib/handlers/GuidelineHandler.d.ts +61 -61
  37. package/lib/handlers/GuidelineHandler.js +315 -315
  38. package/lib/handlers/Handler.d.ts +618 -618
  39. package/lib/handlers/Handler.js +1645 -1645
  40. package/lib/handlers/ImageHandler.d.ts +307 -307
  41. package/lib/handlers/ImageHandler.js +528 -528
  42. package/lib/handlers/InteractionHandler.d.ts +45 -45
  43. package/lib/handlers/InteractionHandler.js +168 -164
  44. package/lib/handlers/LinkHandler.d.ts +115 -115
  45. package/lib/handlers/LinkHandler.js +247 -247
  46. package/lib/handlers/NodeHandler.d.ts +50 -50
  47. package/lib/handlers/NodeHandler.js +274 -274
  48. package/lib/handlers/PortHandler.d.ts +22 -22
  49. package/lib/handlers/PortHandler.js +179 -179
  50. package/lib/handlers/ShortcutHandler.d.ts +119 -119
  51. package/lib/handlers/ShortcutHandler.js +151 -151
  52. package/lib/handlers/TooltipHandler.d.ts +33 -33
  53. package/lib/handlers/TooltipHandler.js +91 -91
  54. package/lib/handlers/TransactionHandler.d.ts +59 -59
  55. package/lib/handlers/TransactionHandler.js +137 -137
  56. package/lib/handlers/WorkareaHandler.d.ts +43 -43
  57. package/lib/handlers/WorkareaHandler.js +354 -354
  58. package/lib/handlers/ZoomHandler.d.ts +48 -48
  59. package/lib/handlers/ZoomHandler.js +143 -143
  60. package/lib/handlers/index.d.ts +23 -23
  61. package/lib/handlers/index.js +48 -48
  62. package/lib/index.d.ts +6 -6
  63. package/lib/index.js +20 -20
  64. package/lib/objects/Arrow.d.ts +2 -2
  65. package/lib/objects/Arrow.js +40 -40
  66. package/lib/objects/Chart.d.ts +10 -10
  67. package/lib/objects/Chart.js +117 -117
  68. package/lib/objects/CirclePort.d.ts +2 -2
  69. package/lib/objects/CirclePort.js +28 -28
  70. package/lib/objects/Cube.d.ts +5 -5
  71. package/lib/objects/Cube.js +71 -71
  72. package/lib/objects/CurvedLink.d.ts +2 -2
  73. package/lib/objects/CurvedLink.js +51 -51
  74. package/lib/objects/Element.d.ts +13 -13
  75. package/lib/objects/Element.js +77 -77
  76. package/lib/objects/Gif.d.ts +3 -3
  77. package/lib/objects/Gif.js +41 -41
  78. package/lib/objects/Iframe.d.ts +9 -9
  79. package/lib/objects/Iframe.js +63 -63
  80. package/lib/objects/Line.d.ts +2 -2
  81. package/lib/objects/Line.js +24 -24
  82. package/lib/objects/Link.d.ts +15 -15
  83. package/lib/objects/Link.js +107 -107
  84. package/lib/objects/Node.d.ts +59 -59
  85. package/lib/objects/Node.js +271 -271
  86. package/lib/objects/OrthogonalLink.d.ts +2 -2
  87. package/lib/objects/OrthogonalLink.js +54 -54
  88. package/lib/objects/Port.d.ts +12 -12
  89. package/lib/objects/Port.js +28 -28
  90. package/lib/objects/Svg.d.ts +12 -12
  91. package/lib/objects/Svg.js +93 -93
  92. package/lib/objects/Video.d.ts +14 -14
  93. package/lib/objects/Video.js +113 -113
  94. package/lib/objects/index.d.ts +15 -15
  95. package/lib/objects/index.js +32 -32
  96. package/lib/utils/ObjectUtil.d.ts +408 -408
  97. package/lib/utils/ObjectUtil.js +13 -13
  98. package/lib/utils/index.d.ts +1 -1
  99. package/lib/utils/index.js +13 -13
  100. package/package.json +1 -1
@@ -1,880 +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 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 || this.handler.prevTarget.stroke,
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.selectedStroke || '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;
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 || this.handler.prevTarget.stroke,
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.selectedStroke || '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;