@inweb/viewer-visualize 26.1.2 → 26.1.3

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.
@@ -1,2307 +1,12 @@
1
- import Konva from "konva";
1
+ import { CANVAS_EVENTS, draggersRegistry, commandsRegistry, componentsRegistry, Options } from "@inweb/viewer-core";
2
2
 
3
- class CommandsRegistry {
4
- constructor() {
5
- this._commands = new Map;
6
- }
7
- registerCommand(id, handler, description, thisArg) {
8
- this._commands.set(id, {
9
- id: id,
10
- handler: handler,
11
- thisArg: thisArg,
12
- description: description
13
- });
14
- }
15
- registerCommandAlias(id, alias) {
16
- this.registerCommand(alias, ((viewer, ...args) => this.executeCommand(id, viewer, ...args)));
17
- }
18
- getCommand(id) {
19
- return this._commands.get(id);
20
- }
21
- getCommands() {
22
- const map = new Map;
23
- this._commands.forEach(((value, key) => map.set(key, value)));
24
- return map;
25
- }
26
- executeCommand(id, viewer, ...args) {
27
- const command = this._commands.get(id);
28
- if (!command) {
29
- if (viewer) {
30
- const isDraggerCommand = viewer.draggers.includes(id);
31
- if (isDraggerCommand) return viewer.setActiveDragger(id);
32
- }
33
- console.warn(`Command '${id}' not found`);
34
- return undefined;
35
- }
36
- const {handler: handler, thisArg: thisArg} = command;
37
- const result = handler.apply(thisArg, [ viewer, ...args ]);
38
- viewer === null || viewer === undefined ? undefined : viewer.emit({
39
- type: "command",
40
- data: id,
41
- args: args
42
- });
43
- return result;
44
- }
45
- }
46
-
47
- const _commandsRegistry = new Map;
48
-
49
- function commandsRegistry(viewerType = "") {
50
- let result = _commandsRegistry.get(viewerType);
51
- if (!result) {
52
- result = new CommandsRegistry;
53
- _commandsRegistry.set(viewerType, result);
54
- }
55
- return result;
56
- }
57
-
58
- class Dragger {
59
- constructor(viewer) {
60
- this.name = "";
61
- }
62
- dispose() {}
63
- }
64
-
65
- class DraggersRegistry {
66
- constructor() {
67
- this._draggers = new Map;
68
- }
69
- registerDragger(name, provider) {
70
- this._draggers.set(name, provider);
71
- }
72
- registerDraggerAlias(name, alias) {
73
- const provider = this.getDragger(name);
74
- if (provider) this.registerDragger(alias, (viewer => provider(viewer)));
75
- }
76
- getDragger(name) {
77
- return this._draggers.get(name);
78
- }
79
- getDraggers() {
80
- const map = new Map;
81
- this._draggers.forEach(((value, key) => map.set(key, value)));
82
- return map;
83
- }
84
- createDragger(name, viewer) {
85
- const provider = this.getDragger(name);
86
- if (!provider) return null;
87
- const dragger = provider(viewer);
88
- dragger.name = name;
89
- return dragger;
90
- }
91
- }
92
-
93
- const _draggersRegistry = new Map;
94
-
95
- function draggersRegistry(viewerType = "") {
96
- let result = _draggersRegistry.get(viewerType);
97
- if (!result) {
98
- result = new DraggersRegistry;
99
- _draggersRegistry.set(viewerType, result);
100
- }
101
- return result;
102
- }
103
-
104
- class Component {
105
- constructor(viewer) {
106
- this.name = "";
107
- }
108
- dispose() {}
109
- }
110
-
111
- class Components {
112
- constructor() {
113
- this._components = new Map;
114
- }
115
- registerComponent(name, provider) {
116
- this._components.set(name, provider);
117
- }
118
- registerComponentAlias(name, alias) {
119
- const provider = this.getComponent(name);
120
- if (provider) this.registerComponent(alias, (viewer => provider(viewer)));
121
- }
122
- getComponent(name) {
123
- return this._components.get(name);
124
- }
125
- getComponents() {
126
- const map = new Map;
127
- this._components.forEach(((value, key) => map.set(key, value)));
128
- return map;
129
- }
130
- createComponent(name, viewer) {
131
- const provider = this.getComponent(name);
132
- if (!provider) return null;
133
- const component = provider(viewer);
134
- component.name = name;
135
- return component;
136
- }
137
- }
138
-
139
- const _components = new Map;
140
-
141
- function componentsRegistry(viewerType = "") {
142
- let result = _components.get(viewerType);
143
- if (!result) {
144
- result = new Components;
145
- _components.set(viewerType, result);
146
- }
147
- return result;
148
- }
149
-
150
- function defaultOptions() {
151
- return {
152
- showWCS: true,
153
- cameraAnimation: true,
154
- antialiasing: true,
155
- groundShadow: false,
156
- shadows: false,
157
- cameraAxisXSpeed: 4,
158
- cameraAxisYSpeed: 1,
159
- ambientOcclusion: false,
160
- enableStreamingMode: true,
161
- enablePartialMode: false,
162
- memoryLimit: 3294967296,
163
- cuttingPlaneFillColor: {
164
- red: 255,
165
- green: 152,
166
- blue: 0
167
- },
168
- edgesColor: {
169
- r: 255,
170
- g: 152,
171
- b: 0
172
- },
173
- facesColor: {
174
- r: 255,
175
- g: 152,
176
- b: 0
177
- },
178
- edgesVisibility: true,
179
- edgesOverlap: true,
180
- facesOverlap: false,
181
- facesTransparancy: 200,
182
- enableCustomHighlight: true,
183
- sceneGraph: false,
184
- edgeModel: true,
185
- reverseZoomWheel: false,
186
- enableZoomWheel: true,
187
- enableGestures: true,
188
- geometryType: "vsfx",
189
- rulerUnit: "Default"
190
- };
191
- }
192
-
193
- class Options {
194
- constructor(emitter) {
195
- this._emitter = emitter;
196
- this._data = defaultOptions();
197
- this.loadFromStorage();
198
- }
199
- static defaults() {
200
- return defaultOptions();
201
- }
202
- notifierChangeEvent() {
203
- console.warn("Options.notifierChangeEvent() has been deprecated since 25.3 and will be removed in a future release, use Options.change() instead.");
204
- this.change();
205
- }
206
- change() {
207
- if (this._emitter !== undefined) {
208
- this.saveToStorage();
209
- this._emitter.emit({
210
- type: "optionschange",
211
- data: this
212
- });
213
- }
214
- }
215
- saveToStorage() {
216
- if (typeof window !== "undefined") try {
217
- localStorage.setItem("od-client-settings", JSON.stringify(this.data));
218
- } catch (error) {
219
- console.error("Cannot save client settings.", error);
220
- }
221
- }
222
- loadFromStorage() {
223
- if (typeof window !== "undefined") try {
224
- const item = localStorage.getItem("od-client-settings");
225
- if (item) {
226
- const data = JSON.parse(item);
227
- this.data = {
228
- ...data
229
- };
230
- }
231
- } catch (error) {
232
- console.error("Cannot load client settings.", error);
233
- }
234
- }
235
- resetToDefaults(fields) {
236
- if (fields !== undefined) {
237
- const defaults = Options.defaults();
238
- const resetData = fields.reduce(((acc, field) => {
239
- acc[field] = defaults[field];
240
- return acc;
241
- }), {});
242
- this.data = {
243
- ...this.data,
244
- ...resetData
245
- };
246
- } else {
247
- this.data = {
248
- ...this.data,
249
- ...Options.defaults()
250
- };
251
- }
252
- }
253
- get data() {
254
- return this._data;
255
- }
256
- set data(value) {
257
- const enablePartialMode = value.enableStreamingMode ? value.enablePartialMode : false;
258
- const sceneGraph = enablePartialMode ? false : value.sceneGraph;
259
- this._data = {
260
- ...Options.defaults(),
261
- ...this._data,
262
- ...value,
263
- enablePartialMode: enablePartialMode,
264
- sceneGraph: sceneGraph
265
- };
266
- this.change();
267
- }
268
- get showWCS() {
269
- return this._data.showWCS;
270
- }
271
- set showWCS(value) {
272
- this._data.showWCS = value;
273
- this.change();
274
- }
275
- get cameraAnimation() {
276
- return this._data.cameraAnimation;
277
- }
278
- set cameraAnimation(value) {
279
- this._data.cameraAnimation = value;
280
- this.change();
281
- }
282
- get antialiasing() {
283
- return this._data.antialiasing;
284
- }
285
- set antialiasing(value) {
286
- this._data.antialiasing = value;
287
- this.change();
288
- }
289
- get groundShadow() {
290
- return this._data.groundShadow;
291
- }
292
- set groundShadow(value) {
293
- this._data.groundShadow = value;
294
- this.change();
295
- }
296
- get shadows() {
297
- return this._data.shadows;
298
- }
299
- set shadows(value) {
300
- this._data.shadows = value;
301
- this.change();
302
- }
303
- get cameraAxisXSpeed() {
304
- return this._data.cameraAxisXSpeed;
305
- }
306
- set cameraAxisXSpeed(value) {
307
- this._data.cameraAxisXSpeed = value;
308
- this.change();
309
- }
310
- get cameraAxisYSpeed() {
311
- return this._data.cameraAxisYSpeed;
312
- }
313
- set cameraAxisYSpeed(value) {
314
- this.cameraAxisYSpeed = value;
315
- this.change();
316
- }
317
- get ambientOcclusion() {
318
- return this._data.ambientOcclusion;
319
- }
320
- set ambientOcclusion(value) {
321
- this._data.ambientOcclusion = value;
322
- this.change();
323
- }
324
- get enableStreamingMode() {
325
- return this._data.enableStreamingMode;
326
- }
327
- set enableStreamingMode(value) {
328
- this._data.enableStreamingMode = value;
329
- if (!value) this._data.enablePartialMode = false;
330
- this.change();
331
- }
332
- get enablePartialMode() {
333
- return this._data.enablePartialMode;
334
- }
335
- set enablePartialMode(value) {
336
- this._data.enablePartialMode = value;
337
- if (value) {
338
- this._data.enableStreamingMode = true;
339
- this._data.sceneGraph = false;
340
- }
341
- this.change();
342
- }
343
- get memoryLimit() {
344
- return this._data.memoryLimit;
345
- }
346
- set memoryLimit(value) {
347
- this._data.memoryLimit = value;
348
- this.change();
349
- }
350
- get cuttingPlaneFillColor() {
351
- return this._data.cuttingPlaneFillColor;
352
- }
353
- set cuttingPlaneFillColor(value) {
354
- this._data.cuttingPlaneFillColor = value;
355
- this.change();
356
- }
357
- get edgesColor() {
358
- return this._data.edgesColor;
359
- }
360
- set edgesColor(value) {
361
- this._data.edgesColor = value;
362
- this.change();
363
- }
364
- get facesColor() {
365
- return this._data.facesColor;
366
- }
367
- set facesColor(value) {
368
- this._data.facesColor = value;
369
- this.change();
370
- }
371
- get edgesVisibility() {
372
- return this._data.edgesVisibility;
373
- }
374
- set edgesVisibility(value) {
375
- this._data.edgesVisibility = value;
376
- this.change();
377
- }
378
- get edgesOverlap() {
379
- return this._data.edgesOverlap;
380
- }
381
- set edgesOverlap(value) {
382
- this._data.edgesOverlap = value;
383
- this.change();
384
- }
385
- get facesOverlap() {
386
- return this._data.facesOverlap;
387
- }
388
- set facesOverlap(value) {
389
- this._data.facesOverlap = value;
390
- this.change();
391
- }
392
- get facesTransparancy() {
393
- return this._data.facesTransparancy;
394
- }
395
- set facesTransparancy(value) {
396
- this._data.facesTransparancy = value;
397
- this.change();
398
- }
399
- get enableCustomHighlight() {
400
- return this._data.enableCustomHighlight;
401
- }
402
- set enableCustomHighlight(value) {
403
- this._data.enableCustomHighlight = value;
404
- this.change();
405
- }
406
- get sceneGraph() {
407
- return this._data.sceneGraph;
408
- }
409
- set sceneGraph(value) {
410
- this._data.sceneGraph = value;
411
- if (value) this._data.enablePartialMode = false;
412
- this.change();
413
- }
414
- get edgeModel() {
415
- return Boolean(this._data.edgeModel);
416
- }
417
- set edgeModel(value) {
418
- this._data.edgeModel = Boolean(value);
419
- this.change();
420
- }
421
- get reverseZoomWheel() {
422
- return this._data.reverseZoomWheel;
423
- }
424
- set reverseZoomWheel(value) {
425
- this._data.reverseZoomWheel = !!value;
426
- this.change();
427
- }
428
- get enableZoomWheel() {
429
- return this._data.enableZoomWheel;
430
- }
431
- set enableZoomWheel(value) {
432
- this._data.enableZoomWheel = !!value;
433
- this.change();
434
- }
435
- get enableGestures() {
436
- return this._data.enableGestures;
437
- }
438
- set enableGestures(value) {
439
- this._data.enableGestures = !!value;
440
- this.change();
441
- }
442
- get geometryType() {
443
- return this._data.geometryType;
444
- }
445
- set geometryType(value) {
446
- this._data.geometryType = value;
447
- this.change();
448
- }
449
- get rulerUnit() {
450
- return this._data.rulerUnit;
451
- }
452
- set rulerUnit(value) {
453
- this._data.rulerUnit = value;
454
- this.change();
455
- }
456
- }
457
-
458
- const CanvasEvents = [ "click", "contextmenu", "dblclick", "mousedown", "mouseleave", "mousemove", "mouseup", "pointercancel", "pointerdown", "pointerleave", "pointermove", "pointerup", "touchcancel", "touchend", "touchmove", "touchstart", "wheel" ];
459
-
460
- const CANVAS_EVENTS = CanvasEvents;
461
-
462
- class WorldTransform {
463
- screenToWorld(position) {
464
- return {
465
- x: position.x,
466
- y: position.y,
467
- z: 0
468
- };
469
- }
470
- worldToScreen(position) {
471
- return {
472
- x: position.x,
473
- y: position.y
474
- };
475
- }
476
- getScale() {
477
- return {
478
- x: 1,
479
- y: 1,
480
- z: 1
481
- };
482
- }
483
- }
484
-
485
- class MarkupColor {
486
- constructor(r, g, b) {
487
- this.setColor(r, g, b);
488
- }
489
- asHex() {
490
- return "#" + this.HEX;
491
- }
492
- asRGB() {
493
- return {
494
- r: this.R,
495
- g: this.G,
496
- b: this.B
497
- };
498
- }
499
- setColor(r, g, b) {
500
- this.R = r;
501
- this.G = g;
502
- this.B = b;
503
- this.HEX = this.rgbToHex(r, g, b);
504
- }
505
- rgbToHex(r, g, b) {
506
- const valueToHex = c => {
507
- const hex = c.toString(16);
508
- return hex === "0" ? "00" : hex;
509
- };
510
- return valueToHex(r) + valueToHex(g) + valueToHex(b);
511
- }
512
- }
513
-
514
- const LineTypeSpecs = new Map([ [ "solid", [] ], [ "dot", [ 30, 30, .001, 30 ] ], [ "dash", [ 30, 30 ] ] ]);
515
-
516
- class KonvaLine {
517
- constructor(params, ref = null) {
518
- var _a, _b;
519
- if (ref) {
520
- this._ref = ref;
521
- return;
522
- }
523
- if (!params) params = {};
524
- if (!params.points) params.points = [ {
525
- x: 50,
526
- y: 50
527
- }, {
528
- x: 100,
529
- y: 100
530
- } ];
531
- const konvaPoints = [];
532
- params.points.forEach((point => konvaPoints.push(point.x, point.y)));
533
- this._ref = new Konva.Line({
534
- stroke: (_a = params.color) !== null && _a !== undefined ? _a : "#ff0000",
535
- strokeWidth: (_b = params.width) !== null && _b !== undefined ? _b : 4,
536
- globalCompositeOperation: "source-over",
537
- lineCap: "round",
538
- lineJoin: "round",
539
- points: konvaPoints,
540
- draggable: true,
541
- strokeScaleEnabled: false,
542
- dash: LineTypeSpecs.get(params.type) || []
543
- });
544
- this._ref.on("transform", (e => {
545
- const attrs = e.target.attrs;
546
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
547
- }));
548
- this._ref.id(this._ref._id.toString());
549
- }
550
- ref() {
551
- return this._ref;
552
- }
553
- id() {
554
- return this._ref.id();
555
- }
556
- enableMouseEditing(value) {
557
- this._ref.draggable(value);
558
- }
559
- type() {
560
- return "Line";
561
- }
562
- getColor() {
563
- return this._ref.stroke();
564
- }
565
- setColor(hex) {
566
- this._ref.stroke(hex);
567
- }
568
- getRotation() {
569
- return this._ref.rotation();
570
- }
571
- setRotation(degrees) {
572
- this._ref.rotation(degrees);
573
- }
574
- getZIndex() {
575
- return this._ref.zIndex();
576
- }
577
- setZIndex(zIndex) {
578
- this._ref.zIndex(zIndex);
579
- }
580
- delete() {
581
- this._ref.destroy();
582
- this._ref = null;
583
- }
584
- getPoints() {
585
- return this._ref.points();
586
- }
587
- setLineWidth(size) {
588
- this._ref.strokeWidth(size);
589
- }
590
- getLineWidth() {
591
- return this._ref.strokeWidth();
592
- }
593
- getLineType() {
594
- const typeSpecs = this._ref.dash() || [];
595
- let type;
596
- switch (typeSpecs) {
597
- case LineTypeSpecs.get("dot"):
598
- type = "dot";
599
- break;
600
-
601
- case LineTypeSpecs.get("dash"):
602
- type = "dash";
603
- break;
604
-
605
- default:
606
- type = "solid";
607
- break;
608
- }
609
- return type;
610
- }
611
- setLineType(type) {
612
- const specs = LineTypeSpecs.get(type);
613
- if (specs) this._ref.dash(specs);
614
- }
615
- addPoints(points) {
616
- let newPoints = this._ref.points();
617
- points.forEach((point => {
618
- newPoints = newPoints.concat([ point.x, point.y ]);
619
- }));
620
- this._ref.points(newPoints);
621
- }
622
- }
623
-
624
- class KonvaText {
625
- constructor(params, ref = null) {
626
- var _a, _b, _c;
627
- this.TEXT_FONT_FAMILY = "Calibri";
628
- if (ref) {
629
- this._ref = ref;
630
- return;
631
- }
632
- if (!params) params = {};
633
- if (!params.position) params.position = {
634
- x: 100,
635
- y: 100
636
- };
637
- if (!params.text) params.text = "default";
638
- this._ref = new Konva.Text({
639
- x: params.position.x,
640
- y: params.position.y,
641
- text: params.text,
642
- fontSize: (_a = params.fontSize) !== null && _a !== undefined ? _a : 34,
643
- fontFamily: this.TEXT_FONT_FAMILY,
644
- fill: (_b = params.color) !== null && _b !== undefined ? _b : "#ff0000",
645
- align: "left",
646
- draggable: true,
647
- rotation: (_c = params.rotation) !== null && _c !== undefined ? _c : 0
648
- });
649
- this._ref.width(this._ref.getTextWidth());
650
- this._ref.on("transform", (e => {
651
- const attrs = e.target.attrs;
652
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
653
- const scaleByX = Math.abs(attrs.scaleX - 1) > 1e-5;
654
- const scaleByY = Math.abs(attrs.scaleY - 1) > 1e-5;
655
- let newWidth = this._ref.width();
656
- if (scaleByX) newWidth *= attrs.scaleX;
657
- let newHeight = this._ref.height();
658
- if (scaleByY) newHeight *= attrs.scaleY;
659
- const minWidth = 50;
660
- if (newWidth < minWidth) newWidth = minWidth;
661
- if (newHeight < Math.round(this.getFontSize())) newHeight = Math.round(this.getFontSize());
662
- if (scaleByX) {
663
- this._ref.width(newWidth);
664
- }
665
- if (scaleByY) {
666
- this._ref.height(newHeight);
667
- }
668
- this._ref.scale({
669
- x: 1,
670
- y: 1
671
- });
672
- }));
673
- this._ref.id(this._ref._id.toString());
674
- }
675
- ref() {
676
- return this._ref;
677
- }
678
- id() {
679
- return this._ref.id();
680
- }
681
- enableMouseEditing(value) {
682
- this._ref.draggable(value);
683
- }
684
- type() {
685
- return "Text";
686
- }
687
- getColor() {
688
- return this._ref.fill();
689
- }
690
- setColor(hex) {
691
- this._ref.fill(hex);
692
- }
693
- getRotation() {
694
- return this._ref.rotation();
695
- }
696
- setRotation(degrees) {
697
- this._ref.rotation(degrees);
698
- }
699
- getZIndex() {
700
- return this._ref.zIndex();
701
- }
702
- setZIndex(zIndex) {
703
- this._ref.zIndex(zIndex);
704
- }
705
- delete() {
706
- this._ref.destroy();
707
- this._ref = null;
708
- }
709
- getText() {
710
- return this._ref.text();
711
- }
712
- setText(text) {
713
- this._ref.text(text);
714
- }
715
- getPosition() {
716
- return this._ref.getPosition();
717
- }
718
- setPosition(x, y) {
719
- this._ref.setPosition({
720
- x: x,
721
- y: y
722
- });
723
- }
724
- getFontSize() {
725
- return this._ref.fontSize();
726
- }
727
- setFontSize(size) {
728
- this._ref.fontSize(size);
729
- }
730
- }
731
-
732
- class KonvaRectangle {
733
- constructor(params, ref = null) {
734
- var _a, _b, _c, _d;
735
- if (ref) {
736
- this._ref = ref;
737
- return;
738
- }
739
- if (!params) params = {};
740
- if (!params.position) params.position = {
741
- x: 100,
742
- y: 100
743
- };
744
- this._ref = new Konva.Rect({
745
- stroke: (_a = params.color) !== null && _a !== undefined ? _a : "#ff0000",
746
- strokeWidth: (_b = params.lineWidth) !== null && _b !== undefined ? _b : 4,
747
- globalCompositeOperation: "source-over",
748
- lineCap: "round",
749
- lineJoin: "round",
750
- x: params.position.x,
751
- y: params.position.y,
752
- width: (_c = params.width) !== null && _c !== undefined ? _c : 200,
753
- height: (_d = params.height) !== null && _d !== undefined ? _d : 200,
754
- draggable: true,
755
- strokeScaleEnabled: false
756
- });
757
- this._ref.on("transform", (e => {
758
- const attrs = e.target.attrs;
759
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
760
- const scaleByX = Math.abs(attrs.scaleX - 1) > 1e-5;
761
- const scaleByY = Math.abs(attrs.scaleY - 1) > 1e-5;
762
- let newWidth = this._ref.width();
763
- if (scaleByX) newWidth *= attrs.scaleX;
764
- let newHeight = this._ref.height();
765
- if (scaleByY) newHeight *= attrs.scaleY;
766
- const minWidth = 50;
767
- const minHeight = 50;
768
- if (newWidth < minWidth) newWidth = minWidth;
769
- if (newHeight < minHeight) newHeight = minHeight;
770
- if (scaleByX) {
771
- this._ref.width(newWidth);
772
- }
773
- if (scaleByY) {
774
- this._ref.height(newHeight);
775
- }
776
- this._ref.scale({
777
- x: 1,
778
- y: 1
779
- });
780
- }));
781
- this._ref.id(this._ref._id.toString());
782
- }
783
- getPosition() {
784
- return this._ref.position();
785
- }
786
- getWidth() {
787
- return this._ref.width();
788
- }
789
- getHeigth() {
790
- return this._ref.height();
791
- }
792
- setWidth(w) {
793
- this._ref.width(w);
794
- }
795
- setHeight(h) {
796
- this._ref.height(h);
797
- }
798
- setPosition(x, y) {
799
- this._ref.setPosition({
800
- x: x,
801
- y: y
802
- });
803
- }
804
- ref() {
805
- return this._ref;
806
- }
807
- id() {
808
- return this._ref.id();
809
- }
810
- enableMouseEditing(value) {
811
- this._ref.draggable(value);
812
- }
813
- type() {
814
- return "Rectangle";
815
- }
816
- getColor() {
817
- return this._ref.stroke();
818
- }
819
- setColor(hex) {
820
- this._ref.stroke(hex);
821
- }
822
- getRotation() {
823
- return this._ref.rotation();
824
- }
825
- setRotation(degrees) {
826
- this._ref.rotation(degrees);
827
- }
828
- getZIndex() {
829
- return this._ref.zIndex();
830
- }
831
- setZIndex(zIndex) {
832
- this._ref.zIndex(zIndex);
833
- }
834
- delete() {
835
- this._ref.destroy();
836
- this._ref = null;
837
- }
838
- setLineWidth(size) {
839
- this._ref.strokeWidth(size);
840
- }
841
- getLineWidth() {
842
- return this._ref.strokeWidth();
843
- }
844
- }
845
-
846
- class KonvaEllipse {
847
- constructor(params, ref = null) {
848
- var _a, _b;
849
- if (ref) {
850
- this._ref = ref;
851
- return;
852
- }
853
- if (!params) params = {};
854
- if (!params.position) params.position = {
855
- x: 100,
856
- y: 100
857
- };
858
- if (!params.radius) params.radius = {
859
- x: 25,
860
- y: 25
861
- };
862
- this._ref = new Konva.Ellipse({
863
- stroke: (_a = params.color) !== null && _a !== undefined ? _a : "#ff0000",
864
- strokeWidth: (_b = params.lineWidth) !== null && _b !== undefined ? _b : 4,
865
- globalCompositeOperation: "source-over",
866
- lineCap: "round",
867
- lineJoin: "round",
868
- x: params.position.x,
869
- y: params.position.y,
870
- radiusX: params.radius.x,
871
- radiusY: params.radius.y,
872
- draggable: true,
873
- strokeScaleEnabled: false
874
- });
875
- this._ref.on("transform", (e => {
876
- const attrs = e.target.attrs;
877
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
878
- const scaleByX = Math.abs(attrs.scaleX - 1) > 1e-5;
879
- const scaleByY = Math.abs(attrs.scaleY - 1) > 1e-5;
880
- let newRadiusX = this._ref.radiusX();
881
- if (scaleByX) newRadiusX *= attrs.scaleX;
882
- let newRadiusY = this._ref.radiusY();
883
- if (scaleByY) newRadiusY *= attrs.scaleY;
884
- const minRadiusX = 25;
885
- const minRadiusY = 25;
886
- if (newRadiusX < minRadiusX) newRadiusX = minRadiusX;
887
- if (newRadiusY < minRadiusY) newRadiusY = minRadiusY;
888
- if (e.evt.ctrlKey || e.evt.shiftKey) {
889
- if (scaleByX) {
890
- this._ref.radius({
891
- x: newRadiusX,
892
- y: newRadiusX
893
- });
894
- } else {
895
- this._ref.radius({
896
- x: newRadiusY,
897
- y: newRadiusY
898
- });
899
- }
900
- } else {
901
- this._ref.radius({
902
- x: newRadiusX,
903
- y: newRadiusY
904
- });
905
- }
906
- this._ref.scale({
907
- x: 1,
908
- y: 1
909
- });
910
- }));
911
- this._ref.id(this._ref._id.toString());
912
- }
913
- getPosition() {
914
- return this._ref.position();
915
- }
916
- setPosition(x, y) {
917
- this._ref.setPosition({
918
- x: x,
919
- y: y
920
- });
921
- }
922
- getRadiusX() {
923
- return this._ref.radiusX();
924
- }
925
- setRadiusX(r) {
926
- this._ref.radiusX(r);
927
- }
928
- getRadiusY() {
929
- return this._ref.radiusY();
930
- }
931
- setRadiusY(r) {
932
- this._ref.radiusY(r);
933
- }
934
- getLineWidth() {
935
- return this._ref.strokeWidth();
936
- }
937
- setLineWidth(size) {
938
- this._ref.strokeWidth(size);
939
- }
940
- ref() {
941
- return this._ref;
942
- }
943
- id() {
944
- return this._ref.id();
945
- }
946
- enableMouseEditing(value) {
947
- this._ref.draggable(value);
948
- }
949
- type() {
950
- return "Ellipse";
951
- }
952
- getColor() {
953
- return this._ref.stroke();
954
- }
955
- setColor(hex) {
956
- this._ref.stroke(hex);
957
- }
958
- getRotation() {
959
- return this._ref.rotation();
960
- }
961
- setRotation(degrees) {
962
- this._ref.rotation(degrees);
963
- }
964
- getZIndex() {
965
- return this._ref.zIndex();
966
- }
967
- setZIndex(zIndex) {
968
- this._ref.zIndex(zIndex);
969
- }
970
- delete() {
971
- this._ref.destroy();
972
- this._ref = null;
973
- }
974
- }
975
-
976
- class KonvaArrow {
977
- constructor(params, ref = null) {
978
- var _a, _b;
979
- if (ref) {
980
- this._ref = ref;
981
- return;
982
- }
983
- if (!params) params = {};
984
- if (!params.start) params.start = {
985
- x: 50,
986
- y: 50
987
- };
988
- if (!params.end) params.end = {
989
- x: 100,
990
- y: 100
991
- };
992
- this._ref = new Konva.Arrow({
993
- stroke: (_a = params.color) !== null && _a !== undefined ? _a : "#ff0000",
994
- fill: (_b = params.color) !== null && _b !== undefined ? _b : "#ff0000",
995
- strokeWidth: 4,
996
- globalCompositeOperation: "source-over",
997
- lineCap: "round",
998
- lineJoin: "round",
999
- points: [ params.start.x, params.start.y, params.end.x, params.end.y ],
1000
- draggable: true,
1001
- strokeScaleEnabled: false
1002
- });
1003
- this._ref.on("transform", (e => {
1004
- const attrs = e.target.attrs;
1005
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
1006
- }));
1007
- this._ref.id(this._ref._id.toString());
1008
- }
1009
- ref() {
1010
- return this._ref;
1011
- }
1012
- id() {
1013
- return this._ref.id();
1014
- }
1015
- enableMouseEditing(value) {
1016
- this._ref.draggable(value);
1017
- }
1018
- type() {
1019
- return "Arrow";
1020
- }
1021
- getColor() {
1022
- return this._ref.stroke();
1023
- }
1024
- setColor(hex) {
1025
- this._ref.stroke(hex);
1026
- this._ref.fill(hex);
1027
- }
1028
- getRotation() {
1029
- return this._ref.rotation();
1030
- }
1031
- setRotation(degrees) {
1032
- this._ref.rotation(degrees);
1033
- }
1034
- getZIndex() {
1035
- return this._ref.zIndex();
1036
- }
1037
- setZIndex(zIndex) {
1038
- this._ref.zIndex(zIndex);
1039
- }
1040
- delete() {
1041
- this._ref.destroy();
1042
- this._ref = null;
1043
- }
1044
- getPoints() {
1045
- const points = this._ref.points();
1046
- return [ {
1047
- x: points[0],
1048
- y: points[1]
1049
- }, {
1050
- x: points[2],
1051
- y: points[3]
1052
- } ];
1053
- }
1054
- setPoints(points) {
1055
- if (points.length === 2) {
1056
- this._ref.points([ points[0].x, points[0].y, points[1].x, points[1].y ]);
1057
- }
1058
- }
1059
- getStartPoint() {
1060
- const points = this._ref.points();
1061
- return {
1062
- x: points[0],
1063
- y: points[1]
1064
- };
1065
- }
1066
- setStartPoint(x, y) {
1067
- const points = this._ref.points();
1068
- this._ref.points([ x, y, points[2], points[3] ]);
1069
- }
1070
- getEndPoint() {
1071
- const points = this._ref.points();
1072
- return {
1073
- x: points[2],
1074
- y: points[3]
1075
- };
1076
- }
1077
- setEndPoint(x, y) {
1078
- const points = this._ref.points();
1079
- this._ref.points([ points[0], points[1], x, y ]);
1080
- }
1081
- }
1082
-
1083
- class KonvaImage {
1084
- constructor(params, ref = null) {
1085
- var _a, _b;
1086
- this._ratio = 1;
1087
- this.EPSILON = 1e-5;
1088
- this.BASE64_HEADER_START = "data:image/";
1089
- this.BASE64_NOT_FOUND = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAADsAAAA7AF5KHG9AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAmhJREFUWIXtlr9rVEEQxz+H8RQUJIdeIopYm0vkCg0GBBtbG1NF7Kxt7dR/IGIw/uhTaBNLERURg2kCEUyCYCPi70b0InjGS57FzOZN3r19d+9HJIVfWO52dma/s7Mz8xa2KAaBCWAR+AkECWOmSOIdwC1gtQOpHc+NfQ8wClQ8+1d0vcdH/lQ3bSIRGAZ2pTjAqNovANXIWlXlAXA2zvi2Ln4AjqYgtagYEutENSLvjRoOImFv5iB32Ae8UrLXwFBk3h9ndF0VJnKSO9gTu3yKu5Z1LKnS8YIcABgw5Ks692JZFXcXRJ46Aq6kikCnHNi/mQ50WwVtfaIoBzL3gRk2drSscJ2wrc4VvUoe2wn/41/iBfoVLRnBGnDSY3AAKacy8AmYR+o7K1zCl6wgrgpOAc/MuhvfgMuk+1JGHQgSBcAloKXy78AjYBppJk5/noTulseBMZ23iD/piHFkEdgTQzKk+5wHjmHC3cmBg0BD5xcSTrFXyQPgIWFtDwMvab+2N8DpbhyY1v/3E8gdDgNfVX9SCVZ0/gW4B0wB71S2BpxLcuCM/jaQSHSDEeAX4VMuAG4gTzyHbcAVXXO6GxxwIX+vvxe7JHcYQ07nHqklj96UIW/YhSWzMKcep8VVtf8B1Dw6h4DfhB+sdbgn2R+gnoEc5NR3dZ+3QJ9H74HqXLPCGlJyTfI9y3YCs0owq3OLOpKkLeBI1HhSDT/mdKIPiUCARMTlQx34TMLjtww8IczmO8AJ/N/2JNSQXAiQ671JePePge0+wzJSQq4FFzlaenIvucUAkiQLhC/mLGNZ9xgn5s63BP4CCk0QDtm4BhoAAAAASUVORK5CYII=";
1090
- if (ref) {
1091
- if (!ref.src || !ref.src.startsWith(this.BASE64_HEADER_START)) ref.src = this.BASE64_NOT_FOUND;
1092
- if (ref.height() <= this.EPSILON) ref.height(32);
1093
- if (ref.width() <= this.EPSILON) ref.width(32);
1094
- this._ref = ref;
1095
- this._canvasImage = ref.image();
1096
- this._ratio = this._ref.height() <= this.EPSILON || this._ref.width() <= this.EPSILON ? 1 : this._ref.height() / this._ref.width();
1097
- return;
1098
- }
1099
- if (!params) params = {};
1100
- if (!params.position) params.position = {
1101
- x: 50,
1102
- y: 50
1103
- };
1104
- if (!params.src || !params.src.startsWith(this.BASE64_HEADER_START)) params.src = this.BASE64_NOT_FOUND;
1105
- this._canvasImage = new Image;
1106
- this._canvasImage.onload = () => {
1107
- this._ref.image(this._canvasImage);
1108
- if (this._ref.height() <= this.EPSILON) this._ref.height(this._canvasImage.height);
1109
- if (this._ref.width() <= this.EPSILON) this._ref.width(this._canvasImage.width);
1110
- this._ratio = this._ref.height() <= this.EPSILON || this._ref.width() <= this.EPSILON ? 1 : this._ref.height() / this._ref.width();
1111
- if ((params.width <= this.EPSILON || params.height <= this.EPSILON) && (params.maxWidth >= this.EPSILON || params.maxWidth >= this.EPSILON)) {
1112
- const heightOutOfCanvas = params.maxHeight - this._canvasImage.height;
1113
- const widthOutOfCanvas = params.maxWidth - this._canvasImage.width;
1114
- if (heightOutOfCanvas <= this.EPSILON || widthOutOfCanvas <= this.EPSILON) {
1115
- if (widthOutOfCanvas <= this.EPSILON && widthOutOfCanvas < heightOutOfCanvas / this._ratio) {
1116
- this._ref.height(params.maxWidth * this._ratio);
1117
- this._ref.width(params.maxWidth);
1118
- } else {
1119
- this._ref.width(params.maxHeight / this._ratio);
1120
- this._ref.height(params.maxHeight);
1121
- }
1122
- }
1123
- }
1124
- };
1125
- this._canvasImage.onerror = () => {
1126
- this._canvasImage.onerror = function() {};
1127
- this._canvasImage.src = this.BASE64_NOT_FOUND;
1128
- };
1129
- this._canvasImage.src = params.src;
1130
- this._ref = new Konva.Image({
1131
- x: params.position.x,
1132
- y: params.position.y,
1133
- image: this._canvasImage,
1134
- width: (_a = params.width) !== null && _a !== undefined ? _a : 0,
1135
- height: (_b = params.height) !== null && _b !== undefined ? _b : 0,
1136
- draggable: true
1137
- });
1138
- this._ref.on("transform", (e => {
1139
- const attrs = e.target.attrs;
1140
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
1141
- const scaleByX = Math.abs(attrs.scaleX - 1) > 1e-5;
1142
- const scaleByY = Math.abs(attrs.scaleY - 1) > 1e-5;
1143
- let newWidth = this._ref.width();
1144
- if (scaleByX) newWidth *= attrs.scaleX;
1145
- let newHeight = this._ref.height();
1146
- if (scaleByY) newHeight *= attrs.scaleY;
1147
- if (e.evt.ctrlKey || e.evt.shiftKey) {
1148
- if (scaleByX) {
1149
- this._ref.width(newWidth);
1150
- this._ref.height(newWidth * this._ratio);
1151
- } else {
1152
- this._ref.width(newHeight / this._ratio);
1153
- this._ref.height(newHeight);
1154
- }
1155
- } else {
1156
- if (scaleByX) {
1157
- this._ref.width(newWidth);
1158
- }
1159
- if (scaleByY) {
1160
- this._ref.height(newHeight);
1161
- }
1162
- }
1163
- this._ref.scale({
1164
- x: 1,
1165
- y: 1
1166
- });
1167
- }));
1168
- this._ref.id(this._ref._id.toString());
1169
- }
1170
- getSrc() {
1171
- return this._canvasImage.src;
1172
- }
1173
- setSrc(src) {
1174
- this._canvasImage.src = src;
1175
- }
1176
- getWidth() {
1177
- return this._ref.width();
1178
- }
1179
- setWidth(w) {
1180
- this._ref.width(w);
1181
- this._ref.height(w * this._ratio);
1182
- }
1183
- getHeight() {
1184
- return this._ref.height();
1185
- }
1186
- setHeight(h) {
1187
- this._ref.height(h);
1188
- this._ref.width(h / this._ratio);
1189
- }
1190
- ref() {
1191
- return this._ref;
1192
- }
1193
- id() {
1194
- return this._ref.id();
1195
- }
1196
- enableMouseEditing(value) {
1197
- this._ref.draggable(value);
1198
- }
1199
- type() {
1200
- return "Image";
1201
- }
1202
- getRotation() {
1203
- return this._ref.rotation();
1204
- }
1205
- setRotation(degrees) {
1206
- this._ref.rotation(degrees);
1207
- }
1208
- getZIndex() {
1209
- return this._ref.zIndex();
1210
- }
1211
- setZIndex(zIndex) {
1212
- this._ref.zIndex(zIndex);
1213
- }
1214
- delete() {
1215
- this._ref.destroy();
1216
- this._ref = null;
1217
- }
1218
- getPosition() {
1219
- return this._ref.getPosition();
1220
- }
1221
- setPosition(x, y) {
1222
- this._ref.setPosition({
1223
- x: x,
1224
- y: y
1225
- });
1226
- }
1227
- }
3
+ export * from "@inweb/viewer-core";
1228
4
 
