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