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