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.
- package/CHANGELOG.md +11 -0
- package/dist/mobx-keystone-yjs.esm.js +176 -116
- package/dist/mobx-keystone-yjs.esm.mjs +176 -116
- package/dist/mobx-keystone-yjs.umd.js +177 -117
- package/dist/types/binding/YjsTextModel.d.ts +5 -4
- package/dist/types/binding/applyMobxKeystonePatchToYjsObject.d.ts +1 -1
- package/dist/types/binding/bindYjsToMobxKeystone.d.ts +2 -2
- package/dist/types/binding/convertJsonToYjsData.d.ts +4 -4
- package/dist/types/binding/convertYjsDataToJson.d.ts +3 -3
- package/dist/types/binding/convertYjsEventToPatches.d.ts +1 -1
- package/dist/types/binding/yjsBindingContext.d.ts +2 -2
- package/dist/types/index.d.ts +6 -6
- package/dist/types/utils/isYjsValueDeleted.d.ts +7 -0
- package/package.json +12 -14
- package/src/binding/YjsTextModel.ts +280 -247
- package/src/binding/applyMobxKeystonePatchToYjsObject.ts +6 -1
- package/src/binding/bindYjsToMobxKeystone.ts +22 -12
- package/src/binding/convertJsonToYjsData.ts +6 -6
- package/src/binding/convertYjsDataToJson.ts +4 -3
- package/src/binding/convertYjsEventToPatches.ts +4 -12
- package/src/index.ts +2 -2
- package/src/utils/isYjsValueDeleted.ts +14 -0
|
@@ -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<
|
|
7
|
+
export type YjsData = Y.Array<any> | Y.Map<any> | Y.Text | PlainValue
|
|
7
8
|
|
|
8
|
-
export
|
|
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:
|
|
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:
|
|
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:
|
|
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 {
|
|
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
|
+
}
|