mobx-keystone-yjs 1.5.3 → 1.5.5

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,11 +1,12 @@
1
+ import { action } from "mobx"
1
2
  import { modelSnapshotOutWithMetadata } from "mobx-keystone"
2
3
  import * as Y from "yjs"
3
4
  import { PlainObject, PlainValue } from "../plainTypes"
4
5
  import { YjsTextModel } from "./YjsTextModel"
5
6
 
6
- export type YjsData = Y.Array<YjsData> | Y.Map<YjsData> | Y.Text | PlainValue
7
+ export type YjsData = Y.Array<any> | Y.Map<any> | Y.Text | PlainValue
7
8
 
8
- export function convertYjsDataToJson(yjsData: YjsData): PlainValue {
9
+ export const convertYjsDataToJson = action((yjsData: YjsData): PlainValue => {
9
10
  if (yjsData instanceof Y.Array) {
10
11
  return yjsData.map((v) => convertYjsDataToJson(v))
11
12
  }
@@ -28,4 +29,4 @@ export function convertYjsDataToJson(yjsData: YjsData): PlainValue {
28
29
 
29
30
  // assume it's a primitive
30
31
  return yjsData
31
- }
32
+ })
@@ -1,7 +1,7 @@
1
1
  import { Patch } from "mobx-keystone"
2
2
  import * as Y from "yjs"
3
- import { PlainArray, PlainObject, PlainValue } from "../plainTypes"
4
3
  import { failure } from "../utils/error"
4
+ import { convertYjsDataToJson } from "./convertYjsDataToJson"
5
5
 
6
6
  export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
7
7
  const patches: Patch[] = []
@@ -17,7 +17,7 @@ export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
17
17
  patches.push({
18
18
  op: "add",
19
19
  path,
20
- value: toPlainValue(source.get(key)),
20
+ value: convertYjsDataToJson(source.get(key)),
21
21
  })
22
22
  break
23
23
 
@@ -25,7 +25,7 @@ export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
25
25
  patches.push({
26
26
  op: "replace",
27
27
  path,
28
- value: toPlainValue(source.get(key)),
28
+ value: convertYjsDataToJson(source.get(key)),
29
29
  })
30
30
  break
31
31
 
@@ -65,7 +65,7 @@ export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
65
65
  patches.push({
66
66
  op: "add",
67
67
  path,
68
- value: toPlainValue(v),
68
+ value: convertYjsDataToJson(v),
69
69
  })
70
70
  retain++
71
71
  })
@@ -82,11 +82,3 @@ export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
82
82
 
83
83
  return patches
84
84
  }
85
-
86
- function toPlainValue(v: Y.Map<any> | Y.Array<any> | PlainValue) {
87
- if (v instanceof Y.Map || v instanceof Y.Array) {
88
- return v.toJSON() as PlainObject | PlainArray
89
- } else {
90
- return v
91
- }
92
- }
package/src/index.ts CHANGED
@@ -1,10 +1,10 @@
1
- export { YjsTextModel, yjsTextModelId } from "./binding/YjsTextModel"
2
1
  export { bindYjsToMobxKeystone } from "./binding/bindYjsToMobxKeystone"
3
2
  export {
4
3
  applyJsonArrayToYArray,
5
4
  applyJsonObjectToYMap,
6
5
  convertJsonToYjsData,
7
6
  } from "./binding/convertJsonToYjsData"
8
- export { yjsBindingContext } from "./binding/yjsBindingContext"
7
+ export { YjsTextModel, yjsTextModelId } from "./binding/YjsTextModel"
9
8
  export type { YjsBindingContext } from "./binding/yjsBindingContext"
9
+ export { yjsBindingContext } from "./binding/yjsBindingContext"
10
10
  export { MobxKeystoneYjsError } from "./utils/error"
@@ -0,0 +1,14 @@
1
+ import * as Y from "yjs"
2
+
3
+ /**
4
+ * Checks if a Y.js value has been deleted or its document destroyed.
5
+ *
6
+ * @param yjsValue The Y.js value to check.
7
+ * @returns `true` if the value is deleted or destroyed, `false` otherwise.
8
+ */
9
+ export function isYjsValueDeleted(yjsValue: unknown): boolean {
10
+ if (yjsValue instanceof Y.AbstractType) {
11
+ return !!(yjsValue as any)._item?.deleted || !!yjsValue.doc?.isDestroyed
12
+ }
13
+ return false
14
+ }