@inditextech/weave-sdk 0.41.0 → 0.42.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.cjs CHANGED
@@ -16961,11 +16961,7 @@ var WeaveNode = class {
16961
16961
  const nodesSnappingPlugin = this.instance.getPlugin("nodesSnapping");
16962
16962
  if (nodesSelectionPlugin && this.isSelecting() && this.isNodeSelected(node$1)) nodesSelectionPlugin.getTransformer().forceUpdate();
16963
16963
  if (nodesSnappingPlugin && transforming && this.isSelecting() && this.isNodeSelected(node$1)) nodesSnappingPlugin.evaluateGuidelines(e);
16964
- if (this.isSelecting() && this.isNodeSelected(node$1)) {
16965
- this.scaleReset(node$1);
16966
- const nodeHandler = this.instance.getNodeHandler(node$1.getAttrs().nodeType);
16967
- if (nodeHandler) this.instance.updateNode(nodeHandler.serialize(node$1));
16968
- }
16964
+ if (this.isSelecting() && this.isNodeSelected(node$1)) this.scaleReset(node$1);
16969
16965
  };
16970
16966
  node.on("transform", (0, import_lodash.throttle)(handleTransform, 100));
16971
16967
  node.on("transformend", (e) => {
@@ -18454,28 +18450,57 @@ var WeaveStateManager = class {
18454
18450
  return;
18455
18451
  }
18456
18452
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent before init");
18457
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18458
18453
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent before add");
18459
- if (index) {
18460
- newChildren?.splice(index, 0, node);
18461
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18462
- }
18463
- if (!index) {
18464
- const childrenAmount = newChildren.length;
18465
- node.props.zIndex = childrenAmount;
18466
- const nodeToAdd = {
18467
- ...node,
18468
- props: {
18469
- ...node.props,
18470
- zIndex: childrenAmount
18471
- }
18472
- };
18473
- this.logger.info({ node: JSON.parse(JSON.stringify(nodeToAdd)) }, "addNode: node to add");
18474
- newChildren.push(nodeToAdd);
18454
+ const doc = (0, __syncedstore_core.getYjsDoc)(state);
18455
+ doc.transact(() => {
18456
+ if (!parent.props.children) parent.props.children = [];
18457
+ if (index) {
18458
+ parent.props.children.splice(index, 0, node);
18459
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18460
+ }
18461
+ if (!index) {
18462
+ const childrenAmount = parent.props.children.length;
18463
+ const nodeToAdd = {
18464
+ ...node,
18465
+ props: {
18466
+ ...node.props,
18467
+ zIndex: childrenAmount
18468
+ }
18469
+ };
18470
+ this.logger.info({ node: JSON.parse(JSON.stringify(nodeToAdd)) }, "addNode: node to add");
18471
+ parent.props.children.push(nodeToAdd);
18472
+ }
18473
+ this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent after add");
18474
+ this.instance.emitEvent("onNodeAdded", node);
18475
+ });
18476
+ }
18477
+ deepSyncSyncedStore(target, source) {
18478
+ for (const key in target) if (!(key in source)) delete target[key];
18479
+ for (const key in source) {
18480
+ const srcVal = source[key];
18481
+ const tgtVal = target[key];
18482
+ const bothAreObjects = this.isObject(srcVal) && this.isObject(tgtVal);
18483
+ if (bothAreObjects && !Array.isArray(srcVal)) this.deepSyncSyncedStore(tgtVal, srcVal);
18484
+ else if (Array.isArray(srcVal)) this.syncArray(target, key, srcVal);
18485
+ else if (tgtVal !== srcVal) target[key] = srcVal;
18486
+ }
18487
+ }
18488
+ syncArray(target, key, sourceArr) {
18489
+ const tgtArr = target[key];
18490
+ if (!Array.isArray(tgtArr)) {
18491
+ target[key] = [...sourceArr];
18492
+ return;
18493
+ }
18494
+ while (tgtArr.length > sourceArr.length) tgtArr.pop();
18495
+ for (let i = 0; i < sourceArr.length; i++) {
18496
+ const srcItem = sourceArr[i];
18497
+ const tgtItem = tgtArr[i];
18498
+ if (this.isObject(srcItem) && this.isObject(tgtItem)) this.deepSyncSyncedStore(tgtItem, srcItem);
18499
+ else if (tgtItem !== srcItem) tgtArr[i] = srcItem;
18475
18500
  }
18476
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18477
- this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent after add");
18478
- this.instance.emitEvent("onNodeAdded", node);
18501
+ }
18502
+ isObject(val) {
18503
+ return typeof val === "object" && val !== null;
18479
18504
  }
18480
18505
  updateNode(node) {
18481
18506
  const state = this.instance.getStore().getState();
@@ -18491,13 +18516,14 @@ var WeaveStateManager = class {
18491
18516
  this.logger.warn({ node }, msg);
18492
18517
  return;
18493
18518
  }
18494
- this.logger.info({ node: JSON.parse(JSON.stringify(nodeState)) }, "updateNode: before update");
18495
- const newNode = JSON.parse(JSON.stringify(nodeState));
18496
- newNode.props = {
18497
- ...newNode.props,
18498
- ...node.props
18499
- };
18500
- if (typeof newNode.props !== "undefined") nodeState.props = newNode.props;
18519
+ this.logger.info({
18520
+ node: JSON.parse(JSON.stringify(nodeState)),
18521
+ toUpdate: node
18522
+ }, "updateNode: before update");
18523
+ const doc = (0, __syncedstore_core.getYjsDoc)(state);
18524
+ doc.transact(() => {
18525
+ this.deepSyncSyncedStore(nodeState.props, node.props);
18526
+ });
18501
18527
  this.logger.info({ node: JSON.parse(JSON.stringify(nodeState)) }, "updateNode: after update");
18502
18528
  this.instance.emitEvent("onNodeUpdated", node);
18503
18529
  }
@@ -18522,15 +18548,18 @@ var WeaveStateManager = class {
18522
18548
  }
18523
18549
  this.logger.info({ key: node.key }, "removeNode: node to remove");
18524
18550
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent before remove");
18525
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18526
- for (let i = newChildren.length - 1; i >= 0; i--) if (newChildren[i].key === node.key) {
18527
- newChildren.splice(i, 1);
18528
- break;
18529
- }
18530
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18531
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18532
- this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent after remove");
18533
- this.instance.emitEvent("onNodeRemoved", node);
18551
+ const doc = (0, __syncedstore_core.getYjsDoc)(state);
18552
+ doc.transact(() => {
18553
+ if (parent.props.children) {
18554
+ for (let i = parent.props.children.length - 1; i >= 0; i--) if (parent.props.children[i].key === node.key) {
18555
+ parent.props.children.splice(i, 1);
18556
+ break;
18557
+ }
18558
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18559
+ this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent after remove");
18560
+ this.instance.emitEvent("onNodeRemoved", node);
18561
+ }
18562
+ });
18534
18563
  }
18535
18564
  removeNodes(nodes) {
18536
18565
  for (const node of nodes) this.removeNode(node);
@@ -18549,18 +18578,32 @@ var WeaveStateManager = class {
18549
18578
  this.logger.warn({ node }, msg);
18550
18579
  return;
18551
18580
  }
18581
+ if (!parent) {
18582
+ const msg = `Parent doesn't exists, cannot move it`;
18583
+ this.logger.warn({ node }, msg);
18584
+ return;
18585
+ }
18552
18586
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "moveNode: parent before move");
18553
- if (parent && parent.props.children) {
18587
+ if (parent.props.children) {
18554
18588
  const childrenAmount = parent.props.children.length;
18555
18589
  const nodeIndex = parent.props.children.findIndex((child) => child.key === node.key);
18556
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18557
- newChildren.splice(nodeIndex, 1);
18558
- if (position === __inditextech_weave_types.WEAVE_NODE_POSITION.UP) newChildren.splice(nodeIndex + 1, 0, { ...node });
18559
- if (position === __inditextech_weave_types.WEAVE_NODE_POSITION.DOWN) newChildren.splice(nodeIndex - 1, 0, { ...node });
18560
- if (position === __inditextech_weave_types.WEAVE_NODE_POSITION.FRONT) newChildren.splice(childrenAmount - 1, 0, { ...node });
18561
- if (position === __inditextech_weave_types.WEAVE_NODE_POSITION.BACK) newChildren.splice(0, 0, { ...node });
18562
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18563
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18590
+ if (nodeIndex === -1) {
18591
+ const msg = `Element doesn't exists on parent, cannot move it`;
18592
+ this.logger.warn({ node }, msg);
18593
+ return;
18594
+ }
18595
+ const doc = (0, __syncedstore_core.getYjsDoc)(state);
18596
+ doc.transact(() => {
18597
+ if (parent.props.children) {
18598
+ const item = JSON.parse(JSON.stringify(parent.props.children[nodeIndex]));
18599
+ parent.props.children.splice(nodeIndex, 1);
18600
+ if (item && position === __inditextech_weave_types.WEAVE_NODE_POSITION.UP) parent.props.children.splice(nodeIndex + 1, 0, item);
18601
+ if (item && position === __inditextech_weave_types.WEAVE_NODE_POSITION.DOWN) parent.props.children.splice(nodeIndex - 1, 0, item);
18602
+ if (item && position === __inditextech_weave_types.WEAVE_NODE_POSITION.FRONT) parent.props.children.splice(childrenAmount - 1, 0, item);
18603
+ if (item && position === __inditextech_weave_types.WEAVE_NODE_POSITION.BACK) parent.props.children.splice(0, 0, item);
18604
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18605
+ }
18606
+ });
18564
18607
  }
18565
18608
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "moveNode: parent after move");
18566
18609
  }
