@kerebron/extension-yjs 0.5.2 → 0.5.4
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/README.md +3 -89
- package/esm/ExtensionYjs.d.ts +10 -1
- package/esm/ExtensionYjs.d.ts.map +1 -1
- package/esm/ExtensionYjs.js +47 -6
- package/esm/ExtensionYjs.js.map +1 -1
- package/esm/MarkYChange.d.ts +7 -0
- package/esm/MarkYChange.d.ts.map +1 -0
- package/esm/MarkYChange.js +21 -0
- package/esm/MarkYChange.js.map +1 -0
- package/esm/ProsemirrorBinding.d.ts +60 -0
- package/esm/ProsemirrorBinding.d.ts.map +1 -0
- package/esm/ProsemirrorBinding.js +405 -0
- package/esm/ProsemirrorBinding.js.map +1 -0
- package/esm/createNodeFromYElement.d.ts +10 -0
- package/esm/createNodeFromYElement.d.ts.map +1 -0
- package/esm/createNodeFromYElement.js +123 -0
- package/esm/createNodeFromYElement.js.map +1 -0
- package/esm/debug.d.ts +13 -0
- package/esm/debug.d.ts.map +1 -0
- package/esm/debug.js +147 -0
- package/esm/debug.js.map +1 -0
- package/esm/keys.d.ts +5 -8
- package/esm/keys.d.ts.map +1 -1
- package/esm/keys.js +1 -6
- package/esm/keys.js.map +1 -1
- package/esm/lib.d.ts +1 -2
- package/esm/lib.d.ts.map +1 -1
- package/esm/lib.js +12 -2
- package/esm/lib.js.map +1 -1
- package/esm/updateYFragment.d.ts +17 -0
- package/esm/updateYFragment.d.ts.map +1 -0
- package/esm/updateYFragment.js +333 -0
- package/esm/updateYFragment.js.map +1 -0
- package/esm/utils.d.ts +2 -0
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +4 -0
- package/esm/utils.js.map +1 -1
- package/esm/yPositionPlugin.d.ts +12 -4
- package/esm/yPositionPlugin.d.ts.map +1 -1
- package/esm/yPositionPlugin.js +114 -61
- package/esm/yPositionPlugin.js.map +1 -1
- package/esm/ySyncPlugin.d.ts +16 -78
- package/esm/ySyncPlugin.d.ts.map +1 -1
- package/esm/ySyncPlugin.js +81 -848
- package/esm/ySyncPlugin.js.map +1 -1
- package/esm/yUndoPlugin.d.ts +1 -1
- package/esm/yUndoPlugin.d.ts.map +1 -1
- package/esm/yUndoPlugin.js +1 -1
- package/esm/yUndoPlugin.js.map +1 -1
- package/package.json +9 -3
- package/src/ExtensionYjs.ts +65 -9
- package/src/MarkYChange.ts +23 -0
- package/src/ProsemirrorBinding.ts +607 -0
- package/src/createNodeFromYElement.ts +175 -0
- package/src/debug.ts +218 -0
- package/src/keys.ts +9 -9
- package/src/lib.ts +11 -3
- package/src/updateYFragment.ts +439 -0
- package/src/utils.ts +6 -0
- package/src/yPositionPlugin.ts +167 -92
- package/src/ySyncPlugin.ts +135 -1193
- package/src/yUndoPlugin.ts +1 -1
- package/esm/convertUtils.d.ts +0 -59
- package/esm/convertUtils.d.ts.map +0 -1
- package/esm/convertUtils.js +0 -89
- package/esm/convertUtils.js.map +0 -1
- package/src/convertUtils.ts +0 -143
package/src/yUndoPlugin.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { ContentType, Item, Text, UndoManager, XmlElement } from 'yjs';
|
|
|
3
3
|
|
|
4
4
|
import type { Command } from '@kerebron/editor/commands';
|
|
5
5
|
|
|
6
|
-
import { getRelativeSelection } from './ySyncPlugin.js';
|
|
7
6
|
import { ySyncPluginKey, yUndoPluginKey } from './keys.js';
|
|
7
|
+
import { getRelativeSelection } from './ProsemirrorBinding.js';
|
|
8
8
|
|
|
9
9
|
export interface UndoPluginState {
|
|
10
10
|
undoManager: UndoManager;
|
package/esm/convertUtils.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import * as Y from 'yjs';
|
|
2
|
-
import { Fragment, Node, type Schema } from 'prosemirror-model';
|
|
3
|
-
import { BindingMetadata } from './ySyncPlugin.js';
|
|
4
|
-
export declare const createEmptyMeta: () => BindingMetadata;
|
|
5
|
-
/**
|
|
6
|
-
* Utility function for converting an Y.Fragment to a ProseMirror fragment.
|
|
7
|
-
*/
|
|
8
|
-
export declare const yXmlFragmentToProseMirrorFragment: (yXmlFragment: Y.XmlFragment, schema: Schema) => Fragment;
|
|
9
|
-
/**
|
|
10
|
-
* Utility function for converting an Y.Fragment to a ProseMirror node.
|
|
11
|
-
*/
|
|
12
|
-
export declare const yXmlFragmentToProseMirrorRootNode: (yXmlFragment: Y.XmlFragment, schema: Schema) => Node;
|
|
13
|
-
/**
|
|
14
|
-
* The initial ProseMirror content should be supplied by Yjs. This function transforms a Y.Fragment
|
|
15
|
-
* to a ProseMirror Doc node and creates a mapping that is used by the sync plugin.
|
|
16
|
-
*
|
|
17
|
-
* @todo deprecate mapping property
|
|
18
|
-
*/
|
|
19
|
-
export declare const initProseMirrorDoc: (yXmlFragment: Y.XmlFragment, schema: Schema) => {
|
|
20
|
-
doc: Node;
|
|
21
|
-
meta: BindingMetadata;
|
|
22
|
-
mapping: Map<Y.AbstractType<any>, Node | Node[]>;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Utility method to convert a Prosemirror Doc Node into a Y.Doc.
|
|
26
|
-
*
|
|
27
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
28
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
29
|
-
* collaboration has begun as all history will be lost
|
|
30
|
-
*/
|
|
31
|
-
export declare function prosemirrorToYDoc(doc: Node, xmlFragment?: string): Y.Doc;
|
|
32
|
-
/**
|
|
33
|
-
* Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.
|
|
34
|
-
*
|
|
35
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
36
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
37
|
-
* collaboration has begun as all history will be lost
|
|
38
|
-
*
|
|
39
|
-
* Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this
|
|
40
|
-
* method is called, but it must be added before any other operations are performed on it.
|
|
41
|
-
*/
|
|
42
|
-
export declare function prosemirrorToYXmlFragment(doc: Node, xmlFragment: Y.XmlFragment): Y.XmlFragment;
|
|
43
|
-
/**
|
|
44
|
-
* Utility method to convert Prosemirror compatible JSON into a Y.Doc.
|
|
45
|
-
*
|
|
46
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
47
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
48
|
-
* collaboration has begun as all history will be lost
|
|
49
|
-
*/
|
|
50
|
-
export declare function prosemirrorJSONToYDoc(schema: Schema, state: any, xmlFragment?: string): Y.Doc;
|
|
51
|
-
/**
|
|
52
|
-
* Utility method to convert Prosemirror compatible JSON to a Y.XmlFragment
|
|
53
|
-
*
|
|
54
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
55
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
56
|
-
* collaboration has begun as all history will be lost
|
|
57
|
-
*/
|
|
58
|
-
export declare function prosemirrorJSONToYXmlFragment(schema: Schema, state: any, xmlFragment: Y.XmlFragment): Y.XmlFragment;
|
|
59
|
-
//# sourceMappingURL=convertUtils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"convertUtils.d.ts","sourceRoot":"","sources":["../src/convertUtils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EACL,eAAe,EAIhB,MAAM,kBAAkB,CAAC;AAE1B,eAAO,MAAM,eAAe,QAAO,eAGjC,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,iCAAiC,GAC5C,cAAc,CAAC,CAAC,WAAW,EAC3B,QAAQ,MAAM,aAUf,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iCAAiC,GAC5C,cAAc,CAAC,CAAC,WAAW,EAC3B,QAAQ,MAAM,SAKb,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAC7B,cAAc,CAAC,CAAC,WAAW,EAC3B,QAAQ,MAAM;;;;CAef,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,IAAI,EACT,WAAW,GAAE,MAAsB,GAClC,CAAC,CAAC,GAAG,CAUP;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,IAAI,EACT,WAAW,EAAE,CAAC,CAAC,WAAW,GACzB,CAAC,CAAC,WAAW,CAOf;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,GAAG,EACV,WAAW,GAAE,MAAsB,GAClC,CAAC,CAAC,GAAG,CAGP;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,GAAG,EACV,WAAW,EAAE,CAAC,CAAC,WAAW,GACzB,CAAC,CAAC,WAAW,CAGf"}
|
package/esm/convertUtils.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import * as Y from 'yjs';
|
|
2
|
-
import { Fragment, Node } from 'prosemirror-model';
|
|
3
|
-
import { createNodeFromYElement, updateYFragment, } from './ySyncPlugin.js';
|
|
4
|
-
export const createEmptyMeta = () => ({
|
|
5
|
-
mapping: new Map(),
|
|
6
|
-
isOMark: new Map(),
|
|
7
|
-
});
|
|
8
|
-
/**
|
|
9
|
-
* Utility function for converting an Y.Fragment to a ProseMirror fragment.
|
|
10
|
-
*/
|
|
11
|
-
export const yXmlFragmentToProseMirrorFragment = (yXmlFragment, schema) => {
|
|
12
|
-
const fragmentContent = yXmlFragment.toArray().map((t) => createNodeFromYElement(
|
|
13
|
-
/** @type {Y.XmlElement} */ (t), schema, createEmptyMeta())).filter((n) => n !== null);
|
|
14
|
-
return Fragment.fromArray(fragmentContent);
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Utility function for converting an Y.Fragment to a ProseMirror node.
|
|
18
|
-
*/
|
|
19
|
-
export const yXmlFragmentToProseMirrorRootNode = (yXmlFragment, schema) => schema.topNodeType.create(null, yXmlFragmentToProseMirrorFragment(yXmlFragment, schema));
|
|
20
|
-
/**
|
|
21
|
-
* The initial ProseMirror content should be supplied by Yjs. This function transforms a Y.Fragment
|
|
22
|
-
* to a ProseMirror Doc node and creates a mapping that is used by the sync plugin.
|
|
23
|
-
*
|
|
24
|
-
* @todo deprecate mapping property
|
|
25
|
-
*/
|
|
26
|
-
export const initProseMirrorDoc = (yXmlFragment, schema) => {
|
|
27
|
-
const meta = createEmptyMeta();
|
|
28
|
-
const fragmentContent = yXmlFragment.toArray().map((t) => createNodeFromYElement(t, schema, meta)).filter((n) => n !== null);
|
|
29
|
-
const doc = schema.topNodeType.create(null, Fragment.fromArray(fragmentContent));
|
|
30
|
-
return { doc, meta, mapping: meta.mapping };
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Utility method to convert a Prosemirror Doc Node into a Y.Doc.
|
|
34
|
-
*
|
|
35
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
36
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
37
|
-
* collaboration has begun as all history will be lost
|
|
38
|
-
*/
|
|
39
|
-
export function prosemirrorToYDoc(doc, xmlFragment = 'prosemirror') {
|
|
40
|
-
const ydoc = new Y.Doc();
|
|
41
|
-
const type =
|
|
42
|
-
/** @type {Y.XmlFragment} */ (ydoc.get(xmlFragment, Y.XmlFragment));
|
|
43
|
-
if (!type.doc) {
|
|
44
|
-
return ydoc;
|
|
45
|
-
}
|
|
46
|
-
prosemirrorToYXmlFragment(doc, type);
|
|
47
|
-
return type.doc;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.
|
|
51
|
-
*
|
|
52
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
53
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
54
|
-
* collaboration has begun as all history will be lost
|
|
55
|
-
*
|
|
56
|
-
* Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this
|
|
57
|
-
* method is called, but it must be added before any other operations are performed on it.
|
|
58
|
-
*/
|
|
59
|
-
export function prosemirrorToYXmlFragment(doc, xmlFragment) {
|
|
60
|
-
const type = xmlFragment || new Y.XmlFragment();
|
|
61
|
-
const ydoc = type.doc
|
|
62
|
-
? type.doc
|
|
63
|
-
: { transact: (transaction) => transaction(undefined) };
|
|
64
|
-
updateYFragment(ydoc, type, doc, { mapping: new Map(), isOMark: new Map() });
|
|
65
|
-
return type;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Utility method to convert Prosemirror compatible JSON into a Y.Doc.
|
|
69
|
-
*
|
|
70
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
71
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
72
|
-
* collaboration has begun as all history will be lost
|
|
73
|
-
*/
|
|
74
|
-
export function prosemirrorJSONToYDoc(schema, state, xmlFragment = 'prosemirror') {
|
|
75
|
-
const doc = Node.fromJSON(schema, state);
|
|
76
|
-
return prosemirrorToYDoc(doc, xmlFragment);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Utility method to convert Prosemirror compatible JSON to a Y.XmlFragment
|
|
80
|
-
*
|
|
81
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
82
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
83
|
-
* collaboration has begun as all history will be lost
|
|
84
|
-
*/
|
|
85
|
-
export function prosemirrorJSONToYXmlFragment(schema, state, xmlFragment) {
|
|
86
|
-
const doc = Node.fromJSON(schema, state);
|
|
87
|
-
return prosemirrorToYXmlFragment(doc, xmlFragment);
|
|
88
|
-
}
|
|
89
|
-
//# sourceMappingURL=convertUtils.js.map
|
package/esm/convertUtils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|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AAI,GAAG,EAAE,EAAE,CAAC,CAAC;IAC7E,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CACnC,MAAc,EACd,KAAU,EACV,cAAsB,aAAa;IAEnC,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACzC,OAAO,iBAAiB,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAC3C,MAAc,EACd,KAAU,EACV,WAA0B;IAE1B,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACzC,OAAO,yBAAyB,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;AACrD,CAAC"}
|
package/src/convertUtils.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import * as Y from 'yjs';
|
|
2
|
-
import { Fragment, Node, type Schema } from 'prosemirror-model';
|
|
3
|
-
import {
|
|
4
|
-
BindingMetadata,
|
|
5
|
-
createNodeFromYElement,
|
|
6
|
-
TransactFunc,
|
|
7
|
-
updateYFragment,
|
|
8
|
-
} from './ySyncPlugin.js';
|
|
9
|
-
|
|
10
|
-
export const createEmptyMeta = (): BindingMetadata => ({
|
|
11
|
-
mapping: new Map(),
|
|
12
|
-
isOMark: new Map(),
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Utility function for converting an Y.Fragment to a ProseMirror fragment.
|
|
17
|
-
*/
|
|
18
|
-
export const yXmlFragmentToProseMirrorFragment = (
|
|
19
|
-
yXmlFragment: Y.XmlFragment,
|
|
20
|
-
schema: Schema,
|
|
21
|
-
) => {
|
|
22
|
-
const fragmentContent = yXmlFragment.toArray().map((t) =>
|
|
23
|
-
createNodeFromYElement(
|
|
24
|
-
/** @type {Y.XmlElement} */ (t),
|
|
25
|
-
schema,
|
|
26
|
-
createEmptyMeta(),
|
|
27
|
-
)
|
|
28
|
-
).filter((n) => n !== null);
|
|
29
|
-
return Fragment.fromArray(fragmentContent);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Utility function for converting an Y.Fragment to a ProseMirror node.
|
|
34
|
-
*/
|
|
35
|
-
export const yXmlFragmentToProseMirrorRootNode = (
|
|
36
|
-
yXmlFragment: Y.XmlFragment,
|
|
37
|
-
schema: Schema,
|
|
38
|
-
) =>
|
|
39
|
-
schema.topNodeType.create(
|
|
40
|
-
null,
|
|
41
|
-
yXmlFragmentToProseMirrorFragment(yXmlFragment, schema),
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The initial ProseMirror content should be supplied by Yjs. This function transforms a Y.Fragment
|
|
46
|
-
* to a ProseMirror Doc node and creates a mapping that is used by the sync plugin.
|
|
47
|
-
*
|
|
48
|
-
* @todo deprecate mapping property
|
|
49
|
-
*/
|
|
50
|
-
export const initProseMirrorDoc = (
|
|
51
|
-
yXmlFragment: Y.XmlFragment,
|
|
52
|
-
schema: Schema,
|
|
53
|
-
) => {
|
|
54
|
-
const meta = createEmptyMeta();
|
|
55
|
-
const fragmentContent = yXmlFragment.toArray().map((t) =>
|
|
56
|
-
createNodeFromYElement(
|
|
57
|
-
t,
|
|
58
|
-
schema,
|
|
59
|
-
meta,
|
|
60
|
-
)
|
|
61
|
-
).filter((n) => n !== null);
|
|
62
|
-
const doc = schema.topNodeType.create(
|
|
63
|
-
null,
|
|
64
|
-
Fragment.fromArray(fragmentContent),
|
|
65
|
-
);
|
|
66
|
-
return { doc, meta, mapping: meta.mapping };
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Utility method to convert a Prosemirror Doc Node into a Y.Doc.
|
|
71
|
-
*
|
|
72
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
73
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
74
|
-
* collaboration has begun as all history will be lost
|
|
75
|
-
*/
|
|
76
|
-
export function prosemirrorToYDoc(
|
|
77
|
-
doc: Node,
|
|
78
|
-
xmlFragment: string = 'prosemirror',
|
|
79
|
-
): Y.Doc {
|
|
80
|
-
const ydoc = new Y.Doc();
|
|
81
|
-
const type =
|
|
82
|
-
/** @type {Y.XmlFragment} */ (ydoc.get(xmlFragment, Y.XmlFragment));
|
|
83
|
-
if (!type.doc) {
|
|
84
|
-
return ydoc;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
prosemirrorToYXmlFragment(doc, type);
|
|
88
|
-
return type.doc;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.
|
|
93
|
-
*
|
|
94
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
95
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
96
|
-
* collaboration has begun as all history will be lost
|
|
97
|
-
*
|
|
98
|
-
* Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this
|
|
99
|
-
* method is called, but it must be added before any other operations are performed on it.
|
|
100
|
-
*/
|
|
101
|
-
export function prosemirrorToYXmlFragment(
|
|
102
|
-
doc: Node,
|
|
103
|
-
xmlFragment: Y.XmlFragment,
|
|
104
|
-
): Y.XmlFragment {
|
|
105
|
-
const type = xmlFragment || new Y.XmlFragment();
|
|
106
|
-
const ydoc: { transact: TransactFunc<void> } = type.doc
|
|
107
|
-
? type.doc
|
|
108
|
-
: { transact: (transaction) => transaction(undefined) };
|
|
109
|
-
updateYFragment(ydoc, type, doc, { mapping: new Map(), isOMark: new Map() });
|
|
110
|
-
return type;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Utility method to convert Prosemirror compatible JSON into a Y.Doc.
|
|
115
|
-
*
|
|
116
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
117
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
118
|
-
* collaboration has begun as all history will be lost
|
|
119
|
-
*/
|
|
120
|
-
export function prosemirrorJSONToYDoc(
|
|
121
|
-
schema: Schema,
|
|
122
|
-
state: any,
|
|
123
|
-
xmlFragment: string = 'prosemirror',
|
|
124
|
-
): Y.Doc {
|
|
125
|
-
const doc = Node.fromJSON(schema, state);
|
|
126
|
-
return prosemirrorToYDoc(doc, xmlFragment);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Utility method to convert Prosemirror compatible JSON to a Y.XmlFragment
|
|
131
|
-
*
|
|
132
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
133
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
134
|
-
* collaboration has begun as all history will be lost
|
|
135
|
-
*/
|
|
136
|
-
export function prosemirrorJSONToYXmlFragment(
|
|
137
|
-
schema: Schema,
|
|
138
|
-
state: any,
|
|
139
|
-
xmlFragment: Y.XmlFragment,
|
|
140
|
-
): Y.XmlFragment {
|
|
141
|
-
const doc = Node.fromJSON(schema, state);
|
|
142
|
-
return prosemirrorToYXmlFragment(doc, xmlFragment);
|
|
143
|
-
}
|