react-design-editor 0.0.33 → 0.0.37

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 (95) hide show
  1. package/README.md +37 -33
  2. package/dist/react-design-editor.js +20849 -11309
  3. package/dist/react-design-editor.min.js +1 -1
  4. package/dist/react-design-editor.min.js.LICENSE.txt +3 -10
  5. package/package.json +35 -31
  6. package/lib/Canvas.d.ts +0 -32
  7. package/lib/Canvas.js +0 -162
  8. package/lib/CanvasObject.d.ts +0 -10
  9. package/lib/CanvasObject.js +0 -96
  10. package/lib/constants/defaults.d.ts +0 -37
  11. package/lib/constants/defaults.js +0 -68
  12. package/lib/constants/index.d.ts +0 -2
  13. package/lib/constants/index.js +0 -24
  14. package/lib/handlers/AlignmentHandler.d.ts +0 -18
  15. package/lib/handlers/AlignmentHandler.js +0 -58
  16. package/lib/handlers/AnimationHandler.d.ts +0 -50
  17. package/lib/handlers/AnimationHandler.js +0 -320
  18. package/lib/handlers/ChartHandler.d.ts +0 -8
  19. package/lib/handlers/ChartHandler.js +0 -8
  20. package/lib/handlers/ContextmenuHandler.d.ts +0 -28
  21. package/lib/handlers/ContextmenuHandler.js +0 -65
  22. package/lib/handlers/CropHandler.d.ts +0 -43
  23. package/lib/handlers/CropHandler.js +0 -261
  24. package/lib/handlers/DrawingHandler.d.ts +0 -28
  25. package/lib/handlers/DrawingHandler.js +0 -318
  26. package/lib/handlers/ElementHandler.d.ts +0 -80
  27. package/lib/handlers/ElementHandler.js +0 -154
  28. package/lib/handlers/EventHandler.d.ts +0 -170
  29. package/lib/handlers/EventHandler.js +0 -877
  30. package/lib/handlers/GridHandler.d.ts +0 -19
  31. package/lib/handlers/GridHandler.js +0 -77
  32. package/lib/handlers/GuidelineHandler.d.ts +0 -61
  33. package/lib/handlers/GuidelineHandler.js +0 -314
  34. package/lib/handlers/Handler.d.ts +0 -603
  35. package/lib/handlers/Handler.js +0 -1612
  36. package/lib/handlers/ImageHandler.d.ts +0 -307
  37. package/lib/handlers/ImageHandler.js +0 -529
  38. package/lib/handlers/InteractionHandler.d.ts +0 -45
  39. package/lib/handlers/InteractionHandler.js +0 -159
  40. package/lib/handlers/LinkHandler.d.ts +0 -115
  41. package/lib/handlers/LinkHandler.js +0 -247
  42. package/lib/handlers/NodeHandler.d.ts +0 -50
  43. package/lib/handlers/NodeHandler.js +0 -274
  44. package/lib/handlers/PortHandler.d.ts +0 -22
  45. package/lib/handlers/PortHandler.js +0 -179
  46. package/lib/handlers/ShortcutHandler.d.ts +0 -119
  47. package/lib/handlers/ShortcutHandler.js +0 -150
  48. package/lib/handlers/TooltipHandler.d.ts +0 -33
  49. package/lib/handlers/TooltipHandler.js +0 -91
  50. package/lib/handlers/TransactionHandler.d.ts +0 -59
  51. package/lib/handlers/TransactionHandler.js +0 -139
  52. package/lib/handlers/WorkareaHandler.d.ts +0 -43
  53. package/lib/handlers/WorkareaHandler.js +0 -354
  54. package/lib/handlers/ZoomHandler.d.ts +0 -48
  55. package/lib/handlers/ZoomHandler.js +0 -143
  56. package/lib/handlers/index.d.ts +0 -21
  57. package/lib/handlers/index.js +0 -44
  58. package/lib/index.d.ts +0 -5
  59. package/lib/index.js +0 -19
  60. package/lib/objects/Arrow.d.ts +0 -2
  61. package/lib/objects/Arrow.js +0 -39
  62. package/lib/objects/Chart.d.ts +0 -10
  63. package/lib/objects/Chart.js +0 -123
  64. package/lib/objects/CirclePort.d.ts +0 -2
  65. package/lib/objects/CirclePort.js +0 -28
  66. package/lib/objects/Cube.d.ts +0 -5
  67. package/lib/objects/Cube.js +0 -71
  68. package/lib/objects/CurvedLink.d.ts +0 -2
  69. package/lib/objects/CurvedLink.js +0 -49
  70. package/lib/objects/Element.d.ts +0 -13
  71. package/lib/objects/Element.js +0 -83
  72. package/lib/objects/Gif.d.ts +0 -3
  73. package/lib/objects/Gif.js +0 -38
  74. package/lib/objects/Iframe.d.ts +0 -9
  75. package/lib/objects/Iframe.js +0 -69
  76. package/lib/objects/Line.d.ts +0 -2
  77. package/lib/objects/Line.js +0 -24
  78. package/lib/objects/Link.d.ts +0 -15
  79. package/lib/objects/Link.js +0 -104
  80. package/lib/objects/Node.d.ts +0 -59
  81. package/lib/objects/Node.js +0 -271
  82. package/lib/objects/OrthogonalLink.d.ts +0 -2
  83. package/lib/objects/OrthogonalLink.js +0 -52
  84. package/lib/objects/Port.d.ts +0 -12
  85. package/lib/objects/Port.js +0 -28
  86. package/lib/objects/Svg.d.ts +0 -8
  87. package/lib/objects/Svg.js +0 -59
  88. package/lib/objects/Video.d.ts +0 -14
  89. package/lib/objects/Video.js +0 -119
  90. package/lib/objects/index.d.ts +0 -15
  91. package/lib/objects/index.js +0 -32
  92. package/lib/utils/ObjectUtil.d.ts +0 -400
  93. package/lib/utils/ObjectUtil.js +0 -13
  94. package/lib/utils/index.d.ts +0 -1
  95. package/lib/utils/index.js +0 -13
