@slate-yjs/core 1.0.9 → 1.1.1

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.
@@ -1,4 +1,12 @@
1
- import { Editor, Element, Node, Operation, Path, Text } from 'slate';
1
+ import {
2
+ Editor,
3
+ Element,
4
+ InsertTextOperation,
5
+ Node,
6
+ Operation,
7
+ Path,
8
+ Text,
9
+ } from 'slate';
2
10
  import * as Y from 'yjs';
3
11
  import { Delta } from '../model/types';
4
12
  import { deltaInsertToSlateNode } from '../utils/convert';
@@ -166,16 +174,34 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
166
174
  * Since we're not actually using slate to update the node
167
175
  * this is a simulation
168
176
  */
177
+ // Inserting text doesn't change a node's properties, so look past
178
+ // inserts into this node for the operation that last set them. An
179
+ // insert_text is only emitted directly after another operation on the
180
+ // same path, so this ends at the operation that set this node's
181
+ // properties.
182
+ let propsOpIndex = ops.length - 1;
183
+ while (
184
+ propsOpIndex >= 0 &&
185
+ ops[propsOpIndex].type === 'insert_text' &&
186
+ Path.equals(
187
+ (ops[propsOpIndex] as InsertTextOperation).path,
188
+ childPath
189
+ )
190
+ ) {
191
+ propsOpIndex -= 1;
192
+ }
193
+ const propsOp = ops[propsOpIndex];
194
+
169
195
  let currentProps = getProperties(child);
170
- if (lastOp?.type === 'insert_node') {
171
- currentProps = lastOp.node;
196
+ if (propsOp?.type === 'insert_node') {
197
+ currentProps = propsOp.node;
172
198
  } else if (
173
- lastOp?.type === 'set_node' &&
174
- Path.equals(lastOp.path, childPath)
199
+ propsOp?.type === 'set_node' &&
200
+ Path.equals(propsOp.path, childPath)
175
201
  ) {
176
202
  currentProps = omitNullEntries({
177
203
  ...currentProps,
178
- ...lastOp.newProperties,
204
+ ...propsOp.newProperties,
179
205
  });
180
206
  }
181
207
 
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { translateYjsEvent } from './applyToSlate';
2
+ import { applySlateOp } from './applyToYjs';
1
3
  import { RelativeRange } from './model/types';
2
4
  import {
3
5
  CursorEditor,
@@ -44,4 +46,7 @@ export {
44
46
  relativeRangeToSlateRange,
45
47
  slatePointToRelativePosition,
46
48
  relativePositionToSlatePoint,
49
+ // Low-level translation
50
+ translateYjsEvent,
51
+ applySlateOp,
47
52
  };