@inweb/markup 25.7.10 → 25.8.1

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.
@@ -29,6 +29,29 @@ class MarkupColor {
29
29
  }
30
30
  }
31
31
 
32
+ class WorldTransform {
33
+ screenToWorld(position) {
34
+ return {
35
+ x: position.x,
36
+ y: position.y,
37
+ z: 0
38
+ };
39
+ }
40
+ worldToScreen(position) {
41
+ return {
42
+ x: position.x,
43
+ y: position.y
44
+ };
45
+ }
46
+ getScale() {
47
+ return {
48
+ x: 1,
49
+ y: 1,
50
+ z: 1
51
+ };
52
+ }
53
+ }
54
+
32
55
  const LineTypeSpecs = new Map([ [ "solid", [] ], [ "dot", [ 30, 30, .001, 30 ] ], [ "dash", [ 30, 30 ] ] ]);
33
56
 
34
57
  class KonvaLine {
@@ -866,31 +889,37 @@ const MarkupMode2Konva = {
866
889
  },
867
890
  Line: {
868
891
  name: "Line",
869
- initializer: ref => new KonvaLine(null, ref)
892
+ initializer: (ref, params = null) => new KonvaLine(params, ref),
893
+ zIndex: 1
870
894
  },
871
895
  Text: {
872
896
  name: "Text",
873
- initializer: ref => new KonvaText(null, ref)
897
+ initializer: (ref, params = null) => new KonvaText(params, ref)
874
898
  },
875
899
  Rectangle: {
876
900
  name: "Rect",
877
- initializer: ref => new KonvaRectangle(null, ref)
901
+ initializer: (ref, params = null) => new KonvaRectangle(params, ref),
902
+ zIndex: 1
878
903
  },
879
904
  Ellipse: {
880
905
  name: "Ellipse",
881
- initializer: ref => new KonvaEllipse(null, ref)
906
+ initializer: (ref, params = null) => new KonvaEllipse(params, ref),
907
+ zIndex: 1
882
908
  },
883
909
  Arrow: {
884
910
  name: "Arrow",
885
- initializer: ref => new KonvaArrow(null, ref)
911
+ initializer: (ref, params = null) => new KonvaArrow(params, ref),
912
+ zIndex: 1
886
913
  },
887
914
  Image: {
888
915
  name: "Image",
889
- initializer: ref => new KonvaImage(null, ref)
916
+ initializer: (ref, params = null) => new KonvaImage(params, ref),
917
+ zIndex: 0
890
918
  },
891
919
  Cloud: {
892
920
  name: "Cloud",
893
- initializer: ref => new KonvaCloud(null, ref)
921
+ initializer: ref => new KonvaCloud(null, ref),
922
+ zIndex: 1
894
923
  }
895
924
  };
896
925
 
