@inweb/markup 25.8.3 → 25.8.5

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.
package/dist/markup.js CHANGED
@@ -12419,11 +12419,10 @@
12419
12419
  */
12420
12420
  class KonvaMarkup {
12421
12421
  constructor() {
12422
- this._isInitialized = false;
12422
+ this._pointerEvents = [];
12423
12423
  this._markupIsActive = false;
12424
12424
  this._markupColor = new MarkupColor(255, 0, 0);
12425
12425
  this._zIndex = 1;
12426
- this._markupContainerName = "markupContainer";
12427
12426
  this.lineWidth = 4;
12428
12427
  this.lineType = "solid";
12429
12428
  this.fontSize = 34;
@@ -12440,12 +12439,13 @@
12440
12439
  this.enableEditMode(draggerName);
12441
12440
  };
12442
12441
  this.resizeContainer = (entries) => {
12443
- var _a, _b;
12444
12442
  const { width, height } = entries[0].contentRect;
12445
12443
  if (!width || !height)
12446
12444
  return; // <- invisible container, or container with parent removed
12447
- (_a = this._konvaStage) === null || _a === void 0 ? void 0 : _a.width(width);
12448
- (_b = this._konvaStage) === null || _b === void 0 ? void 0 : _b.height(height);
12445
+ if (!this._konvaStage)
12446
+ return;
12447
+ this._konvaStage.width(width);
12448
+ this._konvaStage.height(height);
12449
12449
  };
12450
12450
  this.pan = (event) => {
12451
12451
  const dX = event.dX / window.devicePixelRatio;
@@ -12458,14 +12458,16 @@
12458
12458
  };
12459
12459
  }
12460
12460
  initialize(container, pointerEvents, viewer, worldTransformer) {
12461
- if (!Konva)
12462
- 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>');
12461
+ if (!Konva) {
12462
+ console.error('Markup error: Konva is not initialized. Update node_modules or add to your page <script src="https://unpkg.com/konva@9/konva.min.js"></script>');
12463
+ return;
12464
+ }
12463
12465
  this._viewer = viewer;
12464
12466
  this._worldTransformer = worldTransformer !== null && worldTransformer !== void 0 ? worldTransformer : new WorldTransform();
12465
12467
  this._container = container;
12466
12468
  this._pointerEvents = pointerEvents !== null && pointerEvents !== void 0 ? pointerEvents : [];
12467
12469
  this._markupContainer = document.createElement("div");
12468
- this._markupContainer.id = this._markupContainerName;
12470
+ this._markupContainer.id = "markup-container";
12469
12471
  this._markupContainer.style.position = "absolute";
12470
12472
  this._markupContainer.style.top = "0px";
12471
12473
  this._markupContainer.style.left = "0px";
@@ -12482,33 +12484,30 @@
12482
12484
  this._viewer.addEventListener("changeactivedragger", this.changeActiveDragger);
12483
12485
  this._viewer.addEventListener("pan", this.pan);
12484
12486
  }
12485
- this._isInitialized = true;
12486
12487
  }
