@inditextech/weave-sdk 3.2.0-SNAPSHOT.97.1 → 3.2.0

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/types.js CHANGED
@@ -17834,8 +17834,7 @@ function getTargetAndSkipNodes(instance, e, forceTransformer = false) {
17834
17834
  }
17835
17835
  if (e.type === "transform" && nodesSelectionPlugin) {
17836
17836
  node = e.target;
17837
- skipNodes.push(node.getAttrs().id ?? "");
17838
- skipNodes.push(...nodesSelectionPlugin.getTransformer().nodes().map((node$1) => node$1.getAttrs().id ?? ""));
17837
+ skipNodes.push(node.getAttrs().id ?? "", ...nodesSelectionPlugin.getTransformer().nodes().map((node$1) => node$1.getAttrs().id ?? ""));
17839
17838
  }
17840
17839
  return {
17841
17840
  targetNode: forceTransformer ? nodesSelectionPlugin.getTransformer() : node,
@@ -20148,10 +20147,10 @@ const augmentKonvaNodeClass = (config) => {
20148
20147
  Konva.Node.prototype.dblClick = function() {};
20149
20148
  };
20150
20149
  var WeaveNode = class {
20151
- register(instance) {
20150
+ async register(instance) {
20152
20151
  this.instance = instance;
20153
20152
  this.logger = this.instance.getChildLogger(this.getNodeType());
20154
- this.onRegister();
20153
+ await this.onRegister();
20155
20154
  this.instance.getChildLogger(`node-${this.getNodeType()}`).debug(`Node with type [${this.getNodeType()}] registered`);
20156
20155
  return this;
20157
20156
  }
@@ -20723,7 +20722,9 @@ var WeaveNode = class {
20723
20722
  }
20724
20723
  };
20725
20724
  }
20726
- onRegister() {}
20725
+ async onRegister() {
20726
+ return;
20727
+ }
20727
20728
  onAdd(nodeInstance) {}
20728
20729
  onDestroy(nodeInstance) {
20729
20730
  nodeInstance.destroy();
@@ -21922,19 +21923,19 @@ var WeaveRegisterManager = class {
21922
21923
  plugin.register(this.instance);
21923
21924
  this.plugins[pluginName] = plugin;
21924
21925
  }
21925
- registerNodesHandlers() {
21926
+ async registerNodesHandlers() {
21926
21927
  const config = this.instance.getConfiguration();
21927
- if (config.nodes) for (const node of config.nodes) this.registerNodeHandler(node);
21928
+ if (config.nodes) for (const node of config.nodes) await this.registerNodeHandler(node);
21928
21929
  this.logger.info(`Nodes handlers registered`);
21929
21930
  }
21930
- registerNodeHandler(node) {
21931
+ async registerNodeHandler(node) {
21931
21932
  const nodeType = node.getNodeType();
21932
21933
  if (this.nodesHandlers[nodeType]) {
21933
21934
  const msg = `Node handler with type [${nodeType}] already exists`;
21934
21935
  this.logger.error(msg);
21935
21936
  throw new Error(msg);
21936
21937
  }
21937
- node.register(this.instance);
21938
+ await node.register(this.instance);
21938
21939
  this.nodesHandlers[nodeType] = node;
21939
21940
  }
21940
21941
  registerActionsHandlers() {
@@ -21966,7 +21967,7 @@ var WeaveRegisterManager = class {
21966
21967
 
21967
21968
  //#endregion
21968
21969
  //#region package.json
21969
- var version = "3.2.0-SNAPSHOT.97.1";
21970
+ var version = "3.2.0";
21970
21971
 
21971
21972
  //#endregion
21972
21973
  //#region src/managers/setup.ts
@@ -22939,7 +22940,7 @@ var Weave = class {
22939
22940
  this.emitEvent("onRoomLoaded", false);
22940
22941
  this.status = WEAVE_INSTANCE_STATUS.STARTING;
22941
22942
  this.emitEvent("onInstanceStatus", this.status);
22942
- this.registerManager.registerNodesHandlers();
22943
+ await this.registerManager.registerNodesHandlers();
22943
22944
  this.augmentKonvaStageClass();
22944
22945
  this.augmentKonvaNodeClass();
22945
22946
  this.registerManager.registerPlugins();
@@ -23605,7 +23606,7 @@ function loadImageSource(image, options) {
23605
23606
  const imageSource = Konva.Util.createImageElement();
23606
23607
  imageSource.crossOrigin = crossOrigin;
23607
23608
  imageSource.onerror = () => {
23608
- reject();
23609
+ reject(new Error("Failed to load image source"));
23609
23610
  };
23610
23611
  imageSource.onload = async () => {
23611
23612
  resolve(imageSource);
@@ -25132,7 +25133,10 @@ const WEAVE_IMAGE_CROP_ANCHOR_POSITION = {
25132
25133
  };
25133
25134
  const WEAVE_IMAGE_DEFAULT_CONFIG = {
25134
25135
  performance: { cache: { enabled: false } },
25135
- style: { placeholder: { fill: "#aaaaaa" } },
25136
+ style: {
25137
+ placeholder: { fill: "#aaaaaa" },
25138
+ cursor: { loading: "wait" }
25139
+ },
25136
25140
  imageLoading: {
25137
25141
  maxRetryAttempts: 15,
25138
25142
  retryDelayMs: 2e3
@@ -25703,6 +25707,57 @@ var WeaveImageCrop = class WeaveImageCrop {
25703
25707
  }
25704
25708
  };
25705
25709
 
25710
+ //#endregion
25711
+ //#region src/nodes/image/utils.ts
25712
+ const extractCursorUrl = (cursor, fallback) => {
25713
+ const lower = cursor.toLowerCase();
25714
+ const start = lower.indexOf("url(");
25715
+ if (start === -1) return {
25716
+ preload: false,
25717
+ cursor
25718
+ };
25719
+ let i = start + 4;
25720
+ const len = cursor.length;
25721
+ while (i < len && /\s/.test(cursor[i])) i++;
25722
+ let quote = null;
25723
+ if (cursor[i] === "\"" || cursor[i] === "'") {
25724
+ quote = cursor[i];
25725
+ i++;
25726
+ }
25727
+ let buf = "";
25728
+ for (; i < len; i++) {
25729
+ const ch = cursor[i];
25730
+ if (quote) {
25731
+ if (ch === quote) {
25732
+ i++;
25733
+ break;
25734
+ }
25735
+ buf += ch;
25736
+ } else {
25737
+ if (ch === ")") break;
25738
+ buf += ch;
25739
+ }
25740
+ }
25741
+ const url = buf.trim();
25742
+ if (!url) return {
25743
+ preload: false,
25744
+ cursor: fallback
25745
+ };
25746
+ if (!isAllowedUrl(url)) return {
25747
+ preload: false,
25748
+ cursor: fallback
25749
+ };
25750
+ return {
25751
+ preload: true,
25752
+ cursor: url
25753
+ };
25754
+ };
25755
+ const isAllowedUrl = (value) => {
25756
+ if (/^https?:\/\//i.test(value)) return true;
25757
+ if (/^(javascript|data|blob|ftp):/i.test(value)) return false;
25758
+ return true;
25759
+ };
25760
+
25706
25761
  //#endregion
25707
25762
  //#region src/nodes/image/image.ts
25708
25763
  var WeaveImageNode = class extends WeaveNode {
@@ -25730,10 +25785,41 @@ var WeaveImageNode = class extends WeaveNode {
25730
25785
  this.imageTryoutAttempts = {};
25731
25786
  this.imageFallback = {};
25732
25787
  }
25788
+ preloadCursors() {
25789
+ const promiseHandler = (src) => new Promise((resolveInt, rejectInt) => {
25790
+ const img = Konva.Util.createImageElement();
25791
+ img.onload = () => {
25792
+ resolveInt();
25793
+ };
25794
+ img.onerror = () => {
25795
+ rejectInt(new Error(`Failed to load cursor image: ${src}`));
25796
+ };
25797
+ img.src = src;
25798
+ });
25799
+ return new Promise((resolve) => {
25800
+ (async () => {
25801
+ const cursors = Object.keys(this.config.style.cursor);
25802
+ const cursorUrls = [];
25803
+ const cursorFallback = { loading: "wait" };
25804
+ for (const cursorKey of cursors) {
25805
+ const cursorValue = this.config.style.cursor[cursorKey];
25806
+ const { preload, cursor } = extractCursorUrl(cursorValue, cursorFallback[cursorKey]);
25807
+ if (preload) cursorUrls.push({ src: cursor });
25808
+ }
25809
+ if (cursorUrls.length > 0) {
25810
+ const cursorsPreloading = [];
25811
+ for (const { src } of cursorUrls) cursorsPreloading.push(promiseHandler(src));
25812
+ await Promise.allSettled(cursorsPreloading);
25813
+ }
25814
+ resolve();
25815
+ })();
25816
+ });
25817
+ }
25733
25818
  getConfiguration() {
25734
25819
  return this.config;
25735
25820
  }
25736
- onRegister() {
25821
+ async onRegister() {
25822
+ await this.preloadCursors();
25737
25823
  this.logger.info(`image caching enabled: ${this.config.performance.cache.enabled}`);
25738
25824
  }
25739
25825
  triggerCrop(imageNode, options) {
@@ -25810,7 +25896,7 @@ var WeaveImageNode = class extends WeaveNode {
25810
25896
  });
25811
25897
  this.setupDefaultNodeAugmentation(image);
25812
25898
  image.defineMousePointer = () => {
25813
- if (this.imageState[id]?.status === "loading") return "wait";
25899
+ if (this.imageState[id]?.status === "loading") return this.config.style.cursor.loading;
25814
25900
  const selectedNodes = this.getSelectionPlugin()?.getSelectedNodes() ?? [];
25815
25901
  if (this.isSelecting() && selectedNodes.includes(image)) return "grab";
25816
25902
  return "pointer";
@@ -26278,6 +26364,8 @@ var WeaveImageNode = class extends WeaveNode {
26278
26364
  onError(error);
26279
26365
  };
26280
26366
  this.imageSource[imageId].onload = async () => {
26367
+ const stage = this.instance.getStage();
26368
+ stage.container().style.cursor = "pointer";
26281
26369
  this.imageState[imageId] = {
26282
26370
  status: "loaded",
26283
26371
  loaded: true,
@@ -26356,15 +26444,11 @@ var WeaveImageNode = class extends WeaveNode {
26356
26444
  width: imageRect.width,
26357
26445
  height: imageRect.height
26358
26446
  });
26359
- const stage = this.instance.getStage();
26360
- if (!loadFallback) {
26361
- if (!this.instance.isServerSide() && stage.container().style.cursor === "wait") stage.container().style.cursor = "pointer";
26362
- this.imageState[id] = {
26363
- status: "loaded",
26364
- loaded: true,
26365
- error: false
26366
- };
26367
- }
26447
+ if (!loadFallback) this.imageState[id] = {
26448
+ status: "loaded",
26449
+ loaded: true,
26450
+ error: false
26451
+ };
26368
26452
  this.updateImageCrop(image);
26369
26453
  this.resolveAsyncElement(id);
26370
26454
  this.cacheNode(image);
@@ -26410,12 +26494,6 @@ var WeaveImageNode = class extends WeaveNode {
26410
26494
  updatePlaceholderSize(image, imagePlaceholder) {
26411
26495
  const imageAttrs = image.getAttrs();
26412
26496
  if (!this.imageState[imageAttrs.id ?? ""]?.loaded) return;
26413
- if (!imageAttrs.adding && imageAttrs.cropInfo) {
26414
- const actualScale = imageAttrs.uncroppedImage.width / imageAttrs.imageInfo.width;
26415
- const cropScale = imageAttrs.cropInfo ? imageAttrs.cropInfo.scaleX : actualScale;
26416
- imagePlaceholder.width(imageAttrs.cropSize.width * (actualScale / cropScale));
26417
- imagePlaceholder.height(imageAttrs.cropSize.height * (actualScale / cropScale));
26418
- }
26419
26497
  if (!imageAttrs.adding && !imageAttrs.cropInfo) {
26420
26498
  imagePlaceholder.width(imageAttrs.uncroppedImage.width);
26421
26499
  imagePlaceholder.height(imageAttrs.uncroppedImage.height);
@@ -28898,7 +28976,7 @@ var WeaveVideoNode = class extends WeaveNode {
28898
28976
  videoIconGroup.x(videoWidth - videoIconGroupWidth - this.config.style.icon.external.paddingX);
28899
28977
  videoIconGroup.y(videoHeight - videoIconGroupHeight - this.config.style.icon.external.paddingY);
28900
28978
  const nodesSelectionPlugin = this.getNodeSelectionPlugin();
28901
- if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer().forceUpdate();
28979
+ if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer()?.forceUpdate();
28902
28980
  this.resolveAsyncElement(id);
28903
28981
  };
28904
28982
  }
@@ -29291,7 +29369,7 @@ var WeaveVideoNode = class extends WeaveNode {
29291
29369
  videoIconGroup.x(videoWidth - videoIconGroupWidth - this.config.style.icon.external.paddingX);
29292
29370
  videoIconGroup.y(videoHeight - videoIconGroupHeight - this.config.style.icon.external.paddingY);
29293
29371
  const nodesSelectionPlugin = this.getNodeSelectionPlugin();
29294
- if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer().forceUpdate();
29372
+ if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer()?.forceUpdate();
29295
29373
  }
29296
29374
  getNodeSelectionPlugin() {
29297
29375
  const nodesSelectionPlugin = this.instance.getPlugin("nodesSelection");
@@ -31908,7 +31986,7 @@ var WeaveZoomOutToolAction = class extends WeaveAction {
31908
31986
  }
31909
31987
  cleanup() {
31910
31988
  const stage = this.instance.getStage();
31911
- this.instance.triggerAction(this.previousAction);
31989
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31912
31990
  stage.container().style.cursor = "default";
31913
31991
  }
31914
31992
  };
@@ -31942,7 +32020,7 @@ var WeaveZoomInToolAction = class extends WeaveAction {
31942
32020
  }
31943
32021
  cleanup() {
31944
32022
  const stage = this.instance.getStage();
31945
- this.instance.triggerAction(this.previousAction);
32023
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31946
32024
  stage.container().style.cursor = "default";
31947
32025
  }
31948
32026
  };
@@ -31975,7 +32053,7 @@ var WeaveFitToScreenToolAction = class extends WeaveAction {
31975
32053
  }
31976
32054
  cleanup() {
31977
32055
  const stage = this.instance.getStage();
31978
- this.instance.triggerAction(this.previousAction);
32056
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31979
32057
  stage.container().style.cursor = "default";
31980
32058
  }
31981
32059
  };
@@ -32017,7 +32095,7 @@ var WeaveFitToSelectionToolAction = class extends WeaveAction {
32017
32095
  }
32018
32096
  cleanup() {
32019
32097
  const stage = this.instance.getStage();
32020
- if (this.previousAction) this.instance.triggerAction(this.previousAction);
32098
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
32021
32099
  stage.container().style.cursor = "default";
32022
32100
  }
32023
32101
  };
@@ -33629,12 +33707,9 @@ const WEAVE_IMAGE_TOOL_CONFIG_DEFAULT = { style: { cursor: {
33629
33707
  var WeaveImageToolAction = class extends WeaveAction {
33630
33708
  initialized = false;
33631
33709
  initialCursor = null;
33632
- imageFile = null;
33633
- imageURL = null;
33634
- forceMainContainer = false;
33710
+ imageAction = {};
33635
33711
  ignoreKeyboardEvents = false;
33636
33712
  ignorePointerEvents = false;
33637
- uploadType = null;
33638
33713
  onPropsChange = void 0;
33639
33714
  update = void 0;
33640
33715
  constructor(params) {
@@ -33642,14 +33717,10 @@ var WeaveImageToolAction = class extends WeaveAction {
33642
33717
  this.config = mergeExceptArrays(WEAVE_IMAGE_TOOL_CONFIG_DEFAULT, params?.config ?? {});
33643
33718
  this.pointers = new Map();
33644
33719
  this.initialized = false;
33645
- this.state = WEAVE_IMAGE_TOOL_STATE.IDLE;
33646
33720
  this.imageId = null;
33721
+ this.state = WEAVE_IMAGE_TOOL_STATE.IDLE;
33647
33722
  this.tempImageId = null;
33648
33723
  this.tempImageNode = null;
33649
- this.container = void 0;
33650
- this.imageURL = null;
33651
- this.uploadType = null;
33652
- this.clickPoint = null;
33653
33724
  }
33654
33725
  getName() {
33655
33726
  return WEAVE_IMAGE_TOOL_ACTION_NAME;
@@ -33721,66 +33792,54 @@ var WeaveImageToolAction = class extends WeaveAction {
33721
33792
  stage.on("pointerup", (e) => {
33722
33793
  if (this.ignorePointerEvents) return;
33723
33794
  this.pointers.delete(e.evt.pointerId);
33724
- if (this.state === WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION) this.handleAdding();
33795
+ if (this.state === WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION) this.handleAdding(this.imageId ?? "");
33725
33796
  });
33726
33797
  this.initialized = true;
33727
33798
  }
33728
33799
  setState(state) {
33729
33800
  this.state = state;
33730
33801
  }
33731
- async loadImage(params) {
33802
+ async loadImage(nodeId, params) {
33732
33803
  this.setCursor();
33733
33804
  this.setFocusStage();
33734
- if (!this.imageId) {
33735
- this.cancelAction();
33736
- return;
33737
- }
33738
33805
  const imageNodeHandler = this.getImageNodeHandler();
33739
33806
  if (!imageNodeHandler) return;
33740
- const actualImageId = this.imageId;
33741
33807
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33742
33808
  const image = params.image;
33743
33809
  const realImageSize = await getImageSizeFromFile(image.file);
33744
33810
  const downscaledImage = await downscaleImageFile(image.file, image.downscaleRatio);
33745
- const reader = new FileReader();
33746
- reader.onloadend = () => {
33747
- imageNodeHandler.preloadFallbackImage(actualImageId, reader.result, {
33748
- onLoad: () => {
33749
- this.props = {
33750
- ...this.props,
33751
- imageFallback: reader.result,
33752
- imageURL: void 0,
33753
- width: realImageSize.width,
33754
- height: realImageSize.height
33755
- };
33756
- this.addImageNode(params?.position);
33757
- },
33758
- onError: () => {
33759
- this.cancelAction();
33760
- }
33761
- });
33762
- };
33763
- reader.onerror = () => {};
33764
- reader.readAsDataURL(downscaledImage);
33811
+ try {
33812
+ const dataURL = await this.getDataURL(downscaledImage);
33813
+ this.imageAction[nodeId].props = {
33814
+ ...this.imageAction[nodeId].props,
33815
+ imageFallback: dataURL,
33816
+ imageURL: void 0,
33817
+ width: realImageSize.width,
33818
+ height: realImageSize.height
33819
+ };
33820
+ this.addImageNode(nodeId, params?.position);
33821
+ } catch {
33822
+ this.cancelAction();
33823
+ }
33765
33824
  }
33766
33825
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL) {
33767
33826
  const image = params.image;
33768
33827
  setTimeout(() => {
33769
- this.saveImageUrl(actualImageId, image.url);
33828
+ this.saveImageUrl(nodeId, image.url);
33770
33829
  }, 0);
33771
- this.addImageNode(params?.position);
33830
+ this.addImageNode(nodeId, params?.position);
33772
33831
  }
33773
33832
  }
33774
33833
  isTouchDevice() {
33775
33834
  return window.matchMedia("(pointer: coarse)").matches;
33776
33835
  }
33777
- addImageNode(position) {
33836
+ async addImageNode(nodeId, position) {
33778
33837
  const stage = this.instance.getStage();
33779
33838
  this.setCursor();
33780
33839
  this.setFocusStage();
33781
33840
  if (position) {
33782
33841
  this.setState(WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION);
33783
- this.handleAdding(position);
33842
+ this.handleAdding(nodeId, position);
33784
33843
  return;
33785
33844
  }
33786
33845
  const imageNodeHandler = this.getImageNodeHandler();
@@ -33788,11 +33847,15 @@ var WeaveImageToolAction = class extends WeaveAction {
33788
33847
  this.cancelAction();
33789
33848
  return;
33790
33849
  }
33791
- if (this.imageId) {
33850
+ if (this.imageAction[nodeId]) {
33851
+ const { uploadType } = this.imageAction[nodeId];
33792
33852
  const mousePos = stage.getRelativePointerPosition();
33793
33853
  this.tempImageId = v4_default();
33794
- let imageSource = imageNodeHandler.getImageSource(this.imageId);
33795
- if (this.uploadType === "file") imageSource = imageNodeHandler.getFallbackImageSource(this.imageId);
33854
+ let imageSource = imageNodeHandler.getImageSource(nodeId);
33855
+ if (uploadType === "file") {
33856
+ imageSource = imageNodeHandler.getFallbackImageSource(nodeId);
33857
+ imageSource ??= await this.loadImageDataURL(this.imageAction[nodeId].props.imageFallback);
33858
+ }
33796
33859
  if (!imageSource) {
33797
33860
  this.cancelAction();
33798
33861
  return;
@@ -33826,37 +33889,41 @@ var WeaveImageToolAction = class extends WeaveAction {
33826
33889
  });
33827
33890
  this.instance.getMainLayer()?.add(this.tempImageNode);
33828
33891
  }
33829
- this.instance.emitEvent("onAddingImage", { imageURL: this.props.imageURL });
33892
+ this.instance.emitEvent("onAddingImage");
33893
+ this.imageAction[nodeId].clickPoint = null;
33830
33894
  }
33831
- this.clickPoint = null;
33832
33895
  this.setState(WEAVE_IMAGE_TOOL_STATE.DEFINING_POSITION);
33833
33896
  }
33834
- handleAdding(position) {
33897
+ async handleAdding(nodeId, position) {
33835
33898
  const imageNodeHandler = this.getImageNodeHandler();
33836
33899
  if (!imageNodeHandler) {
33837
33900
  this.cancelAction();
33838
33901
  return;
33839
33902
  }
33840
- if (this.imageId) {
33841
- let imageSource = imageNodeHandler.getImageSource(this.imageId);
33842
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) imageSource = imageNodeHandler.getFallbackImageSource(this.imageId);
33903
+ if (this.imageAction[nodeId]) {
33904
+ const { uploadType, imageURL, forceMainContainer } = this.imageAction[nodeId];
33905
+ let imageSource = imageNodeHandler.getImageSource(nodeId);
33906
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33907
+ imageSource = imageNodeHandler.getFallbackImageSource(nodeId);
33908
+ imageSource ??= await this.loadImageDataURL(this.imageAction[nodeId].props.imageFallback);
33909
+ }
33843
33910
  if (!imageSource && !position) {
33844
33911
  this.cancelAction();
33845
33912
  return;
33846
33913
  }
33847
33914
  const { mousePoint, container } = this.instance.getMousePointer(position);
33848
- this.clickPoint = mousePoint;
33849
- this.container = container;
33915
+ this.imageAction[nodeId].clickPoint = mousePoint;
33916
+ this.imageAction[nodeId].container = container;
33850
33917
  const nodeHandler = this.instance.getNodeHandler("image");
33851
- const imageWidth = this.props.width ? this.props.width : imageSource?.width;
33852
- const imageHeight = this.props.height ? this.props.height : imageSource?.height;
33918
+ const imageWidth = this.imageAction[nodeId].props.width ? this.imageAction[nodeId].props.width : imageSource?.width;
33919
+ const imageHeight = this.imageAction[nodeId].props.height ? this.imageAction[nodeId].props.height : imageSource?.height;
33853
33920
  let realImageURL = void 0;
33854
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imageURL) realImageURL = this.imageURL?.url;
33921
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && imageURL) realImageURL = imageURL?.url;
33855
33922
  if (nodeHandler) {
33856
- const node = nodeHandler.create(this.imageId, {
33857
- ...this.props,
33858
- x: this.clickPoint?.x ?? 0,
33859
- y: this.clickPoint?.y ?? 0,
33923
+ const node = nodeHandler.create(nodeId, {
33924
+ ...this.imageAction[nodeId].props,
33925
+ x: this.imageAction[nodeId].clickPoint.x,
33926
+ y: this.imageAction[nodeId].clickPoint.y,
33860
33927
  opacity: 1,
33861
33928
  adding: false,
33862
33929
  imageURL: realImageURL,
@@ -33874,23 +33941,24 @@ var WeaveImageToolAction = class extends WeaveAction {
33874
33941
  height: imageHeight
33875
33942
  }
33876
33943
  });
33877
- this.instance.addNode(node, this.forceMainContainer ? this.instance.getMainLayer()?.getAttrs().id : this.container?.getAttrs().id);
33878
- this.instance.emitEvent("onAddedImage", { nodeId: this.imageId });
33879
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33880
- const uploadImageFunctionInternal = async () => {
33881
- const nodeId = this.imageId ?? "";
33944
+ this.instance.addNode(node, forceMainContainer ? this.instance.getMainLayer()?.getAttrs().id : this.imageAction[nodeId].container?.getAttrs().id);
33945
+ this.instance.emitEvent("onAddedImage", { nodeId });
33946
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33947
+ const uploadImageFunctionInternal = async (imageActionData) => {
33948
+ const { uploadImageFunction, imageFile } = imageActionData;
33882
33949
  try {
33883
- const imageURL = await this.uploadImageFunction(this.imageFile.file);
33884
- this.saveImageUrl(nodeId, imageURL);
33950
+ const imageURL$1 = await uploadImageFunction?.(imageFile.file);
33951
+ if (!imageURL$1) return;
33952
+ this.saveImageUrl(nodeId, imageURL$1);
33885
33953
  this.instance.emitEvent("onImageUploaded", {
33886
- imageURL,
33954
+ imageURL: imageURL$1,
33887
33955
  nodeId
33888
33956
  });
33889
33957
  } catch (error) {
33890
33958
  this.instance.emitEvent("onImageUploadedError", { error });
33891
33959
  }
33892
33960
  };
33893
- uploadImageFunctionInternal();
33961
+ uploadImageFunctionInternal(this.imageAction[nodeId]);
33894
33962
  }
33895
33963
  }
33896
33964
  this.setState(WEAVE_IMAGE_TOOL_STATE.FINISHED);
@@ -33905,42 +33973,51 @@ var WeaveImageToolAction = class extends WeaveAction {
33905
33973
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
33906
33974
  this.ignorePointerEvents = false;
33907
33975
  this.ignoreKeyboardEvents = false;
33908
- this.forceMainContainer = params?.forceMainContainer ?? false;
33909
- this.imageFile = null;
33910
- this.imageURL = null;
33911
- this.imageId = v4_default();
33912
- this.props = this.initProps();
33913
- if (params?.imageId) this.updateProps({ imageId: params.imageId });
33976
+ const nodeId = params?.nodeId ?? v4_default();
33977
+ this.imageId = nodeId;
33978
+ this.imageAction[nodeId] = {
33979
+ props: this.initProps(),
33980
+ imageId: nodeId,
33981
+ clickPoint: null,
33982
+ container: void 0,
33983
+ imageFile: null,
33984
+ imageURL: null,
33985
+ forceMainContainer: params?.forceMainContainer ?? false,
33986
+ uploadType: null,
33987
+ uploadImageFunction: null
33988
+ };
33989
+ if (params?.imageId) this.imageAction[nodeId].imageId = params.imageId;
33914
33990
  if (this.forceExecution) {
33915
33991
  this.ignorePointerEvents = true;
33916
33992
  this.ignoreKeyboardEvents = true;
33917
33993
  }
33918
33994
  if (params?.position) this.setState(WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION);
33919
33995
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE && params.image) {
33920
- this.uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE;
33921
- this.imageFile = params.image;
33922
- this.uploadImageFunction = params.uploadImageFunction;
33923
- this.loadImage({
33996
+ this.imageAction[nodeId].uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE;
33997
+ this.imageAction[nodeId].imageFile = params.image;
33998
+ this.imageAction[nodeId].uploadImageFunction = params.uploadImageFunction;
33999
+ this.loadImage(nodeId, {
33924
34000
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
33925
34001
  image: params.image,
33926
34002
  position: params?.position
33927
34003
  });
33928
34004
  }
33929
34005
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && params.image) {
33930
- this.uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL;
33931
- this.imageURL = params.image;
33932
- this.updateProps({
34006
+ this.imageAction[nodeId].uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL;
34007
+ this.imageAction[nodeId].imageURL = params.image;
34008
+ this.imageAction[nodeId].props = {
34009
+ ...this.imageAction[nodeId].props,
33933
34010
  imageFallback: params.image.fallback,
33934
34011
  width: params.image.width,
33935
34012
  height: params.image.height
33936
- });
33937
- this.loadImage({
34013
+ };
34014
+ this.loadImage(nodeId, {
33938
34015
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL,
33939
34016
  image: params.image,
33940
34017
  position: params?.position
33941
34018
  });
33942
34019
  }
33943
- return { nodeId: this.imageId };
34020
+ return { nodeId };
33944
34021
  }
33945
34022
  saveImageUrl(nodeId, imageURL) {
33946
34023
  const stage = this.instance.getStage();
@@ -33966,11 +34043,7 @@ var WeaveImageToolAction = class extends WeaveAction {
33966
34043
  this.instance.endDrag(WEAVE_IMAGE_TOOL_ACTION_NAME);
33967
34044
  }
33968
34045
  this.initialCursor = null;
33969
- this.forceMainContainer = false;
33970
- this.container = void 0;
33971
34046
  this.tempImageNode = null;
33972
- this.imageURL = null;
33973
- this.clickPoint = null;
33974
34047
  this.setState(WEAVE_IMAGE_TOOL_STATE.IDLE);
33975
34048
  }
33976
34049
  getImageNodeHandler() {
@@ -33993,6 +34066,30 @@ var WeaveImageToolAction = class extends WeaveAction {
33993
34066
  getActualState() {
33994
34067
  return this.state;
33995
34068
  }
34069
+ loadImageDataURL(imageDataURL) {
34070
+ return new Promise((resolve, reject) => {
34071
+ const imageEle = Konva.Util.createImageElement();
34072
+ imageEle.onerror = (error) => {
34073
+ reject(error);
34074
+ };
34075
+ imageEle.onload = async () => {
34076
+ resolve(imageEle);
34077
+ };
34078
+ imageEle.src = imageDataURL;
34079
+ });
34080
+ }
34081
+ getDataURL(blob) {
34082
+ return new Promise((resolve, reject) => {
34083
+ const reader = new FileReader();
34084
+ reader.onloadend = () => {
34085
+ resolve(reader.result);
34086
+ };
34087
+ reader.onerror = () => {
34088
+ reject(new Error("Failed to generate dataURL from file"));
34089
+ };
34090
+ reader.readAsDataURL(blob);
34091
+ });
34092
+ }
33996
34093
  };
33997
34094
 
33998
34095
  //#endregion
@@ -34041,18 +34138,13 @@ const WEAVE_IMAGES_TOOL_DEFAULT_CONFIG = {
34041
34138
  layout: { columns: 4 }
34042
34139
  };
34043
34140
 
34044
- //#endregion
34045
- //#region src/internal-utils/generic.ts
34046
- function sleep(ms) {
34047
- return new Promise((resolve) => setTimeout(resolve, ms));
34048
- }
34049
-
34050
34141
  //#endregion
34051
34142
  //#region src/actions/images-tool/images-tool.ts
34052
34143
  var WeaveImagesToolAction = class extends WeaveAction {
34053
34144
  initialized = false;
34054
34145
  initialCursor = null;
34055
34146
  nodesIds = [];
34147
+ toAdd = 0;
34056
34148
  imagesFile = [];
34057
34149
  imagesURL = [];
34058
34150
  forceMainContainer = false;
@@ -34140,7 +34232,10 @@ var WeaveImagesToolAction = class extends WeaveAction {
34140
34232
  });
34141
34233
  stage.on("pointerup", (e) => {
34142
34234
  this.pointers.delete(e.evt.pointerId);
34143
- if (this.state === WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION) this.handleAdding();
34235
+ if (this.state === WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION) {
34236
+ this.instance.emitEvent("onSelectedPositionImages");
34237
+ this.handleAdding();
34238
+ }
34144
34239
  });
34145
34240
  this.initialized = true;
34146
34241
  }
@@ -34180,9 +34275,9 @@ var WeaveImagesToolAction = class extends WeaveAction {
34180
34275
  const shadowBlur = this.config.style.cursor.imageThumbnails.shadowBlur;
34181
34276
  const shadowOffset = this.config.style.cursor.imageThumbnails.shadowOffset;
34182
34277
  const shadowOpacity = this.config.style.cursor.imageThumbnails.shadowOpacity;
34183
- const aspectRatio = imageSource.width / imageSource.height || 1;
34184
- const imageWidth = maxImageWidth * aspectRatio * (1 / stage.scaleX());
34185
- const imageHeight = maxImageHeight * (1 / stage.scaleY());
34278
+ const scale = Math.min(maxImageWidth / imageSource.width, maxImageHeight / imageSource.height);
34279
+ const imageWidth = imageSource.width * scale * (1 / stage.scaleX());
34280
+ const imageHeight = imageSource.height * scale * (1 / stage.scaleY());
34186
34281
  const imageNode = new Konva.Image({
34187
34282
  x: position$1.x,
34188
34283
  y: position$1.y,
@@ -34200,7 +34295,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34200
34295
  shadowOffset,
34201
34296
  shadowOpacity
34202
34297
  });
34203
- maxWidth = position$1.x + imageWidth;
34298
+ maxWidth = Math.max(maxWidth, position$1.x + imageWidth);
34204
34299
  maxHeight = Math.max(maxHeight, position$1.y + imageHeight);
34205
34300
  position$1 = {
34206
34301
  x: position$1.x + imageThumbnailsPadding / stage.scaleX(),
@@ -34269,6 +34364,18 @@ var WeaveImagesToolAction = class extends WeaveAction {
34269
34364
  let imagePositionX = originPoint.x;
34270
34365
  let imagePositionY = originPoint.y;
34271
34366
  let maxHeight = 0;
34367
+ this.nodesIds = [];
34368
+ const images = [];
34369
+ const checkAddedImages = ({ nodeId }) => {
34370
+ if (this.nodesIds.includes(nodeId)) this.handleImageAdded();
34371
+ if (this.getImagesAdded() <= 0) {
34372
+ this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34373
+ this.cancelAction();
34374
+ this.instance.removeEventListener("onAddedImage", checkAddedImages);
34375
+ this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34376
+ }
34377
+ };
34378
+ this.instance.addEventListener("onAddedImage", checkAddedImages);
34272
34379
  if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE && this.imagesFile) {
34273
34380
  const imagesToUpload = this.imagesFile.length;
34274
34381
  let imagesUploaded = 0;
@@ -34282,23 +34389,35 @@ var WeaveImagesToolAction = class extends WeaveAction {
34282
34389
  };
34283
34390
  this.instance.addEventListener("onImageUploaded", handleUploadImage);
34284
34391
  for (let i = 0; i < this.imagesFile.length; i++) {
34285
- const imageFile = this.imagesFile[i];
34286
- const { imageId, width, height,...restImageFile } = imageFile;
34287
- const uploadImageFunctionInternal = async () => {
34288
- const imageURL = await this.uploadImageFunction(imageFile.file);
34289
- return imageURL;
34392
+ const nodeId = v4_default();
34393
+ const image = this.imagesFile[i];
34394
+ const { width, height } = image;
34395
+ const handleImage = async (nodeId$1, imageFile, position$1) => {
34396
+ const { imageId } = imageFile;
34397
+ const uploadImageFunctionInternal = async () => {
34398
+ const imageURL = await this.uploadImageFunction(imageFile.file);
34399
+ return imageURL;
34400
+ };
34401
+ this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34402
+ type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
34403
+ image: {
34404
+ file: imageFile.file,
34405
+ downscaleRatio: imageFile.downscaleRatio
34406
+ },
34407
+ uploadImageFunction: uploadImageFunctionInternal,
34408
+ ...imageId && { imageId },
34409
+ position: {
34410
+ x: position$1.x,
34411
+ y: position$1.y
34412
+ },
34413
+ forceMainContainer: this.forceMainContainer,
34414
+ nodeId: nodeId$1
34415
+ }, true);
34290
34416
  };
34291
- const { nodeId } = this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34292
- type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
34293
- image: restImageFile,
34294
- uploadImageFunction: uploadImageFunctionInternal,
34295
- ...imageId && { imageId },
34296
- position: {
34297
- x: imagePositionX,
34298
- y: imagePositionY
34299
- },
34300
- forceMainContainer: this.forceMainContainer
34301
- }, true);
34417
+ images.push(handleImage(nodeId, image, {
34418
+ x: imagePositionX,
34419
+ y: imagePositionY
34420
+ }));
34302
34421
  this.nodesIds.push(nodeId);
34303
34422
  maxHeight = Math.max(maxHeight, height);
34304
34423
  imagePositionX += imagesPadding + width;
@@ -34307,40 +34426,51 @@ var WeaveImagesToolAction = class extends WeaveAction {
34307
34426
  imagePositionY = imagePositionY + maxHeight + imagesPadding;
34308
34427
  maxHeight = 0;
34309
34428
  }
34310
- while (imageToolActionHandler.getActualState() !== WEAVE_IMAGES_TOOL_STATE.IDLE) await sleep(10);
34311
34429
  }
34312
- this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34313
34430
  }
34314
- if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imagesURL) {
34315
- for (let i = 0; i < this.imagesURL.length; i++) {
34316
- const imageURL = this.imagesURL[i];
34317
- const { imageId, options,...restImageURL } = imageURL;
34318
- const imageURLElement = restImageURL;
34319
- const { nodeId } = this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34431
+ if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imagesURL) for (let i = 0; i < this.imagesURL.length; i++) {
34432
+ const nodeId = v4_default();
34433
+ const image = this.imagesURL[i];
34434
+ const handleImage = async (nodeId$1, imageURL, position$1) => {
34435
+ const { imageId, options } = imageURL;
34436
+ this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34320
34437
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL,
34321
- image: imageURLElement,
34438
+ image: {
34439
+ url: imageURL.url,
34440
+ fallback: imageURL.fallback,
34441
+ width: imageURL.width,
34442
+ height: imageURL.height
34443
+ },
34322
34444
  ...imageId && { imageId },
34323
34445
  ...options && { options },
34324
34446
  position: {
34325
- x: imagePositionX,
34326
- y: imagePositionY
34447
+ x: position$1.x,
34448
+ y: position$1.y
34327
34449
  },
34328
- forceMainContainer: this.forceMainContainer
34450
+ forceMainContainer: this.forceMainContainer,
34451
+ nodeId: nodeId$1
34329
34452
  }, true);
34330
- this.nodesIds.push(nodeId);
34331
- maxHeight = Math.max(maxHeight, imageURL.height);
34332
- imagePositionX += imagesPadding + imageURL.width;
34333
- if ((i + 1) % layoutColumns === 0) {
34334
- imagePositionX = originPoint.x;
34335
- imagePositionY = imagePositionY + maxHeight + imagesPadding;
34336
- maxHeight = 0;
34337
- }
34338
- while (imageToolActionHandler.getActualState() !== WEAVE_IMAGES_TOOL_STATE.IDLE) await sleep(10);
34339
- }
34340
- this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34453
+ };
34454
+ images.push(handleImage(nodeId, image, {
34455
+ x: imagePositionX,
34456
+ y: imagePositionY
34457
+ }));
34458
+ this.nodesIds.push(nodeId);
34459
+ maxHeight = Math.max(maxHeight, image.height);
34460
+ imagePositionX += imagesPadding + image.width;
34461
+ if ((i + 1) % layoutColumns === 0) {
34462
+ imagePositionX = originPoint.x;
34463
+ imagePositionY = imagePositionY + maxHeight + imagesPadding;
34464
+ maxHeight = 0;
34465
+ }
34466
+ }
34467
+ if (this.nodesIds.length > 0) {
34468
+ this.toAdd = this.nodesIds.length;
34469
+ await Promise.allSettled(images);
34470
+ } else {
34471
+ this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34472
+ this.cancelAction();
34341
34473
  }
34342
- this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34343
- this.cancelAction();
34344
34474
  }
34345
34475
  trigger(cancelAction, params) {
34346
34476
  if (!this.instance) throw new Error("Instance not defined");
@@ -34349,6 +34479,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34349
34479
  const selectionPlugin = this.instance.getPlugin("nodesSelection");
34350
34480
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
34351
34481
  if (params?.position) this.setState(WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION);
34482
+ this.nodesIds = [];
34352
34483
  this.forceMainContainer = params.forceMainContainer ?? false;
34353
34484
  if (params.type === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE) {
34354
34485
  this.uploadType = WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE;
@@ -34396,6 +34527,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34396
34527
  selectionPlugin.setSelectedNodes(addedNodes);
34397
34528
  this.instance.triggerAction(SELECTION_TOOL_ACTION_NAME);
34398
34529
  }
34530
+ this.instance.emitEvent("onFinishedImages");
34399
34531
  this.instance.endDrag(WEAVE_IMAGES_TOOL_ACTION_NAME);
34400
34532
  stage.container().style.cursor = "default";
34401
34533
  this.uploadType = null;
@@ -34403,6 +34535,8 @@ var WeaveImagesToolAction = class extends WeaveAction {
34403
34535
  this.initialCursor = null;
34404
34536
  this.container = void 0;
34405
34537
  this.clickPoint = null;
34538
+ this.nodesIds = [];
34539
+ this.toAdd = 0;
34406
34540
  this.setState(WEAVE_IMAGES_TOOL_STATE.IDLE);
34407
34541
  }
34408
34542
  setCursor() {
@@ -34418,6 +34552,12 @@ var WeaveImagesToolAction = class extends WeaveAction {
34418
34552
  isTouchDevice() {
34419
34553
  return window.matchMedia("(pointer: coarse)").matches;
34420
34554
  }
34555
+ getImagesAdded() {
34556
+ return this.toAdd;
34557
+ }
34558
+ handleImageAdded() {
34559
+ this.toAdd = this.toAdd - 1;
34560
+ }
34421
34561
  setDragAndDropProperties(properties) {
34422
34562
  this.instance.startDrag(WEAVE_IMAGES_TOOL_ACTION_NAME);
34423
34563
  this.instance.setDragProperties(properties);