1229
- class KonvaCloud {
1230
- constructor(params, ref = null) {
1231
- var _a, _b, _c, _d;
1232
- if (ref) {
1233
- this._ref = ref;
1234
- return;
1235
- }
1236
- if (!params) params = {};
1237
- if (!params.position) params.position = {
1238
- x: 100,
1239
- y: 100
1240
- };
1241
- const arcRadius = 16;
1242
- this._ref = new Konva.Shape({
1243
- x: params.position.x,
1244
- y: params.position.y,
1245
- width: (_a = params.width) !== null && _a !== undefined ? _a : 200,
1246
- height: (_b = params.height) !== null && _b !== undefined ? _b : 200,
1247
- stroke: (_c = params.color) !== null && _c !== undefined ? _c : "#ff0000",
1248
- strokeWidth: (_d = params.lineWidth) !== null && _d !== undefined ? _d : 4,
1249
- draggable: true,
1250
- strokeScaleEnabled: false,
1251
- globalCompositeOperation: "source-over",
1252
- sceneFunc: (context, shape) => {
1253
- function calculateMidpoint(position, width, height) {
1254
- const midX = position.x + width / 2;
1255
- const midY = position.y + height / 2;
1256
- return {
1257
- x: midX,
1258
- y: midY
1259
- };
1260
- }
1261
- const points = [ {
1262
- x: 0,
1263
- y: 0
1264
- }, {
1265
- x: 0 + this._ref.width(),
1266
- y: 0
1267
- }, {
1268
- x: 0 + this._ref.width(),
1269
- y: 0 + this._ref.height()
1270
- }, {
1271
- x: 0,
1272
- y: 0 + this._ref.height()
1273
- }, {
1274
- x: 0,
1275
- y: 0
1276
- } ];
1277
- const midPoint = calculateMidpoint({
1278
- x: 0,
1279
- y: 0
1280
- }, this._ref.width(), this._ref.height());
1281
- const baseArcLength = 30;
1282
- context.beginPath();
1283
- for (let iPoint = 0; iPoint < points.length - 1; iPoint++) {
1284
- let approxArcLength = baseArcLength;
1285
- const dx = points[iPoint + 1].x - points[iPoint].x;
1286
- const dy = points[iPoint + 1].y - points[iPoint].y;
1287
- const length = Math.sqrt(dx * dx + dy * dy);
1288
- const arcCount = Math.floor(length / approxArcLength);
1289
- const lengthMod = length % approxArcLength;
1290
- approxArcLength = baseArcLength + arcCount / lengthMod;
1291
- let pX = points[iPoint].x + dx / arcCount / 2;
1292
- let pY = points[iPoint].y + dy / arcCount / 2;
1293
- const pEndX = points[iPoint + 1].x;
1294
- const pEndY = points[iPoint + 1].y;
1295
- const endAngle = Math.atan((pEndY - pY) / (pEndX - pX));
1296
- const startAngle = endAngle + Math.PI;
1297
- const counterClockwise = pX > midPoint.x && pY > midPoint.y;
1298
- for (let iArc = 0; iArc < arcCount; iArc++) {
1299
- if (counterClockwise) {
1300
- context.arc(pX, pY, arcRadius, endAngle, startAngle);
1301
- } else {
1302
- context.arc(pX, pY, arcRadius, startAngle, endAngle);
1303
- }
1304
- pX += dx / arcCount;
1305
- pY += dy / arcCount;
1306
- }
1307
- }
1308
- context.closePath();
1309
- context.fillStrokeShape(shape);
1310
- }
1311
- });
1312
- this._ref.className = "Cloud";
1313
- this._ref.on("transform", (e => {
1314
- const attrs = e.target.attrs;
1315
- if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation);
1316
- const scaleByX = Math.abs(attrs.scaleX - 1) > 1e-5;
1317
- const scaleByY = Math.abs(attrs.scaleY - 1) > 1e-5;
1318
- let newWidth = this._ref.width();
1319
- if (scaleByX) newWidth *= attrs.scaleX;
1320
- let newHeight = this._ref.height();
1321
- if (scaleByY) newHeight *= attrs.scaleY;
1322
- const minWidth = 100;
1323
- const minHeight = 100;
1324
- if (newWidth < minWidth) newWidth = minWidth;
1325
- if (newHeight < minHeight) newHeight = minHeight;
1326
- if (scaleByX) {
1327
- this._ref.width(newWidth);
1328
- }
1329
- if (scaleByY) {
1330
- this._ref.height(newHeight);
1331
- }
1332
- this._ref.scale({
1333
- x: 1,
1334
- y: 1
1335
- });
1336
- }));
1337
- this._ref.getSelfRect = () => ({
1338
- x: 0 - arcRadius,
1339
- y: 0 - arcRadius,
1340
- width: this._ref.width() + 2 * arcRadius,
1341
- height: this._ref.height() + 2 * arcRadius
1342
- });
1343
- this._ref.id(this._ref._id.toString());
1344
- }
1345
- ref() {
1346
- return this._ref;
1347
- }
1348
- id() {
1349
- return this._ref.id();
1350
- }
1351
- enableMouseEditing(value) {
1352
- this._ref.draggable(value);
1353
- }
1354
- type() {
1355
- return "Cloud";
1356
- }
1357
- getColor() {
1358
- return this._ref.stroke();
1359
- }
1360
- setColor(hex) {
1361
- this._ref.stroke(hex);
1362
- }
1363
- getRotation() {
1364
- return this._ref.rotation();
1365
- }
1366
- setRotation(degrees) {
1367
- this._ref.rotation(degrees);
1368
- }
1369
- getZIndex() {
1370
- return this._ref.zIndex();
1371
- }
1372
- setZIndex(zIndex) {
1373
- this._ref.zIndex(zIndex);
1374
- }
1375
- delete() {
1376
- this._ref.destroy();
1377
- this._ref = null;
1378
- }
1379
- getPosition() {
1380
- return this._ref.position();
1381
- }
1382
- setPosition(x, y) {
1383
- this._ref.position({
1384
- x: x,
1385
- y: y
1386
- });
1387
- }
1388
- getWidth() {
1389
- return this._ref.width();
1390
- }
1391
- setWidth(w) {
1392
- this._ref.width(w);
1393
- }
1394
- getHeigth() {
1395
- return this._ref.height();
1396
- }
1397
- setHeight(h) {
1398
- this._ref.height(h);
1399
- }
1400
- getLineWidth() {
1401
- return this._ref.strokeWidth();
1402
- }
1403
- setLineWidth(size) {
1404
- this._ref.strokeWidth(size);
1405
- }
1406
- }
5
+ import { Markup } from "@inweb/markup";
1407
6
 
