/
sketchpad.js
999 lines (932 loc) · 34 KB
/
sketchpad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
import Base from '../../components/base/Base';
import Two from 'two.js';
import Picker from 'vanilla-picker';
import _ from 'lodash';
import Formio from '../../Formio';
import editForm from './Sketchpad.form';
export default class Sketchpad extends Base {
static schema(...extend) {
return Base.schema({
type: 'sketchpad',
label: 'Sketchpad',
key: 'sketchpad',
defaultZoom: 100
}, ...extend);
}
static builderInfo = {
title: 'Sketchpad',
group: 'advanced',
icon: 'fa fa-image',
weight: 110,
documentation: 'http://help.form.io/userguide/',
schema: Sketchpad.schema()
}
static editForm = editForm
constructor(...args) {
super(...args);
_.defaults(this.component, {
defaultZoom: 100
});
this.deleted = [];
this.viewSketchpad = {
canvas: {},
background: {}
};
this.editSketchpad = {
canvas: {},
background: {}
};
//will use dimensions from background viewBox if either width or height is not defined for component
//TODO maybe change this criteria to AND instead of OR, use defined dimension and default another missing dimension to value from viewBox (in this case will need to use promise in case of any missing dimension
this.useBackgroundDimensions = !this.component.width || !this.component.height;
//initialize backgroundReady promise
const backgroundReadyPromise = new Promise((resolve, reject) => {
this.backgroundReady = {
resolve,
reject
};
});
this.backgroundReady.promise = backgroundReadyPromise;
//default state of SVG editor
this.state = {
mode: Object.keys(this.modes)[0],
stroke: '#333',
fill: '#ccc',
linewidth: 1,
circleSize: 10
};
this.zoomInfo = {
viewBox: {},
multiplier: 1.5,
totalMultiplier: 1
};
}
/**
* Builds the component.
*/
build(state) {
state = state || {};
this.calculatedValue = state.calculatedValue;
this.createElement();
this.createLabel(this.element);
this.viewSketchpad.canvas.container = this.ce('div', { class: 'formio-view-sketchpad-canvas' });
this.viewSketchpad.background.container = this.ce('div', { class: 'formio-view-sketchpad-background' });
this.addEventListener(this.viewSketchpad.canvas.container, 'click', this.editSvg.bind(this));
this.element.appendChild(this.ce('div', {
class: 'formio-view-sketchpad-container'
}, [
this.viewSketchpad.canvas.container,
this.viewSketchpad.background.container
]));
this.editSketchpad.canvas.container = this.ce('div', { class: 'formio-edit-sketchpad-canvas' });
this.editSketchpad.background.container = this.ce('div', {
class: 'formio-edit-sketchpad-background'
});
//init two instance
this.two = new Two({
type: Two.Types.svg,
}).appendTo(this.editSketchpad.canvas.container);
//init canvas SVG variable
this.editSketchpad.canvas.svg = this.two.renderer.domElement;
this.addClass(this.editSketchpad.canvas.svg, 'formio-sketchpad-svg');
this.addBackground();
this.backgroundReady.promise.then(() => {
this.backgroundReady.isReady = true;
this.attach();
// Disable if needed.
if (this.shouldDisable) {
this.disabled = true;
}
// Restore the value.
this.restoreValue();
this.autofocus();
this.attachLogic();
});
}
getValue() {
return this.dataValue;
}
get emptyValue() {
return [];
}
get modes() {
return {
pencil: {
icon: 'pencil',
title: 'Pencil',
state: {
mode: 'pencil'
},
eventStart: (coordinate) => {
this.points = [coordinate];
this.prev = coordinate;
this.curve = this.two.makeCurve([
new Two.Vector(this.prev.x, this.prev.y),
new Two.Vector(coordinate.x, coordinate.y + 1)
], true);
this.curve.noFill().stroke = this.state.stroke;
this.curve.linewidth = this.state.linewidth;
this.curve.vertices.forEach((v) => v.addSelf(this.curve.translation));
this.curve.translation.clear();
this.two.update();
this.layers.push(this.curve);
this.curve._renderer.elem.addEventListener('click', (e) => this.click(e, this.layers.length));
},
drag: (coordinate) => {
this.points.push(coordinate);
this.curve.vertices.push(new Two.Vector(coordinate.x, coordinate.y));
this.two.update();
this.prev = coordinate;
},
eventEnd: () => {
const value = this.editValue.slice();
value.push(Object.assign({}, this.state, { points: this.points }));
this.editValue = value;
this.triggerChange();
},
draw: (state) => {
const layer = this.two.makeCurve(state.points.map(point => new Two.Vector(point.x, point.y)), true);
layer.noFill().stroke = state.stroke;
layer.linewidth = state.linewidth;
layer.vertices.forEach((v) => v.addSelf(layer.translation));
layer.translation.clear();
return layer;
}
},
line: {
icon: 'minus',
title: 'Line',
state: {
mode: 'line'
},
eventStart: (coordinate) => {
this.center = coordinate;
this.line = this.two.makeLine(
coordinate.x,
coordinate.y,
coordinate.x,
coordinate.y
);
this.line.fill = this.state.fill;
this.line.stroke = this.state.stroke;
this.line.linewidth = this.state.linewidth;
this.two.update();
this.layers.push(this.line);
const index = this.layers.length - 1;
this.line._renderer.elem.addEventListener('click', (e) => this.click(e, index));
},
drag: (coordinate) => {
this.line.vertices[1].x = coordinate.x;
this.line.vertices[1].y = coordinate.y;
this.two.update();
},
eventEnd: () => {
const value = this.editValue.slice();
const vertices = this.line.vertices.map(vertice => {
return {
x: vertice.x,
y: vertice.y
};
});
value.push(Object.assign({}, this.state, { vertices: vertices }));
this.editValue = value;
this.triggerChange();
},
draw: (state) => {
const layer = this.two.makeLine(
state.vertices[0].x,
state.vertices[0].y,
state.vertices[1].x,
state.vertices[1].y
);
layer.fill = state.fill;
layer.stroke = state.stroke;
layer.linewidth = state.linewidth;
return layer;
}
},
circle: {
icon: 'circle',
title: 'Circle',
state: {
mode: 'circle'
},
eventStart: (coordinate) => {
this.center = coordinate;
const layer = this.two.makeCircle(coordinate.x, coordinate.y, this.state.circleSize);
layer.fill = this.state.fill;
layer.stroke = this.state.stroke;
layer.linewidth = this.state.linewidth;
this.two.update();
this.layers.push(layer);
const index = this.layers.length - 1;
layer._renderer.elem.addEventListener('click', (e) => this.click(e, index));
},
drag: () => {
},
eventEnd: () => {
const value = this.editValue.slice();
value.push(Object.assign({}, this.state, { center: this.center }));
this.editValue = value;
this.triggerChange();
},
draw: (state) => {
const layer = this.two.makeCircle(state.center.x, state.center.y, state.circleSize);
layer.fill = state.fill;
layer.stroke = state.stroke;
layer.linewidth = state.linewidth;
return layer;
},
attach: (element) => {
const radiusInput = this.ce('input', {
type: 'number',
class: 'formio-sketchpad-toolbar-input formio-sketchpad-radius-input',
onChange: (e) => {
this.state.circleSize = e.target.value;
}
});
radiusInput.value = this.state.circleSize;
element.appendChild(radiusInput);
return element;
}
},
rectangle: {
icon: 'square-o',
cursor: {
hover: 'crosshair'
},
title: 'Rectangle',
state: {
mode: 'rectangle'
},
eventStart: (coordinate) => {
this.dragStartPoint = coordinate;
},
drag: (coordinate) => {
this.dragEndPoint = coordinate;
if (this.rectangle) {
this.rectangle.remove();
}
this.width = Math.abs(this.dragEndPoint.x - this.dragStartPoint.x);
this.height = Math.abs(this.dragEndPoint.y - this.dragStartPoint.y);
this.center = {
x: Math.min(this.dragStartPoint.x, this.dragEndPoint.x) + this.width / 2,
y: Math.min(this.dragStartPoint.y, this.dragEndPoint.y) + this.height / 2
};
this.rectangle = this.two.makeRectangle(this.center.x, this.center.y, this.width, this.height);
this.rectangle.fill = this.state.fill;
this.rectangle.stroke = this.state.stroke;
this.rectangle.linewidth = this.state.linewidth;
this.two.update();
this.layers.push(this.rectangle);
const index = this.layers.length - 1;
this.rectangle._renderer.elem.addEventListener('click', (e) => this.click(e, index));
},
eventEnd: () => {
const value = this.editValue.slice();
delete this.rectangle;
const rectangleState = {
center: this.center,
width: this.width,
height: this.height
};
value.push(Object.assign({}, this.state, rectangleState));
this.editValue = value;
this.triggerChange();
},
draw: (state) => {
const layer = this.two.makeRectangle(state.center.x, state.center.y, state.width, state.height);
layer.fill = state.fill;
layer.stroke = state.stroke;
layer.linewidth = state.linewidth;
return layer;
},
},
zoomIn: {
icon: 'search-plus',
cursor: {
hover: 'zoom-in'
},
title: 'Zoom In',
state: {
mode: 'zoomIn'
},
eventStart: (coordinate) => {
this.zoom(coordinate, this.zoomInfo.multiplier);
}
},
zoomOut: {
icon: 'search-minus',
cursor: {
hover: 'zoom-out'
},
title: 'Zoom Out',
state: {
mode: 'zoomOut'
},
eventStart: (coordinate) => {
this.zoom(coordinate, 1 / this.zoomInfo.multiplier);
}
},
drag: {
icon: 'hand-paper-o',
title: 'Drag Zoomed Image',
cursor: {
hover: 'grab',
clicked: 'grabbing'
},
state: {
mode: 'drag'
},
eventStart: (coordinate) => {
this.dragStartPoint = coordinate;
},
drag: (coordinate) => {
if (!this.dragLastPoint) {
this.dragLastPoint = this.dragStartPoint;
}
const offset = {
x: Math.round(coordinate.x - this.dragStartPoint.x),
y: Math.round(coordinate.y - this.dragStartPoint.y)
};
if (offset.x !== 0 || offset.y !== 0) {
this.dragImage(offset);
this.dragLastPoint = coordinate;
}
}
}
};
}
get styles() {
return [
{
icon: 'square-o',
title: 'Stroke Color',
type: 'colorpicker',
property: 'stroke',
attach: (element) => {
const picker = new Picker(element);
picker.setColor(this.state.stroke, true);
picker.onChange = (color) => {
this.state.stroke = color.rgbaString;
element.style.color = color.rgbaString;
};
return element;
}
},
{
icon: 'square',
title: 'Fill Color',
type: 'colorpicker',
property: 'fill',
attach: (element) => {
const picker = new Picker(element);
picker.setColor(this.state.fill, true);
picker.onChange = (color) => {
this.state.fill = color.rgbaString;
element.style.color = color.rgbaString;
};
return element;
}
},
{
icon: 'minus',
title: 'Line Width',
type: 'number',
property: 'linewidth',
attach: (element) => {
const widthInput = this.ce('input', {
type: 'number',
class: 'formio-sketchpad-toolbar-input formio-sketchpad-linewidth-input',
onChange: (e) => {
this.state.linewidth = e.target.value;
}
});
widthInput.value = this.state.linewidth;
element.appendChild(widthInput);
return element;
}
}
];
}
get actions() {
return [
{
icon: 'undo',
action: 'undo',
title: 'Undo'
},
{
icon: 'repeat',
action: 'redo',
title: 'Redo'
},
{
icon: 'search',
action: 'resetZoom',
title: 'Reset Zoom'
},
{
icon: 'ban',
action: 'clearAll',
title: 'Clear All'
}
];
}
editSvg() {
if (this.options.readOnly) {
return;
}
//open editor in modal
this.editorModal = this.createModal();
this.addClass(this.editorModal, 'formio-sketchpad-edit-dialog');
this.addClass(this.editorModal.body, 'formio-sketchpad-edit-dialog-body');
const toolbar = this.createToolbar();
const metaInfoContainer = this.ce('div',
{
class: 'formio-sketchpad-meta-info'
},
this.ce('span',
{},
[
this.totalMultiplierElement = this.ce('span', {},
this.t(Math.round(this.zoomInfo.totalMultiplier) * 100) / 100
),
this.t('x')
]
)
);
this.editorModal.body.appendChild(toolbar);
this.editorModal.body.appendChild(this.ce('div', {
class: 'formio-edit-sketchpad-container'
}, [
this.editSketchpad.canvas.container,
this.editSketchpad.background.container
]));
this.editorModal.body.appendChild(metaInfoContainer);
this.saveSvgButton = this.ce('button', {
class: 'btn btn-success formio-sketchpad-save-button'
}, this.t('Save'));
this.addEventListener(this.saveSvgButton, 'click', () => {
this.saveSvg();
this.editorModal.close(true);
});
this.editorModal.body.appendChild(this.saveSvgButton);
this.editValue = _.cloneDeep(this.dataValue);
this.draw(this.editValue);
const initialDialogClose = this.editorModal.close;
this.editorModal.close = (ignoreWarning) => {
if (ignoreWarning || confirm('Are you sure you want to close? Your unsaved progress will be lost')) {
this.resetZoom();
initialDialogClose();
}
};
this.resetZoom();
}
saveSvg() {
this.dataValue = this.editValue;
this.copySvgToView();
}
createToolbar() {
/* eslint-disable max-len */
return this.ce('div', {
class: 'btn-toolbar formio-sketchpad-toolbar',
role: 'toolbar'
}, [
this.ce('div', {
class: 'btn-group formio-sketchpad-toolbar-group',
role: 'group'
},
this.modeButtons = Object.keys(this.modes).map(key => {
const mode = this.modes[key];
const toolbarButton = this.ce('div', {
class: `btn btn-secondary formio-sketchpad-toolbar-button formio-sketchpad-toolbar-button-${key} ${this.state.mode === mode.state.mode ? ' active' : ''}`,
onClick: () => this.setState(mode.state),
title: mode.title
}, this.ce('i', {
class: `fa fa-${mode.icon}`,
}));
if (mode.attach) {
return mode.attach(toolbarButton);
}
return toolbarButton;
}),
),
this.ce('div', {
class: 'btn-group formio-sketchpad-toolbar-group',
role: 'group'
},
this.styles.map(button => {
const toolbarButtonIcon = this.ce('i', {
class: `fa fa-${button.icon}`,
});
const toolbarButton = this.ce('div', {
class: `btn btn-secondary formio-sketchpad-toolbar-button formio-sketchpad-toolbar-button-${button.property}`,
title: button.title
}, toolbarButtonIcon);
if (button.attach) {
return button.attach(toolbarButton);
}
return toolbarButton;
}),
),
this.ce('div', {
class: 'btn-group float-right formio-sketchpad-toolbar-group',
role: 'group'
},
this.actions.map(button => this.ce('div', {
class: `btn btn-secondary formio-sketchpad-toolbar-button formio-sketchpad-toolbar-button-${button.action}`,
onClick: () => this[button.action](),
title: button.title
}, this.ce('i', {
class: `fa fa-${button.icon}`,
}))),
),
]);
/* eslint-enable max-len */
}
attach() {
// Set up mouse events.
this.editSketchpad.canvas.svg
.addEventListener('mousedown', (e) => {
e.preventDefault();
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
//change cursor
let cursor = 'default';
if (this.modes[this.state.mode].cursor) {
cursor = this.modes[this.state.mode].cursor.clicked || this.modes[this.state.mode].cursor.hover;
}
this.editSketchpad.canvas.svg.style.cursor = cursor;
if (this.modes[this.state.mode].eventStart) {
this.modes[this.state.mode].eventStart(this.getActualCoordinates({
x: e.clientX - offset.left,
y: e.clientY - offset.top
}));
}
const mouseDrag = (e) => {
e.preventDefault();
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
if (this.modes[this.state.mode].drag) {
this.modes[this.state.mode].drag(this.getActualCoordinates({
x: e.clientX - offset.left,
y: e.clientY - offset.top
}));
}
};
const mouseEnd = (e) => {
e.preventDefault();
this.editSketchpad.canvas.svg
.removeEventListener('mousemove', mouseDrag);
this.editSketchpad.canvas.svg
.removeEventListener('mouseup', mouseEnd);
//change cursor
let cursor = 'default';
if (this.modes[this.state.mode].cursor) {
cursor = this.modes[this.state.mode].cursor.hover || cursor;
}
this.editSketchpad.canvas.svg.style.cursor = cursor;
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
if (this.modes[this.state.mode].eventEnd) {
this.modes[this.state.mode].eventEnd(this.getActualCoordinates({
x: e.clientX - offset.left,
y: e.clientY - offset.top
}));
}
};
this.editSketchpad.canvas.svg
.addEventListener('mousemove', mouseDrag);
this.editSketchpad.canvas.svg
.addEventListener('mouseup', mouseEnd);
return false;
});
// Set up touch events.
this.editSketchpad.canvas.svg
.addEventListener('touchstart', (e) => {
e.preventDefault();
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
const touch = e.changedTouches[0];
//change cursor
let cursor = 'default';
if (this.modes[this.state.mode].cursor) {
cursor = this.modes[this.state.mode].cursor.clicked || this.modes[this.state.mode].cursor.hover;
}
this.editSketchpad.canvas.svg.style.cursor = cursor;
if (this.modes[this.state.mode].eventStart) {
this.modes[this.state.mode].eventStart(this.getActualCoordinates({
x: touch.pageX - offset.left,
y: touch.pageY - offset.top
}));
}
const touchDrag = (e) => {
e.preventDefault();
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
const touch = e.changedTouches[0];
if (this.modes[this.state.mode].drag) {
this.modes[this.state.mode].drag(this.getActualCoordinates({
x: touch.pageX - offset.left,
y: touch.pageY - offset.top
}));
}
};
const touchEnd = (e) => {
e.preventDefault();
this.editSketchpad.canvas.svg
.removeEventListener('touchmove', touchDrag);
this.editSketchpad.canvas.svg
.removeEventListener('touchend', touchEnd);
const offset = this.editSketchpad.canvas.svg.getBoundingClientRect();
const touch = e.changedTouches[0];
//change cursor
let cursor = 'default';
if (this.modes[this.state.mode].cursor) {
cursor = this.modes[this.state.mode].cursor.hover || cursor;
}
this.editSketchpad.canvas.svg.style.cursor = cursor;
if (this.modes[this.state.mode].eventEnd) {
this.modes[this.state.mode].eventEnd(this.getActualCoordinates({
x: touch.pageX - offset.left,
y: touch.pageY - offset.top
}));
}
};
this.editSketchpad.canvas.svg
.addEventListener('touchmove', touchDrag);
this.editSketchpad.canvas.svg
.addEventListener('touchend', touchEnd);
return false;
});
this.two.update();
}
get dataReady() {
return this.backgroundReady.promise;
}
addBackground() {
if (this.component.image) {
this.setBackgroundImage(this.component.image);
this.backgroundReady.resolve();
}
else if (this.component.imageUrl) {
Formio.makeStaticRequest(this.component.imageUrl, 'GET', null, { noToken: true, headers: {} })
.then(image => {
this.setBackgroundImage(image);
this.backgroundReady.resolve();
})
.catch(() => {
console.warn(`Sketchpad background didn't load for component: ${this.component.key}`);
this.backgroundReady.resolve();
});
}
//TODO make sure component works without background
}
/* eslint-disable max-statements */
setBackgroundImage(svgMarkup) {
const xmlDoc = new DOMParser().parseFromString(svgMarkup, 'text/xml');
let backgroundSvg = xmlDoc.getElementsByTagName('svg');
if (!backgroundSvg || !backgroundSvg[0]) {
console.warn(`Sketchpad '${this.component.key}': Background SVG doesn't contain <svg> tag on it`);
return;
}
backgroundSvg = backgroundSvg[0];
if (this.useBackgroundDimensions) {
const viewBox = backgroundSvg.getAttribute('viewBox');
let viewBoxMinX, viewBoxMinY, viewBoxWidth, viewBoxHeight;
if (viewBox) {
[viewBoxMinX, viewBoxMinY, viewBoxWidth, viewBoxHeight] = viewBox.split(' ').map(parseFloat);
}
else {
//if viewBox is not defined, use 'x', 'y', 'width' and 'height' SVG attributes (or 0, 0, 640, 480 relatively if any is not defined)
[viewBoxMinX, viewBoxMinY, viewBoxWidth, viewBoxHeight] = [
{ attribute: 'x', defaultValue: 0 },
{ attribute: 'y', defaultValue: 0 },
{ attribute: 'width', defaultValue: 640 },
{ attribute: 'height', defaultValue: 480 }
].map(dimension => {
return parseFloat(backgroundSvg.getAttribute(dimension.attribute)) || dimension.defaultValue;
});
}
//set dimensions to width and height from viewBox of background svg
this.dimensions = {
width: viewBoxWidth,
height: viewBoxHeight
};
//set default and current viewBox sizes for canvas and background (should be based on background)
this.zoomInfo.viewBox.default = {
width: this.dimensions.width,
height: this.dimensions.height,
minX: viewBoxMinX,
minY: viewBoxMinY
};
}
else {
//set dimensions to component width and height
this.dimensions = {
width: this.component.width,
height: this.component.height
};
let viewBoxValue = backgroundSvg.getAttribute('viewBox');
if (!viewBoxValue) {
// since zooming works based on viewBox, we need to have explicitly defined value for it
// if viewBox is not defined on SVG element, browser behaves like it's equal to "0 0 <current_width> <current_height>"
// since background image should match dimensions of editor image, current width and height will always be equal to component.width and component.height
// as a result:
viewBoxValue = `0 0 ${this.dimensions.width} ${this.dimensions.height}`;
backgroundSvg.setAttribute('viewBox', viewBoxValue);
}
let [initialMinX, initialMinY, initialWidth, initialHeight] = viewBoxValue.split(' ').map(parseFloat);
initialMinX = initialMinX || 0;
initialMinY = initialMinY || 0;
initialWidth = initialWidth || this.dimensions.width;
initialHeight = initialHeight || this.dimensions.height;
const width = this.dimensions.width,
height = this.dimensions.height,
minX = Math.round(initialMinX - (this.dimensions.width - initialWidth) / 2),
minY = Math.round(initialMinY - (this.dimensions.height - initialHeight) / 2);
//set initial zoom info for SVG
this.zoomInfo.viewBox.default = {
width: width,
height: height,
minX: minX,
minY: minY
};
}
//set current zoom to default
this.zoomInfo.viewBox.current = _.cloneDeep(this.zoomInfo.viewBox.default);
svgMarkup = new XMLSerializer().serializeToString(backgroundSvg);
this.editSketchpad.background.container.style['min-width'] = `${this.dimensions.width}px`;
this.editSketchpad.background.container.style['min-height'] = `${this.dimensions.height}px`;
//set background containers content to SVG markup
this.viewSketchpad.background.container.innerHTML = svgMarkup;
this.editSketchpad.background.container.innerHTML = svgMarkup;
//init svg variables
this.viewSketchpad.background.svg = this.viewSketchpad.background.container.firstElementChild;
this.editSketchpad.background.svg = this.editSketchpad.background.container.firstElementChild;
//set background image viewBox
const viewBox = this.zoomInfo.viewBox.current;
this.viewSketchpad.background.svg.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
this.editSketchpad.background.svg.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
//set canvas image viewBox (necessary at least for useBackgroundDimensions when background image has minX and minY other that 0
this.editSketchpad.canvas.svg.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
//set dimensions for Two.js instance
this.setEditorSize(this.dimensions.width, this.dimensions.height);
}
/* eslint-enable max-statements */
clear() {
this.two.clear();
}
clearAll() {
this.layers = [];
this.editValue = [];
this.clear();
this.two.update();
}
draw(value) {
this.clear();
const layers = value.map(item => this.modes[item.mode].draw(item));
this.two.update();
this.layers = layers;
if (layers.length) {
layers.forEach((layer, index) => {
layer._renderer.elem.addEventListener('click', (e) => this.click(e, index));
});
}
}
click(event, index) {
console.log(event, index);
}
undo() {
const value = this.editValue.slice();
if (value.length === 0) {
return;
}
this.deleted.push(value.pop());
this.editValue = value;
this.triggerChange();
this.draw(value);
}
redo() {
if (this.deleted.length === 0) {
return;
}
const value = this.editValue.slice();
value.push(this.deleted.pop());
this.editValue = value;
this.triggerChange();
this.draw(value);
}
setState(state) {
Object.assign(this.state, state);
this.setActiveButton(this.state.mode);
//change cursor
this.editSketchpad.canvas.svg.style.cursor = _.get(this.modes[this.state.mode], 'cursor.hover', 'default');
}
setActiveButton(mode) {
this.modeButtons.forEach(button => this.removeClass(button, 'active'));
Object.keys(this.modes).forEach((key, index) => {
if (this.modes[key].state.mode === mode) {
this.addClass(this.modeButtons[index], 'active');
}
});
}
setValue(value) {
if (!this.backgroundReady.isReady || !this.two) {
return;
}
this.draw(value);
this.copySvgToView();
}
copySvgToView() {
//clone view SVG element from editor
const svgElement = this.editSketchpad.canvas.svg.cloneNode(true);
//make view SVG responsive: remove height and width attribute, add viewBox attribute
svgElement.removeAttribute('height');
svgElement.removeAttribute('width');
svgElement.style.cursor = 'pointer';
//set viewBox to default to reset zoom
const viewBox = this.zoomInfo.viewBox.default;
svgElement.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
this.viewSketchpad.canvas.container.innerHTML = '';
this.viewSketchpad.canvas.container.appendChild(svgElement);
}
zoom(coordinate, multiplier) {
this.setTotalMultiplier(this.zoomInfo.totalMultiplier * multiplier);
//calculate new viewBox width for canvas
this.zoomInfo.viewBox.current.width =
Math.round(this.zoomInfo.viewBox.default.width / this.zoomInfo.totalMultiplier);
this.zoomInfo.viewBox.current.height =
Math.round(this.zoomInfo.viewBox.default.height / this.zoomInfo.totalMultiplier);
if (
this.zoomInfo.viewBox.current.width > this.dimensions.width &&
this.zoomInfo.viewBox.current.height > this.dimensions.height
) {
//if should get less than initial size, change editor size instead of viewBox size
this.setEditorSize(
this.dimensions.width * this.zoomInfo.totalMultiplier,
this.dimensions.height * this.zoomInfo.totalMultiplier
);
//restore default viewBox values for canvas and background
this.zoomInfo.viewBox.current = _.cloneDeep(this.zoomInfo.viewBox.default);
}
else {
//if should get more than initial size, change viewBox size
//restore editor size if needed
if (this.two.width !== this.dimensions.width || this.two.height !== this.dimensions.height) {
this.setEditorSize(this.dimensions.width, this.dimensions.height);
}
//calculate SVG offset so that coordinate would be center of zoomed image
this.zoomInfo.viewBox.current.minX = coordinate.x - this.zoomInfo.viewBox.current.width / 2;
this.zoomInfo.viewBox.current.minY = coordinate.y - this.zoomInfo.viewBox.current.height / 2;
this.normalizeSvgOffset();
}
this.updateSvgViewBox();
}
resetZoom() {
this.zoom({ x: 0, y: 0 }, (this.component.defaultZoom / 100) / this.zoomInfo.totalMultiplier);
}
getActualCoordinates(coordinate) {
//recalculate coordinate taking into account current zoom
coordinate.x = Math.round((coordinate.x / this.zoomInfo.totalMultiplier) + this.zoomInfo.viewBox.current.minX);
coordinate.y = Math.round((coordinate.y / this.zoomInfo.totalMultiplier) + this.zoomInfo.viewBox.current.minY);
return coordinate;
}
dragImage(offset) {
//calculate new offsets for SVG
this.zoomInfo.viewBox.current.minX = this.zoomInfo.viewBox.current.minX - offset.x;
this.zoomInfo.viewBox.current.minY = this.zoomInfo.viewBox.current.minY - offset.y;
this.normalizeSvgOffset();
this.updateSvgViewBox();
}
normalizeSvgOffset() {
/* eslint-disable max-len */
//don't let offset go out of SVG on the left and on the top
//canvas
this.zoomInfo.viewBox.current.minX = this.zoomInfo.viewBox.current.minX < this.zoomInfo.viewBox.default.minX ? this.zoomInfo.viewBox.default.minX : this.zoomInfo.viewBox.current.minX;
this.zoomInfo.viewBox.current.minY = this.zoomInfo.viewBox.current.minY < this.zoomInfo.viewBox.default.minY ? this.zoomInfo.viewBox.default.minY : this.zoomInfo.viewBox.current.minY;
//don't let offset go out of SVG on the right and on the bottom
//canvas
const canvasMaxOffsetX = this.zoomInfo.viewBox.default.width - this.zoomInfo.viewBox.current.width + this.zoomInfo.viewBox.default.minX,
canvasMaxOffsetY = this.zoomInfo.viewBox.default.height - this.zoomInfo.viewBox.current.height + this.zoomInfo.viewBox.default.minY;
this.zoomInfo.viewBox.current.minX = this.zoomInfo.viewBox.current.minX > (canvasMaxOffsetX) ? canvasMaxOffsetX : this.zoomInfo.viewBox.current.minX;
this.zoomInfo.viewBox.current.minY = this.zoomInfo.viewBox.current.minY > (canvasMaxOffsetY) ? canvasMaxOffsetY : this.zoomInfo.viewBox.current.minY;
/* eslint-enable max-len */
}
updateSvgViewBox() {
//set viewBox so that SVG gets zoomed to the proper area according to zoomInfo
const viewBox = this.zoomInfo.viewBox.current;
this.editSketchpad.canvas.svg.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
this.editSketchpad.background.svg.setAttribute('viewBox', `${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`);
}
setTotalMultiplier(multiplier) {
this.zoomInfo.totalMultiplier = multiplier;
this.totalMultiplierElement.innerHTML = this.t(Math.round(multiplier * 100) / 100);
}
setEditorSize(width, height) {
this.two.width = width;
this.two.height = height;
this.two.update();
//change width of background svg so it matches editor SVG
this.editSketchpad.background.svg.style.width = width;
this.editSketchpad.background.svg.style.height = height;
this.editSketchpad.background.container.style['min-width'] = width;
this.editSketchpad.background.container.style['min-height'] = height;
}
}