@@ -18647,7 +18690,7 @@ var WeaveRegisterManager = class {
18647
18690
 
18648
18691
  //#endregion
18649
18692
  //#region package.json
18650
- var version = "0.41.0";
18693
+ var version = "0.42.0";
18651
18694
 
18652
18695
  //#endregion
18653
18696
  //#region src/managers/setup.ts
@@ -19791,7 +19834,6 @@ var WeaveTextNode = class extends WeaveNode {
19791
19834
  });
19792
19835
  resetScale(text);
19793
19836
  text.fontSize(text.fontSize() * text.scaleY());
19794
- this.instance.updateNode(this.serialize(text));
19795
19837
  e.cancelBubble = true;
19796
19838
  }
19797
19839
  });
package/dist/sdk.js CHANGED
@@ -16961,11 +16961,7 @@ var WeaveNode = class {
16961
16961
  const nodesSnappingPlugin = this.instance.getPlugin("nodesSnapping");
16962
16962
  if (nodesSelectionPlugin && this.isSelecting() && this.isNodeSelected(node$1)) nodesSelectionPlugin.getTransformer().forceUpdate();
16963
16963
  if (nodesSnappingPlugin && transforming && this.isSelecting() && this.isNodeSelected(node$1)) nodesSnappingPlugin.evaluateGuidelines(e);
16964
- if (this.isSelecting() && this.isNodeSelected(node$1)) {
16965
- this.scaleReset(node$1);
16966
- const nodeHandler = this.instance.getNodeHandler(node$1.getAttrs().nodeType);
16967
- if (nodeHandler) this.instance.updateNode(nodeHandler.serialize(node$1));
16968
- }
16964
+ if (this.isSelecting() && this.isNodeSelected(node$1)) this.scaleReset(node$1);
16969
16965
  };