1408
- const MarkupMode2Konva = {
1409
- SelectMarkup: {
1410
- name: "SelectMarkup",
1411
- initializer: null
1412
- },
1413
- Line: {
1414
- name: "Line",
1415
- initializer: (ref, params = null) => new KonvaLine(params, ref)
1416
- },
1417
- Text: {
1418
- name: "Text",
1419
- initializer: (ref, params = null) => new KonvaText(params, ref)
1420
- },
1421
- Rectangle: {
1422
- name: "Rect",
1423
- initializer: (ref, params = null) => new KonvaRectangle(params, ref)
1424
- },
1425
- Ellipse: {
1426
- name: "Ellipse",
1427
- initializer: (ref, params = null) => new KonvaEllipse(params, ref)
1428
- },
1429
- Arrow: {
1430
- name: "Arrow",
1431
- initializer: (ref, params = null) => new KonvaArrow(params, ref)
1432
- },
1433
- Image: {
1434
- name: "Image",
1435
- initializer: (ref, params = null) => new KonvaImage(params, ref)
1436
- },
1437
- Cloud: {
1438
- name: "Cloud",
1439
- initializer: (ref, params = null) => new KonvaCloud(params, ref)
1440
- }
1441
- };
7
+ export * from "@inweb/markup";
1442
8
 
