slate-vue3 0.3.1 → 0.3.2

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,6 +1,5 @@
1
1
  import { JsonObject } from '@liveblocks/client';
2
2
  import { Store } from './utils';
3
- import { CursorEditor, CursorState } from '../plugins/withCursors';
3
+ import { CursorState } from '../plugins/withCursors';
4
4
  export type CursorStore<TCursorData extends JsonObject = JsonObject> = Store<Record<string, CursorState<TCursorData>>>;
5
- export declare function createRemoteCursorStateStore<TCursorData extends JsonObject>(editor: CursorEditor<TCursorData>): CursorStore<TCursorData>;
6
5
  export declare function useRemoteCursorStateStore<TCursorData extends JsonObject = JsonObject>(): CursorStore<TCursorData>;
@@ -4,10 +4,7 @@ import { DOMEditor } from '../../slate-dom/index.ts';
4
4
  import { JsonObject } from '@liveblocks/client';
5
5
  import { CursorEditor, CursorState } from '../plugins/withCursors';
6
6
  export declare function useOnResize<T extends HTMLElement>(_ref: Ref<T>, onResize: () => void): void;
7
- export type Store<T> = readonly [
8
- (onStoreChange: () => void) => () => void,
9
- () => T
10
- ];
7
+ export type Store<T> = readonly [() => () => void, Ref<T>];
11
8
  export declare function getCursorRange<TCursorData extends JsonObject = JsonObject>(editor: CursorEditor<TCursorData>, cursorState: CursorState<TCursorData>): Range | null;
