@inditextech/weave-sdk 2.13.1 → 2.15.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
@@ -1,7 +1,7 @@
1
1
  import Konva from "konva";
2
2
  import { randomFillSync, randomUUID } from "crypto";
3
3
  import pino from "pino";
4
- import { WEAVE_ASYNC_STATUS, WEAVE_AWARENESS_LAYER_ID, WEAVE_EXPORT_BACKGROUND_COLOR, WEAVE_EXPORT_FORMATS, WEAVE_INSTANCE_STATUS, WEAVE_KONVA_BACKEND, WEAVE_NODE_CUSTOM_EVENTS, WEAVE_NODE_LAYER_ID, WEAVE_NODE_POSITION, WEAVE_STORE_CONNECTION_STATUS, WEAVE_UTILITY_LAYER_ID } from "@inditextech/weave-types";
4
+ import { WEAVE_ASYNC_STATUS, WEAVE_AWARENESS_LAYER_ID, WEAVE_EXPORT_BACKGROUND_COLOR, WEAVE_EXPORT_FORMATS, WEAVE_INSTANCE_STATUS, WEAVE_KONVA_BACKEND, WEAVE_LOG_LEVEL, WEAVE_NODE_CHANGE_TYPE, WEAVE_NODE_CUSTOM_EVENTS, WEAVE_NODE_LAYER_ID, WEAVE_NODE_POSITION, WEAVE_STORE_CONNECTION_STATUS, WEAVE_UTILITY_LAYER_ID } from "@inditextech/weave-types";
5
5
  import { getYjsDoc, getYjsValue, observeDeep, syncedStore } from "@syncedstore/core";
6
6
  import * as Y from "yjs";
7
7
  import React from "react";
@@ -15141,7 +15141,6 @@ var WeaveStore = class {
15141
15141
  }