12487
12488
  dispose() {
12488
- if (!this._isInitialized)
12489
- return;
12489
+ var _a, _b;
12490
12490
  if (this._viewer) {
12491
12491
  this._viewer.removeEventListener("pan", this.pan);
12492
12492
  this._viewer.removeEventListener("changeactivedragger", this.changeActiveDragger);
12493
+ this._pointerEvents.forEach((x) => this._markupContainer.removeEventListener(x, this.redirectToViewer));
12493
12494
  }
12494
- this._pointerEvents.forEach((x) => this._markupContainer.removeEventListener(x, this.redirectToViewer));
12495
12495
  this.destroyKonva();
12496
- this._resizeObserver.disconnect();
12496
+ (_a = this._resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
12497
12497
  this._resizeObserver = undefined;
12498
- this._markupContainer.remove();
12498
+ (_b = this._markupContainer) === null || _b === void 0 ? void 0 : _b.remove();
12499
12499
  this._markupContainer = undefined;
12500
12500
  this._container = undefined;
12501
12501
  this._viewer = undefined;
12502
12502
  this._worldTransformer = undefined;
12503
12503
  this._markupIsActive = false;
12504
- this._isInitialized = false;
12505
12504
  }
12506
12505
  syncOverlay() { }
12507
12506
  clearOverlay() {
12508
12507
  this.removeTextInput();
12509
12508
  this.removeImageInput();
12510
12509
  this.clearSelected();
12511
- this.getObjects().forEach((obj) => obj.ref().destroy());
12510
+ this.getObjects().forEach((obj) => obj.delete());
12512
12511
  }
12513
12512
  getMarkupColor() {
12514
12513
  return this._markupColor.RGB;
@@ -12610,6 +12609,8 @@
12610
12609
  return objects;
12611
12610
  }
12612
12611
  getSelectedObjects() {
12612
+ if (!this._konvaTransformer)
12613
+ return [];
12613
12614
  return this._konvaTransformer
12614
12615
  .nodes()
12615
12616
  .map((ref) => {
@@ -12620,31 +12621,36 @@
12620
12621
  .filter((x) => x);
12621
12622
  }
12622
12623
  selectObjects(objects) {
12624
+ if (!this._konvaTransformer)
12625
+ return;
12623
12626
  const selectedObjs = this._konvaTransformer.nodes().concat(objects.map((x) => x.ref()));
12624
12627
  this._konvaTransformer.nodes(selectedObjs);
12625
12628
  }
12626
12629
  clearSelected() {
12627
- this._konvaTransformer.nodes([]);
12630
+ if (this._konvaTransformer)
12631
+ this._konvaTransformer.nodes([]);
12628
12632
  }
12629
12633
  addObject(object) {
12630
- this._konvaLayer.add(object.ref());
12634
+ if (this._konvaLayer)
12635
+ this._konvaLayer.add(object.ref());
12631
12636
  }
12632
12637
  konvaLayerFind(type) {
12638
+ if (!this._konvaLayer)
12639
+ return [];
12633
12640
  const konvaShape = MarkupMode2Konva[type];
12634
- if (konvaShape && konvaShape.initializer) {
12635
- // for "draggable" Konva uses Rectangles in Transformer. We need only Shapes from layer.
12636
- return this._konvaLayer.find(konvaShape.name).filter((ref) => ref.parent === this._konvaLayer);
12637
- }
12638
- return [];
12641
+ if (!konvaShape || !konvaShape.initializer)
12642
+ return [];
12643
+ // for "draggable" Konva uses Rectangles in Transformer. We need only Shapes from layer.
12644
+ return this._konvaLayer.find(konvaShape.name).filter((ref) => ref.parent === this._konvaLayer);
12639
12645
  }
12640
12646
  initializeKonva() {
12641
12647
  // first we need Konva core things: stage and layer
12642
- this._konvaStage = new Konva.Stage({
12643
- container: this._markupContainerName,
12648
+ const stage = new Konva.Stage({
12649
+ container: this._markupContainer,
12644
12650
  width: this._container.clientWidth,
12645
12651
  height: this._container.clientHeight,
12646
12652
  });
12647
- const stage = this._konvaStage;
12653
+ this._konvaStage = stage;
12648
12654
  const layer = new Konva.Layer({ pixelRation: window.devicePixelRatio });
12649
12655
  stage.add(layer);
12650
12656
  this._konvaLayer = layer;
@@ -12653,8 +12659,8 @@
12653
12659
  keepRatio: false,
12654
12660
  flipEnabled: false,
12655
12661
  });
12656
- this._konvaTransformer = transformer;
12657
12662
  layer.add(transformer);
12663
+ this._konvaTransformer = transformer;
12658
12664
  let isPaint = false;
12659
12665
  let lastLine;
12660
12666
  let mouseDownPos;
@@ -12840,20 +12846,17 @@
12840
12846
  if (!this._markupIsActive)
12841
12847
  return;
12842
12848
  if (e.code === "Delete") {
12843
- const trNodes = this._konvaTransformer.nodes();
12844
- if (trNodes.length > 0) {
12845
- this._konvaTransformer.nodes().forEach((x) => x.destroy());
12846
- this._konvaTransformer.nodes([]);
12847
- }
12848
- layer.draw();
12849
+ this.getSelectedObjects().forEach((obj) => obj.delete());
12850
+ this.clearSelected();
12849
12851
  return;
12850
12852
  }
12851
12853
  e.preventDefault();
12852
12854
  });
12853
12855
  }
12854
12856
  destroyKonva() {
12857
+ var _a;
12855
12858
  this.clearOverlay();
12856
- this._konvaStage.destroy();
12859
+ (_a = this._konvaStage) === null || _a === void 0 ? void 0 : _a.destroy();
12857
12860
  this._konvaLayer = undefined;
12858
12861
  this._konvaTransformer = undefined;
12859
12862
  this._konvaStage = undefined;
@@ -12887,9 +12890,9 @@
12887
12890
  }
12888
12891
  getMarkupTexts() {
12889
12892
  const texts = [];
12890
- const textSize = 0.02;
12891
- const textScale = this._worldTransformer.getScale();
12892
12893
  this.konvaLayerFind("Text").forEach((ref) => {
12894
+ const textSize = 0.02;
12895
+ const textScale = this._worldTransformer.getScale();
12893
12896
  const position = { x: ref.x(), y: ref.y() };
12894
12897
  const worldPoint = this._worldTransformer.screenToWorld(position);
12895
12898
  const shape = new KonvaText(null, ref);
@@ -12999,12 +13002,14 @@
12999
13002
  combineMarkupWithDrawing() {
13000
13003
  this.clearSelected();
13001
13004
  const tempCanvas = document.createElement("canvas");
13002
- tempCanvas.width = this._konvaStage.width();
13003
- tempCanvas.height = this._konvaStage.height();
13004
- const ctx = tempCanvas.getContext("2d");
13005
- if (this._container instanceof HTMLCanvasElement)
13006
- ctx.drawImage(this._container, 0, 0);
13007
- ctx.drawImage(this._konvaStage.toCanvas({ pixelRatio: window.devicePixelRatio }), 0, 0);
13005
+ if (this._konvaStage) {
13006
+ tempCanvas.width = this._konvaStage.width();
13007
+ tempCanvas.height = this._konvaStage.height();
13008
+ const ctx = tempCanvas.getContext("2d");
13009
+ if (this._container instanceof HTMLCanvasElement)
13010
+ ctx.drawImage(this._container, 0, 0);
13011
+ ctx.drawImage(this._konvaStage.toCanvas({ pixelRatio: window.devicePixelRatio }), 0, 0);
13012
+ }
13008
13013
  return tempCanvas.toDataURL("image/jpeg", 0.25);
13009
13014
  }
13010
13015
  addLine(linePoints, color, type, width, id) {
@@ -13021,7 +13026,7 @@
13021
13026
  color: color || this._markupColor.HexColor,
13022
13027
  id,
13023
13028
  });
13024
- this._konvaLayer.add(konvaLine.ref());
13029
+ this.addObject(konvaLine);
13025
13030
  return konvaLine;
13026
13031
  }
13027
13032
  createTextInput(pos, inputX, inputY, angle, text) {
@@ -13104,15 +13109,13 @@
13104
13109
  this._imageInputRef = null;
13105
13110
  this._imageInputPos = null;
13106
13111
  }
13107
- addText(specifiedText, position, angle, color, textSize, fontSize, id) {
13108
- if (!specifiedText)
13112
+ addText(text, position, angle, color, textSize, fontSize, id) {
13113
+ var _a;
13114
+ if (!text)
13109
13115
  return;
13110
- const trNodes = this._konvaTransformer.nodes();
13111
- if (trNodes.length > 0) {
13112
- // in case of edit - remove old Konva.Text object
13113
- trNodes[0].destroy();
13114
- this._konvaTransformer.nodes([]);
13115
- }
13116
+ // in case of edit - remove old Konva.Text object
13117
+ (_a = this.getSelectedObjects().at(0)) === null || _a === void 0 ? void 0 : _a.delete();
13118
+ this.clearSelected();
13116
13119
  this.removeTextInput();
13117
13120
  // in case we have old viewpoint without font_size
13118
13121
  const tolerance = 1.0e-6;
@@ -13123,13 +13126,13 @@
13123
13126
  }
13124
13127
  const konvaText = new KonvaText({
13125
13128
  position: { x: position.x, y: position.y },
13126
- text: specifiedText,
13129
+ text,
13127
13130
  rotation: angle,
13128
13131
  fontSize: fontSize || this.fontSize,
13129
13132
  color: color || this._markupColor.HexColor,
13130
13133
  id,
13131
13134
  });
13132
- this._konvaLayer.add(konvaText.ref());
13135
+ this.addObject(konvaText);
13133
13136
  return konvaText;
13134
13137
  }
13135
13138
  addRectangle(position, width, height, lineWidth, color, id) {
@@ -13143,7 +13146,7 @@
13143
13146
  color: color || this._markupColor.HexColor,
13144
13147
  id,
13145
13148
  });
13146
- this._konvaLayer.add(konvaRectangle.ref());
13149
+ this.addObject(konvaRectangle);
13147
13150
  return konvaRectangle;
13148
13151
  }
