@slate-yjs/core 1.0.4 → 1.0.6

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.
@@ -8052,10 +8052,18 @@ ${err.toString()}`);
8052
8052
  assoc
8053
8053
  });
8054
8054
  const target = parent.children[pathOffset];
8055
- if (!Text.isText(target)) {
8055
+ if (Text.isText(target)) {
8056
+ return { path: [...parentPath, pathOffset], offset: textOffset };
8057
+ }
8058
+ const sideOffset = textOffset === 0 ? pathOffset - 1 : pathOffset + 1;
8059
+ const side = parent.children[sideOffset];
8060
+ if (!Text.isText(side)) {
8056
8061
  return null;
8057
8062
  }
8058
- return { path: [...parentPath, pathOffset], offset: textOffset };
8063
+ return {
8064
+ path: [...parentPath, sideOffset],
8065
+ offset: textOffset === 0 ? side.text.length : 0
8066
+ };
8059
8067
  }
8060
8068
  function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
8061
8069
  if (!sharedRoot.doc) {
@@ -8194,22 +8202,38 @@ ${err.toString()}`);
8194
8202
  }
8195
8203
 
8196
8204
  // src/applyToYjs/node/moveNode.ts
8197
- function moveNode(sharedRoot, slateRoot, op) {
8198
- const newParentPath = Path.parent(op.newPath);
8199
- const newPathOffset = op.newPath[op.newPath.length - 1];
8200
- const parent = Node.get(slateRoot, newParentPath);
8201
- if (Text.isText(parent)) {
8202
- throw new Error("Cannot move slate node into text element");
8205
+ function withoutNode(root, path) {
8206
+ if (Text.isText(root)) {
8207
+ throw new Error("Cannot descend into slate text");
8208
+ }
8209
+ const [index, ...rest] = path;
8210
+ const children = [...root.children];
8211
+ if (rest.length === 0) {
8212
+ children.splice(index, 1);
8213
+ } else {
8214
+ children[index] = withoutNode(children[index], rest);
8203
8215
  }
8204
- const normalizedNewPath = [
8205
- ...newParentPath,
8206
- Math.min(newPathOffset, parent.children.length)
8207
- ];
8216
+ return __spreadProps(__spreadValues({}, root), { children });
8217
+ }
8218
+ function moveNode(sharedRoot, slateRoot, op) {
8208
8219
  const origin = getYTarget(sharedRoot, slateRoot, op.path);
8209
- const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
8210
8220
  const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
8211
8221
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, origin.yParent, origin.targetDelta);
8212
8222
  origin.yParent.delete(origin.textRange.start, origin.textRange.end - origin.textRange.start);
8223
+ const rootWithoutNode = withoutNode(slateRoot, op.path);
8224
+ const newPath = Path.transform(op.path, op);
8225
+ if (!newPath) {
8226
+ throw new Error("Cannot resolve the target path of a move");
8227
+ }
8228
+ const newParentPath = Path.parent(newPath);
8229
+ const parent = Node.get(rootWithoutNode, newParentPath);
8230
+ if (Text.isText(parent)) {
8231
+ throw new Error("Cannot move slate node into text element");
8232
+ }
8233
+ const target = getYTarget(sharedRoot, rootWithoutNode, [
8234
+ ...newParentPath,
8235
+ Math.min(newPath[newPath.length - 1], parent.children.length)
8236
+ ]);
8213
8237
  const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
8214
8238
  const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
8215
8239
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
@@ -8679,18 +8703,23 @@ ${err.toString()}`);
8679
8703
  e.sendCursorData = (cursorData) => {
8680
8704
  e.awareness.setLocalStateField(e.cursorDataField, cursorData);
8681
8705
  };
8706
+ let hiddenAt;
8707
+ const relativeSelection = () => e.selection ? slateRangeToRelativeRange(e.sharedRoot, e, e.selection) : null;
8708
+ const isSameRange = (a, b) => a === b || !!a && !!b && compareRelativePositions(a.anchor, b.anchor) && compareRelativePositions(a.focus, b.focus);
8682
8709
  e.sendCursorPosition = (range) => {
8683
8710
  const localState = e.awareness.getLocalState();
8684
8711
  const currentRange = localState == null ? void 0 : localState[selectionStateField];
8685
8712
  if (!range) {
8713
+ hiddenAt = relativeSelection();
8686
8714
  if (currentRange) {
8687
8715
  e.awareness.setLocalStateField(e.selectionStateField, null);
8688
8716
  }
8689
8717
  return;
8690
8718
  }
8691
- const { anchor, focus } = slateRangeToRelativeRange(e.sharedRoot, e, range);
8692
- if (!currentRange || !compareRelativePositions(anchor, currentRange) || !compareRelativePositions(focus, currentRange)) {
8693
- e.awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
8719
+ hiddenAt = void 0;
8720
+ const relativeRange = slateRangeToRelativeRange(e.sharedRoot, e, range);
8721
+ if (!currentRange || !isSameRange(relativeRange, currentRange)) {
8722
+ e.awareness.setLocalStateField(e.selectionStateField, relativeRange);
8694
8723
  }
8695
8724
  };
8696
8725
  const awarenessChangeListener = (yEvent) => {
@@ -8724,9 +8753,13 @@ ${err.toString()}`);
8724
8753
  const { onChange } = e;
8725
8754
  e.onChange = () => {
8726
8755
  onChange();
8727
- if (YjsEditor.connected(e)) {
8728
- CursorEditor.sendCursorPosition(e);
8756
+ if (!YjsEditor.connected(e)) {
8757
+ return;
8758
+ }
8759
+ if (hiddenAt !== void 0 && isSameRange(relativeSelection(), hiddenAt)) {
8760
+ return;
8729
8761
  }
8762
+ CursorEditor.sendCursorPosition(e);
8730
8763
  };
8731
8764
  }
8732
8765
  };