16970
16966
  node.on("transform", (0, import_lodash.throttle)(handleTransform, 100));
16971
16967
  node.on("transformend", (e) => {
@@ -18454,28 +18450,57 @@ var WeaveStateManager = class {
18454
18450
  return;
18455
18451
  }
18456
18452
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent before init");
18457
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18458
18453
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent before add");
18459
- if (index) {
18460
- newChildren?.splice(index, 0, node);
18461
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18462
- }
18463
- if (!index) {
18464
- const childrenAmount = newChildren.length;
18465
- node.props.zIndex = childrenAmount;
18466
- const nodeToAdd = {
18467
- ...node,
18468
- props: {
18469
- ...node.props,
18470
- zIndex: childrenAmount
18471
- }
18472
- };
18473
- this.logger.info({ node: JSON.parse(JSON.stringify(nodeToAdd)) }, "addNode: node to add");
18474
- newChildren.push(nodeToAdd);
18454
+ const doc = getYjsDoc(state);
18455
+ doc.transact(() => {
18456
+ if (!parent.props.children) parent.props.children = [];
18457
+ if (index) {
18458
+ parent.props.children.splice(index, 0, node);
18459
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18460
+ }
18461
+ if (!index) {
18462
+ const childrenAmount = parent.props.children.length;
18463
+ const nodeToAdd = {
18464
+ ...node,
18465
+ props: {
18466
+ ...node.props,
18467
+ zIndex: childrenAmount
18468
+ }
18469
+ };
18470
+ this.logger.info({ node: JSON.parse(JSON.stringify(nodeToAdd)) }, "addNode: node to add");
18471
+ parent.props.children.push(nodeToAdd);
18472
+ }
18473
+ this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent after add");
18474
+ this.instance.emitEvent("onNodeAdded", node);
18475
+ });
18476
+ }
18477
+ deepSyncSyncedStore(target, source) {
18478
+ for (const key in target) if (!(key in source)) delete target[key];
18479
+ for (const key in source) {
18480
+ const srcVal = source[key];
18481
+ const tgtVal = target[key];
18482
+ const bothAreObjects = this.isObject(srcVal) && this.isObject(tgtVal);
18483
+ if (bothAreObjects && !Array.isArray(srcVal)) this.deepSyncSyncedStore(tgtVal, srcVal);
18484
+ else if (Array.isArray(srcVal)) this.syncArray(target, key, srcVal);
18485
+ else if (tgtVal !== srcVal) target[key] = srcVal;
18486
+ }
18487
+ }
18488
+ syncArray(target, key, sourceArr) {
18489
+ const tgtArr = target[key];
18490
+ if (!Array.isArray(tgtArr)) {
18491
+ target[key] = [...sourceArr];
18492
+ return;
18493
+ }
18494
+ while (tgtArr.length > sourceArr.length) tgtArr.pop();
18495
+ for (let i = 0; i < sourceArr.length; i++) {
18496
+ const srcItem = sourceArr[i];
18497
+ const tgtItem = tgtArr[i];
18498
+ if (this.isObject(srcItem) && this.isObject(tgtItem)) this.deepSyncSyncedStore(tgtItem, srcItem);
18499
+ else if (tgtItem !== srcItem) tgtArr[i] = srcItem;
18475
18500
  }
18476
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18477
- this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "addNode: parent after add");
18478
- this.instance.emitEvent("onNodeAdded", node);
18501
+ }
18502
+ isObject(val) {
18503
+ return typeof val === "object" && val !== null;
18479
18504
  }
18480
18505
  updateNode(node) {
18481
18506
  const state = this.instance.getStore().getState();
@@ -18491,13 +18516,14 @@ var WeaveStateManager = class {
18491
18516
  this.logger.warn({ node }, msg);
18492
18517
  return;
18493
18518
  }
18494
- this.logger.info({ node: JSON.parse(JSON.stringify(nodeState)) }, "updateNode: before update");
18495
- const newNode = JSON.parse(JSON.stringify(nodeState));
18496
- newNode.props = {
18497
- ...newNode.props,
18498
- ...node.props
18499
- };
18500
- if (typeof newNode.props !== "undefined") nodeState.props = newNode.props;
18519
+ this.logger.info({
18520
+ node: JSON.parse(JSON.stringify(nodeState)),
18521
+ toUpdate: node
18522
+ }, "updateNode: before update");
18523
+ const doc = getYjsDoc(state);
18524
+ doc.transact(() => {
18525
+ this.deepSyncSyncedStore(nodeState.props, node.props);
18526
+ });
18501
18527
  this.logger.info({ node: JSON.parse(JSON.stringify(nodeState)) }, "updateNode: after update");
18502
18528
  this.instance.emitEvent("onNodeUpdated", node);
18503
18529
  }
@@ -18522,15 +18548,18 @@ var WeaveStateManager = class {
18522
18548
  }
18523
18549
  this.logger.info({ key: node.key }, "removeNode: node to remove");
18524
18550
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent before remove");
18525
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18526
- for (let i = newChildren.length - 1; i >= 0; i--) if (newChildren[i].key === node.key) {
18527
- newChildren.splice(i, 1);
18528
- break;
18529
- }
18530
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18531
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18532
- this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent after remove");
18533
- this.instance.emitEvent("onNodeRemoved", node);
18551
+ const doc = getYjsDoc(state);
18552
+ doc.transact(() => {
18553
+ if (parent.props.children) {
18554
+ for (let i = parent.props.children.length - 1; i >= 0; i--) if (parent.props.children[i].key === node.key) {
18555
+ parent.props.children.splice(i, 1);
18556
+ break;
18557
+ }
18558
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18559
+ this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "removeNode: parent after remove");
18560
+ this.instance.emitEvent("onNodeRemoved", node);
18561
+ }
18562
+ });
18534
18563
  }