13149
13152
  addEllipse(position, radius, lineWidth, color, id) {
@@ -13156,7 +13159,7 @@
13156
13159
  color: color || this._markupColor.HexColor,
13157
13160
  id,
13158
13161
  });
13159
- this._konvaLayer.add(konvaEllipse.ref());
13162
+ this.addObject(konvaEllipse);
13160
13163
  return konvaEllipse;
13161
13164
  }
13162
13165
  addArrow(start, end, color, id) {
@@ -13168,7 +13171,7 @@
13168
13171
  color: color || this._markupColor.HexColor,
13169
13172
  id,
13170
13173
  });
13171
- this._konvaLayer.add(konvaArrow.ref());
13174
+ this.addObject(konvaArrow);
13172
13175
  return konvaArrow;
13173
13176
  }
13174
13177
  addCloud(position, width, height, lineWidth, color, id) {
@@ -13182,18 +13185,16 @@
13182
13185
  lineWidth: lineWidth || this.lineWidth,
13183
13186
  id,
13184
13187
  });
13185
- this._konvaLayer.add(konvaCloud.ref());
13188
+ this.addObject(konvaCloud);
13186
13189
  return konvaCloud;
13187
13190
  }
13188
13191
  addImage(position, src, width, height, id) {
13192
+ var _a;
13189
13193
  if (!position || !src)
13190
13194
  return;
13191
- const trNodes = this._konvaTransformer.nodes();
13192
- if (trNodes.length > 0) {
13193
- // in case of edit - remove old Image placeholder object
13194
- trNodes[0].destroy();
13195
- this._konvaTransformer.nodes([]);
13196
- }
13195
+ // in case of edit - remove old Image placeholder object
13196
+ (_a = this.getSelectedObjects().at(0)) === null || _a === void 0 ? void 0 : _a.delete();
13197
+ this.clearSelected();
13197
13198
  this.removeImageInput();
13198
13199
  const konvaImage = new KonvaImage({
13199
13200
  position,
@@ -13202,7 +13203,7 @@
13202
13203
  height,
13203
13204
  id,
13204
13205
  });
13205
- this._konvaLayer.add(konvaImage.ref());
13206
+ this.addObject(konvaImage);
13206
13207
  return konvaImage;
13207
13208
  }
13208
13209
  }