12
9
  export type SelectionRect = {
13
10
  width: number;
@@ -1,4 +1,4 @@
1
+ import { XmlText } from 'yjs';
1
2
  import { InsertDelta } from '../model/types';
2
- import * as Y from 'yjs';
3
3
  export declare function cloneInsertDeltaDeep(delta: InsertDelta): InsertDelta;
4
- export declare function cloneDeep(yText: Y.XmlText): Y.XmlText;
4
+ export declare function cloneDeep(yText: XmlText): XmlText;
@@ -1,7 +1,7 @@
1
1
  import { Element, Node } from '../../slate/index.ts';
2
2
  import { DeltaInsert, InsertDelta } from '../model/types';
3
- import * as Y from 'yjs';
4
- export declare function yTextToSlateElement(yText: Y.XmlText): Element;
3
+ import { XmlText } from 'yjs';
4
+ export declare function yTextToSlateElement(yText: XmlText): Element;
5
5
  export declare function deltaInsertToSlateNode(insert: DeltaInsert): Node;
6
6
  export declare function slateNodesToInsertDelta(nodes: Node[]): InsertDelta;
7
- export declare function slateElementToYText({ children, ...attributes }: Element): Y.XmlText;
7
+ export declare function slateElementToYText({ children, ...attributes }: Element): XmlText;
@@ -1,7 +1,7 @@
1
+ import { XmlText } from 'yjs';
1
2
  import { DeltaInsert, InsertDelta } from '../model/types';
2
- import * as Y from 'yjs';
3
3
  export declare function normalizeInsertDelta(delta: InsertDelta): InsertDelta;
4
- export declare function yTextToInsertDelta(yText: Y.XmlText): InsertDelta;
4
+ export declare function yTextToInsertDelta(yText: XmlText): InsertDelta;
5
5
  export declare function getInsertLength({ insert }: DeltaInsert): number;
6
6
  export declare function getInsertDeltaLength(delta: InsertDelta): number;
7
7
  export declare function sliceInsertDelta(delta: InsertDelta, start: number, length: number): InsertDelta;
@@ -1,11 +1,11 @@
1
1
  import { Element, Node, Path } from '../../slate/index.ts';
2
2
  import { YTarget } from '../model/types';
3
- import * as Y from 'yjs';
3
+ import { XmlText } from 'yjs';
4
4
  export declare function getSlateNodeYLength(node: Node | undefined): number;
5
5
  export declare function slatePathOffsetToYOffset(element: Element, pathOffset: number): number;
6
- export declare function getYTarget(yRoot: Y.XmlText, slateRoot: Node, path: Path): YTarget;
6
+ export declare function getYTarget(yRoot: XmlText, slateRoot: Node, path: Path): YTarget;
7
7
  export declare function yOffsetToSlateOffsets(parent: Element, yOffset: number, opts?: {
8
8
  assoc?: number;
9
9
  insert?: boolean;
10
10
  }): [number, number];
11
- export declare function getSlatePath(sharedRoot: Y.XmlText, slateRoot: Node, yText: Y.XmlText): Path;
11
+ export declare function getSlatePath(sharedRoot: XmlText, slateRoot: Node, yText: XmlText): Path;
@@ -1,19 +1,19 @@
1
1
  import { BasePoint, BaseRange, Node } from '../../slate/index.ts';
2
+ import { AbsolutePosition, RelativePosition, XmlText } from 'yjs';
2
3
  import { InsertDelta, RelativeRange } from '../model/types';
3
- import * as Y from 'yjs';
4
4
  export declare const STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
5
- export declare function slatePointToRelativePosition(sharedRoot: Y.XmlText, slateRoot: Node, point: BasePoint): Y.RelativePosition;
6
- export declare function absolutePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, { type, index, assoc }: Y.AbsolutePosition): BasePoint | null;
7
- export declare function relativePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, pos: Y.RelativePosition): BasePoint | null;
8
- export declare function getStoredPosition(sharedRoot: Y.XmlText, key: string): Y.RelativePosition | null;
9
- export declare function getStoredPositions(sharedRoot: Y.XmlText): Record<string, Y.RelativePosition>;
10
- export declare function removeStoredPosition(sharedRoot: Y.XmlText, key: string): void;
11
- export declare function setStoredPosition(sharedRoot: Y.XmlText, key: string, position: Y.RelativePosition): void;
12
- export declare function getStoredPositionsInDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, delta: InsertDelta, deltaOffset?: number): {
5
+ export declare function slatePointToRelativePosition(sharedRoot: XmlText, slateRoot: Node, point: BasePoint): RelativePosition;
6
+ export declare function absolutePositionToSlatePoint(sharedRoot: XmlText, slateRoot: Node, { type, index, assoc }: AbsolutePosition): BasePoint | null;
7
+ export declare function relativePositionToSlatePoint(sharedRoot: XmlText, slateRoot: Node, pos: RelativePosition): BasePoint | null;
8
+ export declare function getStoredPosition(sharedRoot: XmlText, key: string): RelativePosition | null;
9
+ export declare function getStoredPositions(sharedRoot: XmlText): Record<string, RelativePosition>;
10
+ export declare function removeStoredPosition(sharedRoot: XmlText, key: string): void;
11
+ export declare function setStoredPosition(sharedRoot: XmlText, key: string, position: RelativePosition): void;
12
+ export declare function getStoredPositionsInDeltaAbsolute(sharedRoot: XmlText, yText: XmlText, delta: InsertDelta, deltaOffset?: number): {
13
13
  '': {
14
- [k: string]: Y.AbsolutePosition;
14
+ [k: string]: AbsolutePosition;
15
15
  };
16
16
  };
17
- export declare function restoreStoredPositionsWithDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, absolutePositions: Record<string, Record<string, Y.AbsolutePosition>>, delta: InsertDelta, newDeltaOffset?: number, previousDeltaOffset?: number, path?: string): void;
18
- export declare function slateRangeToRelativeRange(sharedRoot: Y.XmlText, slateRoot: Node, range: BaseRange): RelativeRange;
19
- export declare function relativeRangeToSlateRange(sharedRoot: Y.XmlText, slateRoot: Node, range: RelativeRange): BaseRange | null;
17
+ export declare function restoreStoredPositionsWithDeltaAbsolute(sharedRoot: XmlText, yText: XmlText, absolutePositions: Record<string, Record<string, AbsolutePosition>>, delta: InsertDelta, newDeltaOffset?: number, previousDeltaOffset?: number, path?: string): void;
18
+ export declare function slateRangeToRelativeRange(sharedRoot: XmlText, slateRoot: Node, range: BaseRange): RelativeRange;
19
+ export declare function relativeRangeToSlateRange(sharedRoot: XmlText, slateRoot: Node, range: RelativeRange): BaseRange | null;
@@ -1,4 +1,4 @@
1
- import * as Y from 'yjs';
2
- export declare function assertDocumentAttachment<T extends Y.AbstractType<any>>(sharedType: T): asserts sharedType is T & {
1
+ import { AbstractType } from 'yjs';
2
+ export declare function assertDocumentAttachment<T extends AbstractType<any>>(sharedType: T): asserts sharedType is T & {
3
3
  doc: NonNullable<T['doc']>;
4
4
  };
package/dist/yjs.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { d as isPlainObject, T as Text, N as Node, P as Path, E as Editor, t as toRawWeakMap, c as Transforms, R as Range } from "./batch-dirty-paths-DdGxRyrT.js";
2
2
  import { toRaw, ref, onMounted, onUnmounted, watch, onUpdated, computed } from "vue";
3
- import * as Y from "yjs";
4
- import { XmlText, YTextEvent, UndoManager, compareRelativePositions } from "yjs";
3
+ import { XmlText, YTextEvent, createRelativePositionFromTypeIndex, createAbsolutePositionFromRelativePosition, createRelativePositionFromJSON, decodeRelativePosition, encodeRelativePosition, UndoManager, compareRelativePositions } from "yjs";
5
4
  import { L as Location } from "./location-BQJIMGOU.js";
6
5
  import { g as useEditor, e as useFocused } from "./use-focused-C0jEAq2w.js";
7
6
  import { D as DOMEditor } from "./hotkeys-CmyQAN4Q.js";
@@ -142,7 +141,7 @@ function slateElementToYText({
142
141
  children,
143
142
  ...attributes
144
143
  }) {
145
- const yElement = new Y.XmlText();
144
+ const yElement = new XmlText();
146
145
  Object.entries(attributes).forEach(([key, value]) => {
147
146
  yElement.setAttribute(key, value);
148
147
  });
@@ -177,7 +176,7 @@ function getYTarget(yRoot, slateRoot, path) {
177
176
  }
178
177
  const yTarget = (_a = targetDelta[0]) == null ? void 0 : _a.insert;
179
178
  if (childPath.length > 0) {
180
- if (!(yTarget instanceof Y.XmlText)) {
179
+ if (!(yTarget instanceof XmlText)) {
181
180
  throw new Error(
182
181
  "Path doesn't match yText, cannot descent into non-yText"
183
182
  );
@@ -187,7 +186,7 @@ function getYTarget(yRoot, slateRoot, path) {
187
186
  return {
188
187
  yParent: yRoot,
189
188
  textRange: { start: yOffset, end: yOffset + targetLength },
190
- yTarget: yTarget instanceof Y.XmlText ? yTarget : void 0,
189
+ yTarget: yTarget instanceof XmlText ? yTarget : void 0,
191
190
  slateParent: slateRoot,
192
191
  slateTarget: targetNode,
193
192
  targetDelta
@@ -226,7 +225,7 @@ function getSlatePath(sharedRoot, slateRoot, yText) {
226
225
  if (!yParent) {
227
226
  throw new Error("yText isn't a descendant of root element");
228
227
  }
229
- if (!(yParent instanceof Y.XmlText)) {
228
+ if (!(yParent instanceof XmlText)) {
230
229
  throw new Error("Unexpected y parent type");
231
230
  }
232
231
  yNodePath.unshift(yParent);
@@ -478,7 +477,7 @@ function cloneInsertDeltaDeep(delta) {
478
477
  });
479
478
  }
480
479
  function cloneDeep(yText) {
481
- const clone = new Y.XmlText();
480
+ const clone = new XmlText();
482
481
  const attributes = yText.getAttributes();
483
482
  Object.entries(attributes).forEach(([key, value]) => {
484
483
  clone.setAttribute(key, value);
@@ -506,14 +505,14 @@ function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
506
505
  );
507
506
  }
508
507
  const index = textRange.start + point.offset;
509
- return Y.createRelativePositionFromTypeIndex(
508
+ return createRelativePositionFromTypeIndex(
510
509
  yParent,
511
510
  index,
512
511
  index === textRange.end ? -1 : 0
513
512
  );
514
513
  }
515
514
  function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, assoc }) {
516
- if (!(type instanceof Y.XmlText)) {
515
+ if (!(type instanceof XmlText)) {
517
516
  throw new Error("Absolute position points to a non-XMLText");
518
517
  }
519
518
  const parentPath = getSlatePath(sharedRoot, slateRoot, type);
@@ -536,7 +535,7 @@ function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
536
535
  if (!sharedRoot.doc) {
537
536
  throw new Error("sharedRoot isn't attach to a yDoc");
538
537
  }
539
- const absPos = Y.createAbsolutePositionFromRelativePosition(
538
+ const absPos = createAbsolutePositionFromRelativePosition(
540
539
  pos,
541
540
  sharedRoot.doc
542
541
  );
@@ -547,13 +546,13 @@ function getStoredPosition(sharedRoot, key) {
547
546
  if (!rawPosition) {
548
547
  return null;
549
548
  }
550
- return Y.decodeRelativePosition(rawPosition);
549
+ return decodeRelativePosition(rawPosition);
551
550
  }
552
551
  function getStoredPositions(sharedRoot) {
553
552
  return Object.fromEntries(
554
553
  Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
555
554
  key.slice(STORED_POSITION_PREFIX.length),
556
- Y.createRelativePositionFromJSON(position)
555
+ createRelativePositionFromJSON(position)
557
556
  ])
558
557
  );
559
558
  }
@@ -563,8 +562,8 @@ function getStoredPositionsAbsolute(sharedRoot) {
563
562
  Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(
564
563
  ([key, position]) => [
565
564
  key.slice(STORED_POSITION_PREFIX.length),
566
- Y.createAbsolutePositionFromRelativePosition(
567
- Y.decodeRelativePosition(position),
565
+ createAbsolutePositionFromRelativePosition(
566
+ decodeRelativePosition(position),
568
567
  sharedRoot.doc
569
568
  )
570
569
  ]
@@ -577,7 +576,7 @@ function removeStoredPosition(sharedRoot, key) {
577
576
  function setStoredPosition(sharedRoot, key, position) {
578
577
  sharedRoot.setAttribute(
579
578
  STORED_POSITION_PREFIX + key,
580
- Y.encodeRelativePosition(position)
579
+ encodeRelativePosition(position)
581
580
  );
582
581
  }
583
582
  function getAbsolutePositionsInTextRange(absolutePositions, yTarget, textRange) {
@@ -599,7 +598,7 @@ function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "")
599
598
  };
600
599
  const insertDelta = yTextToInsertDelta(yText);
601
600
  insertDelta.forEach(({ insert }, i) => {
602
- if (insert instanceof Y.XmlText) {
601
+ if (insert instanceof XmlText) {
603
602
  Object.assign(
604
603
  positions,
605
604
  getAbsolutePositionsInYText(
@@ -621,7 +620,7 @@ function getStoredPositionsInDeltaAbsolute(sharedRoot, yText, delta, deltaOffset
621
620
  })
622
621
  };
623
622
  delta.forEach(({ insert }, i) => {
624
- if (insert instanceof Y.XmlText) {
623
+ if (insert instanceof XmlText) {
625
624
  Object.assign(
626
625
  positions,
627
626
  getAbsolutePositionsInYText(absolutePositions, insert, i.toString())
@@ -637,7 +636,7 @@ function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePosi
637
636
  setStoredPosition(
638
637
  sharedRoot,
639
638
  key,
640
- Y.createRelativePositionFromTypeIndex(
639
+ createRelativePositionFromTypeIndex(
641
640
  yText,
642
641
  position.index - previousDeltaOffset + newDeltaOffset,
643
642
  position.assoc
@@ -646,7 +645,7 @@ function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePosi
646
645
  });
647
646
  }
648
647
  delta.forEach(({ insert }, i) => {
649
- if (insert instanceof Y.XmlText) {
648
+ if (insert instanceof XmlText) {
650
649
  restoreStoredPositionsWithDeltaAbsolute(
651
650
  sharedRoot,
652
651
  insert,
@@ -1292,72 +1291,46 @@ function withCursors(editor, awareness, {
1292
1291
  return e;
1293
1292
  }
1294
1293
  const EDITOR_TO_CURSOR_STORE = new toRawWeakMap();
1295
- function createRemoteCursorStateStore(editor) {
1296
- let cursors = {};
1297
- const changed = /* @__PURE__ */ new Set();
1298
- const addChanged = changed.add.bind(changed);
1299
- const onStoreChangeListeners = /* @__PURE__ */ new Set();
1300
- let changeHandler = null;
1301
- const subscribe = (onStoreChange) => {
1302
- onStoreChangeListeners.add(onStoreChange);
1303
- if (!changeHandler) {
1304
- changeHandler = (event) => {
1305
- event.added.forEach(addChanged);
1306
- event.removed.forEach(addChanged);
1307
- event.updated.forEach(addChanged);
1308
- onStoreChangeListeners.forEach((listener) => listener());
1309
- };
1310
- CursorEditor.on(editor, "change", changeHandler);
1311
- }
1312
- return () => {
1313
- onStoreChangeListeners.delete(onStoreChange);
1314
- if (changeHandler && onStoreChangeListeners.size === 0) {
1315
- CursorEditor.off(editor, "change", changeHandler);
1316
- changeHandler = null;
1317
- }
1318
- };
1319
- };
1320
- const getSnapshot = () => {
1321
- if (changed.size === 0) {
1294
+ function useRemoteCursorStateStore() {
1295
+ const editor = useEditor();
1296
+ const cursors = ref({});
1297
+ const changed = ref(/* @__PURE__ */ new Set());
1298
+ const addChanged = changed.value.add.bind(changed.value);
1299
+ const changeHandler = (event) => {
1300
+ event.added.forEach(addChanged);
1301
+ event.removed.forEach(addChanged);
1302
+ event.updated.forEach(addChanged);
1303
+ if (changed.value.size === 0) {
1322
1304
  return cursors;
1323
1305
  }
1324
- changed.forEach((clientId) => {
1306
+ changed.value.forEach((clientId) => {
1325
1307
  const state = CursorEditor.cursorState(editor, clientId);
1326
1308
  if (state === null) {
1327
- delete cursors[clientId.toString()];
1309
+ delete cursors.value[clientId.toString()];
1328
1310
  return;
1329
1311
  }
1330
- cursors[clientId] = state;
1312
+ cursors.value[clientId] = state;
1331
1313
  });
1332
- changed.clear();
1333
- cursors = { ...cursors };
1334
- return cursors;
1314
+ changed.value.clear();
1335
1315
  };
1336
- return [subscribe, getSnapshot];
1337
- }
1338
- function useRemoteCursorStateStore() {
1339
- const editor = useEditor();
1340
- const existing = EDITOR_TO_CURSOR_STORE.get(editor);
1341
- if (existing) {
1342
- return existing;
1343
- }
1344
- const store = createRemoteCursorStateStore(editor);
1316
+ const subscribe = () => {
1317
+ CursorEditor.on(editor, "change", changeHandler);
1318
+ return () => CursorEditor.off(editor, "change", changeHandler);
1319
+ };
1320
+ const store = [subscribe, cursors];
1345
1321
  EDITOR_TO_CURSOR_STORE.set(editor, store);
1346
1322
  return store;
1347
1323
  }
1348
1324
  function useRemoteCursorStates() {
1349
- const [subscribe, getSnapshot] = useRemoteCursorStateStore();
1350
- const snapshot = ref(getSnapshot());
1351
- const handleStoreChange = () => snapshot.value = getSnapshot();
1325
+ const [subscribe, cursors] = useRemoteCursorStateStore();
1352
1326
  let unsubscribe;
1353
1327
  onMounted(() => {
1354
- unsubscribe = subscribe(handleStoreChange);
1355
- handleStoreChange();
1328
+ unsubscribe = subscribe();
1356
1329
  });
1357
1330
  onUnmounted(() => {
1358
1331
  unsubscribe == null ? void 0 : unsubscribe();
1359
1332
  });
1360
- return snapshot;
1333
+ return cursors;
1361
1334
  }
1362
1335
  function useUnsetCursorPositionOnBlur() {
1363
1336
  const editor = useEditor();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slate-vue3",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",