@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/sdk.node.js CHANGED
@@ -17833,8 +17833,7 @@ function getTargetAndSkipNodes(instance, e, forceTransformer = false) {
17833
17833
  }
17834
17834
  if (e.type === "transform" && nodesSelectionPlugin) {
17835
17835
  node = e.target;
17836
- skipNodes.push(node.getAttrs().id ?? "");
17837
- skipNodes.push(...nodesSelectionPlugin.getTransformer().nodes().map((node$1) => node$1.getAttrs().id ?? ""));
17836
+ skipNodes.push(node.getAttrs().id ?? "", ...nodesSelectionPlugin.getTransformer().nodes().map((node$1) => node$1.getAttrs().id ?? ""));
17838
17837
  }
17839
17838
  return {
17840
17839
  targetNode: forceTransformer ? nodesSelectionPlugin.getTransformer() : node,
@@ -20147,10 +20146,10 @@ const augmentKonvaNodeClass = (config) => {
20147
20146
  Konva.Node.prototype.dblClick = function() {};
20148
20147
  };
20149
20148
  var WeaveNode = class {
20150
- register(instance) {
20149
+ async register(instance) {
20151
20150
  this.instance = instance;
20152
20151
  this.logger = this.instance.getChildLogger(this.getNodeType());
20153
- this.onRegister();
20152
+ await this.onRegister();
20154
20153
  this.instance.getChildLogger(`node-${this.getNodeType()}`).debug(`Node with type [${this.getNodeType()}] registered`);
20155
20154
  return this;
20156
20155
  }
@@ -20722,7 +20721,9 @@ var WeaveNode = class {
20722
20721
  }
20723
20722
  };
20724
20723
  }
20725
- onRegister() {}
20724
+ async onRegister() {
20725
+ return;
20726
+ }
20726
20727
  onAdd(nodeInstance) {}
20727
20728
  onDestroy(nodeInstance) {
20728
20729
  nodeInstance.destroy();
@@ -21910,19 +21911,19 @@ var WeaveRegisterManager = class {
21910
21911
  plugin.register(this.instance);
21911
21912
  this.plugins[pluginName] = plugin;
21912
21913
  }
21913
- registerNodesHandlers() {
21914
+ async registerNodesHandlers() {
21914
21915
  const config = this.instance.getConfiguration();
21915
- if (config.nodes) for (const node of config.nodes) this.registerNodeHandler(node);
21916
+ if (config.nodes) for (const node of config.nodes) await this.registerNodeHandler(node);
21916
21917
  this.logger.info(`Nodes handlers registered`);
21917
21918
  }
21918
- registerNodeHandler(node) {
21919
+ async registerNodeHandler(node) {
21919
21920
  const nodeType = node.getNodeType();
21920
21921
  if (this.nodesHandlers[nodeType]) {
21921
21922
  const msg = `Node handler with type [${nodeType}] already exists`;
21922
21923
  this.logger.error(msg);
21923
21924
  throw new Error(msg);
21924
21925
  }
21925
- node.register(this.instance);
21926
+ await node.register(this.instance);
21926
21927
  this.nodesHandlers[nodeType] = node;
21927
21928
  }
21928
21929
  registerActionsHandlers() {
@@ -21954,7 +21955,7 @@ var WeaveRegisterManager = class {
21954
21955
 
21955
21956
  //#endregion
21956
21957
  //#region package.json
21957
- var version = "3.2.0-SNAPSHOT.97.1";
21958
+ var version = "3.2.0";
21958
21959
 
21959
21960
  //#endregion
21960
21961
  //#region src/managers/setup.ts
@@ -22927,7 +22928,7 @@ var Weave = class {
22927
22928
  this.emitEvent("onRoomLoaded", false);
22928
22929
  this.status = WEAVE_INSTANCE_STATUS.STARTING;
22929
22930
  this.emitEvent("onInstanceStatus", this.status);
22930
- this.registerManager.registerNodesHandlers();
22931
+ await this.registerManager.registerNodesHandlers();
22931
22932
  this.augmentKonvaStageClass();
22932
22933
  this.augmentKonvaNodeClass();
22933
22934
  this.registerManager.registerPlugins();
@@ -23593,7 +23594,7 @@ function loadImageSource(image, options) {
23593
23594
  const imageSource = Konva.Util.createImageElement();
23594
23595
  imageSource.crossOrigin = crossOrigin;
23595
23596
  imageSource.onerror = () => {
23596
- reject();
23597
+ reject(new Error("Failed to load image source"));
23597
23598
  };
23598
23599
  imageSource.onload = async () => {
23599
23600
  resolve(imageSource);
@@ -25120,7 +25121,10 @@ const WEAVE_IMAGE_CROP_ANCHOR_POSITION = {
25120
25121
  };
25121
25122
  const WEAVE_IMAGE_DEFAULT_CONFIG = {
25122
25123
  performance: { cache: { enabled: false } },
25123
- style: { placeholder: { fill: "#aaaaaa" } },
25124
+ style: {
25125
+ placeholder: { fill: "#aaaaaa" },
25126
+ cursor: { loading: "wait" }
25127
+ },
25124
25128
  imageLoading: {
25125
25129
  maxRetryAttempts: 15,
25126
25130
  retryDelayMs: 2e3
@@ -25691,6 +25695,57 @@ var WeaveImageCrop = class WeaveImageCrop {
25691
25695
  }
25692
25696
  };
25693
25697
 
25698
+ //#endregion
25699
+ //#region src/nodes/image/utils.ts
25700
+ const extractCursorUrl = (cursor, fallback) => {
25701
+ const lower = cursor.toLowerCase();
25702
+ const start = lower.indexOf("url(");
25703
+ if (start === -1) return {
25704
+ preload: false,
25705
+ cursor
25706
+ };
25707
+ let i = start + 4;
25708
+ const len = cursor.length;
25709
+ while (i < len && /\s/.test(cursor[i])) i++;
25710
+ let quote = null;
25711
+ if (cursor[i] === "\"" || cursor[i] === "'") {
25712
+ quote = cursor[i];
25713
+ i++;
25714
+ }
25715
+ let buf = "";
25716
+ for (; i < len; i++) {
25717
+ const ch = cursor[i];
25718
+ if (quote) {
25719
+ if (ch === quote) {
25720
+ i++;
25721
+ break;
25722
+ }
25723
+ buf += ch;
25724
+ } else {
25725
+ if (ch === ")") break;
25726
+ buf += ch;
25727
+ }
25728
+ }
25729
+ const url = buf.trim();
25730
+ if (!url) return {
25731
+ preload: false,
25732
+ cursor: fallback
25733
+ };
25734
+ if (!isAllowedUrl(url)) return {
25735
+ preload: false,
25736
+ cursor: fallback
25737
+ };
25738
+ return {
25739
+ preload: true,
25740
+ cursor: url
25741
+ };
25742
+ };
25743
+ const isAllowedUrl = (value) => {
25744
+ if (/^https?:\/\//i.test(value)) return true;
25745
+ if (/^(javascript|data|blob|ftp):/i.test(value)) return false;
25746
+ return true;
25747
+ };
25748
+
25694
25749
  //#endregion
25695
25750
  //#region src/nodes/image/image.ts
25696
25751
  var WeaveImageNode = class extends WeaveNode {
@@ -25718,10 +25773,41 @@ var WeaveImageNode = class extends WeaveNode {
25718
25773
  this.imageTryoutAttempts = {};
25719
25774
  this.imageFallback = {};
25720
25775
  }
25776
+ preloadCursors() {
25777
+ const promiseHandler = (src) => new Promise((resolveInt, rejectInt) => {
25778
+ const img = Konva.Util.createImageElement();
25779
+ img.onload = () => {
25780
+ resolveInt();
25781
+ };
25782
+ img.onerror = () => {
25783
+ rejectInt(new Error(`Failed to load cursor image: ${src}`));
25784
+ };
25785
+ img.src = src;
25786
+ });
25787
+ return new Promise((resolve) => {
25788
+ (async () => {
25789
+ const cursors = Object.keys(this.config.style.cursor);
25790
+ const cursorUrls = [];
25791
+ const cursorFallback = { loading: "wait" };
25792
+ for (const cursorKey of cursors) {
25793
+ const cursorValue = this.config.style.cursor[cursorKey];
25794
+ const { preload, cursor } = extractCursorUrl(cursorValue, cursorFallback[cursorKey]);
25795
+ if (preload) cursorUrls.push({ src: cursor });
25796
+ }
25797
+ if (cursorUrls.length > 0) {
25798
+ const cursorsPreloading = [];
25799
+ for (const { src } of cursorUrls) cursorsPreloading.push(promiseHandler(src));
25800
+ await Promise.allSettled(cursorsPreloading);
25801
+ }
25802
+ resolve();
25803
+ })();
25804
+ });
25805
+ }
25721
25806
  getConfiguration() {
25722
25807
  return this.config;
25723
25808
  }
25724
- onRegister() {
25809
+ async onRegister() {
25810
+ await this.preloadCursors();
25725
25811
  this.logger.info(`image caching enabled: ${this.config.performance.cache.enabled}`);
25726
25812
  }
25727
25813
  triggerCrop(imageNode, options) {
@@ -25798,7 +25884,7 @@ var WeaveImageNode = class extends WeaveNode {
25798
25884
  });
25799
25885
  this.setupDefaultNodeAugmentation(image);
25800
25886
  image.defineMousePointer = () => {
25801
- if (this.imageState[id]?.status === "loading") return "wait";
25887
+ if (this.imageState[id]?.status === "loading") return this.config.style.cursor.loading;
25802
25888
  const selectedNodes = this.getSelectionPlugin()?.getSelectedNodes() ?? [];
25803
25889
  if (this.isSelecting() && selectedNodes.includes(image)) return "grab";
25804
25890
  return "pointer";
@@ -26266,6 +26352,8 @@ var WeaveImageNode = class extends WeaveNode {
26266
26352
  onError(error);
26267
26353
  };
26268
26354
  this.imageSource[imageId].onload = async () => {
26355
+ const stage = this.instance.getStage();
26356
+ stage.container().style.cursor = "pointer";
26269
26357
  this.imageState[imageId] = {
26270
26358
  status: "loaded",
26271
26359
  loaded: true,
@@ -26344,15 +26432,11 @@ var WeaveImageNode = class extends WeaveNode {
26344
26432
  width: imageRect.width,
26345
26433
  height: imageRect.height
26346
26434
  });
26347
- const stage = this.instance.getStage();
26348
- if (!loadFallback) {
26349
- if (!this.instance.isServerSide() && stage.container().style.cursor === "wait") stage.container().style.cursor = "pointer";
26350
- this.imageState[id] = {
26351
- status: "loaded",
26352
- loaded: true,
26353
- error: false
26354
- };
26355
- }
26435
+ if (!loadFallback) this.imageState[id] = {
26436
+ status: "loaded",
26437
+ loaded: true,
26438
+ error: false
26439
+ };
26356
26440
  this.updateImageCrop(image);
26357
26441
  this.resolveAsyncElement(id);
26358
26442
  this.cacheNode(image);
@@ -26398,12 +26482,6 @@ var WeaveImageNode = class extends WeaveNode {
26398
26482
  updatePlaceholderSize(image, imagePlaceholder) {
26399
26483
  const imageAttrs = image.getAttrs();
26400
26484
  if (!this.imageState[imageAttrs.id ?? ""]?.loaded) return;
26401
- if (!imageAttrs.adding && imageAttrs.cropInfo) {
26402
- const actualScale = imageAttrs.uncroppedImage.width / imageAttrs.imageInfo.width;
26403
- const cropScale = imageAttrs.cropInfo ? imageAttrs.cropInfo.scaleX : actualScale;
26404
- imagePlaceholder.width(imageAttrs.cropSize.width * (actualScale / cropScale));
26405
- imagePlaceholder.height(imageAttrs.cropSize.height * (actualScale / cropScale));
26406
- }
26407
26485
  if (!imageAttrs.adding && !imageAttrs.cropInfo) {
26408
26486
  imagePlaceholder.width(imageAttrs.uncroppedImage.width);
26409
26487
  imagePlaceholder.height(imageAttrs.uncroppedImage.height);
@@ -28886,7 +28964,7 @@ var WeaveVideoNode = class extends WeaveNode {
28886
28964
  videoIconGroup.x(videoWidth - videoIconGroupWidth - this.config.style.icon.external.paddingX);
28887
28965
  videoIconGroup.y(videoHeight - videoIconGroupHeight - this.config.style.icon.external.paddingY);
28888
28966
  const nodesSelectionPlugin = this.getNodeSelectionPlugin();
28889
- if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer().forceUpdate();
28967
+ if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer()?.forceUpdate();
28890
28968
  this.resolveAsyncElement(id);
28891
28969
  };
28892
28970
  }
@@ -29279,7 +29357,7 @@ var WeaveVideoNode = class extends WeaveNode {
29279
29357
  videoIconGroup.x(videoWidth - videoIconGroupWidth - this.config.style.icon.external.paddingX);
29280
29358
  videoIconGroup.y(videoHeight - videoIconGroupHeight - this.config.style.icon.external.paddingY);
29281
29359
  const nodesSelectionPlugin = this.getNodeSelectionPlugin();
29282
- if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer().forceUpdate();
29360
+ if (nodesSelectionPlugin) nodesSelectionPlugin.getTransformer()?.forceUpdate();
29283
29361
  }
29284
29362
  getNodeSelectionPlugin() {
29285
29363
  const nodesSelectionPlugin = this.instance.getPlugin("nodesSelection");
@@ -31896,7 +31974,7 @@ var WeaveZoomOutToolAction = class extends WeaveAction {
31896
31974
  }
31897
31975
  cleanup() {
31898
31976
  const stage = this.instance.getStage();
31899
- this.instance.triggerAction(this.previousAction);
31977
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31900
31978
  stage.container().style.cursor = "default";
31901
31979
  }
31902
31980
  };
@@ -31930,7 +32008,7 @@ var WeaveZoomInToolAction = class extends WeaveAction {
31930
32008
  }
31931
32009
  cleanup() {
31932
32010
  const stage = this.instance.getStage();
31933
- this.instance.triggerAction(this.previousAction);
32011
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31934
32012
  stage.container().style.cursor = "default";
31935
32013
  }
31936
32014
  };
@@ -31963,7 +32041,7 @@ var WeaveFitToScreenToolAction = class extends WeaveAction {
31963
32041
  }
31964
32042
  cleanup() {
31965
32043
  const stage = this.instance.getStage();
31966
- this.instance.triggerAction(this.previousAction);
32044
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
31967
32045
  stage.container().style.cursor = "default";
31968
32046
  }
31969
32047
  };
@@ -32005,7 +32083,7 @@ var WeaveFitToSelectionToolAction = class extends WeaveAction {
32005
32083
  }
32006
32084
  cleanup() {
32007
32085
  const stage = this.instance.getStage();
32008
- if (this.previousAction) this.instance.triggerAction(this.previousAction);
32086
+ if (this.previousAction !== void 0) this.instance.triggerAction(this.previousAction);
32009
32087
  stage.container().style.cursor = "default";
32010
32088
  }
32011
32089
  };
@@ -33617,12 +33695,9 @@ const WEAVE_IMAGE_TOOL_CONFIG_DEFAULT = { style: { cursor: {
33617
33695
  var WeaveImageToolAction = class extends WeaveAction {
33618
33696
  initialized = false;
33619
33697
  initialCursor = null;
33620
- imageFile = null;
33621
- imageURL = null;
33622
- forceMainContainer = false;
33698
+ imageAction = {};
33623
33699
  ignoreKeyboardEvents = false;
33624
33700
  ignorePointerEvents = false;
33625
- uploadType = null;
33626
33701
  onPropsChange = void 0;
33627
33702
  update = void 0;
33628
33703
  constructor(params) {
@@ -33630,14 +33705,10 @@ var WeaveImageToolAction = class extends WeaveAction {
33630
33705
  this.config = mergeExceptArrays(WEAVE_IMAGE_TOOL_CONFIG_DEFAULT, params?.config ?? {});
33631
33706
  this.pointers = new Map();
33632
33707
  this.initialized = false;
33633
- this.state = WEAVE_IMAGE_TOOL_STATE.IDLE;
33634
33708
  this.imageId = null;
33709
+ this.state = WEAVE_IMAGE_TOOL_STATE.IDLE;
33635
33710
  this.tempImageId = null;
33636
33711
  this.tempImageNode = null;
33637
- this.container = void 0;
33638
- this.imageURL = null;
33639
- this.uploadType = null;
33640
- this.clickPoint = null;
33641
33712
  }
33642
33713
  getName() {
33643
33714
  return WEAVE_IMAGE_TOOL_ACTION_NAME;
@@ -33709,66 +33780,54 @@ var WeaveImageToolAction = class extends WeaveAction {
33709
33780
  stage.on("pointerup", (e) => {
33710
33781
  if (this.ignorePointerEvents) return;
33711
33782
  this.pointers.delete(e.evt.pointerId);
33712
- if (this.state === WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION) this.handleAdding();
33783
+ if (this.state === WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION) this.handleAdding(this.imageId ?? "");
33713
33784
  });
33714
33785
  this.initialized = true;
33715
33786
  }
33716
33787
  setState(state) {
33717
33788
  this.state = state;
33718
33789
  }
33719
- async loadImage(params) {
33790
+ async loadImage(nodeId, params) {
33720
33791
  this.setCursor();
33721
33792
  this.setFocusStage();
33722
- if (!this.imageId) {
33723
- this.cancelAction();
33724
- return;
33725
- }
33726
33793
  const imageNodeHandler = this.getImageNodeHandler();
33727
33794
  if (!imageNodeHandler) return;
33728
- const actualImageId = this.imageId;
33729
33795
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33730
33796
  const image = params.image;
33731
33797
  const realImageSize = await getImageSizeFromFile(image.file);
33732
33798
  const downscaledImage = await downscaleImageFile(image.file, image.downscaleRatio);
33733
- const reader = new FileReader();
33734
- reader.onloadend = () => {
33735
- imageNodeHandler.preloadFallbackImage(actualImageId, reader.result, {
33736
- onLoad: () => {
33737
- this.props = {
33738
- ...this.props,
33739
- imageFallback: reader.result,
33740
- imageURL: void 0,
33741
- width: realImageSize.width,
33742
- height: realImageSize.height
33743
- };
33744
- this.addImageNode(params?.position);
33745
- },
33746
- onError: () => {
33747
- this.cancelAction();
33748
- }
33749
- });
33750
- };
33751
- reader.onerror = () => {};
33752
- reader.readAsDataURL(downscaledImage);
33799
+ try {
33800
+ const dataURL = await this.getDataURL(downscaledImage);
33801
+ this.imageAction[nodeId].props = {
33802
+ ...this.imageAction[nodeId].props,
33803
+ imageFallback: dataURL,
33804
+ imageURL: void 0,
33805
+ width: realImageSize.width,
33806
+ height: realImageSize.height
33807
+ };
33808
+ this.addImageNode(nodeId, params?.position);
33809
+ } catch {
33810
+ this.cancelAction();
33811
+ }
33753
33812
  }
33754
33813
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL) {
33755
33814
  const image = params.image;
33756
33815
  setTimeout(() => {
33757
- this.saveImageUrl(actualImageId, image.url);
33816
+ this.saveImageUrl(nodeId, image.url);
33758
33817
  }, 0);
33759
- this.addImageNode(params?.position);
33818
+ this.addImageNode(nodeId, params?.position);
33760
33819
  }
33761
33820
  }
33762
33821
  isTouchDevice() {
33763
33822
  return window.matchMedia("(pointer: coarse)").matches;
33764
33823
  }
33765
- addImageNode(position) {
33824
+ async addImageNode(nodeId, position) {
33766
33825
  const stage = this.instance.getStage();
33767
33826
  this.setCursor();
33768
33827
  this.setFocusStage();
33769
33828
  if (position) {
33770
33829
  this.setState(WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION);
33771
- this.handleAdding(position);
33830
+ this.handleAdding(nodeId, position);
33772
33831
  return;
33773
33832
  }
33774
33833
  const imageNodeHandler = this.getImageNodeHandler();
@@ -33776,11 +33835,15 @@ var WeaveImageToolAction = class extends WeaveAction {
33776
33835
  this.cancelAction();
33777
33836
  return;
33778
33837
  }
33779
- if (this.imageId) {
33838
+ if (this.imageAction[nodeId]) {
33839
+ const { uploadType } = this.imageAction[nodeId];
33780
33840
  const mousePos = stage.getRelativePointerPosition();
33781
33841
  this.tempImageId = v4_default();
33782
- let imageSource = imageNodeHandler.getImageSource(this.imageId);
33783
- if (this.uploadType === "file") imageSource = imageNodeHandler.getFallbackImageSource(this.imageId);
33842
+ let imageSource = imageNodeHandler.getImageSource(nodeId);
33843
+ if (uploadType === "file") {
33844
+ imageSource = imageNodeHandler.getFallbackImageSource(nodeId);
33845
+ imageSource ??= await this.loadImageDataURL(this.imageAction[nodeId].props.imageFallback);
33846
+ }
33784
33847
  if (!imageSource) {
33785
33848
  this.cancelAction();
33786
33849
  return;
@@ -33814,37 +33877,41 @@ var WeaveImageToolAction = class extends WeaveAction {
33814
33877
  });
33815
33878
  this.instance.getMainLayer()?.add(this.tempImageNode);
33816
33879
  }
33817
- this.instance.emitEvent("onAddingImage", { imageURL: this.props.imageURL });
33880
+ this.instance.emitEvent("onAddingImage");
33881
+ this.imageAction[nodeId].clickPoint = null;
33818
33882
  }
33819
- this.clickPoint = null;
33820
33883
  this.setState(WEAVE_IMAGE_TOOL_STATE.DEFINING_POSITION);
33821
33884
  }
33822
- handleAdding(position) {
33885
+ async handleAdding(nodeId, position) {
33823
33886
  const imageNodeHandler = this.getImageNodeHandler();
33824
33887
  if (!imageNodeHandler) {
33825
33888
  this.cancelAction();
33826
33889
  return;
33827
33890
  }
33828
- if (this.imageId) {
33829
- let imageSource = imageNodeHandler.getImageSource(this.imageId);
33830
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) imageSource = imageNodeHandler.getFallbackImageSource(this.imageId);
33891
+ if (this.imageAction[nodeId]) {
33892
+ const { uploadType, imageURL, forceMainContainer } = this.imageAction[nodeId];
33893
+ let imageSource = imageNodeHandler.getImageSource(nodeId);
33894
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33895
+ imageSource = imageNodeHandler.getFallbackImageSource(nodeId);
33896
+ imageSource ??= await this.loadImageDataURL(this.imageAction[nodeId].props.imageFallback);
33897
+ }
33831
33898
  if (!imageSource && !position) {
33832
33899
  this.cancelAction();
33833
33900
  return;
33834
33901
  }
33835
33902
  const { mousePoint, container } = this.instance.getMousePointer(position);
33836
- this.clickPoint = mousePoint;
33837
- this.container = container;
33903
+ this.imageAction[nodeId].clickPoint = mousePoint;
33904
+ this.imageAction[nodeId].container = container;
33838
33905
  const nodeHandler = this.instance.getNodeHandler("image");
33839
- const imageWidth = this.props.width ? this.props.width : imageSource?.width;
33840
- const imageHeight = this.props.height ? this.props.height : imageSource?.height;
33906
+ const imageWidth = this.imageAction[nodeId].props.width ? this.imageAction[nodeId].props.width : imageSource?.width;
33907
+ const imageHeight = this.imageAction[nodeId].props.height ? this.imageAction[nodeId].props.height : imageSource?.height;
33841
33908
  let realImageURL = void 0;
33842
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imageURL) realImageURL = this.imageURL?.url;
33909
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && imageURL) realImageURL = imageURL?.url;
33843
33910
  if (nodeHandler) {
33844
- const node = nodeHandler.create(this.imageId, {
33845
- ...this.props,
33846
- x: this.clickPoint?.x ?? 0,
33847
- y: this.clickPoint?.y ?? 0,
33911
+ const node = nodeHandler.create(nodeId, {
33912
+ ...this.imageAction[nodeId].props,
33913
+ x: this.imageAction[nodeId].clickPoint.x,
33914
+ y: this.imageAction[nodeId].clickPoint.y,
33848
33915
  opacity: 1,
33849
33916
  adding: false,
33850
33917
  imageURL: realImageURL,
@@ -33862,23 +33929,24 @@ var WeaveImageToolAction = class extends WeaveAction {
33862
33929
  height: imageHeight
33863
33930
  }
33864
33931
  });
33865
- this.instance.addNode(node, this.forceMainContainer ? this.instance.getMainLayer()?.getAttrs().id : this.container?.getAttrs().id);
33866
- this.instance.emitEvent("onAddedImage", { nodeId: this.imageId });
33867
- if (this.uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33868
- const uploadImageFunctionInternal = async () => {
33869
- const nodeId = this.imageId ?? "";
33932
+ this.instance.addNode(node, forceMainContainer ? this.instance.getMainLayer()?.getAttrs().id : this.imageAction[nodeId].container?.getAttrs().id);
33933
+ this.instance.emitEvent("onAddedImage", { nodeId });
33934
+ if (uploadType === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE) {
33935
+ const uploadImageFunctionInternal = async (imageActionData) => {
33936
+ const { uploadImageFunction, imageFile } = imageActionData;
33870
33937
  try {
33871
- const imageURL = await this.uploadImageFunction(this.imageFile.file);
33872
- this.saveImageUrl(nodeId, imageURL);
33938
+ const imageURL$1 = await uploadImageFunction?.(imageFile.file);
33939
+ if (!imageURL$1) return;
33940
+ this.saveImageUrl(nodeId, imageURL$1);
33873
33941
  this.instance.emitEvent("onImageUploaded", {
33874
- imageURL,
33942
+ imageURL: imageURL$1,
33875
33943
  nodeId
33876
33944
  });
33877
33945
  } catch (error) {
33878
33946
  this.instance.emitEvent("onImageUploadedError", { error });
33879
33947
  }
33880
33948
  };
33881
- uploadImageFunctionInternal();
33949
+ uploadImageFunctionInternal(this.imageAction[nodeId]);
33882
33950
  }
33883
33951
  }
33884
33952
  this.setState(WEAVE_IMAGE_TOOL_STATE.FINISHED);
@@ -33893,42 +33961,51 @@ var WeaveImageToolAction = class extends WeaveAction {
33893
33961
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
33894
33962
  this.ignorePointerEvents = false;
33895
33963
  this.ignoreKeyboardEvents = false;
33896
- this.forceMainContainer = params?.forceMainContainer ?? false;
33897
- this.imageFile = null;
33898
- this.imageURL = null;
33899
- this.imageId = v4_default();
33900
- this.props = this.initProps();
33901
- if (params?.imageId) this.updateProps({ imageId: params.imageId });
33964
+ const nodeId = params?.nodeId ?? v4_default();
33965
+ this.imageId = nodeId;
33966
+ this.imageAction[nodeId] = {
33967
+ props: this.initProps(),
33968
+ imageId: nodeId,
33969
+ clickPoint: null,
33970
+ container: void 0,
33971
+ imageFile: null,
33972
+ imageURL: null,
33973
+ forceMainContainer: params?.forceMainContainer ?? false,
33974
+ uploadType: null,
33975
+ uploadImageFunction: null
33976
+ };
33977
+ if (params?.imageId) this.imageAction[nodeId].imageId = params.imageId;
33902
33978
  if (this.forceExecution) {
33903
33979
  this.ignorePointerEvents = true;
33904
33980
  this.ignoreKeyboardEvents = true;
33905
33981
  }
33906
33982
  if (params?.position) this.setState(WEAVE_IMAGE_TOOL_STATE.SELECTED_POSITION);
33907
33983
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE && params.image) {
33908
- this.uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE;
33909
- this.imageFile = params.image;
33910
- this.uploadImageFunction = params.uploadImageFunction;
33911
- this.loadImage({
33984
+ this.imageAction[nodeId].uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE;
33985
+ this.imageAction[nodeId].imageFile = params.image;
33986
+ this.imageAction[nodeId].uploadImageFunction = params.uploadImageFunction;
33987
+ this.loadImage(nodeId, {
33912
33988
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
33913
33989
  image: params.image,
33914
33990
  position: params?.position
33915
33991
  });
33916
33992
  }
33917
33993
  if (params.type === WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL && params.image) {
33918
- this.uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL;
33919
- this.imageURL = params.image;
33920
- this.updateProps({
33994
+ this.imageAction[nodeId].uploadType = WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL;
33995
+ this.imageAction[nodeId].imageURL = params.image;
33996
+ this.imageAction[nodeId].props = {
33997
+ ...this.imageAction[nodeId].props,
33921
33998
  imageFallback: params.image.fallback,
33922
33999
  width: params.image.width,
33923
34000
  height: params.image.height
33924
- });
33925
- this.loadImage({
34001
+ };
34002
+ this.loadImage(nodeId, {
33926
34003
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL,
33927
34004
  image: params.image,
33928
34005
  position: params?.position
33929
34006
  });
33930
34007
  }
33931
- return { nodeId: this.imageId };
34008
+ return { nodeId };
33932
34009
  }
33933
34010
  saveImageUrl(nodeId, imageURL) {
33934
34011
  const stage = this.instance.getStage();
@@ -33954,11 +34031,7 @@ var WeaveImageToolAction = class extends WeaveAction {
33954
34031
  this.instance.endDrag(WEAVE_IMAGE_TOOL_ACTION_NAME);
33955
34032
  }
33956
34033
  this.initialCursor = null;
33957
- this.forceMainContainer = false;
33958
- this.container = void 0;
33959
34034
  this.tempImageNode = null;
33960
- this.imageURL = null;
33961
- this.clickPoint = null;
33962
34035
  this.setState(WEAVE_IMAGE_TOOL_STATE.IDLE);
33963
34036
  }
33964
34037
  getImageNodeHandler() {
@@ -33981,6 +34054,30 @@ var WeaveImageToolAction = class extends WeaveAction {
33981
34054
  getActualState() {
33982
34055
  return this.state;
33983
34056
  }
34057
+ loadImageDataURL(imageDataURL) {
34058
+ return new Promise((resolve, reject) => {
34059
+ const imageEle = Konva.Util.createImageElement();
34060
+ imageEle.onerror = (error) => {
34061
+ reject(error);
34062
+ };
34063
+ imageEle.onload = async () => {
34064
+ resolve(imageEle);
34065
+ };
34066
+ imageEle.src = imageDataURL;
34067
+ });
34068
+ }
34069
+ getDataURL(blob) {
34070
+ return new Promise((resolve, reject) => {
34071
+ const reader = new FileReader();
34072
+ reader.onloadend = () => {
34073
+ resolve(reader.result);
34074
+ };
34075
+ reader.onerror = () => {
34076
+ reject(new Error("Failed to generate dataURL from file"));
34077
+ };
34078
+ reader.readAsDataURL(blob);
34079
+ });
34080
+ }
33984
34081
  };
33985
34082
 
33986
34083
  //#endregion
@@ -34029,18 +34126,13 @@ const WEAVE_IMAGES_TOOL_DEFAULT_CONFIG = {
34029
34126
  layout: { columns: 4 }
34030
34127
  };
34031
34128
 
34032
- //#endregion
34033
- //#region src/internal-utils/generic.ts
34034
- function sleep(ms) {
34035
- return new Promise((resolve) => setTimeout(resolve, ms));
34036
- }
34037
-
34038
34129
  //#endregion
34039
34130
  //#region src/actions/images-tool/images-tool.ts
34040
34131
  var WeaveImagesToolAction = class extends WeaveAction {
34041
34132
  initialized = false;
34042
34133
  initialCursor = null;
34043
34134
  nodesIds = [];
34135
+ toAdd = 0;
34044
34136
  imagesFile = [];
34045
34137
  imagesURL = [];
34046
34138
  forceMainContainer = false;
@@ -34128,7 +34220,10 @@ var WeaveImagesToolAction = class extends WeaveAction {
34128
34220
  });
34129
34221
  stage.on("pointerup", (e) => {
34130
34222
  this.pointers.delete(e.evt.pointerId);
34131
- if (this.state === WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION) this.handleAdding();
34223
+ if (this.state === WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION) {
34224
+ this.instance.emitEvent("onSelectedPositionImages");
34225
+ this.handleAdding();
34226
+ }
34132
34227
  });
34133
34228
  this.initialized = true;
34134
34229
  }
@@ -34168,9 +34263,9 @@ var WeaveImagesToolAction = class extends WeaveAction {
34168
34263
  const shadowBlur = this.config.style.cursor.imageThumbnails.shadowBlur;
34169
34264
  const shadowOffset = this.config.style.cursor.imageThumbnails.shadowOffset;
34170
34265
  const shadowOpacity = this.config.style.cursor.imageThumbnails.shadowOpacity;
34171
- const aspectRatio = imageSource.width / imageSource.height || 1;
34172
- const imageWidth = maxImageWidth * aspectRatio * (1 / stage.scaleX());
34173
- const imageHeight = maxImageHeight * (1 / stage.scaleY());
34266
+ const scale = Math.min(maxImageWidth / imageSource.width, maxImageHeight / imageSource.height);
34267
+ const imageWidth = imageSource.width * scale * (1 / stage.scaleX());
34268
+ const imageHeight = imageSource.height * scale * (1 / stage.scaleY());
34174
34269
  const imageNode = new Konva.Image({
34175
34270
  x: position$1.x,
34176
34271
  y: position$1.y,
@@ -34188,7 +34283,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34188
34283
  shadowOffset,
34189
34284
  shadowOpacity
34190
34285
  });
34191
- maxWidth = position$1.x + imageWidth;
34286
+ maxWidth = Math.max(maxWidth, position$1.x + imageWidth);
34192
34287
  maxHeight = Math.max(maxHeight, position$1.y + imageHeight);
34193
34288
  position$1 = {
34194
34289
  x: position$1.x + imageThumbnailsPadding / stage.scaleX(),
@@ -34257,6 +34352,18 @@ var WeaveImagesToolAction = class extends WeaveAction {
34257
34352
  let imagePositionX = originPoint.x;
34258
34353
  let imagePositionY = originPoint.y;
34259
34354
  let maxHeight = 0;
34355
+ this.nodesIds = [];
34356
+ const images = [];
34357
+ const checkAddedImages = ({ nodeId }) => {
34358
+ if (this.nodesIds.includes(nodeId)) this.handleImageAdded();
34359
+ if (this.getImagesAdded() <= 0) {
34360
+ this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34361
+ this.cancelAction();
34362
+ this.instance.removeEventListener("onAddedImage", checkAddedImages);
34363
+ this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34364
+ }
34365
+ };
34366
+ this.instance.addEventListener("onAddedImage", checkAddedImages);
34260
34367
  if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE && this.imagesFile) {
34261
34368
  const imagesToUpload = this.imagesFile.length;
34262
34369
  let imagesUploaded = 0;
@@ -34270,23 +34377,35 @@ var WeaveImagesToolAction = class extends WeaveAction {
34270
34377
  };
34271
34378
  this.instance.addEventListener("onImageUploaded", handleUploadImage);
34272
34379
  for (let i = 0; i < this.imagesFile.length; i++) {
34273
- const imageFile = this.imagesFile[i];
34274
- const { imageId, width, height,...restImageFile } = imageFile;
34275
- const uploadImageFunctionInternal = async () => {
34276
- const imageURL = await this.uploadImageFunction(imageFile.file);
34277
- return imageURL;
34380
+ const nodeId = v4_default();
34381
+ const image = this.imagesFile[i];
34382
+ const { width, height } = image;
34383
+ const handleImage = async (nodeId$1, imageFile, position$1) => {
34384
+ const { imageId } = imageFile;
34385
+ const uploadImageFunctionInternal = async () => {
34386
+ const imageURL = await this.uploadImageFunction(imageFile.file);
34387
+ return imageURL;
34388
+ };
34389
+ this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34390
+ type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
34391
+ image: {
34392
+ file: imageFile.file,
34393
+ downscaleRatio: imageFile.downscaleRatio
34394
+ },
34395
+ uploadImageFunction: uploadImageFunctionInternal,
34396
+ ...imageId && { imageId },
34397
+ position: {
34398
+ x: position$1.x,
34399
+ y: position$1.y
34400
+ },
34401
+ forceMainContainer: this.forceMainContainer,
34402
+ nodeId: nodeId$1
34403
+ }, true);
34278
34404
  };
34279
- const { nodeId } = this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34280
- type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.FILE,
34281
- image: restImageFile,
34282
- uploadImageFunction: uploadImageFunctionInternal,
34283
- ...imageId && { imageId },
34284
- position: {
34285
- x: imagePositionX,
34286
- y: imagePositionY
34287
- },
34288
- forceMainContainer: this.forceMainContainer
34289
- }, true);
34405
+ images.push(handleImage(nodeId, image, {
34406
+ x: imagePositionX,
34407
+ y: imagePositionY
34408
+ }));
34290
34409
  this.nodesIds.push(nodeId);
34291
34410
  maxHeight = Math.max(maxHeight, height);
34292
34411
  imagePositionX += imagesPadding + width;
@@ -34295,40 +34414,51 @@ var WeaveImagesToolAction = class extends WeaveAction {
34295
34414
  imagePositionY = imagePositionY + maxHeight + imagesPadding;
34296
34415
  maxHeight = 0;
34297
34416
  }
34298
- while (imageToolActionHandler.getActualState() !== WEAVE_IMAGES_TOOL_STATE.IDLE) await sleep(10);
34299
34417
  }
34300
- this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34301
34418
  }
34302
- if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imagesURL) {
34303
- for (let i = 0; i < this.imagesURL.length; i++) {
34304
- const imageURL = this.imagesURL[i];
34305
- const { imageId, options,...restImageURL } = imageURL;
34306
- const imageURLElement = restImageURL;
34307
- const { nodeId } = this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34419
+ if (this.uploadType === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.IMAGE_URL && this.imagesURL) for (let i = 0; i < this.imagesURL.length; i++) {
34420
+ const nodeId = v4_default();
34421
+ const image = this.imagesURL[i];
34422
+ const handleImage = async (nodeId$1, imageURL, position$1) => {
34423
+ const { imageId, options } = imageURL;
34424
+ this.instance.triggerAction(WEAVE_IMAGE_TOOL_ACTION_NAME, {
34308
34425
  type: WEAVE_IMAGE_TOOL_UPLOAD_TYPE.IMAGE_URL,
34309
- image: imageURLElement,
34426
+ image: {
34427
+ url: imageURL.url,
34428
+ fallback: imageURL.fallback,
34429
+ width: imageURL.width,
34430
+ height: imageURL.height
34431
+ },
34310
34432
  ...imageId && { imageId },
34311
34433
  ...options && { options },
34312
34434
  position: {
34313
- x: imagePositionX,
34314
- y: imagePositionY
34435
+ x: position$1.x,
34436
+ y: position$1.y
34315
34437
  },
34316
- forceMainContainer: this.forceMainContainer
34438
+ forceMainContainer: this.forceMainContainer,
34439
+ nodeId: nodeId$1
34317
34440
  }, true);
34318
- this.nodesIds.push(nodeId);
34319
- maxHeight = Math.max(maxHeight, imageURL.height);
34320
- imagePositionX += imagesPadding + imageURL.width;
34321
- if ((i + 1) % layoutColumns === 0) {
34322
- imagePositionX = originPoint.x;
34323
- imagePositionY = imagePositionY + maxHeight + imagesPadding;
34324
- maxHeight = 0;
34325
- }
34326
- while (imageToolActionHandler.getActualState() !== WEAVE_IMAGES_TOOL_STATE.IDLE) await sleep(10);
34327
- }
34328
- this.instance.emitEvent("onAddedImages", { nodesIds: this.nodesIds });
34441
+ };
34442
+ images.push(handleImage(nodeId, image, {
34443
+ x: imagePositionX,
34444
+ y: imagePositionY
34445
+ }));
34446
+ this.nodesIds.push(nodeId);
34447
+ maxHeight = Math.max(maxHeight, image.height);
34448
+ imagePositionX += imagesPadding + image.width;
34449
+ if ((i + 1) % layoutColumns === 0) {
34450
+ imagePositionX = originPoint.x;
34451
+ imagePositionY = imagePositionY + maxHeight + imagesPadding;
34452
+ maxHeight = 0;
34453
+ }
34454
+ }
34455
+ if (this.nodesIds.length > 0) {
34456
+ this.toAdd = this.nodesIds.length;
34457
+ await Promise.allSettled(images);
34458
+ } else {
34459
+ this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34460
+ this.cancelAction();
34329
34461
  }
34330
- this.setState(WEAVE_IMAGES_TOOL_STATE.FINISHED);
34331
- this.cancelAction();
34332
34462
  }
34333
34463
  trigger(cancelAction, params) {
34334
34464
  if (!this.instance) throw new Error("Instance not defined");
@@ -34337,6 +34467,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34337
34467
  const selectionPlugin = this.instance.getPlugin("nodesSelection");
34338
34468
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
34339
34469
  if (params?.position) this.setState(WEAVE_IMAGES_TOOL_STATE.SELECTED_POSITION);
34470
+ this.nodesIds = [];
34340
34471
  this.forceMainContainer = params.forceMainContainer ?? false;
34341
34472
  if (params.type === WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE) {
34342
34473
  this.uploadType = WEAVE_IMAGES_TOOL_UPLOAD_TYPE.FILE;
@@ -34384,6 +34515,7 @@ var WeaveImagesToolAction = class extends WeaveAction {
34384
34515
  selectionPlugin.setSelectedNodes(addedNodes);
34385
34516
  this.instance.triggerAction(SELECTION_TOOL_ACTION_NAME);
34386
34517
  }
34518
+ this.instance.emitEvent("onFinishedImages");
34387
34519
  this.instance.endDrag(WEAVE_IMAGES_TOOL_ACTION_NAME);
34388
34520
  stage.container().style.cursor = "default";
34389
34521
  this.uploadType = null;
@@ -34391,6 +34523,8 @@ var WeaveImagesToolAction = class extends WeaveAction {
34391
34523
  this.initialCursor = null;
34392
34524
  this.container = void 0;
34393
34525
  this.clickPoint = null;
34526
+ this.nodesIds = [];
34527
+ this.toAdd = 0;
34394
34528
  this.setState(WEAVE_IMAGES_TOOL_STATE.IDLE);
34395
34529
  }
34396
34530
  setCursor() {
@@ -34406,6 +34540,12 @@ var WeaveImagesToolAction = class extends WeaveAction {
34406
34540
  isTouchDevice() {
34407
34541
  return window.matchMedia("(pointer: coarse)").matches;
34408
34542
  }
34543
+ getImagesAdded() {
34544
+ return this.toAdd;
34545
+ }
34546
+ handleImageAdded() {
34547
+ this.toAdd = this.toAdd - 1;
34548
+ }
34409
34549
  setDragAndDropProperties(properties) {
34410
34550
  this.instance.startDrag(WEAVE_IMAGES_TOOL_ACTION_NAME);
34411
34551
  this.instance.setDragProperties(properties);