@@ -1,1612 +0,0 @@
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 warning_1 = __importDefault(require("warning"));
8
- const uuid_1 = require("uuid");
9
- const lodash_1 = require("lodash");
10
- const _1 = require(".");
11
- const CanvasObject_1 = __importDefault(require("../CanvasObject"));
12
- const constants_1 = require("../constants");
13
- /**
14
- * Main handler for Canvas
15
- * @class Handler
16
- * @implements {HandlerOptions}
17
- */
18
- class Handler {
19
- constructor(options) {
20
- this.propertiesToInclude = constants_1.defaults.propertiesToInclude;
21
- this.workareaOption = constants_1.defaults.workareaOption;
22
- this.canvasOption = constants_1.defaults.canvasOption;
23
- this.gridOption = constants_1.defaults.gridOption;
24
- this.objectOption = constants_1.defaults.objectOption;
25
- this.guidelineOption = constants_1.defaults.guidelineOption;
26
- this.keyEvent = constants_1.defaults.keyEvent;
27
- this.activeSelectionOption = constants_1.defaults.activeSelectionOption;
28
- this.fabricObjects = CanvasObject_1.default;
29
- this.objectMap = {};
30
- this.zoom = 1;
31
- this.isCut = false;
32
- this.isRequsetAnimFrame = false;
33
- /**
34
- * Init class fields
35
- * @param {HandlerOptions} options
36
- */
37
- this.initOption = (options) => {
38
- this.id = options.id;
39
- this.canvas = options.canvas;
40
- this.container = options.container;
41
- this.editable = options.editable;
42
- this.interactionMode = options.interactionMode;
43
- this.minZoom = options.minZoom;
44
- this.maxZoom = options.maxZoom;
45
- this.zoomEnabled = options.zoomEnabled;
46
- this.width = options.width;
47
- this.height = options.height;
48
- this.objects = [];
49
- this.setPropertiesToInclude(options.propertiesToInclude);
50
- this.setWorkareaOption(options.workareaOption);
51
- this.setCanvasOption(options.canvasOption);
52
- this.setGridOption(options.gridOption);
53
- this.setObjectOption(options.objectOption);
54
- this.setFabricObjects(options.fabricObjects);
55
- this.setGuidelineOption(options.guidelineOption);
56
- this.setActiveSelectionOption(options.activeSelectionOption);
57
- this.setKeyEvent(options.keyEvent);
58
- };
59
- /**
60
- * Initialize callback
61
- * @param {HandlerOptions} options
62
- */
63
- this.initCallback = (options) => {
64
- this.onAdd = options.onAdd;
65
- this.onTooltip = options.onTooltip;
66
- this.onZoom = options.onZoom;
67
- this.onContext = options.onContext;
68
- this.onClick = options.onClick;
69
- this.onModified = options.onModified;
70
- this.onDblClick = options.onDblClick;
71
- this.onSelect = options.onSelect;
72
- this.onRemove = options.onRemove;
73
- this.onTransaction = options.onTransaction;
74
- this.onInteraction = options.onInteraction;
75
- this.onLoad = options.onLoad;
76
- };
77
- /**
78
- * Initialize handlers
79
- *
80
- */
81
- this.initHandler = () => {
82
- this.workareaHandler = new _1.WorkareaHandler(this);
83
- this.imageHandler = new _1.ImageHandler(this);
84
- this.chartHandler = new _1.ChartHandler(this);
85
- this.elementHandler = new _1.ElementHandler(this);
86
- this.cropHandler = new _1.CropHandler(this);
87
- this.animationHandler = new _1.AnimationHandler(this);
88
- this.contextmenuHandler = new _1.ContextmenuHandler(this);
89
- this.tooltipHandler = new _1.TooltipHandler(this);
90
- this.zoomHandler = new _1.ZoomHandler(this);
91
- this.interactionHandler = new _1.InteractionHandler(this);
92
- this.transactionHandler = new _1.TransactionHandler(this);
93
- this.gridHandler = new _1.GridHandler(this);
94
- this.portHandler = new _1.PortHandler(this);
95
- this.linkHandler = new _1.LinkHandler(this);
96
- this.nodeHandler = new _1.NodeHandler(this);
97
- this.alignmentHandler = new _1.AlignmentHandler(this);
98
- this.guidelineHandler = new _1.GuidelineHandler(this);
99
- this.eventHandler = new _1.EventHandler(this);
100
- this.drawingHandler = new _1.DrawingHandler(this);
101
- this.shortcutHandler = new _1.ShortcutHandler(this);
102
- };
103
- /**
104
- * Get primary object
105
- * @returns {FabricObject[]}
106
- */
107
- this.getObjects = () => {
108
- const objects = this.canvas.getObjects().filter((obj) => {
109
- if (obj.id === 'workarea') {
110
- return false;
111
- }
112
- else if (obj.id === 'grid') {
113
- return false;
114
- }
115
- else if (obj.superType === 'port') {
116
- return false;
117
- }
118
- else if (!obj.id) {
119
- return false;
120
- }
121
- return true;
122
- });
123
- if (objects.length) {
124
- objects.forEach(obj => (this.objectMap[obj.id] = obj));
125
- }
126
- else {
127
- this.objectMap = {};
128
- }
129
- return objects;
130
- };
131
- /**
132
- * Set key pair
133
- * @param {keyof FabricObject} key
134
- * @param {*} value
135
- * @returns
136
- */
137
- this.set = (key, value) => {
138
- const activeObject = this.canvas.getActiveObject();
139
- if (!activeObject) {
140
- return;
141
- }
142
- activeObject.set(key, value);
143
- activeObject.setCoords();
144
- this.canvas.requestRenderAll();
145
- const { id, superType, type, player, width, height } = activeObject;
146
- if (superType === 'element') {
147
- if (key === 'visible') {
148
- if (value) {
149
- activeObject.element.style.display = 'block';
150
- }
151
- else {
152
- activeObject.element.style.display = 'none';
153
- }
154
- }
155
- const el = this.elementHandler.findById(id);
156
- // update the element
157
- this.elementHandler.setScaleOrAngle(el, activeObject);
158
- this.elementHandler.setSize(el, activeObject);
159
- this.elementHandler.setPosition(el, activeObject);
160
- if (type === 'video' && player) {
161
- player.setPlayerSize(width, height);
162
- }
163
- }
164
- const { onModified } = this;
165
- if (onModified) {
166
- onModified(activeObject);
167
- }
168
- };
169
- /**
170
- * Set option
171
- * @param {Partial<FabricObject>} option
172
- * @returns
173
- */
174
- this.setObject = (option) => {
175
- const activeObject = this.canvas.getActiveObject();
176
- if (!activeObject) {
177
- return;
178
- }
179
- Object.keys(option).forEach(key => {
180
- if (option[key] !== activeObject[key]) {
181
- activeObject.set(key, option[key]);
182
- activeObject.setCoords();
183
- }
184
- });
185
- this.canvas.requestRenderAll();
186
- const { id, superType, type, player, width, height } = activeObject;
187
- if (superType === 'element') {
188
- if ('visible' in option) {
189
- if (option.visible) {
190
- activeObject.element.style.display = 'block';
191
- }
192
- else {
193
- activeObject.element.style.display = 'none';
194
- }
195
- }
196
- const el = this.elementHandler.findById(id);
197
- // update the element
198
- this.elementHandler.setScaleOrAngle(el, activeObject);
199
- this.elementHandler.setSize(el, activeObject);
200
- this.elementHandler.setPosition(el, activeObject);
201
- if (type === 'video' && player) {
202
- player.setPlayerSize(width, height);
203
- }
204
- }
205
- const { onModified } = this;
206
- if (onModified) {
207
- onModified(activeObject);
208
- }
209
- };
210
- /**
211
- * Set key pair by object
212
- * @param {FabricObject} obj
213
- * @param {string} key
214
- * @param {*} value
215
- * @returns
216
- */
217
- this.setByObject = (obj, key, value) => {
218
- if (!obj) {
219
- return;
220
- }
221
- obj.set(key, value);
222
- obj.setCoords();
223
- this.canvas.renderAll();
224
- const { id, superType, type, player, width, height } = obj;
225
- if (superType === 'element') {
226
- if (key === 'visible') {
227
- if (value) {
228
- obj.element.style.display = 'block';
229
- }
230
- else {
231
- obj.element.style.display = 'none';
232
- }
233
- }
234
- const el = this.elementHandler.findById(id);
235
- // update the element
236
- this.elementHandler.setScaleOrAngle(el, obj);
237
- this.elementHandler.setSize(el, obj);
238
- this.elementHandler.setPosition(el, obj);
239
- if (type === 'video' && player) {
240
- player.setPlayerSize(width, height);
241
- }
242
- }
243
- const { onModified } = this;
244
- if (onModified) {
245
- onModified(obj);
246
- }
247
- };
248
- /**
249
- * Set key pair by id
250
- * @param {string} id
251
- * @param {string} key
252
- * @param {*} value
253
- */
254
- this.setById = (id, key, value) => {
255
- const findObject = this.findById(id);
256
- this.setByObject(findObject, key, value);
257
- };
258
- /**
259
- * Set partial by object
260
- * @param {FabricObject} obj
261
- * @param {FabricObjectOption} option
262
- * @returns
263
- */
264
- this.setByPartial = (obj, option) => {
265
- if (!obj) {
266
- return;
267
- }
268
- obj.set(option);
269
- obj.setCoords();
270
- this.canvas.renderAll();
271
- const { id, superType, type, player, width, height } = obj;
272
- if (superType === 'element') {
273
- if ('visible' in option) {
274
- if (option.visible) {
275
- obj.element.style.display = 'block';
276
- }
277
- else {
278
- obj.element.style.display = 'none';
279
- }
280
- }
281
- const el = this.elementHandler.findById(id);
282
- // update the element
283
- this.elementHandler.setScaleOrAngle(el, obj);
284
- this.elementHandler.setSize(el, obj);
285
- this.elementHandler.setPosition(el, obj);
286
- if (type === 'video' && player) {
287
- player.setPlayerSize(width, height);
288
- }
289
- }
290
- };
291
- /**
292
- * Set shadow
293
- * @param {fabric.Shadow} option
294
- * @returns
295
- */
296
- this.setShadow = (option) => {
297
- const activeObject = this.canvas.getActiveObject();
298
- if (!activeObject) {
299
- return;
300
- }
301
- activeObject.setShadow(option);
302
- this.canvas.requestRenderAll();
303
- const { onModified } = this;
304
- if (onModified) {
305
- onModified(activeObject);
306
- }
307
- };
308
- /**
309
- * Set the image
310
- * @param {FabricImage} obj
311
- * @param {(File | string)} [source]
312
- * @returns
313
- */
314
- this.setImage = (obj, source) => {
315
- if (!source) {
316
- this.loadImage(obj, null);
317
- obj.set('file', null);
318
- obj.set('src', null);
319
- return;
320
- }
321
- if (source instanceof File) {
322
- const reader = new FileReader();
323
- reader.onload = () => {
324
- this.loadImage(obj, reader.result);
325
- obj.set('file', source);
326
- obj.set('src', null);
327
- };
328
- reader.readAsDataURL(source);
329
- }
330
- else {
331
- this.loadImage(obj, source);
332
- obj.set('file', null);
333
- obj.set('src', source);
334
- }
335
- };
336
- /**
337
- * Set image by id
338
- * @param {string} id
339
- * @param {*} source
340
- */
341
- this.setImageById = (id, source) => {
342
- const findObject = this.findById(id);
343
- this.setImage(findObject, source);
344
- };
345
- /**
346
- * Set visible
347
- * @param {boolean} [visible]
348
- * @returns
349
- */
350
- this.setVisible = (visible) => {
351
- const activeObject = this.canvas.getActiveObject();
352
- if (!activeObject) {
353
- return;
354
- }
355
- if (activeObject.superType === 'element') {
356
- if (visible) {
357
- activeObject.element.style.display = 'block';
358
- }
359
- else {
360
- activeObject.element.style.display = 'none';
361
- }
362
- }
363
- activeObject.set({
364
- visible,
365
- });
366
- this.canvas.renderAll();
367
- };
368
- /**
369
- * Set the position on Object
370
- *
371
- * @param {FabricObject} obj
372
- * @param {boolean} [centered]
373
- */
374
- this.centerObject = (obj, centered) => {
375
- if (centered) {
376
- this.canvas.centerObject(obj);
377
- obj.setCoords();
378
- }
379
- else {
380
- this.setByPartial(obj, {
381
- left: obj.left / this.canvas.getZoom() -
382
- obj.width / 2 -
383
- this.canvas.viewportTransform[4] / this.canvas.getZoom(),
384
- top: obj.top / this.canvas.getZoom() -
385
- obj.height / 2 -
386
- this.canvas.viewportTransform[5] / this.canvas.getZoom(),
387
- });
388
- }
389
- };
390
- /**
391
- * Add object
392
- * @param {FabricObjectOption} obj
393
- * @param {boolean} [centered=true]
394
- * @param {boolean} [loaded=false]
395
- * @returns
396
- */
397
- this.add = (obj, centered = true, loaded = false) => {
398
- const { editable, onAdd, gridOption, objectOption } = this;
399
- const option = {
400
- hasControls: editable,
401
- hasBorders: editable,
402
- selectable: editable,
403
- lockMovementX: !editable,
404
- lockMovementY: !editable,
405
- hoverCursor: !editable ? 'pointer' : 'move',
406
- };
407
- if (obj.type === 'i-text') {
408
- option.editable = false;
409
- }
410
- else {
411
- option.editable = editable;
412
- }
413
- if (editable && this.workarea.layout === 'fullscreen') {
414
- option.scaleX = this.workarea.scaleX;
415
- option.scaleY = this.workarea.scaleY;
416
- }
417
- const newOption = Object.assign({}, objectOption, obj, {
418
- container: this.container.id,
419
- editable,
420
- }, option);
421
- // Individually create canvas object
422
- if (obj.superType === 'link') {
423
- return this.linkHandler.create(newOption, loaded);
424
- }
425
- let createdObj;
426
- // Create canvas object
427
- if (obj.type === 'image') {
428
- createdObj = this.addImage(newOption);
429
- }
430
- else if (obj.type === 'group') {
431
- // TODO...
432
- // Group add function needs to be fixed
433
- const objects = this.addGroup(newOption, centered, loaded);
434
- const groupOption = Object.assign({}, newOption, { objects, name: 'New Group' });
435
- createdObj = this.fabricObjects[obj.type].create(groupOption);
436
- }
437
- else {
438
- createdObj = this.fabricObjects[obj.type].create(newOption);
439
- }
440
- this.canvas.add(createdObj);
441
- this.objects = this.getObjects();
442
- if (!editable && !(obj.superType === 'element')) {
443
- createdObj.on('mousedown', this.eventHandler.object.mousedown);
444
- }
445
- if (createdObj.dblclick) {
446
- createdObj.on('mousedblclick', this.eventHandler.object.mousedblclick);
447
- }
448
- if (this.objects.some(object => object.type === 'gif')) {
449
- this.startRequestAnimFrame();
450
- }
451
- else {
452
- this.stopRequestAnimFrame();
453
- }
454
- if (obj.superType !== 'drawing' && obj.superType !== 'link' && editable && !loaded) {
455
- this.centerObject(createdObj, centered);
456
- }
457
- if (createdObj.superType === 'node') {
458
- this.portHandler.create(createdObj);
459
- if (createdObj.iconButton) {
460
- this.canvas.add(createdObj.iconButton);
461
- }
462
- }
463
- if (!editable && createdObj.animation && createdObj.animation.autoplay) {
464
- this.animationHandler.play(createdObj.id);
465
- }
466
- if (createdObj.superType === 'node') {
467
- createdObj.setShadow({
468
- color: createdObj.stroke,
469
- });
470
- }
471
- if (gridOption.enabled) {
472
- this.gridHandler.setCoords(createdObj);
473
- }
474
- if (!this.transactionHandler.active && !loaded) {
475
- this.transactionHandler.save('add');
476
- }
477
- if (onAdd && editable && !loaded) {
478
- onAdd(createdObj);
479
- }
480
- return createdObj;
481
- };
482
- /**
483
- * Add group object
484
- *
485
- * @param {FabricGroup} obj
486
- * @param {boolean} [centered=true]
487
- * @param {boolean} [loaded=false]
488
- * @returns
489
- */
490
- this.addGroup = (obj, centered = true, loaded = false) => {
491
- return obj.objects.map(child => {
492
- return this.add(child, centered, loaded);
493
- });
494
- };
495
- /**
496
- * Add iamge object
497
- * @param {FabricImage} obj
498
- * @returns
499
- */
500
- this.addImage = (obj) => {
501
- const { objectOption } = this;
502
- const { filters = [], ...otherOption } = obj;
503
- const image = new Image();
504
- if (obj.src) {
505
- image.src = obj.src;
506
- }
507
- const createdObj = new fabric_1.fabric.Image(image, {
508
- ...objectOption,
509
- ...otherOption,
510
- });
511
- createdObj.set({
512
- filters: this.imageHandler.createFilters(filters),
513
- });
514
- this.setImage(createdObj, obj.src || obj.file);
515
- return createdObj;
516
- };
517
- /**
518
- * Remove object
519
- * @param {FabricObject} target
520
- * @returns {any}
521
- */
522
- this.remove = (target) => {
523
- const activeObject = target || this.canvas.getActiveObject();
524
- if (this.prevTarget && this.prevTarget.superType === 'link') {
525
- this.linkHandler.remove(this.prevTarget);
526
- if (!this.transactionHandler.active) {
527
- this.transactionHandler.save('remove');
528
- }
529
- return;
530
- }
531
- if (!activeObject) {
532
- return;
533
- }
534
- if (typeof activeObject.deletable !== 'undefined' && !activeObject.deletable) {
535
- return;
536
- }
537
- if (activeObject.type !== 'activeSelection') {
538
- this.canvas.discardActiveObject();
539
- if (activeObject.superType === 'element') {
540
- this.elementHandler.removeById(activeObject.id);
541
- }
542
- if (activeObject.superType === 'link') {
543
- this.linkHandler.remove(activeObject);
544
- }
545
- else if (activeObject.superType === 'node') {
546
- if (activeObject.toPort) {
547
- if (activeObject.toPort.links.length) {
548
- activeObject.toPort.links.forEach((link) => {
549
- this.linkHandler.remove(link, 'from');
550
- });
551
- }
552
- this.canvas.remove(activeObject.toPort);
553
- }
554
- if (activeObject.fromPort && activeObject.fromPort.length) {
555
- activeObject.fromPort.forEach((port) => {
556
- if (port.links.length) {
557
- port.links.forEach((link) => {
558
- this.linkHandler.remove(link, 'to');
559
- });
560
- }
561
- this.canvas.remove(port);
562
- });
563
- }
564
- }
565
- this.canvas.remove(activeObject);
566
- }
567
- else {
568
- const { _objects: activeObjects } = activeObject;
569
- const existDeleted = activeObjects.some((obj) => typeof obj.deletable !== 'undefined' && !obj.deletable);
570
- if (existDeleted) {
571
- return;
572
- }
573
- this.canvas.discardActiveObject();
574
- activeObjects.forEach((obj) => {
575
- if (obj.superType === 'element') {
576
- this.elementHandler.removeById(obj.id);
577
- }
578
- else if (obj.superType === 'node') {
579
- if (obj.toPort) {
580
- if (obj.toPort.links.length) {
581
- obj.toPort.links.forEach((link) => {
582
- this.linkHandler.remove(link, 'from');
583
- });
584
- }
585
- this.canvas.remove(obj.toPort);
586
- }
587
- if (obj.fromPort && obj.fromPort.length) {
588
- obj.fromPort.forEach((port) => {
589
- if (port.links.length) {
590
- port.links.forEach((link) => {
591
- this.linkHandler.remove(link, 'to');
592
- });
593
- }
594
- this.canvas.remove(port);
595
- });
596
- }
597
- }
598
- this.canvas.remove(obj);
599
- });
600
- }
601
- if (!this.transactionHandler.active) {
602
- this.transactionHandler.save('remove');
603
- }
604
- this.objects = this.getObjects();
605
- const { onRemove } = this;
606
- if (onRemove) {
607
- onRemove(activeObject);
608
- }
609
- };
610
- /**
611
- * Remove object by id
612
- * @param {string} id
613
- */
614
- this.removeById = (id) => {
615
- const findObject = this.findById(id);
616
- if (findObject) {
617
- this.remove(findObject);
618
- }
619
- };
620
- /**
621
- * Delete at origin object list
622
- * @param {string} id
623
- */
624
- this.removeOriginById = (id) => {
625
- const object = this.findOriginByIdWithIndex(id);
626
- if (object.index > 0) {
627
- this.objects.splice(object.index, 1);
628
- }
629
- };
630
- /**
631
- * Duplicate object
632
- * @returns
633
- */
634
- this.duplicate = () => {
635
- const { onAdd, propertiesToInclude, gridOption: { grid = 10 }, } = this;
636
- const activeObject = this.canvas.getActiveObject();
637
- if (!activeObject) {
638
- return;
639
- }
640
- if (typeof activeObject.cloneable !== 'undefined' && !activeObject.cloneable) {
641
- return;
642
- }
643
- activeObject.clone((clonedObj) => {
644
- this.canvas.discardActiveObject();
645
- clonedObj.set({
646
- left: clonedObj.left + grid,
647
- top: clonedObj.top + grid,
648
- evented: true,
649
- });
650
- if (clonedObj.type === 'activeSelection') {
651
- const activeSelection = clonedObj;
652
- activeSelection.canvas = this.canvas;
653
- activeSelection.forEachObject((obj) => {
654
- obj.set('id', uuid_1.v4());
655
- this.canvas.add(obj);
656
- this.objects = this.getObjects();
657
- if (obj.dblclick) {
658
- obj.on('mousedblclick', this.eventHandler.object.mousedblclick);
659
- }
660
- });
661
- if (onAdd) {
662
- onAdd(activeSelection);
663
- }
664
- activeSelection.setCoords();
665
- }
666
- else {
667
- if (activeObject.id === clonedObj.id) {
668
- clonedObj.set('id', uuid_1.v4());
669
- }
670
- this.canvas.add(clonedObj);
671
- this.objects = this.getObjects();
672
- if (clonedObj.dblclick) {
673
- clonedObj.on('mousedblclick', this.eventHandler.object.mousedblclick);
674
- }
675
- if (onAdd) {
676
- onAdd(clonedObj);
677
- }
678
- }
679
- this.canvas.setActiveObject(clonedObj);
680
- this.portHandler.create(clonedObj);
681
- this.canvas.requestRenderAll();
682
- }, propertiesToInclude);
683
- };
684
- /**
685
- * Duplicate object by id
686
- * @param {string} id
687
- * @returns
688
- */
689
- this.duplicateById = (id) => {
690
- const { onAdd, propertiesToInclude, gridOption: { grid = 10 }, } = this;
691
- const findObject = this.findById(id);
692
- if (findObject) {
693
- if (typeof findObject.cloneable !== 'undefined' && !findObject.cloneable) {
694
- return false;
695
- }
696
- findObject.clone((cloned) => {
697
- cloned.set({
698
- left: cloned.left + grid,
699
- top: cloned.top + grid,
700
- id: uuid_1.v4(),
701
- evented: true,
702
- });
703
- this.canvas.add(cloned);
704
- this.objects = this.getObjects();
705
- if (onAdd) {
706
- onAdd(cloned);
707
- }
708
- if (cloned.dblclick) {
709
- cloned.on('mousedblclick', this.eventHandler.object.mousedblclick);
710
- }
711
- this.canvas.setActiveObject(cloned);
712
- this.portHandler.create(cloned);
713
- this.canvas.requestRenderAll();
714
- }, propertiesToInclude);
715
- }
716
- return true;
717
- };
718
- /**
719
- * Cut object
720
- *
721
- */
722
- this.cut = () => {
723
- this.copy();
724
- this.remove();
725
- this.isCut = true;
726
- };
727
- /**
728
- * Copy to clipboard
729
- *
730
- * @param {*} value
731
- */
732
- this.copyToClipboard = (value) => {
733
- const textarea = document.createElement('textarea');
734
- document.body.appendChild(textarea);
735
- textarea.value = value;
736
- textarea.select();
737
- document.execCommand('copy');
738
- document.body.removeChild(textarea);
739
- this.canvas.wrapperEl.focus();
740
- };
741
- /**
742
- * Copy object
743
- *
744
- * @returns
745
- */
746
- this.copy = () => {
747
- const { propertiesToInclude } = this;
748
- const activeObject = this.canvas.getActiveObject();
749
- if (activeObject && activeObject.superType === 'link') {
750
- return false;
751
- }
752
- if (activeObject) {
753
- if (typeof activeObject.cloneable !== 'undefined' && !activeObject.cloneable) {
754
- return false;
755
- }
756
- if (activeObject.type === 'activeSelection') {
757
- const activeSelection = activeObject;
758
- if (activeSelection.getObjects().some((obj) => obj.superType === 'node')) {
759
- if (this.keyEvent.clipboard) {
760
- const links = [];
761
- const objects = activeSelection.getObjects().map((obj, index) => {
762
- if (typeof obj.cloneable !== 'undefined' && !obj.cloneable) {
763
- return null;
764
- }
765
- if (obj.fromPort.length) {
766
- obj.fromPort.forEach((port) => {
767
- if (port.links.length) {
768
- port.links.forEach((link) => {
769
- const linkTarget = {
770
- fromNodeIndex: index,
771
- fromPortId: port.id,
772
- type: 'curvedLink',
773
- superType: 'link',
774
- };
775
- const findIndex = activeSelection
776
- .getObjects()
777
- .findIndex((compObj) => compObj.id === link.toNode.id);
778
- if (findIndex >= 0) {
779
- linkTarget.toNodeIndex = findIndex;
780
- links.push(linkTarget);
781
- }
782
- });
783
- }
784
- });
785
- }
786
- return {
787
- name: obj.name,
788
- description: obj.description,
789
- superType: obj.superType,
790
- type: obj.type,
791
- nodeClazz: obj.nodeClazz,
792
- configuration: obj.configuration,
793
- properties: {
794
- left: activeObject.left + activeObject.width / 2 + obj.left || 0,
795
- top: activeObject.top + activeObject.height / 2 + obj.top || 0,
796
- iconName: obj.descriptor.icon,
797
- },
798
- };
799
- });
800
- this.copyToClipboard(JSON.stringify(objects.concat(links), null, '\t'));
801
- return true;
802
- }
803
- this.clipboard = activeObject;
804
- return true;
805
- }
806
- }
807
- activeObject.clone((cloned) => {
808
- if (this.keyEvent.clipboard) {
809
- if (cloned.superType === 'node') {
810
- const node = {
811
- name: cloned.name,
812
- description: cloned.description,
813
- superType: cloned.superType,
814
- type: cloned.type,
815
- nodeClazz: cloned.nodeClazz,
816
- configuration: cloned.configuration,
817
- properties: {
818
- left: cloned.left || 0,
819
- top: cloned.top || 0,
820
- iconName: cloned.descriptor.icon,
821
- },
822
- };
823
- const objects = [node];
824
- this.copyToClipboard(JSON.stringify(objects, null, '\t'));
825
- }
826
- else {
827
- this.copyToClipboard(JSON.stringify(cloned.toObject(propertiesToInclude), null, '\t'));
828
- }
829
- }
830
- else {
831
- this.clipboard = cloned;
832
- }
833
- }, propertiesToInclude);
834
- }
835
- return true;
836
- };
837
- /**
838
- * Paste object
839
- *
840
- * @returns
841
- */
842
- this.paste = () => {
843
- const { onAdd, propertiesToInclude, gridOption: { grid = 10 }, clipboard, isCut, } = this;
844
- const padding = isCut ? 0 : grid;
845
- if (!clipboard) {
846
- return false;
847
- }
848
- if (typeof clipboard.cloneable !== 'undefined' && !clipboard.cloneable) {
849
- return false;
850
- }
851
- this.isCut = false;
852
- if (clipboard.type === 'activeSelection') {
853
- if (clipboard.getObjects().some((obj) => obj.superType === 'node')) {
854
- this.canvas.discardActiveObject();
855
- const objects = [];
856
- const linkObjects = [];
857
- clipboard.getObjects().forEach((obj) => {
858
- if (typeof obj.cloneable !== 'undefined' && !obj.cloneable) {
859
- return;
860
- }
861
- const clonedObj = obj.duplicate();
862
- if (clonedObj.type === 'SwitchNode') {
863
- clonedObj.set({
864
- left: obj.left + padding + padding,
865
- top: obj.top + padding,
866
- });
867
- }
868
- else {
869
- clonedObj.set({
870
- left: obj.left + padding,
871
- top: obj.top + padding,
872
- });
873
- }
874
- if (obj.fromPort.length) {
875
- obj.fromPort.forEach((port) => {
876
- if (port.links.length) {
877
- port.links.forEach((link) => {
878
- const linkTarget = {
879
- fromNode: clonedObj.id,
880
- fromPort: port.id,
881
- };
882
- const findIndex = clipboard
883
- .getObjects()
884
- .findIndex((compObj) => compObj.id === link.toNode.id);
885
- if (findIndex >= 0) {
886
- linkTarget.toNodeIndex = findIndex;
887
- linkObjects.push(linkTarget);
888
- }
889
- });
890
- }
891
- });
892
- }
893
- if (clonedObj.dblclick) {
894
- clonedObj.on('mousedblclick', this.eventHandler.object.mousedblclick);
895
- }
896
- this.canvas.add(clonedObj);
897
- this.objects = this.getObjects();
898
- this.portHandler.create(clonedObj);
899
- objects.push(clonedObj);
900
- });
901
- if (linkObjects.length) {
902
- linkObjects.forEach((linkObject) => {
903
- const { fromNode, fromPort, toNodeIndex } = linkObject;
904
- const toNode = objects[toNodeIndex];
905
- const link = {
906
- type: 'curvedLink',
907
- fromNodeId: fromNode,
908
- fromPortId: fromPort,
909
- toNodeId: toNode.id,
910
- toPortId: toNode.toPort.id,
911
- };
912
- this.linkHandler.create(link);
913
- });
914
- }
915
- const activeSelection = new fabric_1.fabric.ActiveSelection(objects, {
916
- canvas: this.canvas,
917
- ...this.activeSelectionOption,
918
- });
919
- if (isCut) {
920
- this.clipboard = null;
921
- }
922
- else {
923
- this.clipboard = activeSelection;
924
- }
925
- if (!this.transactionHandler.active) {
926
- this.transactionHandler.save('paste');
927
- }
928
- this.canvas.setActiveObject(activeSelection);
929
- this.canvas.renderAll();
930
- return true;
931
- }
932
- }
933
- clipboard.clone((clonedObj) => {
934
- this.canvas.discardActiveObject();
935
- clonedObj.set({
936
- left: clonedObj.left + padding,
937
- top: clonedObj.top + padding,
938
- id: isCut ? clipboard.id : uuid_1.v4(),
939
- evented: true,
940
- });
941
- if (clonedObj.type === 'activeSelection') {
942
- clonedObj.canvas = this.canvas;
943
- clonedObj.forEachObject((obj) => {
944
- obj.set('id', isCut ? obj.id : uuid_1.v4());
945
- this.canvas.add(obj);
946
- if (obj.dblclick) {
947
- obj.on('mousedblclick', this.eventHandler.object.mousedblclick);
948
- }
949
- });
950
- }
951
- else {
952
- if (clonedObj.superType === 'node') {
953
- clonedObj = clonedObj.duplicate();
954
- }
955
- this.canvas.add(clonedObj);
956
- if (clonedObj.dblclick) {
957
- clonedObj.on('mousedblclick', this.eventHandler.object.mousedblclick);
958
- }
959
- }
960
- const newClipboard = clipboard.set({
961
- top: clonedObj.top,
962
- left: clonedObj.left,
963
- });
964
- if (isCut) {
965
- this.clipboard = null;
966
- }
967
- else {
968
- this.clipboard = newClipboard;
969
- }
970
- if (clonedObj.superType === 'node') {
971
- this.portHandler.create(clonedObj);
972
- }
973
- if (!this.transactionHandler.active) {
974
- this.transactionHandler.save('paste');
975
- }
976
- // TODO...
977
- // After toGroup svg elements not rendered.
978
- this.objects = this.getObjects();
979
- if (onAdd) {
980
- onAdd(clonedObj);
981
- }
982
- clonedObj.setCoords();
983
- this.canvas.setActiveObject(clonedObj);
984
- this.canvas.requestRenderAll();
985
- }, propertiesToInclude);
986
- return true;
987
- };
988
- /**
989
- * Load the image
990
- * @param {FabricImage} obj
991
- * @param {string} src
992
- */
993
- this.loadImage = (obj, src) => {
994
- let url = src;
995
- if (!url) {
996
- url = './images/sample/transparentBg.png';
997
- }
998
- fabric_1.fabric.util.loadImage(url, source => {
999
- if (obj.type !== 'image') {
1000
- obj.setPatternFill({
1001
- source,
1002
- repeat: 'repeat',
1003
- }, null);
1004
- obj.setCoords();
1005
- this.canvas.renderAll();
1006
- return;
1007
- }
1008
- obj.setElement(source);
1009
- obj.setCoords();
1010
- this.canvas.renderAll();
1011
- });
1012
- };
1013
- /**
1014
- * Find object by object
1015
- * @param {FabricObject} obj
1016
- */
1017
- this.find = (obj) => this.findById(obj.id);
1018
- /**
1019
- * Find object by id
1020
- * @param {string} id
1021
- * @returns {(FabricObject | null)}
1022
- */
1023
- this.findById = (id) => {
1024
- let findObject;
1025
- const exist = this.objects.some(obj => {
1026
- if (obj.id === id) {
1027
- findObject = obj;
1028
- return true;
1029
- }
1030
- return false;
1031
- });
1032
- if (!exist) {
1033
- warning_1.default(true, 'Not found object by id.');
1034
- return null;
1035
- }
1036
- return findObject;
1037
- };
1038
- /**
1039
- * Find object in origin list
1040
- * @param {string} id
1041
- * @returns
1042
- */
1043
- this.findOriginById = (id) => {
1044
- let findObject;
1045
- const exist = this.objects.some(obj => {
1046
- if (obj.id === id) {
1047
- findObject = obj;
1048
- return true;
1049
- }
1050
- return false;
1051
- });
1052
- if (!exist) {
1053
- console.warn('Not found object by id.');
1054
- return null;
1055
- }
1056
- return findObject;
1057
- };
1058
- /**
1059
- * Return origin object list
1060
- * @param {string} id
1061
- * @returns
1062
- */
1063
- this.findOriginByIdWithIndex = (id) => {
1064
- let findObject;
1065
- let index = -1;
1066
- const exist = this.objects.some((obj, i) => {
1067
- if (obj.id === id) {
1068
- findObject = obj;
1069
- index = i;
1070
- return true;
1071
- }
1072
- return false;
1073
- });
1074
- if (!exist) {
1075
- console.warn('Not found object by id.');
1076
- return {};
1077
- }
1078
- return {
1079
- object: findObject,
1080
- index,
1081
- };
1082
- };
1083
- /**
1084
- * Select object
1085
- * @param {FabricObject} obj
1086
- * @param {boolean} [find]
1087
- */
1088
- this.select = (obj, find) => {
1089
- let findObject = obj;
1090
- if (find) {
1091
- findObject = this.find(obj);
1092
- }
1093
- if (findObject) {
1094
- this.canvas.discardActiveObject();
1095
- this.canvas.setActiveObject(findObject);
1096
- this.canvas.requestRenderAll();
1097
- }
1098
- };
1099
- /**
1100
- * Select by id
1101
- * @param {string} id
1102
- */
1103
- this.selectById = (id) => {
1104
- const findObject = this.findById(id);
1105
- if (findObject) {
1106
- this.canvas.discardActiveObject();
1107
- this.canvas.setActiveObject(findObject);
1108
- this.canvas.requestRenderAll();
1109
- }
1110
- };
1111
- /**
1112
- * Select all
1113
- * @returns
1114
- */
1115
- this.selectAll = () => {
1116
- this.canvas.discardActiveObject();
1117
- const filteredObjects = this.canvas.getObjects().filter((obj) => {
1118
- if (obj.id === 'workarea') {
1119
- return false;
1120
- }
1121
- else if (!obj.evented) {
1122
- return false;
1123
- }
1124
- else if (obj.superType === 'link') {
1125
- return false;
1126
- }
1127
- else if (obj.superType === 'port') {
1128
- return false;
1129
- }
1130
- else if (obj.superType === 'element') {
1131
- return false;
1132
- }
1133
- else if (obj.locked) {
1134
- return false;
1135
- }
1136
- return true;
1137
- });
1138
- if (!filteredObjects.length) {
1139
- return;
1140
- }
1141
- if (filteredObjects.length === 1) {
1142
- this.canvas.setActiveObject(filteredObjects[0]);
1143
- this.canvas.renderAll();
1144
- return;
1145
- }
1146
- const activeSelection = new fabric_1.fabric.ActiveSelection(filteredObjects, {
1147
- canvas: this.canvas,
1148
- ...this.activeSelectionOption,
1149
- });
1150
- this.canvas.setActiveObject(activeSelection);
1151
- this.canvas.renderAll();
1152
- };
1153
- /**
1154
- * Save origin width, height
1155
- * @param {FabricObject} obj
1156
- * @param {number} width
1157
- * @param {number} height
1158
- */
1159
- this.originScaleToResize = (obj, width, height) => {
1160
- if (obj.id === 'workarea') {
1161
- this.setByPartial(obj, {
1162
- workareaWidth: obj.width,
1163
- workareaHeight: obj.height,
1164
- });
1165
- }
1166
- this.setByPartial(obj, {
1167
- scaleX: width / obj.width,
1168
- scaleY: height / obj.height,
1169
- });
1170
- };
1171
- /**
1172
- * When set the width, height, Adjust the size
1173
- * @param {number} width
1174
- * @param {number} height
1175
- */
1176
- this.scaleToResize = (width, height) => {
1177
- const activeObject = this.canvas.getActiveObject();
1178
- const { id } = activeObject;
1179
- const obj = {
1180
- id,
1181
- scaleX: width / activeObject.width,
1182
- scaleY: height / activeObject.height,
1183
- };
1184
- this.setObject(obj);
1185
- activeObject.setCoords();
1186
- this.canvas.requestRenderAll();
1187
- };
1188
- /**
1189
- * Import json
1190
- * @param {*} json
1191
- * @param {(canvas: FabricCanvas) => void} [callback]
1192
- */
1193
- this.importJSON = async (json, callback) => {
1194
- if (typeof json === 'string') {
1195
- json = JSON.parse(json);
1196
- }
1197
- let prevLeft = 0;
1198
- let prevTop = 0;
1199
- this.canvas.setBackgroundColor(this.canvasOption.backgroundColor, this.canvas.renderAll.bind(this.canvas));
1200
- const workareaExist = json.filter((obj) => obj.id === 'workarea');
1201
- if (!this.workarea) {
1202
- this.workareaHandler.initialize();
1203
- }
1204
- if (!workareaExist.length) {
1205
- this.canvas.centerObject(this.workarea);
1206
- this.workarea.setCoords();
1207
- prevLeft = this.workarea.left;
1208
- prevTop = this.workarea.top;
1209
- }
1210
- else {
1211
- const workarea = workareaExist[0];
1212
- prevLeft = workarea.left;
1213
- prevTop = workarea.top;
1214
- this.workarea.set(workarea);
1215
- await this.workareaHandler.setImage(workarea.src, true);
1216
- this.workarea.setCoords();
1217
- }
1218
- json.forEach((obj) => {
1219
- if (obj.id === 'workarea') {
1220
- return;
1221
- }
1222
- const canvasWidth = this.canvas.getWidth();
1223
- const canvasHeight = this.canvas.getHeight();
1224
- const { width, height, scaleX, scaleY, layout, left, top } = this.workarea;
1225
- if (layout === 'fullscreen') {
1226
- const leftRatio = canvasWidth / (width * scaleX);
1227
- const topRatio = canvasHeight / (height * scaleY);
1228
- obj.left *= leftRatio;
1229
- obj.top *= topRatio;
1230
- obj.scaleX *= leftRatio;
1231
- obj.scaleY *= topRatio;
1232
- }
1233
- else {
1234
- const diffLeft = left - prevLeft;
1235
- const diffTop = top - prevTop;
1236
- obj.left += diffLeft;
1237
- obj.top += diffTop;
1238
- }
1239
- if (obj.superType === 'element') {
1240
- obj.id = uuid_1.v4();
1241
- }
1242
- this.add(obj, false, true);
1243
- this.canvas.renderAll();
1244
- });
1245
- this.objects = this.getObjects();
1246
- if (callback) {
1247
- callback(this.canvas);
1248
- }
1249
- return Promise.resolve(this.canvas);
1250
- };
1251
- /**
1252
- * Export json
1253
- */
1254
- this.exportJSON = () => this.canvas.toObject(this.propertiesToInclude).objects;
1255
- /**
1256
- * Active selection to group
1257
- * @returns
1258
- */
1259
- this.toGroup = (target) => {
1260
- const activeObject = target || this.canvas.getActiveObject();
1261
- if (!activeObject) {
1262
- return null;
1263
- }
1264
- if (activeObject.type !== 'activeSelection') {
1265
- return null;
1266
- }
1267
- const group = activeObject.toGroup();
1268
- group.set({
1269
- id: uuid_1.v4(),
1270
- name: 'New group',
1271
- type: 'group',
1272
- ...this.objectOption,
1273
- });
1274
- this.objects = this.getObjects();
1275
- if (!this.transactionHandler.active) {
1276
- this.transactionHandler.save('group');
1277
- }
1278
- if (this.onSelect) {
1279
- this.onSelect(group);
1280
- }
1281
- this.canvas.renderAll();
1282
- return group;
1283
- };
1284
- /**
1285
- * Group to active selection
1286
- * @returns
1287
- */
1288
- this.toActiveSelection = (target) => {
1289
- const activeObject = target || this.canvas.getActiveObject();
1290
- if (!activeObject) {
1291
- return;
1292
- }
1293
- if (activeObject.type !== 'group') {
1294
- return;
1295
- }
1296
- const activeSelection = activeObject.toActiveSelection();
1297
- this.objects = this.getObjects();
1298
- if (!this.transactionHandler.active) {
1299
- this.transactionHandler.save('ungroup');
1300
- }
1301
- if (this.onSelect) {
1302
- this.onSelect(activeSelection);
1303
- }
1304
- this.canvas.renderAll();
1305
- return activeSelection;
1306
- };
1307
- /**
1308
- * Bring forward
1309
- */
1310
- this.bringForward = () => {
1311
- const activeObject = this.canvas.getActiveObject();
1312
- if (activeObject) {
1313
- this.canvas.bringForward(activeObject);
1314
- if (!this.transactionHandler.active) {
1315
- this.transactionHandler.save('bringForward');
1316
- }
1317
- const { onModified } = this;
1318
- if (onModified) {
1319
- onModified(activeObject);
1320
- }
1321
- }
1322
- };
1323
- /**
1324
- * Bring to front
1325
- */
1326
- this.bringToFront = () => {
1327
- const activeObject = this.canvas.getActiveObject();
1328
- if (activeObject) {
1329
- this.canvas.bringToFront(activeObject);
1330
- if (!this.transactionHandler.active) {
1331
- this.transactionHandler.save('bringToFront');
1332
- }
1333
- const { onModified } = this;
1334
- if (onModified) {
1335
- onModified(activeObject);
1336
- }
1337
- }
1338
- };
1339
- /**
1340
- * Send backwards
1341
- * @returns
1342
- */
1343
- this.sendBackwards = () => {
1344
- const activeObject = this.canvas.getActiveObject();
1345
- if (activeObject) {
1346
- const firstObject = this.canvas.getObjects()[1];
1347
- if (firstObject.id === activeObject.id) {
1348
- return;
1349
- }
1350
- if (!this.transactionHandler.active) {
1351
- this.transactionHandler.save('sendBackwards');
1352
- }
1353
- this.canvas.sendBackwards(activeObject);
1354
- const { onModified } = this;
1355
- if (onModified) {
1356
- onModified(activeObject);
1357
- }
1358
- }
1359
- };
1360
- /**
1361
- * Send to back
1362
- */
1363
- this.sendToBack = () => {
1364
- const activeObject = this.canvas.getActiveObject();
1365
- if (activeObject) {
1366
- this.canvas.sendToBack(activeObject);
1367
- this.canvas.sendToBack(this.canvas.getObjects()[1]);
1368
- if (!this.transactionHandler.active) {
1369
- this.transactionHandler.save('sendToBack');
1370
- }
1371
- const { onModified } = this;
1372
- if (onModified) {
1373
- onModified(activeObject);
1374
- }
1375
- }
1376
- };
1377
- /**
1378
- * Clear canvas
1379
- * @param {boolean} [includeWorkarea=false]
1380
- */
1381
- this.clear = (includeWorkarea = false) => {
1382
- const ids = this.canvas.getObjects().reduce((prev, curr) => {
1383
- if (curr.superType === 'element') {
1384
- prev.push(curr.id);
1385
- return prev;
1386
- }
1387
- return prev;
1388
- }, []);
1389
- this.elementHandler.removeByIds(ids);
1390
- if (includeWorkarea) {
1391
- this.canvas.clear();
1392
- this.workarea = null;
1393
- }
1394
- else {
1395
- this.canvas.discardActiveObject();
1396
- this.canvas.getObjects().forEach((obj) => {
1397
- if (obj.id === 'grid' || obj.id === 'workarea') {
1398
- return;
1399
- }
1400
- this.canvas.remove(obj);
1401
- });
1402
- }
1403
- this.objects = this.getObjects();
1404
- this.canvas.renderAll();
1405
- };
1406
- /**
1407
- * Start request animation frame
1408
- */
1409
- this.startRequestAnimFrame = () => {
1410
- if (!this.isRequsetAnimFrame) {
1411
- this.isRequsetAnimFrame = true;
1412
- const render = () => {
1413
- this.canvas.renderAll();
1414
- this.requestFrame = fabric_1.fabric.util.requestAnimFrame(render);
1415
- };
1416
- fabric_1.fabric.util.requestAnimFrame(render);
1417
- }
1418
- };
1419
- /**
1420
- * Stop request animation frame
1421
- */
1422
- this.stopRequestAnimFrame = () => {
1423
- this.isRequsetAnimFrame = false;
1424
- const cancelRequestAnimFrame = (() => window.cancelAnimationFrame ||
1425
- // || window.webkitCancelRequestAnimationFrame
1426
- // || window.mozCancelRequestAnimationFrame
1427
- // || window.oCancelRequestAnimationFrame
1428
- // || window.msCancelRequestAnimationFrame
1429
- clearTimeout)();
1430
- cancelRequestAnimFrame(this.requestFrame);
1431
- };
1432
- /**
1433
- * Save target object as image
1434
- * @param {FabricObject} targetObject
1435
- * @param {string} [option={ name: 'New Image', format: 'png', quality: 1 }]
1436
- */
1437
- this.saveImage = (targetObject, option = { name: 'New Image', format: 'png', quality: 1 }) => {
1438
- let dataUrl;
1439
- let target = targetObject;
1440
- if (target) {
1441
- dataUrl = target.toDataURL(option);
1442
- }
1443
- else {
1444
- target = this.canvas.getActiveObject();
1445
- if (target) {
1446
- dataUrl = target.toDataURL(option);
1447
- }
1448
- }
1449
- if (dataUrl) {
1450
- const anchorEl = document.createElement('a');
1451
- anchorEl.href = dataUrl;
1452
- anchorEl.download = `${option.name}.png`;
1453
- document.body.appendChild(anchorEl); // required for firefox
1454
- anchorEl.click();
1455
- anchorEl.remove();
1456
- }
1457
- };
1458
- /**
1459
- * Save canvas as image
1460
- * @param {string} [option={ name: 'New Image', format: 'png', quality: 1 }]
1461
- */
1462
- this.saveCanvasImage = (option = { name: 'New Image', format: 'png', quality: 1 }) => {
1463
- const dataUrl = this.canvas.toDataURL(option);
1464
- if (dataUrl) {
1465
- const anchorEl = document.createElement('a');
1466
- anchorEl.href = dataUrl;
1467
- anchorEl.download = `${option.name}.png`;
1468
- document.body.appendChild(anchorEl); // required for firefox
1469
- anchorEl.click();
1470
- anchorEl.remove();
1471
- }
1472
- };
1473
- /**
1474
- * Sets "angle" of an instance with centered rotation
1475
- *
1476
- * @param {number} angle
1477
- */
1478
- this.rotate = (angle) => {
1479
- const activeObject = this.canvas.getActiveObject();
1480
- if (activeObject) {
1481
- this.set('rotation', angle);
1482
- activeObject.rotate(angle);
1483
- this.canvas.requestRenderAll();
1484
- }
1485
- };
1486
- /**
1487
- * Destroy canvas
1488
- *
1489
- */
1490
- this.destroy = () => {
1491
- this.eventHandler.destroy();
1492
- this.guidelineHandler.destroy();
1493
- this.contextmenuHandler.destory();
1494
- this.tooltipHandler.destroy();
1495
- this.clear(true);
1496
- };
1497
- /**
1498
- * Set canvas option
1499
- *
1500
- * @param {CanvasOption} canvasOption
1501
- */
1502
- this.setCanvasOption = (canvasOption) => {
1503
- this.canvasOption = Object.assign({}, this.canvasOption, canvasOption);
1504
- this.canvas.setBackgroundColor(canvasOption.backgroundColor, this.canvas.renderAll.bind(this.canvas));
1505
- if (typeof canvasOption.width !== 'undefined' && typeof canvasOption.height !== 'undefined') {
1506
- if (this.eventHandler) {
1507
- this.eventHandler.resize(canvasOption.width, canvasOption.height);
1508
- }
1509
- else {
1510
- this.canvas.setWidth(canvasOption.width).setHeight(canvasOption.height);
1511
- }
1512
- }
1513
- if (typeof canvasOption.selection !== 'undefined') {
1514
- this.canvas.selection = canvasOption.selection;
1515
- }
1516
- if (typeof canvasOption.hoverCursor !== 'undefined') {
1517
- this.canvas.hoverCursor = canvasOption.hoverCursor;
1518
- }
1519
- if (typeof canvasOption.defaultCursor !== 'undefined') {
1520
- this.canvas.defaultCursor = canvasOption.defaultCursor;
1521
- }
1522
- if (typeof canvasOption.preserveObjectStacking !== 'undefined') {
1523
- this.canvas.preserveObjectStacking = canvasOption.preserveObjectStacking;
1524
- }
1525
- };
1526
- /**
1527
- * Set keyboard event
1528
- *
1529
- * @param {KeyEvent} keyEvent
1530
- */
1531
- this.setKeyEvent = (keyEvent) => {
1532
- this.keyEvent = Object.assign({}, this.keyEvent, keyEvent);
1533
- };
1534
- /**
1535
- * Set fabric objects
1536
- *
1537
- * @param {FabricObjects} fabricObjects
1538
- */
1539
- this.setFabricObjects = (fabricObjects) => {
1540
- this.fabricObjects = Object.assign({}, this.fabricObjects, fabricObjects);
1541
- };
1542
- /**
1543
- * Set workarea option
1544
- *
1545
- * @param {WorkareaOption} workareaOption
1546
- */
1547
- this.setWorkareaOption = (workareaOption) => {
1548
- this.workareaOption = Object.assign({}, this.workareaOption, workareaOption);
1549
- if (this.workarea) {
1550
- this.workarea.set({
1551
- ...workareaOption,
1552
- });
1553
- }
1554
- };
1555
- /**
1556
- * Set guideline option
1557
- *
1558
- * @param {GuidelineOption} guidelineOption
1559
- */
1560
- this.setGuidelineOption = (guidelineOption) => {
1561
- this.guidelineOption = Object.assign({}, this.guidelineOption, guidelineOption);
1562
- if (this.guidelineHandler) {
1563
- this.guidelineHandler.initialize();
1564
- }
1565
- };
1566
- /**
1567
- * Set grid option
1568
- *
1569
- * @param {GridOption} gridOption
1570
- */
1571
- this.setGridOption = (gridOption) => {
1572
- this.gridOption = Object.assign({}, this.gridOption, gridOption);
1573
- };
1574
- /**
1575
- * Set object option
1576
- *
1577
- * @param {FabricObjectOption} objectOption
1578
- */
1579
- this.setObjectOption = (objectOption) => {
1580
- this.objectOption = Object.assign({}, this.objectOption, objectOption);
1581
- };
1582
- /**
1583
- * Set activeSelection option
1584
- *
1585
- * @param {Partial<FabricObjectOption<fabric.ActiveSelection>>} activeSelectionOption
1586
- */
1587
- this.setActiveSelectionOption = (activeSelectionOption) => {
1588
- this.activeSelectionOption = Object.assign({}, this.activeSelectionOption, activeSelectionOption);
1589
- };
1590
- /**
1591
- * Set propertiesToInclude
1592
- *
1593
- * @param {string[]} propertiesToInclude
1594
- */
1595
- this.setPropertiesToInclude = (propertiesToInclude) => {
1596
- this.propertiesToInclude = lodash_1.union(propertiesToInclude, this.propertiesToInclude);
1597
- };
1598
- this.initialize(options);
1599
- }
1600
- /**
1601
- * Initialize handler
1602
- *
1603
- * @author salgum1114
1604
- * @param {HandlerOptions} options
1605
- */
1606
- initialize(options) {
1607
- this.initOption(options);
1608
- this.initCallback(options);
1609
- this.initHandler();
1610
- }
1611
- }
1612
- exports.default = Handler;