18535
18564
  removeNodes(nodes) {
18536
18565
  for (const node of nodes) this.removeNode(node);
@@ -18549,18 +18578,32 @@ var WeaveStateManager = class {
18549
18578
  this.logger.warn({ node }, msg);
18550
18579
  return;
18551
18580
  }
18581
+ if (!parent) {
18582
+ const msg = `Parent doesn't exists, cannot move it`;
18583
+ this.logger.warn({ node }, msg);
18584
+ return;
18585
+ }
18552
18586
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "moveNode: parent before move");
18553
- if (parent && parent.props.children) {
18587
+ if (parent.props.children) {
18554
18588
  const childrenAmount = parent.props.children.length;
18555
18589
  const nodeIndex = parent.props.children.findIndex((child) => child.key === node.key);
18556
- const newChildren = JSON.parse(JSON.stringify(parent.props.children ?? []));
18557
- newChildren.splice(nodeIndex, 1);
18558
- if (position === WEAVE_NODE_POSITION.UP) newChildren.splice(nodeIndex + 1, 0, { ...node });
18559
- if (position === WEAVE_NODE_POSITION.DOWN) newChildren.splice(nodeIndex - 1, 0, { ...node });
18560
- if (position === WEAVE_NODE_POSITION.FRONT) newChildren.splice(childrenAmount - 1, 0, { ...node });
18561
- if (position === WEAVE_NODE_POSITION.BACK) newChildren.splice(0, 0, { ...node });
18562
- for (let i = 0; i < newChildren.length; i++) newChildren[i].props.zIndex = i;
18563
- if (typeof newChildren !== "undefined") parent.props.children = newChildren;
18590
+ if (nodeIndex === -1) {
18591
+ const msg = `Element doesn't exists on parent, cannot move it`;
18592
+ this.logger.warn({ node }, msg);
18593
+ return;
18594
+ }
18595
+ const doc = getYjsDoc(state);
18596
+ doc.transact(() => {
18597
+ if (parent.props.children) {
18598
+ const item = JSON.parse(JSON.stringify(parent.props.children[nodeIndex]));
18599
+ parent.props.children.splice(nodeIndex, 1);
18600
+ if (item && position === WEAVE_NODE_POSITION.UP) parent.props.children.splice(nodeIndex + 1, 0, item);
18601
+ if (item && position === WEAVE_NODE_POSITION.DOWN) parent.props.children.splice(nodeIndex - 1, 0, item);
18602
+ if (item && position === WEAVE_NODE_POSITION.FRONT) parent.props.children.splice(childrenAmount - 1, 0, item);
18603
+ if (item && position === WEAVE_NODE_POSITION.BACK) parent.props.children.splice(0, 0, item);
18604
+ for (let i = 0; i < parent.props.children.length; i++) parent.props.children[i].props.zIndex = i;
18605
+ }
18606
+ });
18564
18607
  }
18565
18608
  this.logger.info({ parent: JSON.parse(JSON.stringify(parent)) }, "moveNode: parent after move");
18566
18609
  }
@@ -18647,7 +18690,7 @@ var WeaveRegisterManager = class {
18647
18690
 
18648
18691
  //#endregion
18649
18692
  //#region package.json
18650
- var version = "0.41.0";
18693
+ var version = "0.42.0";
18651
18694
 
18652
18695
  //#endregion
18653
18696
  //#region src/managers/setup.ts
@@ -19791,7 +19834,6 @@ var WeaveTextNode = class extends WeaveNode {
19791
19834
  });
19792
19835
  resetScale(text);
19793
19836
  text.fontSize(text.fontSize() * text.scaleY());
19794
- this.instance.updateNode(this.serialize(text));
19795
19837
  e.cancelBubble = true;
19796
19838
  }
19797
19839
  });