15142
15142
  observeDeep(this.getState(), () => {
15143
15143
  const newState = JSON.parse(JSON.stringify(this.getState()));
15144
- this.logger.debug({ newState }, "State changed");
15145
15144
  this.instance.emitEvent("onStateChange", newState);
15146
15145
  const nodesSelectionPlugin = this.instance.getPlugin("nodesSelection");
15147
15146
  if (this.isRoomLoaded && nodesSelectionPlugin?.getSelectedNodes().length === 1) {
@@ -17675,8 +17674,11 @@ function moveNodeToContainer(instance, node, containerToMove, originalNode, orig
17675
17674
  if (nodeHandler) {
17676
17675
  instance.stateTransactional(() => {
17677
17676
  const actualNode = nodeHandler.serialize(node);
17678
- instance.removeNodeNT(actualNode);
17679
- instance.addNodeNT(actualNode, layerToMoveAttrs.id);
17677
+ instance.removeNodeNT(actualNode, { emitUserChangeEvent: false });
17678
+ instance.addNodeNT(actualNode, layerToMoveAttrs.id, {
17679
+ emitUserChangeEvent: true,
17680
+ overrideUserChangeType: WEAVE_NODE_CHANGE_TYPE.UPDATE
17681
+ });
17680
17682
  });
17681
17683
  return true;
17682
17684
  }
@@ -18591,6 +18593,10 @@ const WEAVE_NODES_MULTI_SELECTION_FEEDBACK_PLUGIN_DEFAULT_CONFIG = { style: {
18591
18593
  //#endregion
18592
18594
  //#region src/constants.ts
18593
18595
  const DEFAULT_THROTTLE_MS = 50;
18596
+ const DEFAULT_ADD_NODE_OPTIONS = { emitUserChangeEvent: true };
18597
+ const DEFAULT_UPDATE_NODE_OPTIONS = { emitUserChangeEvent: true };
18598
+ const DEFAULT_REMOVE_NODE_OPTIONS = { emitUserChangeEvent: true };
18599
+ const DEFAULT_MOVE_NODE_OPTIONS = { emitUserChangeEvent: true };
18594
18600
 
18595
18601
  //#endregion
18596
18602
  //#region src/plugins/users-presence/constants.ts
@@ -21148,7 +21154,7 @@ var WeaveGroupsManager = class {
21148
21154
  const groupHandler = this.instance.getNodeHandler("group");
21149
21155
  if (groupHandler) {
21150
21156
  const groupNode$1 = groupHandler.create(groupId, { draggable: true });
21151
- this.instance.addNode(groupNode$1, parentNodeId);
21157
+ this.instance.addNode(groupNode$1, parentNodeId, { emitUserChangeEvent: false });
21152
21158
  }
21153
21159
  const nodesWithZIndex = realNodes.map((node) => {
21154
21160
  const instance = mainLayer?.findOne(`#${node.key}`);
@@ -21174,7 +21180,7 @@ var WeaveGroupsManager = class {
21174
21180
  const handler = this.instance.getNodeHandler("group");
21175
21181
  if (handler) {
21176
21182
  const stateNode = handler.serialize(konvaGroup);
21177
- this.instance.addNode(stateNode, groupId);
21183
+ this.instance.addNode(stateNode, groupId, { emitUserChangeEvent: false });
21178
21184
  }
21179
21185
  }
21180
21186
  continue;
@@ -21192,7 +21198,7 @@ var WeaveGroupsManager = class {
21192
21198
  const handler = this.instance.getNodeHandler(konvaNode.getAttrs().nodeType);
21193
21199
  if (handler) {
21194
21200
  const stateNode = handler.serialize(konvaNode);
21195
- this.instance.addNode(stateNode, groupId);
21201
+ this.instance.addNode(stateNode, groupId, { emitUserChangeEvent: false });
21196
21202
  }
21197
21203
  }
21198
21204
  }
@@ -21277,7 +21283,7 @@ var WeaveGroupsManager = class {
21277
21283
  const groupHandler = this.instance.getNodeHandler("group");
21278
21284
  if (groupHandler) {
21279
21285
  const groupNode = groupHandler.serialize(konvaGroup);
21280
- this.instance.removeNode(groupNode);
21286
+ this.instance.removeNode(groupNode, { emitUserChangeEvent: false });
21281
21287
  }
21282
21288
  setTimeout(() => {
21283
21289
  this.getNodesMultiSelectionFeedbackPlugin()?.cleanupSelectedHalos();
@@ -21307,12 +21313,13 @@ var WeaveGroupsManager = class {
21307
21313
  //#endregion
21308
21314
  //#region src/logger/logger.ts
21309
21315
  var WeaveLogger = class {
21310
- constructor(config) {
21316
+ constructor(instance, config) {
21317
+ this.instance = instance;
21311
21318
  this.config = config;
21312
21319
  this.disabled = this.config.disabled ?? false;
21313
21320
  this.logger = pino({
21314
21321
  name: "weave.js",
21315
- level: this.config.level ?? "error",
21322
+ level: this.config.level ?? WEAVE_LOG_LEVEL.ERROR,
21316
21323
  browser: {
21317
21324
  write: {
21318
21325
  warn: (o) => {
@@ -21380,7 +21387,14 @@ var WeaveLogger = class {
21380
21387
  return this.logger;
21381
21388
  }
21382
21389
  getChildLogger(name) {
21383
- return this.logger.child({ name });
21390
+ const configuration = this.instance.getConfiguration();
21391
+ const modulesLogging = configuration.logger?.modules ?? [];
21392
+ let childLoggerLevel = configuration.logger?.level ?? WEAVE_LOG_LEVEL.ERROR;
21393
+ for (const moduleLevel of modulesLogging) {
21394
+ const [moduleName, level] = moduleLevel.split(":");
21395
+ if (name === moduleName) childLoggerLevel = level;
21396
+ }
21397
+ return this.logger.child({ name }, { level: childLoggerLevel });
21384
21398
  }
21385
21399
  };
21386
21400
 
@@ -21614,76 +21628,6 @@ var WeaveCloningManager = class {
21614
21628
  minPoint
21615
21629
  };
21616
21630
  }
21617
- cloneNodes(instancesToClone, targetContainer, onPoint) {
21618
- if (instancesToClone.length === 0) return;
21619
- if (!targetContainer) return;
21620
- const groupId = v4_default();
21621
- const newGroup = new Konva.Group({ id: groupId });
21622
- targetContainer.add(newGroup);
21623
- const nodesWithZIndex = instancesToClone.map((node) => ({
21624
- node,
21625
- zIndex: node.zIndex()
21626
- })).filter((node) => node.zIndex !== -1);
21627
- const sortedNodesByZIndex = (0, import_lodash.orderBy)(nodesWithZIndex, ["zIndex"], ["asc"]).map((node) => node.node);
21628
- for (const [index, node] of sortedNodesByZIndex.entries()) {
21629
- const nodeAttrs = node.getAttrs();
21630
- if (nodeAttrs.type === "group") {
21631
- const clonedNode$1 = node.clone({
21632
- id: v4_default(),
21633
- type: "group"
21634
- });
21635
- const nodePos$1 = clonedNode$1.getAbsolutePosition();
21636
- const nodeRotation$1 = clonedNode$1.getAbsoluteRotation();
21637
- const parent$1 = node.getParent();
21638
- if (parent$1 && parent$1.getAttrs().nodeId && !parent$1.getAttrs().containerId) {
21639
- const realParent = this.instance.getStage().findOne(`#${parent$1.getAttrs().nodeId}`);
21640
- if (realParent) {
21641
- nodePos$1.x += realParent.x();
21642
- nodePos$1.y += realParent.y();
21643
- }
21644
- }
21645
- clonedNode$1.moveTo(newGroup);
21646
- clonedNode$1.zIndex(index);
21647
- clonedNode$1.setAbsolutePosition(nodePos$1);
21648
- clonedNode$1.rotation(nodeRotation$1);
21649
- continue;
21650
- }
21651
- const clonedNode = node.clone({ id: v4_default() });
21652
- const nodePos = clonedNode.getAbsolutePosition();
21653
- const nodeRotation = clonedNode.getAbsoluteRotation();
21654
- const parent = node.getParent();
21655
- if (parent && parent.getAttrs().nodeId && !parent.getAttrs().containerId) {
21656
- const realParent = this.instance.getStage().findOne(`#${parent.getAttrs().nodeId}`);
21657
- if (realParent) {
21658
- nodePos.x += realParent.x();
21659
- nodePos.y += realParent.y();
21660
- }
21661
- }
21662
- clonedNode.moveTo(newGroup);
21663
- clonedNode.zIndex(index);
21664
- clonedNode.setAbsolutePosition(nodePos);
21665
- clonedNode.rotation(nodeRotation);
21666
- }
21667
- const actualPos = newGroup.getClientRect({ relativeTo: targetContainer });
21668
- newGroup.x(onPoint.x - actualPos.x);
21669
- newGroup.y(onPoint.y - actualPos.y);
21670
- const groupChildren = [...newGroup.getChildren()];
21671
- for (const [index, node] of groupChildren.entries()) {
21672
- const nodePos = node.getAbsolutePosition();
21673
- const nodeRotation = node.getAbsoluteRotation();
21674
- node.moveTo(targetContainer);
21675
- node.zIndex(index);
21676
- node.setAbsolutePosition(nodePos);
21677
- node.rotation(nodeRotation);
21678
- const handler = this.instance.getNodeHandler(node.getAttrs().nodeType);
21679
- if (handler) {
21680
- const stateNode = handler.serialize(node);
21681
- this.instance.addNode(stateNode, targetContainer.getAttrs().id);
21682
- }
21683
- node.destroy();
21684
- }
21685
- newGroup.destroy();
21686
- }
21687
21631
  recursivelyUpdateKeys(nodes) {
21688
21632
  for (const child of nodes) {
21689
21633
  const newNodeId = v4_default();
@@ -21702,7 +21646,7 @@ var WeaveCloningManager = class {
21702
21646
  serializedNode.key = newNodeId;
21703
21647
  serializedNode.props.id = newNodeId;
21704
21648
  const realParent = this.instance.getInstanceRecursive(parent);
21705
- this.instance.addNode(serializedNode, realParent?.getAttrs().id);
21649
+ this.instance.addNode(serializedNode, realParent?.getAttrs().id, { emitUserChangeEvent: false });
21706
21650
  return this.instance.getStage().findOne(`#${newNodeId}`);
21707
21651
  }
21708
21652
  addClone(node) {
@@ -21767,7 +21711,7 @@ var WeaveZIndexManager = class {
21767
21711
  const handler = this.instance.getNodeHandler(instance.getAttrs().nodeType);
21768
21712
  if (handler) {
21769
21713
  const nodeState = handler.serialize(instance);
21770
- this.instance.moveNode(nodeState, WEAVE_NODE_POSITION.UP);
21714
+ this.instance.zMoveNode(nodeState, WEAVE_NODE_POSITION.UP);
21771
21715
  }
21772
21716
  }
21773
21717
  moveDown(instance) {
@@ -21775,7 +21719,7 @@ var WeaveZIndexManager = class {
21775
21719
  const handler = this.instance.getNodeHandler(instance.getAttrs().nodeType);
21776
21720
  if (handler) {
21777
21721
  const nodeState = handler.serialize(instance);
21778
- this.instance.moveNode(nodeState, WEAVE_NODE_POSITION.DOWN);
21722
+ this.instance.zMoveNode(nodeState, WEAVE_NODE_POSITION.DOWN);
21779
21723
  }
21780
21724
  }
21781
21725
  sendToBack(instances) {
@@ -21785,7 +21729,7 @@ var WeaveZIndexManager = class {
21785
21729
  const handler = this.instance.getNodeHandler(node.getAttrs().nodeType);
21786
21730
  if (handler) {
21787
21731
  const nodeState = handler.serialize(node);
21788
- this.instance.moveNode(nodeState, WEAVE_NODE_POSITION.BACK);
21732
+ this.instance.zMoveNode(nodeState, WEAVE_NODE_POSITION.BACK);
21789
21733
  }
21790
21734
  }
21791
21735
  }
@@ -21796,7 +21740,7 @@ var WeaveZIndexManager = class {
21796
21740
  const handler = this.instance.getNodeHandler(node.getAttrs().nodeType);
21797
21741
  if (handler) {
21798
21742
  const nodeState = handler.serialize(node);
21799
- this.instance.moveNode(nodeState, WEAVE_NODE_POSITION.FRONT);
21743
+ this.instance.zMoveNode(nodeState, WEAVE_NODE_POSITION.FRONT);
21800
21744
  }
21801
21745
  }
21802
21746
  }
@@ -21991,7 +21935,7 @@ var WeaveStateManager = class {
21991
21935
  this.instance.emitEvent("onNodeRemoved", node);
21992
21936
  }
21993
21937
  }
21994
- moveNode(node, position) {
21938
+ zMoveNode(node, position) {
21995
21939
  const state = this.instance.getStore().getState();
21996
21940
  if ((0, import_lodash.isEmpty)(state.weave)) {
21997
21941
  const msg = `State is empty, cannot update the node`;
@@ -22113,7 +22057,7 @@ var WeaveRegisterManager = class {
22113
22057
 
22114
22058
  //#endregion
22115
22059
  //#region package.json
22116
- var version = "2.13.1";
22060
+ var version = "2.15.0";
22117
22061
 
22118
22062
  //#endregion
22119
22063
  //#region src/managers/setup.ts
@@ -22629,14 +22573,19 @@ var WeaveMutexManager = class {
22629
22573
  (changes) => {
22630
22574
  const actUser = this.instance.getStore().getUser();
22631
22575
  for (const change of changes) {
22632
- if (!change[this.WEAVE_USER_MUTEX_LOCK_KEY] && change[WEAVE_CONNECTED_USER_INFO_KEY] && actUser.id !== change[WEAVE_CONNECTED_USER_INFO_KEY].id) {
22576
+ const remoteUser = change[WEAVE_CONNECTED_USER_INFO_KEY];
22577
+ if (!remoteUser) continue;
22578
+ const actualMutexLock = this.getUserMutexLock(this.getUserMutexKey(change[WEAVE_CONNECTED_USER_INFO_KEY]));
22579
+ if (change[this.WEAVE_USER_MUTEX_LOCK_KEY] === void 0 && actualMutexLock !== void 0 && actUser.id !== remoteUser.id) {
22633
22580
  const user = change[WEAVE_CONNECTED_USER_INFO_KEY];
22581
+ this.logger.debug(`Asked for release for remote user: ${user.name}`);
22634
22582
  this.releaseMutexLockRemote(user);
22635
22583
  continue;
22636
22584
  }
22637
- if (change[WEAVE_CONNECTED_USER_INFO_KEY] && actUser.id !== change[WEAVE_CONNECTED_USER_INFO_KEY].id) {
22585
+ if (change[this.WEAVE_USER_MUTEX_LOCK_KEY] !== void 0 && actualMutexLock === void 0 && actUser.id !== remoteUser.id) {
22638
22586
  const user = change[WEAVE_CONNECTED_USER_INFO_KEY];
22639
22587
  const mutexInfo = change[this.WEAVE_USER_MUTEX_LOCK_KEY];
22588
+ this.logger.debug(`Asked for lock for remote user: ${user.name}`);
22640
22589
  this.setMutexLockRemote({
22641
22590
  nodeIds: mutexInfo.nodeIds,
22642
22591
  operation: mutexInfo.operation,
@@ -22709,13 +22658,17 @@ var WeaveMutexManager = class {
22709
22658
  operation,
22710
22659
  metadata
22711
22660
  });
22712
- if (sendAwareness) store.setAwarenessInfo(this.WEAVE_USER_MUTEX_LOCK_KEY, {
22713
- user,
22714
- nodeIds,
22715
- operation,
22716
- metadata
22717
- });
22661
+ if (sendAwareness) {
22662
+ this.logger.debug(`Propagating lock for user: ${user.name}`);
22663
+ store.setAwarenessInfo(this.WEAVE_USER_MUTEX_LOCK_KEY, {
22664
+ user,
22665
+ nodeIds,
22666
+ operation,
22667
+ metadata
22668
+ });
22669
+ }
22718
22670
  this.instance.emitEvent("onMutexLockChange", { locks: [...this.userMutexLocked.keys()] });
22671
+ this.logger.debug(`Setting lock for user: ${user.name}`);
22719
22672
  return true;
22720
22673
  }
22721
22674
  if (preLockedNodes.length !== nodeIds.length) {
@@ -22741,14 +22694,18 @@ var WeaveMutexManager = class {
22741
22694
  if (nodeInstance) nodeInstance.releaseMutex();
22742
22695
  }
22743
22696
  this.userMutexLocked.delete(userMutexKey);
22744
- if (sendAwareness) store.setAwarenessInfo(this.WEAVE_USER_MUTEX_LOCK_KEY, void 0);
22697
+ if (sendAwareness) {
22698
+ this.logger.debug(`Propagating release for user: ${user.name}`);
22699
+ store.setAwarenessInfo(this.WEAVE_USER_MUTEX_LOCK_KEY, void 0);
22700
+ }
22745
22701
  this.instance.emitEvent("onMutexLockChange", { locks: [...this.userMutexLocked.keys()] });
22702
+ this.logger.debug(`Releasing lock for user: ${user.name}`);
22746
22703
  }
22747
22704
  }
22748
22705
  };
22749
22706
 
22750
22707
  //#endregion
22751
- //#region src/managers/async/watch-map.ts
22708
+ //#region src/utils/watch-map.ts
22752
22709
  function watchMap(onChange, map = new Map()) {
22753
22710
  const handler = { get(target, prop, receiver) {
22754
22711
  if (prop === "set") return (key, value$1) => {
@@ -22851,6 +22808,53 @@ var WeaveHooksManager = class {
22851
22808
  }
22852
22809
  };
22853
22810
 
22811
+ //#endregion
22812
+ //#region src/managers/users/constants.ts
22813
+ const WEAVE_USER_INFO_KEY = "userInfo";
22814
+
22815
+ //#endregion
22816
+ //#region src/managers/users/users.ts
22817
+ var WeaveUsersManager = class {
22818
+ constructor(instance) {
22819
+ this.instance = instance;
22820
+ this.logger = this.instance.getChildLogger("users-manager");
22821
+ this.logger.debug("Users manager created");
22822
+ this.connectedUsers = new Map();
22823
+ this.init();
22824
+ }
22825
+ init() {
22826
+ this.instance.addEventListener("onAwarenessChange", (changes) => {
22827
+ let usersChanged = false;
22828
+ const actualUsers = Array.from(this.connectedUsers.values());
22829
+ const users = [];
22830
+ for (const change of changes) {
22831
+ if (!change[WEAVE_USER_INFO_KEY]) continue;
22832
+ if (change[WEAVE_USER_INFO_KEY]) {
22833
+ const userInformation = change[WEAVE_USER_INFO_KEY];
22834
+ users.push(userInformation);
22835
+ }
22836
+ }
22837
+ for (const user of users) if (!this.connectedUsers.has(user.id)) {
22838
+ this.logger.debug(`User connected: ${user.name}`);
22839
+ this.connectedUsers.set(user.id, user);
22840
+ usersChanged = true;
22841
+ }
22842
+ for (const userInformation of actualUsers) {
22843
+ const user = users.find((u) => u.id === userInformation.id);
22844
+ if (user === void 0) {
22845
+ this.logger.debug(`User disconnected: ${userInformation.name}`);
22846
+ this.connectedUsers.delete(userInformation.id);
22847
+ usersChanged = true;
22848
+ }
22849
+ }
22850
+ if (usersChanged) this.instance.emitEvent("onUsersChange");
22851
+ });
22852
+ }
22853
+ getUsers() {
22854
+ return Array.from(this.connectedUsers.values());
22855
+ }
22856
+ };
22857
+
22854
22858
  //#endregion
22855
22859
  //#region src/weave.ts
22856
22860
  var Weave = class {
@@ -22864,7 +22868,7 @@ var Weave = class {
22864
22868
  this.id = v4_default();
22865
22869
  this.initialized = false;
22866
22870
  this.config = mergeExceptArrays({}, weaveConfig);
22867
- this.logger = new WeaveLogger(this.config?.logger ?? {
22871
+ this.logger = new WeaveLogger(this, this.config?.logger ?? {
22868
22872
  disabled: false,
22869
22873
  level: "error"
22870
22874
  });
@@ -22885,6 +22889,7 @@ var Weave = class {
22885
22889
  this.exportManager = new WeaveExportManager(this);
22886
22890
  this.actionsManager = new WeaveActionsManager(this);
22887
22891
  this.pluginsManager = new WeavePluginsManager(this);
22892
+ this.usersManager = new WeaveUsersManager(this);
22888
22893
  this.mutexManager = new WeaveMutexManager(this);
22889
22894
  this.asyncManager = new WeaveAsyncManager(this);
22890
22895
  this.hooksManager = new WeaveHooksManager(this);
@@ -22998,6 +23003,52 @@ var Weave = class {
22998
23003
  this.moduleLogger.debug(`Removing listening to event [${event}]`);
22999
23004
  this.emitter.off(event, callback);
23000
23005
  }
23006
+ emitUserChangeEvent(data, changeType) {
23007
+ const stage = this.getStage();
23008
+ const user = this.getStore().getUser();
23009
+ const node = data.node;
23010
+ let parent = void 0;
23011
+ let nodeParent = void 0;
23012
+ if (nodeParent === void 0 && data?.parentId !== void 0 && stage) nodeParent = stage.findOne(`#${data.parentId}`);
23013
+ if (nodeParent === void 0 && data?.parentId === void 0 && stage) {
23014
+ const parentNode = stage.findOne(`#${node.key}`);
23015
+ if (parentNode) nodeParent = this.getNodeContainer(parentNode);
23016
+ }
23017
+ if (nodeParent !== void 0) {
23018
+ const handler = this.getNodeHandler(nodeParent?.getAttrs().nodeType);
23019
+ if (handler) parent = handler.serialize(nodeParent);
23020
+ }
23021
+ if (!parent) return;
23022
+ this.emitEvent("onUserChange", {
23023
+ user,
23024
+ changeType,
23025
+ parent,
23026
+ node
23027
+ });
23028
+ this.cleanupTransactionIdToInstance(node);
23029
+ }
23030
+ setTransactionIdToInstance(node) {
23031
+ const realNode = this.getStage().findOne(`#${node.key}`);
23032
+ if (realNode) {
23033
+ node.props.transactionId = v4_default();
23034
+ realNode.setAttr("transactionId", node.props.transactionId);
23035
+ }
23036
+ }
23037
+ cleanupTransactionIdToInstance(node) {
23038
+ const realNode = this.getStage().findOne(`#${node.key}`);
23039
+ if (realNode) realNode.setAttr("transactionId", void 0);
23040
+ }
23041
+ decorateWithZIndex(node) {
23042
+ const realNode = this.getStage().findOne(`#${node.key}`);
23043
+ if (realNode) {
23044
+ const zIndex = realNode.zIndex();
23045
+ node.props = {
23046
+ ...node.props,
23047
+ zIndex
23048
+ };
23049
+ }
23050
+ return node;
23051
+ }
23001
23052
  getLogger() {
23002
23053
  return this.logger;
23003
23054
  }
@@ -23114,12 +23165,12 @@ var Weave = class {
23114
23165
  update(newState) {
23115
23166
  this.getStore().setState(newState);
23116
23167
  this.renderer.render(() => {
23117
- this.emitEvent("onRender", {});
23168
+ this.emitEvent("onRender");
23118
23169
  });
23119
23170
  }
23120
23171
  render() {
23121
23172
  this.renderer.render(() => {
23122
- this.emitEvent("onRender", {});
23173
+ this.emitEvent("onRender");
23123
23174
  });
23124
23175
  }
23125
23176
  findNodeById(tree, key, parent = null, index = -1) {
@@ -23131,43 +23182,102 @@ var Weave = class {
23131
23182
  getNode(nodeKey) {
23132
23183
  return this.stateManager.getNode(nodeKey);
23133
23184
  }
23134
- addNode(node, parentId = "mainLayer", index = void 0) {
23185
+ addNode(node, parentId = "mainLayer", options = DEFAULT_ADD_NODE_OPTIONS) {
23135
23186
  this.stateTransactional(() => {
23136
- this.stateManager.addNode(node, parentId, index);
23187
+ this.addNodeNT(node, parentId, options);
23137
23188
  });
23138
23189
  }
23139
- addNodeNT(node, parentId = "mainLayer", index = void 0) {
23190
+ addNodeNT(node, parentId = "mainLayer", options = DEFAULT_ADD_NODE_OPTIONS) {
23191
+ const { index, emitUserChangeEvent, overrideUserChangeType } = mergeExceptArrays(DEFAULT_ADD_NODE_OPTIONS, options);
23192
+ if (emitUserChangeEvent) this.setTransactionIdToInstance(node);
23140
23193
  this.stateManager.addNode(node, parentId, index);
23194
+ if (emitUserChangeEvent) {
23195
+ const handleSendEvent = (addedNode) => {
23196
+ if (node.props.transactionId === addedNode.getAttrs().transactionId) {
23197
+ const decoratedNode = this.decorateWithZIndex(node);
23198
+ this.emitUserChangeEvent({
23199
+ node: decoratedNode,
23200
+ parentId
23201
+ }, overrideUserChangeType ?? WEAVE_NODE_CHANGE_TYPE.CREATE);
23202
+ this.removeEventListener("onNodeRenderedAdded", handleSendEvent);
23203
+ }
23204
+ };
23205
+ this.addEventListener("onNodeRenderedAdded", handleSendEvent);
23206
+ }
23141
23207
  }
23142
- updateNode(node) {
23208
+ updateNode(node, options = DEFAULT_UPDATE_NODE_OPTIONS) {
23143
23209
  this.stateTransactional(() => {
23144
- this.stateManager.updateNode(node);
23210
+ this.updateNodeNT(node, options);
23145
23211
  });
23146
23212
  }
23147
- updateNodeNT(node) {
23213
+ updateNodeNT(node, options = DEFAULT_UPDATE_NODE_OPTIONS) {
23214
+ const { emitUserChangeEvent } = mergeExceptArrays(DEFAULT_UPDATE_NODE_OPTIONS, options);
23215
+ if (emitUserChangeEvent) this.setTransactionIdToInstance(node);
23148
23216
  this.stateManager.updateNode(node);
23217
+ if (emitUserChangeEvent) {
23218
+ const handleSendEvent = (updatedNode) => {
23219
+ if (node.props.transactionId === updatedNode.getAttrs().transactionId) {
23220
+ const decoratedNode = this.decorateWithZIndex(node);
23221
+ this.emitUserChangeEvent({ node: decoratedNode }, WEAVE_NODE_CHANGE_TYPE.UPDATE);
23222
+ this.removeEventListener("onNodeRenderedUpdated", handleSendEvent);
23223
+ }
23224
+ };
23225
+ this.addEventListener("onNodeRenderedUpdated", handleSendEvent);
23226
+ }
23149
23227
  }
23150
- updateNodes(nodes) {
23228
+ updateNodes(nodes, options = DEFAULT_UPDATE_NODE_OPTIONS) {
23151
23229
  this.stateTransactional(() => {
23152
- this.stateManager.updateNodes(nodes);
23230
+ this.updateNodesNT(nodes, options);
23153
23231
  });
23154
23232
  }
23155
- updateNodesNT(nodes) {
23233
+ updateNodesNT(nodes, options = DEFAULT_UPDATE_NODE_OPTIONS) {
23234
+ const { emitUserChangeEvent } = mergeExceptArrays(DEFAULT_UPDATE_NODE_OPTIONS, options);
23235
+ const transactionsIds = {};
23236
+ if (emitUserChangeEvent) for (const node of nodes) {
23237
+ this.setTransactionIdToInstance(node);
23238
+ transactionsIds[node.key] = node.props.transactionId;
23239
+ }
23156
23240
  this.stateManager.updateNodes(nodes);
23241
+ if (emitUserChangeEvent) {
23242
+ const handleSendEvent = (updatedNode) => {
23243
+ for (const node of nodes) if (transactionsIds[node.key] === updatedNode.getAttrs().transactionId) {
23244
+ const decoratedNode = this.decorateWithZIndex(node);
23245
+ this.emitUserChangeEvent({ node: decoratedNode }, WEAVE_NODE_CHANGE_TYPE.UPDATE);
23246
+ delete transactionsIds[node.key];
23247
+ if (Object.keys(transactionsIds).length === 0) this.removeEventListener("onNodeRenderedUpdated", handleSendEvent);
23248
+ break;
23249
+ }
23250
+ };
23251
+ this.addEventListener("onNodeRenderedUpdated", handleSendEvent);
23252
+ }
23157
23253
  }
23158
- removeNode(node) {
23254
+ removeNode(node, options = DEFAULT_REMOVE_NODE_OPTIONS) {
23159
23255
  this.stateTransactional(() => {
23160
- this.stateManager.removeNode(node);
23161
- this.runPhaseHooks("onRemoveNode", (hook) => {
23162
- const nodeInstance = this.getStage().findOne(`#${node.key}`);
23163
- if (nodeInstance) hook({ node: nodeInstance });
23164
- });
23165
- const selectionPlugin = this.getPlugin("nodesSelection");
23166
- if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
23256
+ this.removeNodeNT(node, options);
23167
23257
  });
23168
23258
  }
23169
- removeNodeNT(node) {
23259
+ removeNodeNT(node, options = DEFAULT_REMOVE_NODE_OPTIONS) {
23260
+ const { emitUserChangeEvent } = mergeExceptArrays(DEFAULT_REMOVE_NODE_OPTIONS, options);
23261
+ let parentId = void 0;
23262
+ let decoratedNode = void 0;
23263
+ if (emitUserChangeEvent) {
23264
+ this.setTransactionIdToInstance(node);
23265
+ decoratedNode = this.decorateWithZIndex(node);
23266
+ parentId = this.getContainerByNodeId(node.key);
23267
+ }
23170
23268
  this.stateManager.removeNode(node);
23269
+ if (decoratedNode && emitUserChangeEvent) {
23270
+ const handleSendEvent = (removedNode) => {
23271
+ if (node.props.transactionId === removedNode.getAttrs().transactionId) {
23272
+ this.emitUserChangeEvent({
23273
+ node: decoratedNode,
23274
+ parentId
23275
+ }, WEAVE_NODE_CHANGE_TYPE.DELETE);
23276
+ this.removeEventListener("onNodeRenderedRemoved", handleSendEvent);
23277
+ }
23278
+ };
23279
+ this.addEventListener("onNodeRenderedRemoved", handleSendEvent);
23280
+ }
23171
23281
  this.runPhaseHooks("onRemoveNode", (hook) => {
23172
23282
  const nodeInstance = this.getStage().findOne(`#${node.key}`);
23173
23283
  if (nodeInstance) hook({ node: nodeInstance });
@@ -23175,25 +23285,36 @@ var Weave = class {
23175
23285
  const selectionPlugin = this.getPlugin("nodesSelection");
23176
23286
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
23177
23287
  }
23178
- removeNodes(nodes) {
23288
+ removeNodes(nodes, options = DEFAULT_REMOVE_NODE_OPTIONS) {
23179
23289
  this.stateTransactional(() => {
23180
- for (const node of nodes) this.removeNodeNT(node);
23181
- const selectionPlugin = this.getPlugin("nodesSelection");
23182
- if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
23290
+ this.removeNodesNT(nodes, options);
23183
23291
  });
23184
23292
  }
23185
- removeNodesNT(nodes) {
23186
- for (const node of nodes) this.removeNodeNT(node);
23293
+ removeNodesNT(nodes, options = DEFAULT_REMOVE_NODE_OPTIONS) {
23294
+ for (const node of nodes) this.removeNodeNT(node, options);
23187
23295
  const selectionPlugin = this.getPlugin("nodesSelection");
23188
23296
  if (selectionPlugin) selectionPlugin.setSelectedNodes([]);
23189
23297
  }
23190
- moveNode(node, position) {
23298
+ zMoveNode(node, position, options = DEFAULT_MOVE_NODE_OPTIONS) {
23191
23299
  this.stateTransactional(() => {
23192
- this.stateManager.moveNode(node, position);
23193
- });
23194
- }
23195
- moveNodeNT(node, position) {
23196
- this.stateManager.moveNode(node, position);
23300
+ this.zMoveNodeNT(node, position, options);
23301
+ });
23302
+ }
23303
+ zMoveNodeNT(node, position, options = DEFAULT_MOVE_NODE_OPTIONS) {
23304
+ const { emitUserChangeEvent } = mergeExceptArrays(DEFAULT_MOVE_NODE_OPTIONS, options);
23305
+ if (emitUserChangeEvent) this.setTransactionIdToInstance(node);
23306
+ this.updateNodeNT(node, { emitUserChangeEvent: false });
23307
+ this.stateManager.zMoveNode(node, position);
23308
+ if (emitUserChangeEvent) {
23309
+ const handleSendEvent = (movedNode) => {
23310
+ if (node.props.transactionId === movedNode.getAttrs().transactionId) {
23311
+ const decoratedNode = this.decorateWithZIndex(node);
23312
+ this.emitUserChangeEvent({ node: decoratedNode }, WEAVE_NODE_CHANGE_TYPE.UPDATE);
23313
+ this.removeEventListener("onNodeRenderedUpdated", handleSendEvent);
23314
+ }
23315
+ };
23316
+ this.addEventListener("onNodeRenderedUpdated", handleSendEvent);
23317
+ }
23197
23318
  }
23198
23319
  getElementsTree() {
23199
23320
  return this.stateManager.getElementsTree();
@@ -23201,6 +23322,15 @@ var Weave = class {
23201
23322
  isEmpty() {
23202
23323
  return this.getElementsTree().length === 0;
23203
23324
  }
23325
+ getContainerByNodeId(nodeId) {
23326
+ let parentId = void 0;
23327
+ const nodeParent = this.getStage().findOne(`#${nodeId}`);
23328
+ if (nodeParent) {
23329
+ const parent = this.getNodeContainer(nodeParent);
23330
+ if (parent) parentId = parent.getAttrs().id;
23331
+ }
23332
+ return parentId;
23333
+ }
23204
23334
  getNodeContainerId(node) {
23205
23335
  const stage = this.getStage();
23206
23336
  let nodeContainer = node.getParent()?.getAttrs().id ?? "";
@@ -23277,9 +23407,6 @@ var Weave = class {
23277
23407
  nodesToGroupSerialized(instancesToClone) {
23278
23408
  return this.cloningManager.nodesToGroupSerialized(instancesToClone);
23279
23409
  }
23280
- cloneNodes(instancesToClone, targetContainer, onPoint) {
23281
- this.cloningManager.cloneNodes(instancesToClone, targetContainer, onPoint);
23282
- }
23283
23410
  getFonts() {
23284
23411
  return this.fontsManager.getFonts();
23285
23412
  }
@@ -23422,6 +23549,9 @@ var Weave = class {
23422
23549
  getNodeMutexLock(nodeId) {
23423
23550
  return this.mutexManager.getNodeMutexLock(nodeId);
23424
23551
  }
23552
+ getUsers() {
23553
+ return this.usersManager.getUsers();
23554
+ }
23425
23555
  };
23426
23556
 
23427
23557
  //#endregion
@@ -35228,22 +35358,16 @@ var WeaveConnectedUsersPlugin = class extends WeavePlugin {
35228
35358
  store.setAwarenessInfo(WEAVE_CONNECTED_USER_INFO_KEY, userInfo$1);
35229
35359
  } else store.setAwarenessInfo(WEAVE_CONNECTED_USER_INFO_KEY, void 0);
35230
35360
  });
35231
- this.instance.addEventListener("onAwarenessChange", (changes) => {
35361
+ this.instance.addEventListener("onUsersChange", () => {
35232
35362
  if (!this.enabled) {
35233
- this.connectedUsers = {};
35234
35363
  this.instance.emitEvent("onConnectedUsersChange", {});
35235
35364
  return;
35236
35365
  }
35237
- const newConnectedUsers = {};
35238
- for (const change of changes) {
35239
- if (!change[WEAVE_CONNECTED_USER_INFO_KEY]) continue;
35240
- if (change[WEAVE_CONNECTED_USER_INFO_KEY]) {
35241
- const userInformation = change[WEAVE_CONNECTED_USER_INFO_KEY];
35242
- newConnectedUsers[userInformation.id] = userInformation;
35243
- }
35244
- }
35245
- if (!(0, import_lodash.isEqual)(this.connectedUsers, newConnectedUsers)) this.instance.emitEvent("onConnectedUsersChange", newConnectedUsers);
35246
- this.connectedUsers = newConnectedUsers;
35366
+ const actualUsers = this.instance.getUsers();
35367
+ const newUsers = {};
35368
+ for (const user of actualUsers) newUsers[user.id] = user;
35369
+ if (!(0, import_lodash.isEqual)(this.connectedUsers, newUsers)) this.instance.emitEvent("onConnectedUsersChange", newUsers);
35370
+ this.connectedUsers = newUsers;
35247
35371
  });
35248
35372
  }
35249
35373
  enable() {
@@ -35296,21 +35420,30 @@ var WeaveUsersSelectionPlugin = class extends WeavePlugin {
35296
35420
  store.setAwarenessInfo(WEAVE_USER_SELECTION_KEY, void 0);
35297
35421
  }
35298
35422
  });
35299
- this.instance.addEventListener("onAwarenessChange", (changes) => {
35300
- const selfUser = this.config.getUser();
35301
- const allActiveUsers = [];
35302
- for (const change of changes) {
35303
- if (!change[WEAVE_USER_SELECTION_KEY]) continue;
35304
- if (change[WEAVE_USER_SELECTION_KEY] && selfUser.id !== change[WEAVE_USER_SELECTION_KEY].user) {
35423
+ this.instance.addEventListener(
35424
+ "onAwarenessChange",
35425
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35426
+ (changes) => {
35427
+ const selfUser = this.config.getUser();
35428
+ for (const change of changes) if (change[WEAVE_USER_SELECTION_KEY] && selfUser.id !== change[WEAVE_USER_SELECTION_KEY].user) {
35305
35429
  const userSelection = change[WEAVE_USER_SELECTION_KEY];
35306
- allActiveUsers.push(userSelection.user);
35307
35430
  this.usersSelection[userSelection.user] = userSelection;
35308
35431
  }
35432
+ this.renderSelectors();
35309
35433
  }
35310
- const allNodesSelections = Object.keys(this.usersSelection);
35311
- const inactiveUsers = allNodesSelections.filter((user) => !allNodesSelections.includes(user));
35312
- for (let i = 0; i < inactiveUsers.length; i++) delete this.usersSelection[inactiveUsers[i]];
35313
- this.renderSelectors();
35434
+ );
35435
+ this.instance.addEventListener("onUsersChange", () => {
35436
+ const actualUsers = this.instance.getUsers();
35437
+ const usersWithSelection = Object.keys(this.usersSelection);
35438
+ let hasChanges = false;
35439
+ for (const userId of usersWithSelection) {
35440
+ const userExists = actualUsers.find((user) => user.id === userId);
35441
+ if (userExists === void 0) {
35442
+ delete this.usersSelection[userId];
35443
+ hasChanges = true;
35444
+ }
35445
+ }
35446
+ if (hasChanges) this.renderSelectors();
35314
35447
  });
35315
35448
  stage.on("dragstart dragmove dragend", () => {
35316
35449
  this.renderSelectors();