1443
- class KonvaMarkup {
1444
- constructor() {
1445
- this._containerEvents = [];
1446
- this._markupIsActive = false;
1447
- this._markupColor = new MarkupColor(255, 0, 0);
1448
- this.lineWidth = 4;
1449
- this.lineType = "solid";
1450
- this.fontSize = 34;
1451
- this.changeActiveDragger = event => {
1452
- const draggerName = event.data;
1453
- this._markupContainer.className = this._container.className.split(" ").filter((x => !x.startsWith("oda-cursor-"))).filter((x => x)).concat(`oda-cursor-${draggerName.toLowerCase()}`).join(" ");
1454
- this.removeTextInput();
1455
- this.removeImageInput();
1456
- this.enableEditMode(draggerName);
1457
- };
1458
- this.resizeContainer = entries => {
1459
- const {width: width, height: height} = entries[0].contentRect;
1460
- if (!width || !height) return;
1461
- if (!this._konvaStage) return;
1462
- this._konvaStage.width(width);
1463
- this._konvaStage.height(height);
1464
- };
1465
- this.pan = event => {
1466
- const newPos = {
1467
- x: this._konvaStage.x() + event.dX,
1468
- y: this._konvaStage.y() + event.dY
1469
- };
1470
- this._konvaStage.position(newPos);
1471
- };
1472
- this.zoomAt = event => {
1473
- const newScale = this._konvaStage.scaleX() * event.data;
1474
- this._konvaStage.scale({
1475
- x: newScale,
1476
- y: newScale
1477
- });
1478
- const newPos = {
1479
- x: event.x - (event.x - this._konvaStage.x()) * event.data,
1480
- y: event.y - (event.y - this._konvaStage.y()) * event.data
1481
- };
1482
- this._konvaStage.position(newPos);
1483
- };
1484
- this.redirectToViewer = event => {
1485
- if (this._viewer) this._viewer.emit(event);
1486
- };
1487
- this.getRelativePointPosition = (point, node) => {
1488
- const transform = node.getAbsoluteTransform().copy();
1489
- transform.invert();
1490
- return transform.point(point);
1491
- };
1492
- this.getRelativePointerPosition = node => this.getRelativePointPosition(node.getStage().getPointerPosition(), node);
1493
- }
1494
- initialize(container, containerEvents, viewer, worldTransformer) {
1495
- if (!Konva) throw new Error('Markup error: Konva is not initialized. Forgot to add <script src="https://unpkg.com/konva@9/konva.min.js"><\/script> to your page?');
1496
- this._viewer = viewer;
1497
- this._worldTransformer = worldTransformer !== null && worldTransformer !== undefined ? worldTransformer : new WorldTransform;
1498
- this._container = container;
1499
- this._containerEvents = containerEvents !== null && containerEvents !== undefined ? containerEvents : [];
1500
- this._markupContainer = document.createElement("div");
1501
- this._markupContainer.id = "markup-container";
1502
- this._markupContainer.style.position = "absolute";
1503
- this._markupContainer.style.top = "0px";
1504
- this._markupContainer.style.left = "0px";
1505
- this._markupContainer.style.outline = "0px";
1506
- this._markupContainer.style.pointerEvents = "none";
1507
- const parentDiv = this._container.parentElement;
1508
- parentDiv.appendChild(this._markupContainer);
1509
- this._resizeObserver = new ResizeObserver(this.resizeContainer);
1510
- this._resizeObserver.observe(parentDiv);
1511
- this._markupColor.setColor(255, 0, 0);
1512
- this.initializeKonva();
1513
- if (this._viewer) {
1514
- this._viewer.addEventListener("changeactivedragger", this.changeActiveDragger);
1515
- this._viewer.addEventListener("pan", this.pan);
1516
- this._viewer.addEventListener("zoomat", this.zoomAt);
1517
- }
1518
- }
1519
- dispose() {
1520
- var _a, _b;
1521
- if (this._viewer) {
1522
- this._viewer.removeEventListener("zoomat", this.zoomAt);
1523
- this._viewer.removeEventListener("pan", this.pan);
1524
- this._viewer.removeEventListener("changeactivedragger", this.changeActiveDragger);
1525
- }
1526
- this.destroyKonva();
1527
- (_a = this._resizeObserver) === null || _a === undefined ? undefined : _a.disconnect();
1528
- this._resizeObserver = undefined;
1529
- (_b = this._markupContainer) === null || _b === undefined ? undefined : _b.remove();
1530
- this._markupContainer = undefined;
1531
- this._container = undefined;
1532
- this._viewer = undefined;
1533
- this._worldTransformer = undefined;
1534
- this._markupIsActive = false;
1535
- }
1536
- syncOverlay() {}
1537
- clearOverlay() {
1538
- this.removeTextInput();
1539
- this.removeImageInput();
1540
- this.clearSelected();
1541
- this.getObjects().forEach((obj => obj.delete()));
1542
- }
1543
- getMarkupColor() {
1544
- return this._markupColor.asRGB();
1545
- }
1546
- setMarkupColor(r, g, b) {
1547
- this._markupColor.setColor(r, g, b);
1548
- this._viewer.emit({
1549
- type: "changemarkupcolor",
1550
- data: {
1551
- r: r,
1552
- g: g,
1553
- b: b
1554
- }
1555
- });
1556
- }
1557
- colorizeAllMarkup(r, g, b) {
1558
- const hexColor = new MarkupColor(r, g, b).asHex();
1559
- this.getObjects().filter((obj => {
1560
- var _a;
1561
- return (_a = obj.setColor) === null || _a === undefined ? undefined : _a.call(obj, hexColor);
1562
- }));
1563
- }
1564
- colorizeSelectedMarkups(r, g, b) {
1565
- const hexColor = new MarkupColor(r, g, b).asHex();
1566
- this.getSelectedObjects().filter((obj => {
1567
- var _a;
1568
- return (_a = obj.setColor) === null || _a === undefined ? undefined : _a.call(obj, hexColor);
1569
- }));
1570
- }
1571
- setViewpoint(viewpoint) {
1572
- var _a, _b, _c, _d, _e, _f, _g, _h;
1573
- this.clearSelected();
1574
- this.removeTextInput();
1575
- this.removeImageInput();
1576
- this._konvaStage.scale({
1577
- x: 1,
1578
- y: 1
1579
- });
1580
- this._konvaStage.position({
1581
- x: 0,
1582
- y: 0
1583
- });
1584
- const markupColor = ((_a = viewpoint.custom_fields) === null || _a === undefined ? undefined : _a.markup_color) || {
1585
- r: 255,
1586
- g: 0,
1587
- b: 0
1588
- };
1589
- this.setMarkupColor(markupColor.r, markupColor.g, markupColor.b);
1590
- (_b = viewpoint.lines) === null || _b === undefined ? undefined : _b.forEach((line => {
1591
- const linePoints = [];
1592
- line.points.forEach((point => {
1593
- const screenPoint = this._worldTransformer.worldToScreen(point);
1594
- linePoints.push(screenPoint.x);
1595
- linePoints.push(screenPoint.y);
1596
- }));
1597
- this.addLine(linePoints, line.color, line.type, line.width, line.id);
1598
- }));
1599
- (_c = viewpoint.texts) === null || _c === undefined ? undefined : _c.forEach((text => {
1600
- const screenPoint = this._worldTransformer.worldToScreen(text.position);
1601
- this.addText(text.text, screenPoint, text.angle, text.color, text.text_size, text.font_size, text.id);
1602
- }));
1603
- (_d = viewpoint.rectangles) === null || _d === undefined ? undefined : _d.forEach((rect => {
1604
- const screenPoint = this._worldTransformer.worldToScreen(rect.position);
1605
- this.addRectangle(screenPoint, rect.width, rect.height, rect.line_width, rect.color, rect.id);
1606
- }));
1607
- (_e = viewpoint.ellipses) === null || _e === undefined ? undefined : _e.forEach((ellipse => {
1608
- const screenPoint = this._worldTransformer.worldToScreen(ellipse.position);
1609
- this.addEllipse(screenPoint, ellipse.radius, ellipse.line_width, ellipse.color, ellipse.id);
1610
- }));
1611
- (_f = viewpoint.arrows) === null || _f === undefined ? undefined : _f.forEach((arrow => {
1612
- const startPoint = this._worldTransformer.worldToScreen(arrow.start);
1613
- const endPoint = this._worldTransformer.worldToScreen(arrow.end);
1614
- this.addArrow(startPoint, endPoint, arrow.color, arrow.id);
1615
- }));
1616
- (_g = viewpoint.clouds) === null || _g === undefined ? undefined : _g.forEach((cloud => {
1617
- const screenPoint = this._worldTransformer.worldToScreen(cloud.position);
1618
- this.addCloud(screenPoint, cloud.width, cloud.height, cloud.line_width, cloud.color, cloud.id);
1619
- }));
1620
- (_h = viewpoint.images) === null || _h === undefined ? undefined : _h.forEach((image => {
1621
- const screenPoint = this._worldTransformer.worldToScreen(image.position);
1622
- this.addImage(screenPoint, image.src, image.width, image.height, image.id);
1623
- }));
1624
- }
1625
- getViewpoint(viewpoint) {
1626
- if (!viewpoint) viewpoint = {};
1627
- viewpoint.lines = this.getMarkupLines();
1628
- viewpoint.texts = this.getMarkupTexts();
1629
- viewpoint.arrows = this.getMarkupArrows();
1630
- viewpoint.clouds = this.getMarkupClouds();
1631
- viewpoint.ellipses = this.getMarkupEllipses();
1632
- viewpoint.images = this.getMarkupImages();
1633
- viewpoint.rectangles = this.getMarkupRectangles();
1634
- viewpoint.custom_fields = {
1635
- markup_color: this.getMarkupColor()
1636
- };
1637
- viewpoint.snapshot = {
1638
- data: this.combineMarkupWithDrawing()
1639
- };
1640
- return viewpoint;
1641
- }
1642
- enableEditMode(mode) {
1643
- if (!mode || !MarkupMode2Konva[mode]) {
1644
- this.clearSelected();
1645
- this.removeTextInput();
1646
- this.removeImageInput();
1647
- this._markupContainer.style.pointerEvents = "none";
1648
- this._markupIsActive = false;
1649
- } else {
1650
- this._markupMode = mode;
1651
- this._markupContainer.style.pointerEvents = "all";
1652
- this._markupIsActive = true;
1653
- }
1654
- return this;
1655
- }
1656
- createObject(type, params) {
1657
- const konvaShape = MarkupMode2Konva[type];
1658
- if (!konvaShape || !konvaShape.initializer) throw new Error(`Markup CreateObject - unsupported markup type ${type}`);
1659
- const object = konvaShape.initializer(null, params);
1660
- this.addObject(object);
1661
- return object;
1662
- }
1663
- getObjects() {
1664
- const objects = [];
1665
- Object.keys(MarkupMode2Konva).forEach((type => {
1666
- const konvaShape = MarkupMode2Konva[type];
1667
- this.konvaLayerFind(type).forEach((ref => objects.push(konvaShape.initializer(ref))));
1668
- }));
1669
- return objects;
1670
- }
1671
- getSelectedObjects() {
1672
- if (!this._konvaTransformer) return [];
1673
- return this._konvaTransformer.nodes().map((ref => {
1674
- const name = ref.className;
1675
- const konvaShape = Object.values(MarkupMode2Konva).find((shape => shape.name === name));
1676
- return konvaShape ? konvaShape.initializer(ref) : null;
1677
- })).filter((x => x));
1678
- }
1679
- selectObjects(objects) {
1680
- if (!this._konvaTransformer) return;
1681
- const selectedObjs = this._konvaTransformer.nodes().concat(objects.map((x => x.ref())));
1682
- this._konvaTransformer.nodes(selectedObjs);
1683
- }
1684
- clearSelected() {
1685
- if (this._konvaTransformer) this._konvaTransformer.nodes([]);
1686
- }
1687
- addObject(object) {
1688
- if (object.type() === "Image") this._groupImages.add(object.ref()); else if (object.type() === "Text") this._groupTexts.add(object.ref()); else this._groupGeometry.add(object.ref());
1689
- }
1690
- konvaLayerFind(type) {
1691
- if (!this._konvaLayer) return [];
1692
- const konvaShape = MarkupMode2Konva[type];
1693
- if (!konvaShape || !konvaShape.initializer) return [];
1694
- return this._konvaLayer.find(konvaShape.name).filter((ref => ref.parent === this._konvaLayer || ref.parent === this._groupImages || ref.parent === this._groupGeometry || ref.parent === this._groupTexts));
1695
- }
1696
- initializeKonva() {
1697
- const stage = new Konva.Stage({
1698
- container: this._markupContainer,
1699
- width: this._container.clientWidth,
1700
- height: this._container.clientHeight
1701
- });
1702
- this._konvaStage = stage;
1703
- const layer = new Konva.Layer({
1704
- pixelRation: window.devicePixelRatio
1705
- });
1706
- stage.add(layer);
1707
- this._groupImages = new Konva.Group;
1708
- layer.add(this._groupImages);
1709
- this._groupGeometry = new Konva.Group;
1710
- layer.add(this._groupGeometry);
1711
- this._groupTexts = new Konva.Group;
1712
- layer.add(this._groupTexts);
1713
- this._konvaLayer = layer;
1714
- const transformer = new Konva.Transformer({
1715
- shouldOverdrawWholeArea: false,
1716
- keepRatio: false,
1717
- flipEnabled: false
1718
- });
1719
- layer.add(transformer);
1720
- this._konvaTransformer = transformer;
1721
- let isPaint = false;
1722
- let lastLine;
1723
- let mouseDownPos;
1724
- let lastObj;
1725
- stage.on("mousedown touchstart", (e => {
1726
- if (!this._markupIsActive || e.target !== stage || this._markupMode === "Text" || this._markupMode === "Image") return;
1727
- if (e.target === stage && transformer.nodes().length > 0) {
1728
- transformer.nodes([]);
1729
- return;
1730
- }
1731
- const pos = this.getRelativePointerPosition(stage);
1732
- mouseDownPos = pos;
1733
- isPaint = [ "Arrow", "Cloud", "Ellipse", "Line", "Rectangle" ].some((m => m === this._markupMode));
1734
- if (this._markupMode === "Line") {
1735
- lastLine = this.addLine([ pos.x, pos.y, pos.x, pos.y ]);
1736
- }
1737
- }));
1738
- stage.on("mouseup touchend", (e => {
1739
- if (!this._markupIsActive) return;
1740
- if (isPaint) {
1741
- const pos = this.getRelativePointerPosition(stage);
1742
- const defParams = mouseDownPos && pos.x === mouseDownPos.x && pos.y === mouseDownPos.y;
1743
- const startX = defParams ? mouseDownPos.x : Math.min(mouseDownPos.x, pos.x);
1744
- const startY = defParams ? mouseDownPos.y : Math.min(mouseDownPos.y, pos.y);
1745
- const dX = defParams ? 200 : Math.abs(mouseDownPos.x - pos.x);
1746
- const dY = defParams ? 200 : Math.abs(mouseDownPos.y - pos.y);
1747
- if (defParams) {
1748
- if (this._markupMode === "Rectangle") {
1749
- this.addRectangle({
1750
- x: startX,
1751
- y: startY
1752
- }, dX, dY);
1753
- } else if (this._markupMode === "Ellipse") {
1754
- this.addEllipse({
1755
- x: startX,
1756
- y: startY
1757
- }, {
1758
- x: dX / 2,
1759
- y: dY / 2
1760
- });
1761
- } else if (this._markupMode === "Arrow") {
1762
- this.addArrow({
1763
- x: mouseDownPos.x,
1764
- y: mouseDownPos.y
1765
- }, {
1766
- x: defParams ? mouseDownPos.x + 200 : pos.x,
1767
- y: defParams ? startY : pos.y
1768
- });
1769
- } else if (this._markupMode === "Cloud") {
1770
- this.addCloud({
1771
- x: startX,
1772
- y: startY
1773
- }, Math.max(100, dX), Math.max(100, dY));
1774
- }
1775
- }
1776
- }
1777
- lastObj = undefined;
1778
- isPaint = false;
1779
- }));
1780
- stage.on("mousemove touchmove", (e => {
1781
- if (!this._markupIsActive) return;
1782
- if (!isPaint) {
1783
- return;
1784
- }
1785
- const pos = this.getRelativePointerPosition(stage);
1786
- const defParams = mouseDownPos && pos.x === mouseDownPos.x && pos.y === mouseDownPos.y;
1787
- const startX = defParams ? mouseDownPos.x : Math.min(mouseDownPos.x, pos.x);
1788
- const startY = defParams ? mouseDownPos.y : Math.min(mouseDownPos.y, pos.y);
1789
- const dX = defParams ? 200 : Math.abs(mouseDownPos.x - pos.x);
1790
- const dY = defParams ? 200 : Math.abs(mouseDownPos.y - pos.y);
1791
- if (this._markupMode === "Line") {
1792
- lastLine.addPoints([ {
1793
- x: pos.x,
1794
- y: pos.y
1795
- } ]);
1796
- } else if (this._markupMode === "Arrow") {
1797
- if (lastObj) lastObj.setEndPoint(pos.x, pos.y); else lastObj = this.addArrow({
1798
- x: mouseDownPos.x,
1799
- y: mouseDownPos.y
1800
- }, {
1801
- x: pos.x,
1802
- y: pos.y
1803
- });
1804
- } else if (this._markupMode === "Rectangle") {
1805
- if (lastObj) {
1806
- lastObj.setPosition(startX, startY);
1807
- lastObj.setWidth(dX);
1808
- lastObj.setHeight(dY);
1809
- } else lastObj = this.addRectangle({
1810
- x: startX,
1811
- y: startY
1812
- }, dX, dY);
1813
- } else if (this._markupMode === "Ellipse") {
1814
- if (lastObj) {
1815
- lastObj.setPosition(startX, startY);
1816
- lastObj.setRadiusX(dX);
1817
- lastObj.setRadiusY(dY);
1818
- } else lastObj = this.addEllipse({
1819
- x: startX,
1820
- y: startY
1821
- }, {
1822
- x: dX,
1823
- y: dY
1824
- });
1825
- } else if (this._markupMode === "Cloud") {
1826
- if (lastObj) {
1827
- lastObj.setPosition(startX, startY);
1828
- lastObj.setWidth(Math.max(100, dX));
1829
- lastObj.setHeight(Math.max(100, dY));
1830
- } else lastObj = this.addCloud({
1831
- x: startX,
1832
- y: startY
1833
- }, dX, dY);
1834
- }
1835
- }));
1836
- stage.on("click tap", (e => {
1837
- if (!this._markupIsActive) return;
1838
- if (e.target === stage) {
1839
- if (this._markupMode === "Text") {
1840
- if (this._textInputRef && this._textInputRef.value) this.addText(this._textInputRef.value, this._textInputPos, this._textInputAngle); else if (transformer.nodes().length === 0) {
1841
- const pos = this.getRelativePointerPosition(stage);
1842
- this.createTextInput(pos, e.evt.pageX, e.evt.pageY, 0, null);
1843
- }
1844
- } else if (this._markupMode === "Image") {
1845
- if (this._imageInputRef && this._imageInputRef.value) this.addImage({
1846
- x: this._imageInputPos.x,
1847
- y: this._imageInputPos.y
1848
- }, this._imageInputRef.value, 0, 0, this._imageInputRef.value); else if (transformer.nodes().length === 0) {
1849
- const pos = this.getRelativePointerPosition(stage);
1850
- this.createImageInput(pos);
1851
- }
1852
- }
1853
- transformer.nodes([]);
1854
- return;
1855
- }
1856
- if (this._markupMode === "Text" || this._markupMode === "SelectMarkup") {
1857
- if (e.target.className === "Text" && transformer.nodes().length === 1 && transformer.nodes()[0] === e.target) {
1858
- if (this._textInputRef && this._textInputRef.value) this.addText(this._textInputRef.value, this._textInputPos, this._textInputAngle); else this.createTextInput({
1859
- x: e.target.attrs.x,
1860
- y: e.target.attrs.y
1861
- }, e.evt.pageX, e.evt.pageY, e.target.attrs.rotation, e.target.attrs.text);
1862
- return;
1863
- } else {
1864
- this.removeTextInput();
1865
- }
1866
- }
1867
- if (this._markupMode === "Image" || this._markupMode === "SelectMarkup") {
1868
- if (e.target.className === "Image" && transformer.nodes().length === 1 && transformer.nodes()[0] === e.target) {
1869
- if (this._imageInputRef && this._imageInputRef.value) this.addImage(this._imageInputPos, this._imageInputRef.value, 0, 0); else this.createImageInput({
1870
- x: e.target.attrs.x,
1871
- y: e.target.attrs.y
1872
- });
1873
- return;
1874
- } else {
1875
- this.removeImageInput();
1876
- }
1877
- }
1878
- if (transformer.nodes().filter((x => x.className === "Cloud" || x.className === "Image")).length > 0 || e.target.className === "Cloud" || e.target.className === "Image") {
1879
- transformer.rotateEnabled(false);
1880
- } else {
1881
- transformer.rotateEnabled(true);
1882
- }
1883
- const metaPressed = e.evt.shiftKey || e.evt.ctrlKey || e.evt.metaKey;
1884
- const isSelected = transformer.nodes().indexOf(e.target) >= 0;
1885
- if (!metaPressed && !isSelected) {
1886
- transformer.nodes([ e.target ]);
1887
- } else if (metaPressed && isSelected) {
1888
- const nodes = transformer.nodes().slice();
1889
- nodes.splice(nodes.indexOf(e.target), 1);
1890
- transformer.nodes(nodes);
1891
- } else if (metaPressed && !isSelected) {
1892
- const nodes = transformer.nodes().concat([ e.target ]);
1893
- transformer.nodes(nodes);
1894
- }
1895
- }));
1896
- const container = stage.container();
1897
- container.tabIndex = 1;
1898
- container.focus();
1899
- container.addEventListener("keydown", (e => {
1900
- if (!this._markupIsActive) return;
1901
- if (e.code === "Delete") {
1902
- this.getSelectedObjects().forEach((obj => obj.delete()));
1903
- this.clearSelected();
1904
- return;
1905
- }
1906
- e.preventDefault();
1907
- }));
1908
- }
1909
- destroyKonva() {
1910
- var _a;
1911
- this.removeTextInput();
1912
- this.removeImageInput();
1913
- this.clearOverlay();
1914
- (_a = this._konvaStage) === null || _a === undefined ? undefined : _a.destroy();
1915
- this._groupImages = undefined;
1916
- this._groupGeometry = undefined;
1917
- this._groupTexts = undefined;
1918
- this._konvaLayer = undefined;
1919
- this._konvaTransformer = undefined;
1920
- this._konvaStage = undefined;
1921
- }
1922
- getMarkupLines() {
1923
- const lines = [];
1924
- this.konvaLayerFind("Line").forEach((ref => {
1925
- const linePoints = ref.points();
1926
- if (!linePoints) return;
1927
- const worldPoints = [];
1928
- const absoluteTransform = ref.getAbsoluteTransform();
1929
- for (let i = 0; i < linePoints.length; i += 2) {
1930
- const atPoint = absoluteTransform.point({
1931
- x: linePoints[i],
1932
- y: linePoints[i + 1]
1933
- });
1934
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1935
- worldPoints.push(worldPoint);
1936
- }
1937
- const konvaLine = new KonvaLine(null, ref);
1938
- const line = {
1939
- id: konvaLine.id(),
1940
- points: worldPoints,
1941
- color: konvaLine.getColor() || "#ff0000",
1942
- type: konvaLine.getLineType() || this.lineType,
1943
- width: konvaLine.getLineWidth() || this.lineWidth
1944
- };
1945
- lines.push(line);
1946
- }));
1947
- return lines;
1948
- }
1949
- getMarkupTexts() {
1950
- const texts = [];
1951
- this.konvaLayerFind("Text").forEach((ref => {
1952
- const textSize = .02;
1953
- const textScale = this._worldTransformer.getScale();
1954
- const position = ref.position();
1955
- const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1956
- const atPoint = stageAbsoluteTransform.point({
1957
- x: position.x,
1958
- y: position.y
1959
- });
1960
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1961
- const shape = new KonvaText(null, ref);
1962
- const text = {
1963
- id: shape.id(),
1964
- position: worldPoint,
1965
- text: shape.getText(),
1966
- text_size: textSize * textScale.y,
1967
- angle: shape.getRotation(),
1968
- color: shape.getColor(),
1969
- font_size: shape.getFontSize() * stageAbsoluteTransform.getMatrix()[0]
1970
- };
1971
- texts.push(text);
1972
- }));
1973
- return texts;
1974
- }
1975
- getMarkupRectangles() {
1976
- const rectangles = [];
1977
- this.konvaLayerFind("Rectangle").forEach((ref => {
1978
- const position = ref.position();
1979
- const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1980
- const atPoint = stageAbsoluteTransform.point({
1981
- x: position.x,
1982
- y: position.y
1983
- });
1984
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1985
- const scale = stageAbsoluteTransform.getMatrix()[0];
1986
- const shape = new KonvaRectangle(null, ref);
1987
- const rectangle = {
1988
- id: shape.id(),
1989
- position: worldPoint,
1990
- width: shape.getWidth() * scale,
1991
- height: shape.getHeigth() * scale,
1992
- line_width: shape.getLineWidth(),
1993
- color: shape.getColor()
1994
- };
1995
- rectangles.push(rectangle);
1996
- }));
1997
- return rectangles;
1998
- }
1999
- getMarkupEllipses() {
2000
- const ellipses = [];
2001
- this.konvaLayerFind("Ellipse").forEach((ref => {
2002
- const position = ref.position();
2003
- const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
2004
- const atPoint = stageAbsoluteTransform.point({
2005
- x: position.x,
2006
- y: position.y
2007
- });
2008
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
2009
- const scale = stageAbsoluteTransform.getMatrix()[0];
2010
- const shape = new KonvaEllipse(null, ref);
2011
- const ellipse = {
2012
- id: shape.id(),
2013
- position: worldPoint,
2014
- radius: {
2015
- x: ref.getRadiusX() * scale,
2016
- y: ref.getRadiusY() * scale
2017
- },
2018
- line_width: shape.getLineWidth(),
2019
- color: shape.getColor()
2020
- };
2021
- ellipses.push(ellipse);
2022
- }));
2023
- return ellipses;
2024
- }
2025
- getMarkupArrows() {
2026
- const arrows = [];
2027
- this.konvaLayerFind("Arrow").forEach((ref => {
2028
- const absoluteTransform = ref.getAbsoluteTransform();
2029
- const atStartPoint = absoluteTransform.point({
2030
- x: ref.points()[0],
2031
- y: ref.points()[1]
2032
- });
2033
- const worldStartPoint = this._worldTransformer.screenToWorld(atStartPoint);
2034
- const atEndPoint = absoluteTransform.point({
2035
- x: ref.points()[2],
2036
- y: ref.points()[3]
2037
- });
2038
- const worldEndPoint = this._worldTransformer.screenToWorld(atEndPoint);
2039
- const shape = new KonvaArrow(null, ref);
2040
- const arrow = {
2041
- id: shape.id(),
2042
- start: worldStartPoint,
2043
- end: worldEndPoint,
2044
- color: shape.getColor()
2045
- };
2046
- arrows.push(arrow);
2047
- }));
2048
- return arrows;
2049
- }
2050
- getMarkupImages() {
2051
- const images = [];
2052
- this.konvaLayerFind("Image").forEach((ref => {
2053
- const position = ref.position();
2054
- const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
2055
- const atPoint = stageAbsoluteTransform.point({
2056
- x: position.x,
2057
- y: position.y
2058
- });
2059
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
2060
- const scale = stageAbsoluteTransform.getMatrix()[0];
2061
- const shape = new KonvaImage(null, ref);
2062
- const image = {
2063
- id: shape.id(),
2064
- position: worldPoint,
2065
- src: shape.getSrc(),
2066
- width: shape.getWidth() * scale,
2067
- height: shape.getHeight() * scale
2068
- };
2069
- images.push(image);
2070
- }));
2071
- return images;
2072
- }
2073
- getMarkupClouds() {
2074
- const clouds = [];
2075
- this.konvaLayerFind("Cloud").forEach((ref => {
2076
- const position = ref.position();
2077
- const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
2078
- const atPoint = stageAbsoluteTransform.point({
2079
- x: position.x,
2080
- y: position.y
2081
- });
2082
- const worldPoint = this._worldTransformer.screenToWorld(atPoint);
2083
- const scale = stageAbsoluteTransform.getMatrix()[0];
2084
- const shape = new KonvaCloud(null, ref);
2085
- const cloud = {
2086
- id: shape.id(),
2087
- position: worldPoint,
2088
- width: shape.getWidth() * scale,
2089
- height: shape.getHeigth() * scale,
2090
- line_width: shape.getLineWidth(),
2091
- color: shape.getColor()
2092
- };
2093
- clouds.push(cloud);
2094
- }));
2095
- return clouds;
2096
- }
2097
- combineMarkupWithDrawing() {
2098
- this.clearSelected();
2099
- const tempCanvas = document.createElement("canvas");
2100
- if (this._konvaStage) {
2101
- tempCanvas.width = this._konvaStage.width();
2102
- tempCanvas.height = this._konvaStage.height();
2103
- const ctx = tempCanvas.getContext("2d");
2104
- if (this._container instanceof HTMLCanvasElement) ctx.drawImage(this._container, 0, 0);
2105
- ctx.drawImage(this._konvaStage.toCanvas({
2106
- pixelRatio: window.devicePixelRatio
2107
- }), 0, 0);
2108
- }
2109
- return tempCanvas.toDataURL("image/jpeg", .25);
2110
- }
2111
- addLine(linePoints, color, type, width, id) {
2112
- if (!linePoints || linePoints.length === 0) return;
2113
- const points = [];
2114
- for (let i = 0; i < linePoints.length; i += 2) {
2115
- points.push({
2116
- x: linePoints[i],
2117
- y: linePoints[i + 1]
2118
- });
2119
- }
2120
- const konvaLine = new KonvaLine({
2121
- points: points,
2122
- type: type || this.lineType,
2123
- width: width || this.lineWidth,
2124
- color: color || this._markupColor.asHex(),
2125
- id: id
2126
- });
2127
- this.addObject(konvaLine);
2128
- return konvaLine;
2129
- }
2130
- createTextInput(pos, inputX, inputY, angle, text) {
2131
- if (!this._textInputRef) {
2132
- this._textInputPos = pos;
2133
- this._textInputAngle = angle;
2134
- this._textInputRef = document.createElement("textarea");
2135
- this._textInputRef.style.zIndex = "9999";
2136
- this._textInputRef.style.position = "absolute";
2137
- this._textInputRef.style.display = "block";
2138
- this._textInputRef.style.top = inputY + "px";
2139
- this._textInputRef.style.left = inputX + "px";
2140
- this._textInputRef.style.fontSize = `${this.fontSize}px`;
2141
- this._textInputRef.style.color = `${this._markupColor.asHex()}`;
2142
- this._textInputRef.style.fontFamily = "Calibri";
2143
- this._textInputRef.onkeydown = event => {
2144
- if (event.key === "Enter" && !event.shiftKey) {
2145
- event.preventDefault();
2146
- this.addText(this._textInputRef.value, this._textInputPos, this._textInputAngle);
2147
- }
2148
- if (event.key === "Escape") {
2149
- event.preventDefault();
2150
- this.removeTextInput();
2151
- }
2152
- };
2153
- if (text) this._textInputRef.value = text;
2154
- document.body.appendChild(this._textInputRef);
2155
- setTimeout((() => {
2156
- this._textInputRef.focus();
2157
- }), 50);
2158
- } else {
2159
- this.removeTextInput();
2160
- }
2161
- }
2162
- removeTextInput() {
2163
- var _a;
2164
- (_a = this._textInputRef) === null || _a === undefined ? undefined : _a.remove();
2165
- this._textInputRef = null;
2166
- this._textInputPos = null;
2167
- this._textInputAngle = 0;
2168
- }
2169
- createImageInput(pos) {
2170
- if (!this._imageInputRef) {
2171
- const convertBase64 = file => new Promise(((resolve, reject) => {
2172
- const fileReader = new FileReader;
2173
- fileReader.readAsDataURL(file);
2174
- fileReader.onload = () => {
2175
- resolve(fileReader.result);
2176
- };
2177
- fileReader.onerror = error => {
2178
- reject(error);
2179
- };
2180
- }));
2181
- this._imageInputPos = pos;
2182
- this._imageInputRef = document.createElement("input");
2183
- this._imageInputRef.style.display = "none";
2184
- this._imageInputRef.type = "file";
2185
- this._imageInputRef.accept = "image/png, image/jpeg";
2186
- this._imageInputRef.onchange = async event => {
2187
- const file = event.target.files[0];
2188
- const base64 = await convertBase64(file);
2189
- this.addImage({
2190
- x: this._imageInputPos.x,
2191
- y: this._imageInputPos.y
2192
- }, base64.toString(), 0, 0);
2193
- };
2194
- this._imageInputRef.oncancel = event => {
2195
- this.removeImageInput();
2196
- };
2197
- document.body.appendChild(this._imageInputRef);
2198
- setTimeout((() => {
2199
- this._imageInputRef.click();
2200
- }), 50);
2201
- } else {
2202
- this.removeImageInput();
2203
- }
2204
- }
2205
- removeImageInput() {
2206
- var _a;
2207
- (_a = this._imageInputRef) === null || _a === undefined ? undefined : _a.remove();
2208
- this._imageInputRef = null;
2209
- this._imageInputPos = null;
2210
- }
2211
- addText(text, position, angle, color, textSize, fontSize, id) {
2212
- var _a;
2213
- if (!text) return;
2214
- (_a = this.getSelectedObjects().at(0)) === null || _a === undefined ? undefined : _a.delete();
2215
- this.clearSelected();
2216
- this.removeTextInput();
2217
- const tolerance = 1e-6;
2218
- if (textSize && textSize > tolerance && (!fontSize || fontSize < tolerance)) {
2219
- const size = .02;
2220
- const scale = this._worldTransformer.getScale();
2221
- fontSize = textSize / (scale.y / size) / 34;
2222
- }
2223
- const konvaText = new KonvaText({
2224
- position: {
2225
- x: position.x,
2226
- y: position.y
2227
- },
2228
- text: text,
2229
- rotation: angle,
2230
- fontSize: fontSize || this.fontSize,
2231
- color: color || this._markupColor.asHex(),
2232
- id: id
2233
- });
2234
- this.addObject(konvaText);
2235
- return konvaText;
2236
- }
2237
- addRectangle(position, width, height, lineWidth, color, id) {
2238
- if (!position) return;
2239
- const konvaRectangle = new KonvaRectangle({
2240
- position: position,
2241
- width: width,
2242
- height: height,
2243
- lineWidth: lineWidth || this.lineWidth,
2244
- color: color || this._markupColor.asHex(),
2245
- id: id
2246
- });
2247
- this.addObject(konvaRectangle);
2248
- return konvaRectangle;
2249
- }
2250
- addEllipse(position, radius, lineWidth, color, id) {
2251
- if (!position) return;
2252
- const konvaEllipse = new KonvaEllipse({
2253
- position: position,
2254
- radius: radius,
2255
- lineWidth: lineWidth,
2256
- color: color || this._markupColor.asHex(),
2257
- id: id
2258
- });
2259
- this.addObject(konvaEllipse);
2260
- return konvaEllipse;
2261
- }
2262
- addArrow(start, end, color, id) {
2263
- if (!start || !end) return;
2264
- const konvaArrow = new KonvaArrow({
2265
- start: start,
2266
- end: end,
2267
- color: color || this._markupColor.asHex(),
2268
- id: id
2269
- });
2270
- this.addObject(konvaArrow);
2271
- return konvaArrow;
2272
- }
2273
- addCloud(position, width, height, lineWidth, color, id) {
2274
- if (!position || !width || !height) return;
2275
- const konvaCloud = new KonvaCloud({
2276
- position: position,
2277
- width: width,
2278
- height: height,
2279
- color: color || this._markupColor.asHex(),
2280
- lineWidth: lineWidth || this.lineWidth,
2281
- id: id
2282
- });
2283
- this.addObject(konvaCloud);
2284
- return konvaCloud;
2285
- }
2286
- addImage(position, src, width, height, id) {
2287
- var _a;
2288
- if (!position || !src) return;
2289
- (_a = this.getSelectedObjects().at(0)) === null || _a === undefined ? undefined : _a.delete();
2290
- this.clearSelected();
2291
- this.removeImageInput();
2292
- const konvaImage = new KonvaImage({
2293
- position: position,
2294
- src: src,
2295
- width: width,
2296
- height: height,
2297
- maxWidth: this._konvaStage.width() - position.x,
2298
- maxHeight: this._konvaStage.height() - position.y,
2299
- id: id
2300
- });
2301
- this.addObject(konvaImage);
2302
- return konvaImage;
2303
- }
2304
- }
9
+ import { EventEmitter2 } from "@inweb/eventemitter2";
2305
10
 