@@ -921,10 +950,10 @@ class KonvaMarkup {
921
950
  this.pan = event => {
922
951
  const dX = event.dX / window.devicePixelRatio;
923
952
  const dY = event.dY / window.devicePixelRatio;
924
- Object.keys(MarkupMode2Konva).forEach((mode => this.konvaLayerFind(mode).forEach((ref => ref.move({
953
+ this.getObjects().forEach((obj => obj.ref().move({
925
954
  x: dX,
926
955
  y: dY
927
- })))));
956
+ })));
928
957
  };
929
958
  this.redirectToViewer = event => {
930
959
  if (this._viewer) this._viewer.emit(event);
@@ -933,7 +962,7 @@ class KonvaMarkup {
933
962
  initialize(container, pointerEvents, viewer, worldTransformer) {
934
963
  if (!Konva) throw new Error('Markup: Error during initialization. Konva is not initialized. Update node_modules or add to your page <script src="https://unpkg.com/konva@9/konva.min.js"><\/script>');
935
964
  this._viewer = viewer;
936
- this._worldTransformer = worldTransformer;
965
+ this._worldTransformer = worldTransformer !== null && worldTransformer !== void 0 ? worldTransformer : new WorldTransform;
937
966
  this._container = container;
938
967
  this._pointerEvents = pointerEvents !== null && pointerEvents !== void 0 ? pointerEvents : [];
939
968
  this._markupContainer = document.createElement("div");
@@ -977,8 +1006,8 @@ class KonvaMarkup {
977
1006
  clearOverlay() {
978
1007
  this.removeTextInput();
979
1008
  this.removeImageInput();
980
- this._konvaTransformer.nodes([]);
981
- Object.keys(MarkupMode2Konva).forEach((mode => this.konvaLayerFind(mode).forEach((ref => ref.destroy()))));
1009
+ this.clearSelected();
1010
+ this.getObjects().forEach((obj => obj.ref().destroy()));
982
1011
  }
983
1012
  getMarkupColor() {
984
1013
  return this._markupColor.RGB;
@@ -986,23 +1015,19 @@ class KonvaMarkup {
986
1015
  setMarkupColor(r, g, b) {
987
1016
  this._markupColor.setColor(r, g, b);
988
1017
  }
989
- colorizeAllMarkup(r = 255, g = 0, b = 0) {
990
- const hex = new MarkupColor(r, g, b).HexColor;
991
- Object.keys(MarkupMode2Konva).forEach((mode => {
992
- this.konvaLayerFind(mode).forEach((ref => {
993
- const konvaShape = MarkupMode2Konva[mode];
994
- const konvaObj = konvaShape.initializer(ref);
995
- if (konvaObj.setColor) konvaObj.setColor(hex);
996
- }));
1018
+ colorizeAllMarkup(r, g, b) {
1019
+ const hexColor = new MarkupColor(r, g, b).HexColor;
1020
+ this.getObjects().forEach((obj => {
1021
+ const colorable = obj;
1022
+ if (colorable && colorable.setColor) colorable.setColor(hexColor);
997
1023
  }));
998
1024
  this._konvaLayer.draw();
999
1025
  }
1000
1026
  colorizeSelectedMarkups(r, g, b) {
1027
+ const hexColor = new MarkupColor(r, g, b).HexColor;
1001
1028
  this.getSelectedObjects().forEach((obj => {
1002
1029
  const colorable = obj;
1003
- if (colorable && colorable.setColor) {
1004
- colorable.setColor(new MarkupColor(r, g, b).HexColor);
1005
- }
1030
+ if (colorable && colorable.setColor) colorable.setColor(hexColor);
1006
1031
  }));
1007
1032
  }
1008
1033
  setViewpoint(viewpoint) {
@@ -1045,110 +1070,29 @@ class KonvaMarkup {
1045
1070
  return this;
1046
1071
  }
1047
1072
  createObject(type, params) {
1048
- let object = null;
1049
- let zIndex = this._zIndex;
1050
- switch (type.toLocaleLowerCase()) {
1051
- case "line":
1052
- object = new KonvaLine(params);
1053
- zIndex = 1;
1054
- break;
1055
-
1056
- case "text":
1057
- object = new KonvaText(params);
1058
- break;
1059
-
1060
- case "rectangle":
1061
- object = new KonvaRectangle(params);
1062
- zIndex = 1;
1063
- break;
1064
-
1065
- case "ellipse":
1066
- object = new KonvaEllipse(params);
1067
- zIndex = 1;
1068
- break;
1069
-
1070
- case "arrow":
1071
- object = new KonvaArrow(params);
1072
- break;
1073
-
1074
- case "image":
1075
- object = new KonvaImage(params);
1076
- zIndex = 0;
1077
- break;
1078
-
1079
- case "cloud":
1080
- object = new KonvaCloud(params);
1081
- zIndex = 1;
1082
- break;
1083
-
1084
- default:
1085
- throw new Error("Markup CreateObject - unsupported type has been detected.");
1086
- }
1073
+ var _a;
1074
+ const konvaShape = MarkupMode2Konva[type];
1075
+ if (!konvaShape || !konvaShape.initializer) throw new Error(`Markup CreateObject - unsupported markup type ${type}`);
1076
+ const object = konvaShape.initializer(null, params);
1087
1077
  this.addObject(object);
1088
- object.setZIndex(zIndex);
1078
+ object.setZIndex((_a = konvaShape.zIndex) !== null && _a !== void 0 ? _a : this._zIndex);
1089
1079
  this._zIndex++;
1090
1080
  return object;
1091
1081
  }
1092
1082
  getObjects() {
1093
1083
  const objects = [];
1094
- this.konvaLayerFind("Line").forEach((line => {
1095
- objects.push(new KonvaLine(null, line));
1096
- }));
1097
- this.konvaLayerFind("Text").forEach((text => {
1098
- objects.push(new KonvaText(null, text));
1099
- }));
1100
- this.konvaLayerFind("Rectangle").forEach((rectangle => {
1101
- objects.push(new KonvaRectangle(null, rectangle));
1102
- }));
1103
- this.konvaLayerFind("Ellipse").forEach((ellipse => {
1104
- objects.push(new KonvaEllipse(null, ellipse));
1105
- }));
1106
- this.konvaLayerFind("Arrow").forEach((arrow => {
1107
- objects.push(new KonvaArrow(null, arrow));
1108
- }));
1109
- this.konvaLayerFind("Image").forEach((image => {
1110
- objects.push(new KonvaImage(null, image));
1111
- }));
1112
- this.konvaLayerFind("Cloud").forEach((cloud => {
1113
- objects.push(new KonvaCloud(null, cloud));
1084
+ Object.keys(MarkupMode2Konva).forEach((type => {
1085
+ const konvaShape = MarkupMode2Konva[type];
1086
+ this.konvaLayerFind(type).forEach((ref => objects.push(konvaShape.initializer(ref))));
1114
1087
  }));
1115
1088
  return objects;
1116
1089
  }
1117
1090
  getSelectedObjects() {
1118
- const objects = [];
1119
- this._konvaTransformer.nodes().forEach((obj => {
1120
- const konvaShapeName = obj.className;
1121
- switch (konvaShapeName) {
1122
- case "Line":
1123
- objects.push(new KonvaLine(null, obj));
1124
- break;
1125
-
1126
- case "Text":
1127
- objects.push(new KonvaText(null, obj));
1128
- break;
1129
-
1130
- case "Rect":
1131
- objects.push(new KonvaRectangle(null, obj));
1132
- break;
1133
-
1134
- case "Ellipse":
1135
- objects.push(new KonvaEllipse(null, obj));
1136
- break;
1137
-
1138
- case "Arrow":
1139
- objects.push(new KonvaArrow(null, obj));
1140
- break;
1141
-
1142
- case "Image":
1143
- objects.push(new KonvaImage(null, obj));
1144
- break;
1145
-
1146
- case "Cloud":
1147
- objects.push(new KonvaCloud(null, obj));
1148
- break;
1149
- }
1150
- }));
1151
- return objects;
1091
+ return this._konvaTransformer.nodes().map((ref => {
1092
+ const name = ref.className;
1093
+ const konvaShape = Object.values(MarkupMode2Konva).find((shape => shape.name === name));
1094
+ return konvaShape ? konvaShape.initializer(ref) : null;
1095
+ })).filter((x => x));
1152
1096
  }
1153
1097
  selectObjects(objects) {
1154
1098
  const selectedObjs = this._konvaTransformer.nodes().concat(objects.map((x => x.ref())));
@@ -1157,13 +1101,6 @@ class KonvaMarkup {
1157
1101
  clearSelected() {
1158
1102
  this._konvaTransformer.nodes([]);
1159
1103
  }
1160
- getPoint3dFromArray(array) {
1161
- return {
1162
- x: array[0],
1163
- y: array[1],
1164
- z: array[2]
1165
- };
1166
- }
1167
1104
  fillViewpointShapes(viewpoint) {
1168
1105
  const markupLines = this.getMarkupLines();
1169
1106
  if (markupLines && markupLines.length > 0) {
@@ -1211,8 +1148,8 @@ class KonvaMarkup {
1211
1148
  addObject(object) {
1212
1149
  this._konvaLayer.add(object.ref());
1213
1150
  }
1214
- konvaLayerFind(markupShape) {
1215
- const konvaShape = MarkupMode2Konva[markupShape];
1151
+ konvaLayerFind(type) {
1152
+ const konvaShape = MarkupMode2Konva[type];
1216
1153
  if (konvaShape && konvaShape.initializer) {
1217
1154
  return this._konvaLayer.find(konvaShape.name).filter((ref => ref.parent === this._konvaLayer));
1218
1155
  }
@@ -1454,7 +1391,7 @@ class KonvaMarkup {
1454
1391
  const konvaLine = new KonvaLine(null, line);
1455
1392
  lines.push({
1456
1393
  id: konvaLine.id(),
1457
- points: worldPoints.map((p => this.getPoint3dFromArray(p))),
1394
+ points: worldPoints,
1458
1395
  color: konvaLine.getColor() || "#ff0000",
1459
1396
  type: konvaLine.getLineType() || this.lineType,
1460
1397
  width: konvaLine.getLineWidth() || this.lineWidth
@@ -1468,14 +1405,15 @@ class KonvaMarkup {
1468
1405
  const textScale = this._worldTransformer.getScale();
1469
1406
  this.konvaLayerFind("Text").forEach((text => {
1470
1407
  if (!text) return;
1471
- const position = this._worldTransformer.screenToWorld({
1408
+ const position = {
1472
1409
  x: text.x(),
1473
1410
  y: text.y()
1474
- });
1411
+ };
1412
+ const worldPoint = this._worldTransformer.screenToWorld(position);
1475
1413
  const shape = new KonvaText(null, text);
1476
1414
  texts.push({
1477
1415
  id: shape.id(),
1478
- position: this.getPoint3dFromArray(position),
1416
+ position: worldPoint,
1479
1417
  text: shape.getText(),
1480
1418
  text_size: textSize * textScale.y,
1481
1419
  angle: shape.getRotation(),
@@ -1493,7 +1431,7 @@ class KonvaMarkup {
1493
1431
  const shape = new KonvaRectangle(null, rect);
1494
1432
  rectangles.push({
1495
1433
  id: shape.id(),
1496
- position: this.getPoint3dFromArray(worldPoint),
1434
+ position: worldPoint,
1497
1435
  width: shape.getWidth(),
1498
1436
  height: shape.getHeigth(),
1499
1437
  line_width: shape.getLineWidth(),
@@ -1510,7 +1448,7 @@ class KonvaMarkup {
1510
1448
  const shape = new KonvaEllipse(null, ellipse);
1511
1449
  ellipses.push({
1512
1450
  id: shape.id(),
1513
- position: this.getPoint3dFromArray(worldPoint),
1451
+ position: worldPoint,
1514
1452
  radius: {
1515
1453
  x: ellipse.getRadiusX(),
1516
1454
  y: ellipse.getRadiusY()
@@ -1538,8 +1476,8 @@ class KonvaMarkup {
1538
1476
  const shape = new KonvaArrow(null, arrow);
1539
1477
  arrows.push({
1540
1478
  id: shape.id(),
1541
- start: this.getPoint3dFromArray(worldStartPoint),
1542
- end: this.getPoint3dFromArray(worldEndPoint),
1479
+ start: worldStartPoint,
1480
+ end: worldEndPoint,
1543
1481
  color: shape.getColor()
1544
1482
  });
1545
1483
  }));
@@ -1553,7 +1491,7 @@ class KonvaMarkup {
1553
1491
  const shape = new KonvaImage(null, image);
1554
1492
  images.push({
1555
1493
  id: shape.id(),
1556
- position: this.getPoint3dFromArray(worldPoint),
1494
+ position: worldPoint,
1557
1495
  src: shape.getSrc(),
1558
1496
  width: shape.getWidth(),
1559
1497
  height: shape.getHeight()
@@ -1569,7 +1507,7 @@ class KonvaMarkup {
1569
1507
  const shape = new KonvaCloud(null, cloud);
1570
1508
  clouds.push({
1571
1509
  id: shape.id(),
1572
- position: this.getPoint3dFromArray(worldPoint),
1510
+ position: worldPoint,
1573
1511
  width: shape.getWidth(),
1574
1512
  height: shape.getHeigth(),
1575
1513
  line_width: shape.getLineWidth(),