mobx-keystone-yjs 1.5.4 → 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.
@@ -3,7 +3,6 @@ import { YjsTextModel, yjsTextModelId } from "./YjsTextModel"
3
3
  import { SnapshotOutOf } from "mobx-keystone"
4
4
  import { YjsData } from "./convertYjsDataToJson"
5
5
  import { PlainArray, PlainObject, PlainPrimitive, PlainValue } from "../plainTypes"
6
- import { action, runInAction } from "mobx"
7
6
 
8
7
  function isPlainPrimitive(v: PlainValue): v is PlainPrimitive {
9
8
  const t = typeof v
@@ -24,53 +23,51 @@ function isPlainObject(v: PlainValue): v is PlainObject {
24
23
  * Frozen values are a special case and they are kept as immutable plain values.
25
24
  */
26
25
  export function convertJsonToYjsData(v: PlainValue): YjsData {
27
- return runInAction(() => {
28
- if (isPlainPrimitive(v)) {
26
+ if (isPlainPrimitive(v)) {
27
+ return v
28
+ }
29
+
30
+ if (isPlainArray(v)) {
31
+ const arr = new Y.Array()
32
+ applyJsonArrayToYArray(arr, v)
33
+ return arr
34
+ }
35
+
36
+ if (isPlainObject(v)) {
37
+ if (v.$frozen === true) {
38
+ // frozen value, save as immutable object
29
39
  return v
30
40
  }
31
41
 
32
- if (isPlainArray(v)) {
33
- const arr = new Y.Array()
34
- applyJsonArrayToYArray(arr, v)
35
- return arr
42
+ if (v.$modelType === yjsTextModelId) {
43
+ const text = new Y.Text()
44
+ const yjsTextModel = v as unknown as SnapshotOutOf<YjsTextModel>
45
+ yjsTextModel.deltaList.forEach((frozenDeltas) => {
46
+ text.applyDelta(frozenDeltas.data)
47
+ })
48
+ return text
36
49
  }
37
50
 
38
- if (isPlainObject(v)) {
39
- if (v.$frozen === true) {
40
- // frozen value, save as immutable object
41
- return v
42
- }
43
-
44
- if (v.$modelType === yjsTextModelId) {
45
- const text = new Y.Text()
46
- const yjsTextModel = v as unknown as SnapshotOutOf<YjsTextModel>
47
- yjsTextModel.deltaList.forEach((frozenDeltas) => {
48
- text.applyDelta(frozenDeltas.data)
49
- })
50
- return text
51
- }
52
-
53
- const map = new Y.Map()
54
- applyJsonObjectToYMap(map, v)
55
- return map
56
- }
51
+ const map = new Y.Map()
52
+ applyJsonObjectToYMap(map, v)
53
+ return map
54
+ }
57
55
 
58
- throw new Error(`unsupported value type: ${v}`)
59
- })
56
+ throw new Error(`unsupported value type: ${v}`)
60
57
  }
61
58
 
62
59
  /**
63
60
  * Applies a JSON array to a Y.Array, using the convertJsonToYjsData to convert the values.
64
61
  */
65
- export const applyJsonArrayToYArray = action((dest: Y.Array<any>, source: PlainArray) => {
62
+ export const applyJsonArrayToYArray = (dest: Y.Array<any>, source: PlainArray) => {
66
63
  dest.push(source.map(convertJsonToYjsData))
67
- })
64
+ }
68
65
 
69
66
  /**
70
67
  * Applies a JSON object to a Y.Map, using the convertJsonToYjsData to convert the values.
71
68
  */
72
- export const applyJsonObjectToYMap = action((dest: Y.Map<any>, source: PlainObject) => {
69
+ export const applyJsonObjectToYMap = (dest: Y.Map<any>, source: PlainObject) => {
73
70
  Object.entries(source).forEach(([k, v]) => {
74
71
  dest.set(k, convertJsonToYjsData(v))
75
72
  })
76
- })
73
+ }
@@ -1,8 +1,8 @@
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
- import { action } from "mobx"
6
6
 
7
7
  export type YjsData = Y.Array<any> | Y.Map<any> | Y.Text | PlainValue
8
8
 
@@ -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
+ }