@liveblocks/react-tiptap 3.19.5-rc1 → 3.20.0-exp1
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/dist/LiveblocksExtension.cjs +51 -5
- package/dist/LiveblocksExtension.cjs.map +1 -1
- package/dist/LiveblocksExtension.js +51 -5
- package/dist/LiveblocksExtension.js.map +1 -1
- package/dist/ai/AiExtension.cjs +2 -1
- package/dist/ai/AiExtension.cjs.map +1 -1
- package/dist/ai/AiExtension.js +2 -1
- package/dist/ai/AiExtension.js.map +1 -1
- package/dist/collaboration-liveblocks/cursors.cjs +267 -0
- package/dist/collaboration-liveblocks/cursors.cjs.map +1 -0
- package/dist/collaboration-liveblocks/cursors.js +264 -0
- package/dist/collaboration-liveblocks/cursors.js.map +1 -0
- package/dist/collaboration-liveblocks/mapping.cjs +218 -0
- package/dist/collaboration-liveblocks/mapping.cjs.map +1 -0
- package/dist/collaboration-liveblocks/mapping.js +207 -0
- package/dist/collaboration-liveblocks/mapping.js.map +1 -0
- package/dist/collaboration-liveblocks/plugin.cjs +249 -0
- package/dist/collaboration-liveblocks/plugin.cjs.map +1 -0
- package/dist/collaboration-liveblocks/plugin.js +246 -0
- package/dist/collaboration-liveblocks/plugin.js.map +1 -0
- package/dist/collaboration-liveblocks/remote.cjs +210 -0
- package/dist/collaboration-liveblocks/remote.cjs.map +1 -0
- package/dist/collaboration-liveblocks/remote.js +207 -0
- package/dist/collaboration-liveblocks/remote.js.map +1 -0
- package/dist/collaboration-liveblocks/schema.cjs +150 -0
- package/dist/collaboration-liveblocks/schema.cjs.map +1 -0
- package/dist/collaboration-liveblocks/schema.js +135 -0
- package/dist/collaboration-liveblocks/schema.js.map +1 -0
- package/dist/collaboration-liveblocks/steps.cjs +359 -0
- package/dist/collaboration-liveblocks/steps.cjs.map +1 -0
- package/dist/collaboration-liveblocks/steps.js +356 -0
- package/dist/collaboration-liveblocks/steps.js.map +1 -0
- package/dist/comments/CommentsExtension.cjs +7 -1
- package/dist/comments/CommentsExtension.cjs.map +1 -1
- package/dist/comments/CommentsExtension.js +7 -1
- package/dist/comments/CommentsExtension.js.map +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/mentions/MentionExtension.cjs +4 -1
- package/dist/mentions/MentionExtension.cjs.map +1 -1
- package/dist/mentions/MentionExtension.js +4 -1
- package/dist/mentions/MentionExtension.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.cjs.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +6 -6
- package/src/styles/index.css +12 -4
- package/styles.css +1 -1
- package/styles.css.map +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { LiveObject, LiveText } from '@liveblocks/client';
|
|
2
|
+
import { Slice, Fragment } from '@tiptap/pm/model';
|
|
3
|
+
import { buildLiveblocksTreeIndex, findTextRangeByLiveText, findListRangeByLiveList, getChildPosition, findNodeRangeByLiveNode } from './mapping.js';
|
|
4
|
+
import { liveblocksTiptapNodeToJsonNodes, attributesToMarks, getLiveblocksNodeText } from './schema.js';
|
|
5
|
+
|
|
6
|
+
function getMarkType(schema, type) {
|
|
7
|
+
return schema.marks[type];
|
|
8
|
+
}
|
|
9
|
+
function isJsonObject(value) {
|
|
10
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
function isLiveblocksTiptapNode(value) {
|
|
13
|
+
return value instanceof LiveObject && typeof value.get("id") === "string" && typeof value.get("type") === "string";
|
|
14
|
+
}
|
|
15
|
+
function getLiveblocksNodeAttrs(node) {
|
|
16
|
+
const attrs = node.get("attrs");
|
|
17
|
+
return isJsonObject(attrs) ? attrs : void 0;
|
|
18
|
+
}
|
|
19
|
+
function createSliceFromLiveblocksNode(schema, node) {
|
|
20
|
+
const nodes = liveblocksTiptapNodeToJsonNodes(node).map(
|
|
21
|
+
(jsonNode) => schema.nodeFromJSON(jsonNode)
|
|
22
|
+
);
|
|
23
|
+
return new Slice(Fragment.fromArray(nodes), 0, 0);
|
|
24
|
+
}
|
|
25
|
+
function applyMarksFromAttributes(tr, schema, from, to, attributes) {
|
|
26
|
+
const marks = attributesToMarks({ ...attributes });
|
|
27
|
+
for (const mark of marks ?? []) {
|
|
28
|
+
const markType = getMarkType(schema, mark.type);
|
|
29
|
+
if (markType !== void 0) {
|
|
30
|
+
tr.addMark(from, to, markType.create(mark.attrs));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (marks === void 0) {
|
|
34
|
+
tr.removeMark(from, to);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function findNodeRangeForLiveText(index, text) {
|
|
38
|
+
return index.nodeRanges.find(
|
|
39
|
+
(range) => getLiveblocksNodeText(range.node) === text
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
function applyRemoteStorageUpdates(view, liveRoot, updates) {
|
|
43
|
+
if (updates.length === 0) {
|
|
44
|
+
return { type: "unsupported" };
|
|
45
|
+
}
|
|
46
|
+
let tr = view.state.tr;
|
|
47
|
+
for (const update of updates) {
|
|
48
|
+
if (update.type === "LiveText") {
|
|
49
|
+
if (!(update.node instanceof LiveText)) {
|
|
50
|
+
return { type: "unsupported" };
|
|
51
|
+
}
|
|
52
|
+
for (const change of update.updates) {
|
|
53
|
+
const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);
|
|
54
|
+
let range = findTextRangeByLiveText(index, update.node);
|
|
55
|
+
if (range === void 0 && change.type === "delete") {
|
|
56
|
+
const wrapperRange = findNodeRangeForLiveText(index, update.node);
|
|
57
|
+
if (wrapperRange !== void 0) {
|
|
58
|
+
const from2 = wrapperRange.from + change.index;
|
|
59
|
+
tr = tr.delete(from2, from2 + change.length);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
return { type: "unsupported" };
|
|
63
|
+
}
|
|
64
|
+
if (range === void 0) {
|
|
65
|
+
return { type: "unsupported" };
|
|
66
|
+
}
|
|
67
|
+
const from = range.from + change.index - range.liveOffset;
|
|
68
|
+
if (change.type === "insert") {
|
|
69
|
+
tr = tr.insertText(change.text, from);
|
|
70
|
+
if (change.attributes !== void 0) {
|
|
71
|
+
applyMarksFromAttributes(
|
|
72
|
+
tr,
|
|
73
|
+
view.state.schema,
|
|
74
|
+
from,
|
|
75
|
+
from + change.text.length,
|
|
76
|
+
change.attributes
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
} else if (change.type === "delete") {
|
|
80
|
+
tr = tr.delete(from, from + change.length);
|
|
81
|
+
} else {
|
|
82
|
+
applyMarksFromAttributes(
|
|
83
|
+
tr,
|
|
84
|
+
view.state.schema,
|
|
85
|
+
from,
|
|
86
|
+
from + change.length,
|
|
87
|
+
change.attributes
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (update.type === "LiveList") {
|
|
94
|
+
for (const change of update.updates) {
|
|
95
|
+
const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);
|
|
96
|
+
const range = findListRangeByLiveList(index, update.node);
|
|
97
|
+
if (range === void 0) {
|
|
98
|
+
return { type: "unsupported" };
|
|
99
|
+
}
|
|
100
|
+
if (change.type === "insert") {
|
|
101
|
+
if (!isLiveblocksTiptapNode(change.item)) {
|
|
102
|
+
return { type: "unsupported" };
|
|
103
|
+
}
|
|
104
|
+
const pos = getChildPosition(range.pmNode, range.from, change.index);
|
|
105
|
+
if (pos === void 0) {
|
|
106
|
+
return { type: "unsupported" };
|
|
107
|
+
}
|
|
108
|
+
tr = tr.replace(
|
|
109
|
+
pos,
|
|
110
|
+
pos,
|
|
111
|
+
createSliceFromLiveblocksNode(view.state.schema, change.item)
|
|
112
|
+
);
|
|
113
|
+
} else if (change.type === "delete") {
|
|
114
|
+
const from = getChildPosition(range.pmNode, range.from, change.index);
|
|
115
|
+
const to = getChildPosition(
|
|
116
|
+
range.pmNode,
|
|
117
|
+
range.from,
|
|
118
|
+
change.index + 1
|
|
119
|
+
);
|
|
120
|
+
if (from === void 0 || to === void 0) {
|
|
121
|
+
return { type: "unsupported" };
|
|
122
|
+
}
|
|
123
|
+
tr = tr.delete(from, to);
|
|
124
|
+
} else if (change.type === "set") {
|
|
125
|
+
if (!isLiveblocksTiptapNode(change.item)) {
|
|
126
|
+
return { type: "unsupported" };
|
|
127
|
+
}
|
|
128
|
+
const from = getChildPosition(range.pmNode, range.from, change.index);
|
|
129
|
+
const to = getChildPosition(
|
|
130
|
+
range.pmNode,
|
|
131
|
+
range.from,
|
|
132
|
+
change.index + 1
|
|
133
|
+
);
|
|
134
|
+
if (from === void 0 || to === void 0) {
|
|
135
|
+
return { type: "unsupported" };
|
|
136
|
+
}
|
|
137
|
+
tr = tr.replace(
|
|
138
|
+
from,
|
|
139
|
+
to,
|
|
140
|
+
createSliceFromLiveblocksNode(view.state.schema, change.item)
|
|
141
|
+
);
|
|
142
|
+
} else {
|
|
143
|
+
if (!isLiveblocksTiptapNode(change.item)) {
|
|
144
|
+
return { type: "unsupported" };
|
|
145
|
+
}
|
|
146
|
+
const from = getChildPosition(
|
|
147
|
+
range.pmNode,
|
|
148
|
+
range.from,
|
|
149
|
+
change.previousIndex
|
|
150
|
+
);
|
|
151
|
+
const to = getChildPosition(
|
|
152
|
+
range.pmNode,
|
|
153
|
+
range.from,
|
|
154
|
+
change.previousIndex + 1
|
|
155
|
+
);
|
|
156
|
+
const rawInsertPos = getChildPosition(
|
|
157
|
+
range.pmNode,
|
|
158
|
+
range.from,
|
|
159
|
+
change.index > change.previousIndex ? change.index + 1 : change.index
|
|
160
|
+
);
|
|
161
|
+
if (from === void 0 || to === void 0 || rawInsertPos === void 0) {
|
|
162
|
+
return { type: "unsupported" };
|
|
163
|
+
}
|
|
164
|
+
const slice = createSliceFromLiveblocksNode(
|
|
165
|
+
view.state.schema,
|
|
166
|
+
change.item
|
|
167
|
+
);
|
|
168
|
+
tr = tr.delete(from, to);
|
|
169
|
+
const insertPos = tr.mapping.map(rawInsertPos, -1);
|
|
170
|
+
tr = tr.replace(insertPos, insertPos, slice);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (update.type === "LiveObject") {
|
|
176
|
+
if (!isLiveblocksTiptapNode(update.node)) {
|
|
177
|
+
return { type: "unsupported" };
|
|
178
|
+
}
|
|
179
|
+
const attrUpdate = update.updates.attrs;
|
|
180
|
+
if (attrUpdate === void 0) {
|
|
181
|
+
return { type: "unsupported" };
|
|
182
|
+
}
|
|
183
|
+
const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);
|
|
184
|
+
const range = findNodeRangeByLiveNode(index, update.node);
|
|
185
|
+
if (range === void 0 || range.pmNode.type.name === "doc") {
|
|
186
|
+
return { type: "unsupported" };
|
|
187
|
+
}
|
|
188
|
+
tr = tr.setNodeMarkup(
|
|
189
|
+
range.from,
|
|
190
|
+
void 0,
|
|
191
|
+
attrUpdate.type === "delete" ? null : getLiveblocksNodeAttrs(update.node)
|
|
192
|
+
);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
return { type: "unsupported" };
|
|
196
|
+
}
|
|
197
|
+
return { type: "applied", tr };
|
|
198
|
+
}
|
|
199
|
+
function applyRemoteLiveTextUpdates(view, liveRoot, updates) {
|
|
200
|
+
if (!updates.every((update) => update.type === "LiveText")) {
|
|
201
|
+
return { type: "unsupported" };
|
|
202
|
+
}
|
|
203
|
+
return applyRemoteStorageUpdates(view, liveRoot, updates);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export { applyRemoteLiveTextUpdates, applyRemoteStorageUpdates };
|
|
207
|
+
//# sourceMappingURL=remote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.js","sources":["../../src/collaboration-liveblocks/remote.ts"],"sourcesContent":["import {\n type Json,\n type JsonObject,\n LiveObject,\n LiveText,\n type StorageUpdate,\n} from \"@liveblocks/client\";\nimport { Fragment, type MarkType, type Schema, Slice } from \"@tiptap/pm/model\";\nimport type { Transaction } from \"@tiptap/pm/state\";\nimport type { EditorView } from \"@tiptap/pm/view\";\n\nimport {\n buildLiveblocksTreeIndex,\n findListRangeByLiveList,\n findNodeRangeByLiveNode,\n findTextRangeByLiveText,\n getChildPosition,\n} from \"./mapping\";\nimport {\n attributesToMarks,\n type LiveblocksTiptapNode,\n getLiveblocksNodeText,\n liveblocksTiptapNodeToJsonNodes,\n} from \"./schema\";\n\ntype RemoteApplyResult =\n | {\n type: \"applied\";\n tr: Transaction;\n }\n | {\n type: \"unsupported\";\n };\n\nfunction getMarkType(schema: Schema, type: string): MarkType | undefined {\n return schema.marks[type];\n}\n\nfunction isJsonObject(value: unknown): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isLiveblocksTiptapNode(value: unknown): value is LiveblocksTiptapNode {\n return (\n value instanceof LiveObject &&\n typeof value.get(\"id\") === \"string\" &&\n typeof value.get(\"type\") === \"string\"\n );\n}\n\nfunction getLiveblocksNodeAttrs(\n node: LiveblocksTiptapNode\n): JsonObject | undefined {\n const attrs = node.get(\"attrs\");\n return isJsonObject(attrs) ? attrs : undefined;\n}\n\nfunction createSliceFromLiveblocksNode(\n schema: Schema,\n node: LiveblocksTiptapNode\n): Slice {\n const nodes = liveblocksTiptapNodeToJsonNodes(node).map((jsonNode) =>\n schema.nodeFromJSON(jsonNode)\n );\n\n return new Slice(Fragment.fromArray(nodes), 0, 0);\n}\n\nfunction applyMarksFromAttributes(\n tr: Transaction,\n schema: Schema,\n from: number,\n to: number,\n attributes: Record<string, Json | null>\n): void {\n const marks = attributesToMarks({ ...attributes });\n\n for (const mark of marks ?? []) {\n const markType = getMarkType(schema, mark.type);\n if (markType !== undefined) {\n tr.addMark(from, to, markType.create(mark.attrs));\n }\n }\n\n if (marks === undefined) {\n tr.removeMark(from, to);\n }\n}\n\nfunction findNodeRangeForLiveText(\n index: ReturnType<typeof buildLiveblocksTreeIndex>,\n text: LiveText\n) {\n return index.nodeRanges.find(\n (range) => getLiveblocksNodeText(range.node) === text\n );\n}\n\nexport function applyRemoteStorageUpdates(\n view: EditorView,\n liveRoot: LiveblocksTiptapNode,\n updates: readonly StorageUpdate[]\n): RemoteApplyResult {\n if (updates.length === 0) {\n return { type: \"unsupported\" };\n }\n\n let tr = view.state.tr;\n\n for (const update of updates) {\n if (update.type === \"LiveText\") {\n if (!(update.node instanceof LiveText)) {\n return { type: \"unsupported\" };\n }\n\n for (const change of update.updates) {\n const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);\n let range = findTextRangeByLiveText(index, update.node);\n\n if (range === undefined && change.type === \"delete\") {\n const wrapperRange = findNodeRangeForLiveText(index, update.node);\n if (wrapperRange !== undefined) {\n const from = wrapperRange.from + change.index;\n tr = tr.delete(from, from + change.length);\n continue;\n }\n\n return { type: \"unsupported\" };\n }\n\n if (range === undefined) {\n return { type: \"unsupported\" };\n }\n\n const from = range.from + change.index - range.liveOffset;\n\n if (change.type === \"insert\") {\n tr = tr.insertText(change.text, from);\n if (change.attributes !== undefined) {\n applyMarksFromAttributes(\n tr,\n view.state.schema,\n from,\n from + change.text.length,\n change.attributes\n );\n }\n } else if (change.type === \"delete\") {\n tr = tr.delete(from, from + change.length);\n } else {\n applyMarksFromAttributes(\n tr,\n view.state.schema,\n from,\n from + change.length,\n change.attributes\n );\n }\n }\n\n continue;\n }\n\n if (update.type === \"LiveList\") {\n for (const change of update.updates) {\n const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);\n const range = findListRangeByLiveList(index, update.node);\n if (range === undefined) {\n return { type: \"unsupported\" };\n }\n\n if (change.type === \"insert\") {\n if (!isLiveblocksTiptapNode(change.item)) {\n return { type: \"unsupported\" };\n }\n\n const pos = getChildPosition(range.pmNode, range.from, change.index);\n if (pos === undefined) {\n return { type: \"unsupported\" };\n }\n\n tr = tr.replace(\n pos,\n pos,\n createSliceFromLiveblocksNode(view.state.schema, change.item)\n );\n } else if (change.type === \"delete\") {\n const from = getChildPosition(range.pmNode, range.from, change.index);\n const to = getChildPosition(\n range.pmNode,\n range.from,\n change.index + 1\n );\n if (from === undefined || to === undefined) {\n return { type: \"unsupported\" };\n }\n\n tr = tr.delete(from, to);\n } else if (change.type === \"set\") {\n if (!isLiveblocksTiptapNode(change.item)) {\n return { type: \"unsupported\" };\n }\n\n const from = getChildPosition(range.pmNode, range.from, change.index);\n const to = getChildPosition(\n range.pmNode,\n range.from,\n change.index + 1\n );\n if (from === undefined || to === undefined) {\n return { type: \"unsupported\" };\n }\n\n tr = tr.replace(\n from,\n to,\n createSliceFromLiveblocksNode(view.state.schema, change.item)\n );\n } else {\n if (!isLiveblocksTiptapNode(change.item)) {\n return { type: \"unsupported\" };\n }\n\n const from = getChildPosition(\n range.pmNode,\n range.from,\n change.previousIndex\n );\n const to = getChildPosition(\n range.pmNode,\n range.from,\n change.previousIndex + 1\n );\n const rawInsertPos = getChildPosition(\n range.pmNode,\n range.from,\n change.index > change.previousIndex\n ? change.index + 1\n : change.index\n );\n if (\n from === undefined ||\n to === undefined ||\n rawInsertPos === undefined\n ) {\n return { type: \"unsupported\" };\n }\n\n const slice = createSliceFromLiveblocksNode(\n view.state.schema,\n change.item\n );\n tr = tr.delete(from, to);\n const insertPos = tr.mapping.map(rawInsertPos, -1);\n tr = tr.replace(insertPos, insertPos, slice);\n }\n }\n\n continue;\n }\n\n if (update.type === \"LiveObject\") {\n if (!isLiveblocksTiptapNode(update.node)) {\n return { type: \"unsupported\" };\n }\n\n const attrUpdate = update.updates.attrs;\n if (attrUpdate === undefined) {\n return { type: \"unsupported\" };\n }\n\n const index = buildLiveblocksTreeIndex(tr.doc, liveRoot);\n const range = findNodeRangeByLiveNode(index, update.node);\n if (range === undefined || range.pmNode.type.name === \"doc\") {\n return { type: \"unsupported\" };\n }\n\n tr = tr.setNodeMarkup(\n range.from,\n undefined,\n attrUpdate.type === \"delete\" ? null : getLiveblocksNodeAttrs(update.node)\n );\n\n continue;\n }\n\n return { type: \"unsupported\" };\n }\n\n return { type: \"applied\", tr };\n}\n\nexport function applyRemoteLiveTextUpdates(\n view: EditorView,\n liveRoot: LiveblocksTiptapNode,\n updates: readonly StorageUpdate[]\n): RemoteApplyResult {\n if (!updates.every((update) => update.type === \"LiveText\")) {\n return { type: \"unsupported\" };\n }\n\n return applyRemoteStorageUpdates(view, liveRoot, updates);\n}\n\n"],"names":["from"],"mappings":";;;;;AAkCA,SAAS,WAAA,CAAY,QAAgB,IAAoC,EAAA;AACvE,EAAO,OAAA,MAAA,CAAO,MAAM,IAAI,CAAA,CAAA;AAC1B,CAAA;AAEA,SAAS,aAAa,KAAqC,EAAA;AACzD,EAAO,OAAA,OAAO,UAAU,QAAY,IAAA,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA,CAAA;AAC5E,CAAA;AAEA,SAAS,uBAAuB,KAA+C,EAAA;AAC7E,EAAA,OACE,KAAiB,YAAA,UAAA,IACjB,OAAO,KAAA,CAAM,GAAI,CAAA,IAAI,CAAM,KAAA,QAAA,IAC3B,OAAO,KAAA,CAAM,GAAI,CAAA,MAAM,CAAM,KAAA,QAAA,CAAA;AAEjC,CAAA;AAEA,SAAS,uBACP,IACwB,EAAA;AACxB,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,OAAO,CAAA,CAAA;AAC9B,EAAO,OAAA,YAAA,CAAa,KAAK,CAAA,GAAI,KAAQ,GAAA,KAAA,CAAA,CAAA;AACvC,CAAA;AAEA,SAAS,6BAAA,CACP,QACA,IACO,EAAA;AACP,EAAM,MAAA,KAAA,GAAQ,+BAAgC,CAAA,IAAI,CAAE,CAAA,GAAA;AAAA,IAAI,CAAC,QAAA,KACvD,MAAO,CAAA,YAAA,CAAa,QAAQ,CAAA;AAAA,GAC9B,CAAA;AAEA,EAAA,OAAO,IAAI,KAAM,CAAA,QAAA,CAAS,UAA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EA,IAAO,OAAA,EAAE,MAAM,aAAc,EAAA,CAAA;AAAA,GAC/B;AAEA,EAAO,OAAA,EAAE,IAAM,EAAA,SAAA,EAAW,EAAG,EAAA,CAAA;AAC/B,CAAA;AAEgB,SAAA,0BAAA,CACd,IACA,EAAA,QAAA,EACA,OACmB,EAAA;AACnB,EAAI,IAAA,CAAC,QAAQ,KAAM,CAAA,CAAC,WAAW,MAAO,CAAA,IAAA,KAAS,UAAU,CAAG,EAAA;AAC1D,IAAO,OAAA,EAAE,MAAM,aAAc,EAAA,CAAA;AAAA,GAC/B;AAEA,EAAO,OAAA,yBAAA,CAA0B,IAAM,EAAA,QAAA,EAAU,OAAO,CAAA,CAAA;AAC1D;;;;"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var client = require('@liveblocks/client');
|
|
4
|
+
|
|
5
|
+
const TEXT_MARKS_ATTRIBUTE = "__liveblocks_tiptap_marks";
|
|
6
|
+
function isJsonObject(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function marksToAttributes(marks) {
|
|
10
|
+
if (marks === void 0 || marks.length === 0) {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
[TEXT_MARKS_ATTRIBUTE]: marks.map((mark) => ({
|
|
15
|
+
type: mark.type,
|
|
16
|
+
...mark.attrs !== void 0 ? { attrs: mark.attrs } : {}
|
|
17
|
+
}))
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function attributesToMarks(attributes) {
|
|
21
|
+
const rawMarks = attributes?.[TEXT_MARKS_ATTRIBUTE];
|
|
22
|
+
if (!Array.isArray(rawMarks)) {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
const marks = [];
|
|
26
|
+
for (const rawMark of rawMarks) {
|
|
27
|
+
if (!isJsonObject(rawMark) || typeof rawMark.type !== "string") {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
marks.push({
|
|
31
|
+
type: rawMark.type,
|
|
32
|
+
...isJsonObject(rawMark.attrs) ? { attrs: rawMark.attrs } : {}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return marks.length > 0 ? marks : void 0;
|
|
36
|
+
}
|
|
37
|
+
function marksToAttributesPatch(marks) {
|
|
38
|
+
const attributes = marksToAttributes(marks);
|
|
39
|
+
return {
|
|
40
|
+
[TEXT_MARKS_ATTRIBUTE]: attributes?.[TEXT_MARKS_ATTRIBUTE] === void 0 ? null : attributes[TEXT_MARKS_ATTRIBUTE]
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function createLiveblocksTiptapNode(node) {
|
|
44
|
+
if (node.type === "text") {
|
|
45
|
+
const text = new client.LiveText();
|
|
46
|
+
text.insert(0, node.text ?? "", marksToAttributes(node.marks));
|
|
47
|
+
return new client.LiveObject({
|
|
48
|
+
id: client.nanoid(),
|
|
49
|
+
type: node.type,
|
|
50
|
+
text
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return new client.LiveObject({
|
|
54
|
+
id: client.nanoid(),
|
|
55
|
+
type: node.type,
|
|
56
|
+
...node.attrs !== void 0 ? { attrs: node.attrs } : {},
|
|
57
|
+
content: new client.LiveList(
|
|
58
|
+
(node.content ?? []).map((child) => createLiveblocksTiptapNode(child))
|
|
59
|
+
)
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function getLiveblocksNodeId(node) {
|
|
63
|
+
return node.get("id");
|
|
64
|
+
}
|
|
65
|
+
function getLiveblocksNodeType(node) {
|
|
66
|
+
return node.get("type");
|
|
67
|
+
}
|
|
68
|
+
function getLiveblocksNodeContent(node) {
|
|
69
|
+
const content = node.get("content");
|
|
70
|
+
return content instanceof client.LiveList ? content : void 0;
|
|
71
|
+
}
|
|
72
|
+
function getLiveblocksNodeText(node) {
|
|
73
|
+
const text = node.get("text");
|
|
74
|
+
return text instanceof client.LiveText ? text : void 0;
|
|
75
|
+
}
|
|
76
|
+
function updateLiveblocksNodeAttrs(node, attrs) {
|
|
77
|
+
if (attrs === void 0) {
|
|
78
|
+
node.delete("attrs");
|
|
79
|
+
} else {
|
|
80
|
+
node.set("attrs", attrs);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function liveTextToTextNodes(text) {
|
|
84
|
+
const nodes = [];
|
|
85
|
+
for (const delta of text.toDelta()) {
|
|
86
|
+
if (delta.text.length === 0) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
nodes.push({
|
|
90
|
+
type: "text",
|
|
91
|
+
text: delta.text,
|
|
92
|
+
...delta.attributes !== void 0 ? { marks: attributesToMarks(delta.attributes) } : {}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return nodes;
|
|
96
|
+
}
|
|
97
|
+
function liveblocksTiptapNodeToJsonNodes(node) {
|
|
98
|
+
const type = node.get("type");
|
|
99
|
+
if (type === "text") {
|
|
100
|
+
const text = node.get("text");
|
|
101
|
+
return text instanceof client.LiveText ? liveTextToTextNodes(text) : [];
|
|
102
|
+
}
|
|
103
|
+
const content = node.get("content");
|
|
104
|
+
const jsonNode = {
|
|
105
|
+
type,
|
|
106
|
+
...node.get("attrs") !== void 0 ? { attrs: node.get("attrs") } : {}
|
|
107
|
+
};
|
|
108
|
+
if (content instanceof client.LiveList && content.length > 0) {
|
|
109
|
+
const children = [];
|
|
110
|
+
for (let index = 0; index < content.length; index++) {
|
|
111
|
+
const child = content.get(index);
|
|
112
|
+
if (child !== void 0) {
|
|
113
|
+
children.push(...liveblocksTiptapNodeToJsonNodes(child));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (children.length > 0) {
|
|
117
|
+
jsonNode.content = children;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return [jsonNode];
|
|
121
|
+
}
|
|
122
|
+
function liveblocksTiptapNodeToJson(node) {
|
|
123
|
+
const [jsonNode] = liveblocksTiptapNodeToJsonNodes(node);
|
|
124
|
+
if (jsonNode === void 0 || jsonNode.type === "doc" && (!Array.isArray(jsonNode.content) || jsonNode.content.length === 0)) {
|
|
125
|
+
return createDefaultDocument();
|
|
126
|
+
}
|
|
127
|
+
return jsonNode;
|
|
128
|
+
}
|
|
129
|
+
function createDefaultDocument() {
|
|
130
|
+
return { type: "doc", content: [{ type: "paragraph" }] };
|
|
131
|
+
}
|
|
132
|
+
function stringifyDocument(node) {
|
|
133
|
+
return JSON.stringify(node);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
exports.TEXT_MARKS_ATTRIBUTE = TEXT_MARKS_ATTRIBUTE;
|
|
137
|
+
exports.attributesToMarks = attributesToMarks;
|
|
138
|
+
exports.createDefaultDocument = createDefaultDocument;
|
|
139
|
+
exports.createLiveblocksTiptapNode = createLiveblocksTiptapNode;
|
|
140
|
+
exports.getLiveblocksNodeContent = getLiveblocksNodeContent;
|
|
141
|
+
exports.getLiveblocksNodeId = getLiveblocksNodeId;
|
|
142
|
+
exports.getLiveblocksNodeText = getLiveblocksNodeText;
|
|
143
|
+
exports.getLiveblocksNodeType = getLiveblocksNodeType;
|
|
144
|
+
exports.liveblocksTiptapNodeToJson = liveblocksTiptapNodeToJson;
|
|
145
|
+
exports.liveblocksTiptapNodeToJsonNodes = liveblocksTiptapNodeToJsonNodes;
|
|
146
|
+
exports.marksToAttributes = marksToAttributes;
|
|
147
|
+
exports.marksToAttributesPatch = marksToAttributesPatch;
|
|
148
|
+
exports.stringifyDocument = stringifyDocument;
|
|
149
|
+
exports.updateLiveblocksNodeAttrs = updateLiveblocksNodeAttrs;
|
|
150
|
+
//# sourceMappingURL=schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.cjs","sources":["../../src/collaboration-liveblocks/schema.ts"],"sourcesContent":["import {\n type Json,\n type JsonObject,\n LiveList,\n LiveObject,\n LiveText,\n type LiveTextAttributes,\n type LiveTextAttributesPatch,\n type LiveTextDelta,\n nanoid,\n} from \"@liveblocks/client\";\n\nexport const TEXT_MARKS_ATTRIBUTE = \"__liveblocks_tiptap_marks\";\n\nexport type ProseMirrorJsonNode = {\n type: string;\n attrs?: JsonObject;\n content?: ProseMirrorJsonNode[];\n text?: string;\n marks?: ProseMirrorJsonMark[];\n};\n\nexport type ProseMirrorJsonMark = {\n type: string;\n attrs?: JsonObject;\n};\n\ntype LiveblocksTiptapNodeData = {\n id: string;\n type: string;\n attrs?: JsonObject;\n content?: LiveList<LiveblocksTiptapNode>;\n text?: LiveText;\n};\n\nexport type LiveblocksTiptapNode = LiveObject<LiveblocksTiptapNodeData>;\n\nfunction isJsonObject(value: Json | undefined): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nexport function marksToAttributes(\n marks: ProseMirrorJsonMark[] | undefined\n): LiveTextAttributes | undefined {\n if (marks === undefined || marks.length === 0) {\n return undefined;\n }\n\n return {\n [TEXT_MARKS_ATTRIBUTE]: marks.map((mark) => ({\n type: mark.type,\n ...(mark.attrs !== undefined ? { attrs: mark.attrs } : {}),\n })),\n };\n}\n\nexport function attributesToMarks(\n attributes: JsonObject | undefined\n): ProseMirrorJsonMark[] | undefined {\n const rawMarks = attributes?.[TEXT_MARKS_ATTRIBUTE];\n\n if (!Array.isArray(rawMarks)) {\n return undefined;\n }\n\n const marks: ProseMirrorJsonMark[] = [];\n for (const rawMark of rawMarks) {\n if (!isJsonObject(rawMark) || typeof rawMark.type !== \"string\") {\n continue;\n }\n\n marks.push({\n type: rawMark.type,\n ...(isJsonObject(rawMark.attrs) ? { attrs: rawMark.attrs } : {}),\n });\n }\n\n return marks.length > 0 ? marks : undefined;\n}\n\nexport function marksToAttributesPatch(\n marks: ProseMirrorJsonMark[] | undefined\n): LiveTextAttributesPatch {\n const attributes = marksToAttributes(marks);\n return {\n [TEXT_MARKS_ATTRIBUTE]:\n attributes?.[TEXT_MARKS_ATTRIBUTE] === undefined\n ? null\n : attributes[TEXT_MARKS_ATTRIBUTE],\n };\n}\n\nexport function createLiveblocksTiptapNode(\n node: ProseMirrorJsonNode\n): LiveblocksTiptapNode {\n if (node.type === \"text\") {\n const text = new LiveText();\n text.insert(0, node.text ?? \"\", marksToAttributes(node.marks));\n\n return new LiveObject({\n id: nanoid(),\n type: node.type,\n text,\n });\n }\n\n return new LiveObject({\n id: nanoid(),\n type: node.type,\n ...(node.attrs !== undefined ? { attrs: node.attrs } : {}),\n content: new LiveList(\n (node.content ?? []).map((child) => createLiveblocksTiptapNode(child))\n ),\n });\n}\n\nexport function getLiveblocksNodeId(node: LiveblocksTiptapNode): string {\n return node.get(\"id\");\n}\n\nexport function getLiveblocksNodeType(node: LiveblocksTiptapNode): string {\n return node.get(\"type\");\n}\n\nexport function getLiveblocksNodeContent(\n node: LiveblocksTiptapNode\n): LiveList<LiveblocksTiptapNode> | undefined {\n const content = node.get(\"content\");\n return content instanceof LiveList ? content : undefined;\n}\n\nexport function getLiveblocksNodeText(\n node: LiveblocksTiptapNode\n): LiveText | undefined {\n const text = node.get(\"text\");\n return text instanceof LiveText ? text : undefined;\n}\n\nexport function updateLiveblocksNodeAttrs(\n node: LiveblocksTiptapNode,\n attrs: JsonObject | undefined\n): void {\n if (attrs === undefined) {\n node.delete(\"attrs\");\n } else {\n node.set(\"attrs\", attrs);\n }\n}\n\nfunction liveTextToTextNodes(text: LiveText): ProseMirrorJsonNode[] {\n const nodes: ProseMirrorJsonNode[] = [];\n\n for (const delta of text.toDelta()) {\n if (delta.text.length === 0) {\n continue;\n }\n\n nodes.push({\n type: \"text\",\n text: delta.text,\n ...(delta.attributes !== undefined\n ? { marks: attributesToMarks(delta.attributes) }\n : {}),\n });\n }\n\n return nodes;\n}\n\nexport function liveblocksTiptapNodeToJsonNodes(\n node: LiveblocksTiptapNode\n): ProseMirrorJsonNode[] {\n const type = node.get(\"type\");\n\n if (type === \"text\") {\n const text = node.get(\"text\");\n return text instanceof LiveText ? liveTextToTextNodes(text) : [];\n }\n\n const content = node.get(\"content\");\n const jsonNode: ProseMirrorJsonNode = {\n type,\n ...(node.get(\"attrs\") !== undefined ? { attrs: node.get(\"attrs\") } : {}),\n };\n\n if (content instanceof LiveList && content.length > 0) {\n const children: ProseMirrorJsonNode[] = [];\n\n for (let index = 0; index < content.length; index++) {\n const child = content.get(index);\n if (child !== undefined) {\n children.push(...liveblocksTiptapNodeToJsonNodes(child));\n }\n }\n\n if (children.length > 0) {\n jsonNode.content = children;\n }\n }\n\n return [jsonNode];\n}\n\nexport function liveblocksTiptapNodeToJson(\n node: LiveblocksTiptapNode\n): ProseMirrorJsonNode {\n const [jsonNode] = liveblocksTiptapNodeToJsonNodes(node);\n\n if (\n jsonNode === undefined ||\n (jsonNode.type === \"doc\" &&\n (!Array.isArray(jsonNode.content) || jsonNode.content.length === 0))\n ) {\n return createDefaultDocument();\n }\n\n return jsonNode;\n}\n\nexport function createDefaultDocument(): ProseMirrorJsonNode {\n return { type: \"doc\", content: [{ type: \"paragraph\" }] };\n}\n\nexport function stringifyDocument(node: ProseMirrorJsonNode): string {\n return JSON.stringify(node);\n}\n\nexport type { LiveTextDelta };\n"],"names":["LiveText","LiveObject","nanoid","LiveList"],"mappings":";;;;AAYO,MAAM,oBAAuB,GAAA,4BAAA;AAyBpC,SAAS,aAAa,KAA8C,EAAA;AAClE,EAAO,OAAA,OAAO,UAAU,QAAY,IAAA,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA,CAAA;AAC5E,CAAA;AAEO,SAAS,kBACd,KACgC,EAAA;AAChC,EAAA,IAAI,KAAU,KAAA,KAAA,CAAA,IAAa,KAAM,CAAA,MAAA,KAAW,CAAG,EAAA;AAC7C,IAAO,OAAA,KAAA,CAAA,CAAA;AAAA,GACT;AAEA,EAAO,OAAA;AAAA,IACL,CAAC,oBAAoB,GAAG,KAAM,CAAA,GAAA,CAAI,CAAC,IAAU,MAAA;AAAA,MAC3C,MAAM,IAAK,CAAA,IAAA;AAAA,MACX,G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,KAAO,EAAA,OAAA,EAAS,CAAC,EAAE,IAAA,EAAM,WAAY,EAAC,CAAE,EAAA,CAAA;AACzD,CAAA;AAEO,SAAS,kBAAkB,IAAmC,EAAA;AACnE,EAAO,OAAA,IAAA,CAAK,UAAU,IAAI,CAAA,CAAA;AAC5B;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { LiveText, LiveObject, nanoid, LiveList } from '@liveblocks/client';
|
|
2
|
+
|
|
3
|
+
const TEXT_MARKS_ATTRIBUTE = "__liveblocks_tiptap_marks";
|
|
4
|
+
function isJsonObject(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
function marksToAttributes(marks) {
|
|
8
|
+
if (marks === void 0 || marks.length === 0) {
|
|
9
|
+
return void 0;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
[TEXT_MARKS_ATTRIBUTE]: marks.map((mark) => ({
|
|
13
|
+
type: mark.type,
|
|
14
|
+
...mark.attrs !== void 0 ? { attrs: mark.attrs } : {}
|
|
15
|
+
}))
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function attributesToMarks(attributes) {
|
|
19
|
+
const rawMarks = attributes?.[TEXT_MARKS_ATTRIBUTE];
|
|
20
|
+
if (!Array.isArray(rawMarks)) {
|
|
21
|
+
return void 0;
|
|
22
|
+
}
|
|
23
|
+
const marks = [];
|
|
24
|
+
for (const rawMark of rawMarks) {
|
|
25
|
+
if (!isJsonObject(rawMark) || typeof rawMark.type !== "string") {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
marks.push({
|
|
29
|
+
type: rawMark.type,
|
|
30
|
+
...isJsonObject(rawMark.attrs) ? { attrs: rawMark.attrs } : {}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return marks.length > 0 ? marks : void 0;
|
|
34
|
+
}
|
|
35
|
+
function marksToAttributesPatch(marks) {
|
|
36
|
+
const attributes = marksToAttributes(marks);
|
|
37
|
+
return {
|
|
38
|
+
[TEXT_MARKS_ATTRIBUTE]: attributes?.[TEXT_MARKS_ATTRIBUTE] === void 0 ? null : attributes[TEXT_MARKS_ATTRIBUTE]
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function createLiveblocksTiptapNode(node) {
|
|
42
|
+
if (node.type === "text") {
|
|
43
|
+
const text = new LiveText();
|
|
44
|
+
text.insert(0, node.text ?? "", marksToAttributes(node.marks));
|
|
45
|
+
return new LiveObject({
|
|
46
|
+
id: nanoid(),
|
|
47
|
+
type: node.type,
|
|
48
|
+
text
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return new LiveObject({
|
|
52
|
+
id: nanoid(),
|
|
53
|
+
type: node.type,
|
|
54
|
+
...node.attrs !== void 0 ? { attrs: node.attrs } : {},
|
|
55
|
+
content: new LiveList(
|
|
56
|
+
(node.content ?? []).map((child) => createLiveblocksTiptapNode(child))
|
|
57
|
+
)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function getLiveblocksNodeId(node) {
|
|
61
|
+
return node.get("id");
|
|
62
|
+
}
|
|
63
|
+
function getLiveblocksNodeType(node) {
|
|
64
|
+
return node.get("type");
|
|
65
|
+
}
|
|
66
|
+
function getLiveblocksNodeContent(node) {
|
|
67
|
+
const content = node.get("content");
|
|
68
|
+
return content instanceof LiveList ? content : void 0;
|
|
69
|
+
}
|
|
70
|
+
function getLiveblocksNodeText(node) {
|
|
71
|
+
const text = node.get("text");
|
|
72
|
+
return text instanceof LiveText ? text : void 0;
|
|
73
|
+
}
|
|
74
|
+
function updateLiveblocksNodeAttrs(node, attrs) {
|
|
75
|
+
if (attrs === void 0) {
|
|
76
|
+
node.delete("attrs");
|
|
77
|
+
} else {
|
|
78
|
+
node.set("attrs", attrs);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function liveTextToTextNodes(text) {
|
|
82
|
+
const nodes = [];
|
|
83
|
+
for (const delta of text.toDelta()) {
|
|
84
|
+
if (delta.text.length === 0) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
nodes.push({
|
|
88
|
+
type: "text",
|
|
89
|
+
text: delta.text,
|
|
90
|
+
...delta.attributes !== void 0 ? { marks: attributesToMarks(delta.attributes) } : {}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return nodes;
|
|
94
|
+
}
|
|
95
|
+
function liveblocksTiptapNodeToJsonNodes(node) {
|
|
96
|
+
const type = node.get("type");
|
|
97
|
+
if (type === "text") {
|
|
98
|
+
const text = node.get("text");
|
|
99
|
+
return text instanceof LiveText ? liveTextToTextNodes(text) : [];
|
|
100
|
+
}
|
|
101
|
+
const content = node.get("content");
|
|
102
|
+
const jsonNode = {
|
|
103
|
+
type,
|
|
104
|
+
...node.get("attrs") !== void 0 ? { attrs: node.get("attrs") } : {}
|
|
105
|
+
};
|
|
106
|
+
if (content instanceof LiveList && content.length > 0) {
|
|
107
|
+
const children = [];
|
|
108
|
+
for (let index = 0; index < content.length; index++) {
|
|
109
|
+
const child = content.get(index);
|
|
110
|
+
if (child !== void 0) {
|
|
111
|
+
children.push(...liveblocksTiptapNodeToJsonNodes(child));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (children.length > 0) {
|
|
115
|
+
jsonNode.content = children;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return [jsonNode];
|
|
119
|
+
}
|
|
120
|
+
function liveblocksTiptapNodeToJson(node) {
|
|
121
|
+
const [jsonNode] = liveblocksTiptapNodeToJsonNodes(node);
|
|
122
|
+
if (jsonNode === void 0 || jsonNode.type === "doc" && (!Array.isArray(jsonNode.content) || jsonNode.content.length === 0)) {
|
|
123
|
+
return createDefaultDocument();
|
|
124
|
+
}
|
|
125
|
+
return jsonNode;
|
|
126
|
+
}
|
|
127
|
+
function createDefaultDocument() {
|
|
128
|
+
return { type: "doc", content: [{ type: "paragraph" }] };
|
|
129
|
+
}
|
|
130
|
+
function stringifyDocument(node) {
|
|
131
|
+
return JSON.stringify(node);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { TEXT_MARKS_ATTRIBUTE, attributesToMarks, createDefaultDocument, createLiveblocksTiptapNode, getLiveblocksNodeContent, getLiveblocksNodeId, getLiveblocksNodeText, getLiveblocksNodeType, liveblocksTiptapNodeToJson, liveblocksTiptapNodeToJsonNodes, marksToAttributes, marksToAttributesPatch, stringifyDocument, updateLiveblocksNodeAttrs };
|
|
135
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sources":["../../src/collaboration-liveblocks/schema.ts"],"sourcesContent":["import {\n type Json,\n type JsonObject,\n LiveList,\n LiveObject,\n LiveText,\n type LiveTextAttributes,\n type LiveTextAttributesPatch,\n type LiveTextDelta,\n nanoid,\n} from \"@liveblocks/client\";\n\nexport const TEXT_MARKS_ATTRIBUTE = \"__liveblocks_tiptap_marks\";\n\nexport type ProseMirrorJsonNode = {\n type: string;\n attrs?: JsonObject;\n content?: ProseMirrorJsonNode[];\n text?: string;\n marks?: ProseMirrorJsonMark[];\n};\n\nexport type ProseMirrorJsonMark = {\n type: string;\n attrs?: JsonObject;\n};\n\ntype LiveblocksTiptapNodeData = {\n id: string;\n type: string;\n attrs?: JsonObject;\n content?: LiveList<LiveblocksTiptapNode>;\n text?: LiveText;\n};\n\nexport type LiveblocksTiptapNode = LiveObject<LiveblocksTiptapNodeData>;\n\nfunction isJsonObject(value: Json | undefined): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nexport function marksToAttributes(\n marks: ProseMirrorJsonMark[] | undefined\n): LiveTextAttributes | undefined {\n if (marks === undefined || marks.length === 0) {\n return undefined;\n }\n\n return {\n [TEXT_MARKS_ATTRIBUTE]: marks.map((mark) => ({\n type: mark.type,\n ...(mark.attrs !== undefined ? { attrs: mark.attrs } : {}),\n })),\n };\n}\n\nexport function attributesToMarks(\n attributes: JsonObject | undefined\n): ProseMirrorJsonMark[] | undefined {\n const rawMarks = attributes?.[TEXT_MARKS_ATTRIBUTE];\n\n if (!Array.isArray(rawMarks)) {\n return undefined;\n }\n\n const marks: ProseMirrorJsonMark[] = [];\n for (const rawMark of rawMarks) {\n if (!isJsonObject(rawMark) || typeof rawMark.type !== \"string\") {\n continue;\n }\n\n marks.push({\n type: rawMark.type,\n ...(isJsonObject(rawMark.attrs) ? { attrs: rawMark.attrs } : {}),\n });\n }\n\n return marks.length > 0 ? marks : undefined;\n}\n\nexport function marksToAttributesPatch(\n marks: ProseMirrorJsonMark[] | undefined\n): LiveTextAttributesPatch {\n const attributes = marksToAttributes(marks);\n return {\n [TEXT_MARKS_ATTRIBUTE]:\n attributes?.[TEXT_MARKS_ATTRIBUTE] === undefined\n ? null\n : attributes[TEXT_MARKS_ATTRIBUTE],\n };\n}\n\nexport function createLiveblocksTiptapNode(\n node: ProseMirrorJsonNode\n): LiveblocksTiptapNode {\n if (node.type === \"text\") {\n const text = new LiveText();\n text.insert(0, node.text ?? \"\", marksToAttributes(node.marks));\n\n return new LiveObject({\n id: nanoid(),\n type: node.type,\n text,\n });\n }\n\n return new LiveObject({\n id: nanoid(),\n type: node.type,\n ...(node.attrs !== undefined ? { attrs: node.attrs } : {}),\n content: new LiveList(\n (node.content ?? []).map((child) => createLiveblocksTiptapNode(child))\n ),\n });\n}\n\nexport function getLiveblocksNodeId(node: LiveblocksTiptapNode): string {\n return node.get(\"id\");\n}\n\nexport function getLiveblocksNodeType(node: LiveblocksTiptapNode): string {\n return node.get(\"type\");\n}\n\nexport function getLiveblocksNodeContent(\n node: LiveblocksTiptapNode\n): LiveList<LiveblocksTiptapNode> | undefined {\n const content = node.get(\"content\");\n return content instanceof LiveList ? content : undefined;\n}\n\nexport function getLiveblocksNodeText(\n node: LiveblocksTiptapNode\n): LiveText | undefined {\n const text = node.get(\"text\");\n return text instanceof LiveText ? text : undefined;\n}\n\nexport function updateLiveblocksNodeAttrs(\n node: LiveblocksTiptapNode,\n attrs: JsonObject | undefined\n): void {\n if (attrs === undefined) {\n node.delete(\"attrs\");\n } else {\n node.set(\"attrs\", attrs);\n }\n}\n\nfunction liveTextToTextNodes(text: LiveText): ProseMirrorJsonNode[] {\n const nodes: ProseMirrorJsonNode[] = [];\n\n for (const delta of text.toDelta()) {\n if (delta.text.length === 0) {\n continue;\n }\n\n nodes.push({\n type: \"text\",\n text: delta.text,\n ...(delta.attributes !== undefined\n ? { marks: attributesToMarks(delta.attributes) }\n : {}),\n });\n }\n\n return nodes;\n}\n\nexport function liveblocksTiptapNodeToJsonNodes(\n node: LiveblocksTiptapNode\n): ProseMirrorJsonNode[] {\n const type = node.get(\"type\");\n\n if (type === \"text\") {\n const text = node.get(\"text\");\n return text instanceof LiveText ? liveTextToTextNodes(text) : [];\n }\n\n const content = node.get(\"content\");\n const jsonNode: ProseMirrorJsonNode = {\n type,\n ...(node.get(\"attrs\") !== undefined ? { attrs: node.get(\"attrs\") } : {}),\n };\n\n if (content instanceof LiveList && content.length > 0) {\n const children: ProseMirrorJsonNode[] = [];\n\n for (let index = 0; index < content.length; index++) {\n const child = content.get(index);\n if (child !== undefined) {\n children.push(...liveblocksTiptapNodeToJsonNodes(child));\n }\n }\n\n if (children.length > 0) {\n jsonNode.content = children;\n }\n }\n\n return [jsonNode];\n}\n\nexport function liveblocksTiptapNodeToJson(\n node: LiveblocksTiptapNode\n): ProseMirrorJsonNode {\n const [jsonNode] = liveblocksTiptapNodeToJsonNodes(node);\n\n if (\n jsonNode === undefined ||\n (jsonNode.type === \"doc\" &&\n (!Array.isArray(jsonNode.content) || jsonNode.content.length === 0))\n ) {\n return createDefaultDocument();\n }\n\n return jsonNode;\n}\n\nexport function createDefaultDocument(): ProseMirrorJsonNode {\n return { type: \"doc\", content: [{ type: \"paragraph\" }] };\n}\n\nexport function stringifyDocument(node: ProseMirrorJsonNode): string {\n return JSON.stringify(node);\n}\n\nexport type { LiveTextDelta };\n"],"names":[],"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