2306
11
  class OdaGeAction {
2307
12
  constructor(module) {
@@ -4716,45 +2421,6 @@ components.registerComponent("ZoomWheelComponent", (viewer => new ZoomWheelCompo
4716
2421
 
4717
2422
  components.registerComponent("GestureManagerComponent", (viewer => new GestureManagerComponent(viewer)));
4718
2423
 
4719
- class EventEmitter2 {
4720
- constructor() {
4721
- this._listeners = {};
4722
- }
4723
- addEventListener(type, listener) {
4724
- if (this._listeners[type] === undefined) this._listeners[type] = [];
4725
- this._listeners[type].push(listener);
4726
- return this;
4727
- }
4728
- removeEventListener(type, listener) {
4729
- if (this._listeners[type] === undefined) return this;
4730
- const listeners = this._listeners[type].filter((x => x !== listener));
4731
- if (listeners.length !== 0) this._listeners[type] = listeners; else delete this._listeners[type];
4732
- return this;
4733
- }
4734
- removeAllListeners(type) {
4735
- if (type) delete this._listeners[type]; else this._listeners = {};
4736
- return this;
4737
- }
4738
- emitEvent(event) {
4739
- if (this._listeners[event.type] === undefined) return false;
4740
- const invoke = this._listeners[event.type].slice();
4741
- invoke.forEach((listener => listener.call(this, event)));
4742
- return true;
4743
- }
4744
- on(type, listener) {
4745
- return this.addEventListener(type, listener);
4746
- }
4747
- off(type, listener) {
4748
- return this.removeEventListener(type, listener);
4749
- }
4750
- emit(type, ...args) {
4751
- if (typeof type === "string") return this.emitEvent({
4752
- type: type,
4753
- args: args
4754
- }); else if (typeof type === "object") return this.emitEvent(type); else return false;
4755
- }
4756
- }
4757
-
4758
2424
  function loadScript(url) {
4759
2425
  return new Promise(((resolve, reject) => {
4760
2426
  const script = document.createElement("script");
@@ -5531,7 +3197,7 @@ class MarkupFactory {
5531
3197
  let markup;
5532
3198
  switch (markupType) {
5533
3199
  case "Konva":
5534
- markup = new KonvaMarkup;
3200
+ markup = new Markup;
5535
3201
  break;
5536
3202
 
5537
3203
  case "Visualize":
@@ -6363,5 +4029,5 @@ class Viewer extends EventEmitter2 {
6363
4029
  }
6364
4030
  }
6365
4031
 
6366
- export { CANVAS_EVENTS, CanvasEvents, Component, Dragger, KonvaMarkup as Markup, OdBaseDragger, Options, Viewer, commands, commandsRegistry, components, componentsRegistry, defaultOptions, draggers, draggersRegistry };
4032
+ export { OdBaseDragger, Viewer, commands, components, draggers };
6367
4033
  //# sourceMappingURL=viewer-visualize.module.js.map