@pm-cm/yjs 0.0.1

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/index.js ADDED
@@ -0,0 +1,473 @@
1
+ // src/bridge.ts
2
+ import { prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, ySyncPluginKey } from "y-prosemirror";
3
+
4
+ // src/types.ts
5
+ var ORIGIN_TEXT_TO_PM = "bridge:text-to-prosemirror";
6
+ var ORIGIN_PM_TO_TEXT = "bridge:prosemirror-to-text";
7
+ var ORIGIN_INIT = "bridge:init";
8
+
9
+ // src/bridge.ts
10
+ var defaultNormalize = (s) => s.replace(/\r\n?/g, "\n");
11
+ var defaultOnError = (event) => console.error(`[bridge] ${event.code}: ${event.message}`, event.cause);
12
+ function replaceSharedText(sharedText, next, origin, normalize = defaultNormalize) {
13
+ if (!sharedText.doc) {
14
+ return { ok: false, reason: "detached" };
15
+ }
16
+ const normalized = normalize(next);
17
+ const current = sharedText.toString();
18
+ if (current === normalized) {
19
+ return { ok: false, reason: "unchanged" };
20
+ }
21
+ let start = 0;
22
+ const minLen = Math.min(current.length, normalized.length);
23
+ while (start < minLen && current.charCodeAt(start) === normalized.charCodeAt(start)) {
24
+ start++;
25
+ }
26
+ let endCurrent = current.length;
27
+ let endNext = normalized.length;
28
+ while (endCurrent > start && endNext > start && current.charCodeAt(endCurrent - 1) === normalized.charCodeAt(endNext - 1)) {
29
+ endCurrent--;
30
+ endNext--;
31
+ }
32
+ sharedText.doc.transact(() => {
33
+ const deleteCount = endCurrent - start;
34
+ if (deleteCount > 0) {
35
+ sharedText.delete(start, deleteCount);
36
+ }
37
+ const insertStr = normalized.slice(start, endNext);
38
+ if (insertStr.length > 0) {
39
+ sharedText.insert(start, insertStr);
40
+ }
41
+ }, origin);
42
+ return { ok: true };
43
+ }
44
+ function replaceSharedProseMirror(doc, fragment, text, origin, config) {
45
+ if (!fragment.doc) {
46
+ return { ok: false, reason: "detached" };
47
+ }
48
+ if (fragment.doc !== doc) {
49
+ throw new Error("fragment belongs to a different Y.Doc than the provided doc");
50
+ }
51
+ const normalize = config.normalize ?? defaultNormalize;
52
+ const onError = config.onError ?? defaultOnError;
53
+ let nextDoc;
54
+ try {
55
+ nextDoc = config.parse(normalize(text), config.schema);
56
+ } catch (error) {
57
+ onError({ code: "parse-error", message: "failed to parse text into ProseMirror document", cause: error });
58
+ return { ok: false, reason: "parse-error" };
59
+ }
60
+ doc.transact(() => {
61
+ prosemirrorToYXmlFragment(nextDoc, fragment);
62
+ }, origin);
63
+ return { ok: true };
64
+ }
65
+ function createYjsBridge(config, options) {
66
+ const {
67
+ doc,
68
+ sharedText,
69
+ sharedProseMirror,
70
+ schema,
71
+ serialize,
72
+ parse
73
+ } = config;
74
+ const normalize = config.normalize ?? defaultNormalize;
75
+ const onError = config.onError ?? defaultOnError;
76
+ if (!sharedText.doc) {
77
+ throw new Error("sharedText is not attached to any Y.Doc");
78
+ }
79
+ if (sharedText.doc !== doc) {
80
+ throw new Error("sharedText belongs to a different Y.Doc than the provided doc");
81
+ }
82
+ if (!sharedProseMirror.doc) {
83
+ throw new Error("sharedProseMirror is not attached to any Y.Doc");
84
+ }
85
+ if (sharedProseMirror.doc !== doc) {
86
+ throw new Error("sharedProseMirror belongs to a different Y.Doc than the provided doc");
87
+ }
88
+ let lastBridgedText = null;
89
+ const syncTextToProsemirror = (origin) => {
90
+ const text = normalize(sharedText.toString());
91
+ if (lastBridgedText === text) {
92
+ return true;
93
+ }
94
+ const result = replaceSharedProseMirror(doc, sharedProseMirror, text, origin, {
95
+ schema,
96
+ parse,
97
+ normalize,
98
+ onError
99
+ });
100
+ if (result.ok) {
101
+ lastBridgedText = text;
102
+ }
103
+ return result.ok;
104
+ };
105
+ const sharedProseMirrorToText = (fragment) => {
106
+ try {
107
+ const pmDoc = yXmlFragmentToProseMirrorRootNode(fragment, schema);
108
+ return normalize(serialize(pmDoc));
109
+ } catch (error) {
110
+ onError({ code: "serialize-error", message: "failed to convert ProseMirror fragment to text", cause: error });
111
+ return null;
112
+ }
113
+ };
114
+ const bootstrap = () => {
115
+ const text = normalize(sharedText.toString());
116
+ const hasText = text.length > 0;
117
+ const hasProsemirror = sharedProseMirror.length > 0;
118
+ if (!hasText && !hasProsemirror) {
119
+ const initial = options?.initialText ?? "";
120
+ if (initial.length > 0) {
121
+ const initResult = replaceSharedProseMirror(doc, sharedProseMirror, initial, ORIGIN_INIT, {
122
+ schema,
123
+ parse,
124
+ normalize,
125
+ onError
126
+ });
127
+ if (!initResult.ok) {
128
+ return { source: "initial", parseError: true };
129
+ }
130
+ const pmDoc = yXmlFragmentToProseMirrorRootNode(sharedProseMirror, schema);
131
+ const canonicalText = serialize(pmDoc);
132
+ replaceSharedText(sharedText, canonicalText, ORIGIN_INIT, normalize);
133
+ lastBridgedText = normalize(canonicalText);
134
+ return { source: "initial" };
135
+ }
136
+ return { source: "empty" };
137
+ }
138
+ if (hasText && !hasProsemirror) {
139
+ const ok = syncTextToProsemirror(ORIGIN_INIT);
140
+ return { source: "text", ...!ok && { parseError: true } };
141
+ }
142
+ if (!hasText && hasProsemirror) {
143
+ const textFromProsemirror = sharedProseMirrorToText(sharedProseMirror);
144
+ if (textFromProsemirror !== null) {
145
+ replaceSharedText(sharedText, textFromProsemirror, ORIGIN_INIT, normalize);
146
+ lastBridgedText = normalize(textFromProsemirror);
147
+ return { source: "prosemirror" };
148
+ }
149
+ return { source: "prosemirror", parseError: true };
150
+ }
151
+ const prosemirrorText = sharedProseMirrorToText(sharedProseMirror);
152
+ if (prosemirrorText === null) {
153
+ const fallbackText = hasText ? text : options?.initialText ?? "";
154
+ let parseError = false;
155
+ if (fallbackText.length > 0) {
156
+ replaceSharedText(sharedText, fallbackText, ORIGIN_INIT, normalize);
157
+ const fallbackResult = replaceSharedProseMirror(doc, sharedProseMirror, fallbackText, ORIGIN_INIT, {
158
+ schema,
159
+ parse,
160
+ normalize,
161
+ onError
162
+ });
163
+ if (!fallbackResult.ok) parseError = true;
164
+ }
165
+ return { source: "text", ...parseError && { parseError: true } };
166
+ }
167
+ if (prosemirrorText !== text) {
168
+ const prefer = options?.prefer ?? "text";
169
+ if (prefer === "prosemirror") {
170
+ replaceSharedText(sharedText, prosemirrorText, ORIGIN_INIT, normalize);
171
+ lastBridgedText = normalize(prosemirrorText);
172
+ return { source: "prosemirror" };
173
+ } else {
174
+ const ok = syncTextToProsemirror(ORIGIN_INIT);
175
+ return { source: "text", ...!ok && { parseError: true } };
176
+ }
177
+ } else {
178
+ lastBridgedText = text;
179
+ return { source: "both-match" };
180
+ }
181
+ };
182
+ const textObserver = (_, transaction) => {
183
+ if (transaction.origin === ORIGIN_PM_TO_TEXT || transaction.origin === ORIGIN_INIT) {
184
+ return;
185
+ }
186
+ syncTextToProsemirror(ORIGIN_TEXT_TO_PM);
187
+ };
188
+ const bootstrapResult = bootstrap();
189
+ sharedText.observe(textObserver);
190
+ return {
191
+ bootstrapResult,
192
+ syncToSharedText(doc2) {
193
+ const text = serialize(doc2);
194
+ const result = replaceSharedText(sharedText, text, ORIGIN_PM_TO_TEXT, normalize);
195
+ if (result.ok || result.reason === "unchanged") {
196
+ lastBridgedText = normalize(text);
197
+ }
198
+ return result;
199
+ },
200
+ isYjsSyncChange(tr) {
201
+ const meta = tr.getMeta(ySyncPluginKey);
202
+ return typeof meta === "object" && meta !== null && "isChangeOrigin" in meta && meta.isChangeOrigin === true;
203
+ },
204
+ dispose() {
205
+ sharedText.unobserve(textObserver);
206
+ }
207
+ };
208
+ }
209
+
210
+ // src/awareness-proxy.ts
211
+ function createAwarenessProxy(awareness, cursorField = "pmCursor") {
212
+ return new Proxy(awareness, {
213
+ get(target, prop, receiver) {
214
+ if (prop === "getLocalState") {
215
+ return () => {
216
+ const state = target.getLocalState();
217
+ return state ? { ...state, [cursorField]: null } : state;
218
+ };
219
+ }
220
+ if (prop === "setLocalStateField") {
221
+ return (field, value2) => {
222
+ if (field === cursorField) return;
223
+ target.setLocalStateField(field, value2);
224
+ };
225
+ }
226
+ const value = Reflect.get(target, prop, receiver);
227
+ return typeof value === "function" ? value.bind(target) : value;
228
+ }
229
+ });
230
+ }
231
+
232
+ // src/collab-plugins.ts
233
+ import { initProseMirrorDoc, yCursorPlugin, ySyncPlugin, yUndoPlugin } from "y-prosemirror";
234
+
235
+ // src/bridge-sync-plugin.ts
236
+ import { Plugin, PluginKey } from "prosemirror-state";
237
+ var bridgeSyncPluginKey = new PluginKey("pm-cm-bridge-sync");
238
+ var wiredBridges = /* @__PURE__ */ new WeakSet();
239
+ var defaultOnWarning = (event) => console.warn(`[pm-cm] ${event.code}: ${event.message}`);
240
+ function createBridgeSyncPlugin(bridge, options = {}) {
241
+ const warn = options.onWarning ?? defaultOnWarning;
242
+ if (wiredBridges.has(bridge)) {
243
+ warn({ code: "bridge-already-wired", message: "this bridge is already wired to another plugin instance" });
244
+ }
245
+ wiredBridges.add(bridge);
246
+ return new Plugin({
247
+ key: bridgeSyncPluginKey,
248
+ state: {
249
+ init() {
250
+ return { needsSync: false };
251
+ },
252
+ apply(tr, _prev) {
253
+ if (!tr.docChanged) return { needsSync: false };
254
+ if (bridge.isYjsSyncChange(tr)) return { needsSync: false };
255
+ return { needsSync: true };
256
+ }
257
+ },
258
+ view() {
259
+ return {
260
+ update(view) {
261
+ const state = bridgeSyncPluginKey.getState(view.state);
262
+ if (state?.needsSync) {
263
+ const result = bridge.syncToSharedText(view.state.doc);
264
+ if (!result.ok) {
265
+ if (result.reason === "detached") {
266
+ options.onSyncFailure?.(result, view);
267
+ warn({ code: "sync-failed", message: `bridge sync failed: ${result.reason}` });
268
+ }
269
+ }
270
+ }
271
+ },
272
+ destroy() {
273
+ wiredBridges.delete(bridge);
274
+ }
275
+ };
276
+ }
277
+ });
278
+ }
279
+
280
+ // src/cursor-sync-plugin.ts
281
+ import { Plugin as Plugin2, PluginKey as PluginKey2 } from "prosemirror-state";
282
+ import { absolutePositionToRelativePosition, ySyncPluginKey as ySyncPluginKey2 } from "y-prosemirror";
283
+ import { createRelativePositionFromTypeIndex } from "yjs";
284
+ import { buildCursorMap, cursorMapLookup, reverseCursorMapLookup } from "@pm-cm/core";
285
+ var cursorSyncPluginKey = new PluginKey2("pm-cm-cursor-sync");
286
+ function getYSyncState(view) {
287
+ const raw = ySyncPluginKey2.getState(view.state);
288
+ if (!raw) return null;
289
+ if (typeof raw === "object" && "type" in raw && raw.type && "binding" in raw && raw.binding && typeof raw.binding === "object" && "mapping" in raw.binding && raw.binding.mapping instanceof Map) {
290
+ return raw;
291
+ }
292
+ return null;
293
+ }
294
+ function toRelativePosition(view, pmPos) {
295
+ const ySyncState = getYSyncState(view);
296
+ if (!ySyncState) return null;
297
+ return absolutePositionToRelativePosition(
298
+ pmPos,
299
+ ySyncState.type,
300
+ ySyncState.binding.mapping
301
+ // eslint-disable-line @typescript-eslint/no-explicit-any -- y-prosemirror internal mapping type
302
+ );
303
+ }
304
+ function broadcastPmCursor(awareness, cursorFieldName, view, pmAnchor, pmHead) {
305
+ const relAnchor = toRelativePosition(view, pmAnchor);
306
+ const relHead = toRelativePosition(view, pmHead);
307
+ if (relAnchor === null || relHead === null) return false;
308
+ awareness.setLocalStateField(cursorFieldName, { anchor: relAnchor, head: relHead });
309
+ return true;
310
+ }
311
+ function broadcastTextCursor(awareness, cmCursorFieldName, sharedText, textAnchor, textHead) {
312
+ const len = sharedText.length;
313
+ const clamp = (v) => Math.max(0, Math.min(v, len));
314
+ const relAnchor = createRelativePositionFromTypeIndex(sharedText, clamp(textAnchor));
315
+ const relHead = createRelativePositionFromTypeIndex(sharedText, clamp(textHead));
316
+ awareness.setLocalStateField(cmCursorFieldName, { anchor: relAnchor, head: relHead });
317
+ }
318
+ var defaultOnWarning2 = (event) => console.warn(`[pm-cm] ${event.code}: ${event.message}`);
319
+ function createCursorSyncPlugin(options) {
320
+ const { awareness, serialize, locate, sharedText } = options;
321
+ const warn = options.onWarning ?? defaultOnWarning2;
322
+ const cursorFieldName = options.cursorFieldName ?? "pmCursor";
323
+ const cmCursorFieldName = options.cmCursorFieldName ?? "cursor";
324
+ let warnedSyncPluginMissing = false;
325
+ let cachedMap = null;
326
+ let cachedMapDoc = null;
327
+ function getOrBuildMap(doc) {
328
+ if (cachedMapDoc !== doc || !cachedMap) {
329
+ cachedMap = buildCursorMap(doc, serialize, locate);
330
+ cachedMapDoc = doc;
331
+ }
332
+ return cachedMap;
333
+ }
334
+ return new Plugin2({
335
+ key: cursorSyncPluginKey,
336
+ state: {
337
+ init() {
338
+ return { pendingCm: null, mappedTextOffset: null };
339
+ },
340
+ apply(tr, prev, _oldState, newState) {
341
+ const cmMeta = tr.getMeta(cursorSyncPluginKey);
342
+ if (cmMeta) {
343
+ return { pendingCm: cmMeta, mappedTextOffset: prev.mappedTextOffset };
344
+ }
345
+ let mappedTextOffset = prev.mappedTextOffset;
346
+ if (tr.selectionSet || tr.docChanged) {
347
+ const map = getOrBuildMap(newState.doc);
348
+ mappedTextOffset = cursorMapLookup(map, newState.selection.anchor);
349
+ }
350
+ return {
351
+ pendingCm: prev.pendingCm !== null ? null : prev.pendingCm,
352
+ mappedTextOffset
353
+ };
354
+ }
355
+ },
356
+ view() {
357
+ return {
358
+ update(view, prevState) {
359
+ const pluginState = cursorSyncPluginKey.getState(view.state);
360
+ const prevPluginState = cursorSyncPluginKey.getState(prevState);
361
+ if (pluginState?.pendingCm != null && pluginState.pendingCm !== prevPluginState?.pendingCm) {
362
+ const map = getOrBuildMap(view.state.doc);
363
+ const pmAnchor = reverseCursorMapLookup(map, pluginState.pendingCm.anchor);
364
+ const pmHead = reverseCursorMapLookup(map, pluginState.pendingCm.head);
365
+ if (pmAnchor !== null && pmHead !== null) {
366
+ const ok = broadcastPmCursor(awareness, cursorFieldName, view, pmAnchor, pmHead);
367
+ if (!ok && !warnedSyncPluginMissing) {
368
+ warnedSyncPluginMissing = true;
369
+ warn({ code: "ysync-plugin-missing", message: "ySyncPlugin state not available \u2014 cursor broadcast skipped" });
370
+ }
371
+ }
372
+ if (sharedText) {
373
+ broadcastTextCursor(
374
+ awareness,
375
+ cmCursorFieldName,
376
+ sharedText,
377
+ pluginState.pendingCm.anchor,
378
+ pluginState.pendingCm.head
379
+ );
380
+ }
381
+ return;
382
+ }
383
+ if (view.hasFocus() && (view.state.selection !== prevState.selection || view.state.doc !== prevState.doc)) {
384
+ const { anchor, head } = view.state.selection;
385
+ const ok = broadcastPmCursor(awareness, cursorFieldName, view, anchor, head);
386
+ if (!ok && !warnedSyncPluginMissing) {
387
+ warnedSyncPluginMissing = true;
388
+ warn({ code: "ysync-plugin-missing", message: "ySyncPlugin state not available \u2014 cursor broadcast skipped" });
389
+ }
390
+ if (sharedText) {
391
+ const map = getOrBuildMap(view.state.doc);
392
+ const textAnchor = cursorMapLookup(map, anchor);
393
+ const textHead = cursorMapLookup(map, head);
394
+ if (textAnchor !== null && textHead !== null) {
395
+ broadcastTextCursor(awareness, cmCursorFieldName, sharedText, textAnchor, textHead);
396
+ }
397
+ }
398
+ }
399
+ }
400
+ };
401
+ }
402
+ });
403
+ }
404
+ function syncCmCursor(view, anchor, head, onWarning) {
405
+ if (!cursorSyncPluginKey.getState(view.state)) {
406
+ (onWarning ?? defaultOnWarning2)({ code: "cursor-sync-not-installed", message: "cursor sync plugin is not installed on this EditorView" });
407
+ return;
408
+ }
409
+ const sanitize = (v) => Math.max(0, Math.floor(v));
410
+ view.dispatch(
411
+ view.state.tr.setMeta(cursorSyncPluginKey, {
412
+ anchor: sanitize(anchor),
413
+ head: sanitize(head ?? anchor)
414
+ })
415
+ );
416
+ }
417
+
418
+ // src/collab-plugins.ts
419
+ function createCollabPlugins(schema, options) {
420
+ const cursorFieldName = options.cursorFieldName ?? "pmCursor";
421
+ const enableCursorSync = options.cursorSync ?? false;
422
+ const { sharedProseMirror } = options;
423
+ if (enableCursorSync && !options.serialize) {
424
+ throw new Error("createCollabPlugins: cursorSync requires serialize to be provided");
425
+ }
426
+ const { doc, mapping: rawMapping } = initProseMirrorDoc(sharedProseMirror, schema);
427
+ const mapping = rawMapping;
428
+ const pmAwareness = enableCursorSync ? createAwarenessProxy(options.awareness, cursorFieldName) : options.awareness;
429
+ const plugins = [
430
+ ySyncPlugin(sharedProseMirror, { mapping: rawMapping }),
431
+ yCursorPlugin(pmAwareness, options.yCursorPluginOpts ?? {}, cursorFieldName),
432
+ yUndoPlugin(options.yUndoPluginOpts)
433
+ ];
434
+ if (options.bridge) {
435
+ plugins.push(createBridgeSyncPlugin(options.bridge, { onWarning: options.onWarning }));
436
+ }
437
+ if (enableCursorSync && options.serialize) {
438
+ plugins.push(
439
+ createCursorSyncPlugin({
440
+ awareness: options.awareness,
441
+ serialize: options.serialize,
442
+ cursorFieldName,
443
+ cmCursorFieldName: options.cmCursorFieldName,
444
+ locate: options.locate,
445
+ sharedText: options.sharedText,
446
+ onWarning: options.onWarning
447
+ })
448
+ );
449
+ }
450
+ return { plugins, doc, mapping };
451
+ }
452
+
453
+ // src/index.ts
454
+ import { buildCursorMap as buildCursorMap2, cursorMapLookup as cursorMapLookup2, reverseCursorMapLookup as reverseCursorMapLookup2 } from "@pm-cm/core";
455
+ export {
456
+ ORIGIN_INIT,
457
+ ORIGIN_PM_TO_TEXT,
458
+ ORIGIN_TEXT_TO_PM,
459
+ bridgeSyncPluginKey,
460
+ buildCursorMap2 as buildCursorMap,
461
+ createAwarenessProxy,
462
+ createBridgeSyncPlugin,
463
+ createCollabPlugins,
464
+ createCursorSyncPlugin,
465
+ createYjsBridge,
466
+ cursorMapLookup2 as cursorMapLookup,
467
+ cursorSyncPluginKey,
468
+ replaceSharedProseMirror,
469
+ replaceSharedText,
470
+ reverseCursorMapLookup2 as reverseCursorMapLookup,
471
+ syncCmCursor
472
+ };
473
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/bridge.ts","../src/types.ts","../src/awareness-proxy.ts","../src/collab-plugins.ts","../src/bridge-sync-plugin.ts","../src/cursor-sync-plugin.ts","../src/index.ts"],"sourcesContent":["import type { Node } from 'prosemirror-model'\nimport type { Transaction } from 'prosemirror-state'\nimport { prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, ySyncPluginKey } from 'y-prosemirror'\nimport type { Doc, Text as YText, XmlFragment as YXmlFragment } from 'yjs'\nimport type { Normalize, OnError } from '@pm-cm/core'\nimport type { BootstrapResult, YjsBridgeConfig, YjsBridgeHandle } from './types.js'\nimport { ORIGIN_INIT, ORIGIN_TEXT_TO_PM, ORIGIN_PM_TO_TEXT } from './types.js'\n\nconst defaultNormalize: Normalize = (s) => s.replace(/\\r\\n?/g, '\\n')\nconst defaultOnError: OnError = (event) => console.error(`[bridge] ${event.code}: ${event.message}`, event.cause)\n\n/** Result of {@link replaceSharedText}. */\nexport type ReplaceTextResult =\n | { ok: true }\n | { ok: false; reason: 'unchanged' }\n | { ok: false; reason: 'detached' }\n\n/** Result of {@link replaceSharedProseMirror}. */\nexport type ReplaceProseMirrorResult =\n | { ok: true }\n | { ok: false; reason: 'parse-error' }\n | { ok: false; reason: 'detached' }\n\n/**\n * Union of all replace-result types. Kept for backward compatibility.\n * Prefer the narrower {@link ReplaceTextResult} / {@link ReplaceProseMirrorResult}.\n */\nexport type ReplaceResult = ReplaceTextResult | ReplaceProseMirrorResult\n\n/**\n * Replace `Y.Text` content using a minimal diff (common prefix/suffix trimming).\n * Returns a {@link ReplaceResult} indicating success or failure reason.\n */\nexport function replaceSharedText(\n sharedText: YText,\n next: string,\n origin: unknown,\n normalize: Normalize = defaultNormalize,\n): ReplaceTextResult {\n if (!sharedText.doc) {\n return { ok: false, reason: 'detached' }\n }\n\n const normalized = normalize(next)\n const current = sharedText.toString()\n if (current === normalized) {\n return { ok: false, reason: 'unchanged' }\n }\n\n // Minimal diff: find common prefix and suffix, replace only the changed middle.\n let start = 0\n const minLen = Math.min(current.length, normalized.length)\n while (start < minLen && current.charCodeAt(start) === normalized.charCodeAt(start)) {\n start++\n }\n\n let endCurrent = current.length\n let endNext = normalized.length\n while (endCurrent > start && endNext > start && current.charCodeAt(endCurrent - 1) === normalized.charCodeAt(endNext - 1)) {\n endCurrent--\n endNext--\n }\n\n sharedText.doc.transact(() => {\n const deleteCount = endCurrent - start\n if (deleteCount > 0) {\n sharedText.delete(start, deleteCount)\n }\n const insertStr = normalized.slice(start, endNext)\n if (insertStr.length > 0) {\n sharedText.insert(start, insertStr)\n }\n }, origin)\n\n return { ok: true }\n}\n\n/**\n * Replace `Y.XmlFragment` by parsing serialized text into a ProseMirror document.\n * Returns a {@link ReplaceResult} indicating success or failure reason.\n */\nexport function replaceSharedProseMirror(\n doc: Doc,\n fragment: YXmlFragment,\n text: string,\n origin: unknown,\n config: Pick<YjsBridgeConfig, 'schema' | 'parse' | 'normalize' | 'onError'>,\n): ReplaceProseMirrorResult {\n if (!fragment.doc) {\n return { ok: false, reason: 'detached' }\n }\n if (fragment.doc !== doc) {\n throw new Error('fragment belongs to a different Y.Doc than the provided doc')\n }\n\n const normalize = config.normalize ?? defaultNormalize\n const onError = config.onError ?? defaultOnError\n let nextDoc: Node\n try {\n nextDoc = config.parse(normalize(text), config.schema)\n } catch (error) {\n onError({ code: 'parse-error', message: 'failed to parse text into ProseMirror document', cause: error })\n return { ok: false, reason: 'parse-error' }\n }\n\n doc.transact(() => {\n prosemirrorToYXmlFragment(nextDoc, fragment)\n }, origin)\n return { ok: true }\n}\n\n/** Options for {@link createYjsBridge}. */\nexport type YjsBridgeOptions = {\n initialText?: string\n /** Which side wins when both sharedText and sharedProseMirror exist and differ. Default `'text'`. */\n prefer?: 'text' | 'prosemirror'\n}\n\n/**\n * Create a collaborative bridge that keeps `Y.Text` and `Y.XmlFragment` in sync.\n *\n * Runs a synchronous bootstrap to reconcile existing state, then installs a\n * `Y.Text` observer for the text → ProseMirror direction.\n *\n * @throws If `sharedText` or `sharedProseMirror` belong to a different `Y.Doc`.\n */\nexport function createYjsBridge(\n config: YjsBridgeConfig,\n options?: YjsBridgeOptions,\n): YjsBridgeHandle {\n const {\n doc,\n sharedText,\n sharedProseMirror,\n schema,\n serialize,\n parse,\n } = config\n const normalize = config.normalize ?? defaultNormalize\n const onError = config.onError ?? defaultOnError\n\n if (!sharedText.doc) {\n throw new Error('sharedText is not attached to any Y.Doc')\n }\n if (sharedText.doc !== doc) {\n throw new Error('sharedText belongs to a different Y.Doc than the provided doc')\n }\n if (!sharedProseMirror.doc) {\n throw new Error('sharedProseMirror is not attached to any Y.Doc')\n }\n if (sharedProseMirror.doc !== doc) {\n throw new Error('sharedProseMirror belongs to a different Y.Doc than the provided doc')\n }\n\n let lastBridgedText: string | null = null\n\n /** Returns `true` if the parse succeeded. */\n const syncTextToProsemirror = (origin: unknown): boolean => {\n const text = normalize(sharedText.toString())\n if (lastBridgedText === text) {\n return true\n }\n\n const result = replaceSharedProseMirror(doc, sharedProseMirror, text, origin, {\n schema,\n parse,\n normalize,\n onError,\n })\n if (result.ok) {\n lastBridgedText = text\n }\n return result.ok\n }\n\n const sharedProseMirrorToText = (fragment: YXmlFragment): string | null => {\n try {\n const pmDoc = yXmlFragmentToProseMirrorRootNode(fragment, schema)\n return normalize(serialize(pmDoc))\n } catch (error) {\n onError({ code: 'serialize-error', message: 'failed to convert ProseMirror fragment to text', cause: error })\n return null\n }\n }\n\n // Bootstrap\n const bootstrap = (): BootstrapResult => {\n const text = normalize(sharedText.toString())\n const hasText = text.length > 0\n const hasProsemirror = sharedProseMirror.length > 0\n\n if (!hasText && !hasProsemirror) {\n const initial = options?.initialText ?? ''\n if (initial.length > 0) {\n // Set Y.XmlFragment first, then derive Y.Text from serialize(parse(initial))\n // to ensure both shared types are in the same canonical form.\n const initResult = replaceSharedProseMirror(doc, sharedProseMirror, initial, ORIGIN_INIT, {\n schema,\n parse,\n normalize,\n onError,\n })\n if (!initResult.ok) {\n return { source: 'initial', parseError: true }\n }\n const pmDoc = yXmlFragmentToProseMirrorRootNode(sharedProseMirror, schema)\n const canonicalText = serialize(pmDoc)\n replaceSharedText(sharedText, canonicalText, ORIGIN_INIT, normalize)\n lastBridgedText = normalize(canonicalText)\n return { source: 'initial' }\n }\n return { source: 'empty' }\n }\n\n if (hasText && !hasProsemirror) {\n const ok = syncTextToProsemirror(ORIGIN_INIT)\n return { source: 'text', ...(!ok && { parseError: true }) }\n }\n\n if (!hasText && hasProsemirror) {\n const textFromProsemirror = sharedProseMirrorToText(sharedProseMirror)\n if (textFromProsemirror !== null) {\n replaceSharedText(sharedText, textFromProsemirror, ORIGIN_INIT, normalize)\n lastBridgedText = normalize(textFromProsemirror)\n return { source: 'prosemirror' }\n }\n return { source: 'prosemirror', parseError: true }\n }\n\n const prosemirrorText = sharedProseMirrorToText(sharedProseMirror)\n if (prosemirrorText === null) {\n const fallbackText = hasText ? text : (options?.initialText ?? '')\n let parseError = false\n if (fallbackText.length > 0) {\n replaceSharedText(sharedText, fallbackText, ORIGIN_INIT, normalize)\n const fallbackResult = replaceSharedProseMirror(doc, sharedProseMirror, fallbackText, ORIGIN_INIT, {\n schema,\n parse,\n normalize,\n onError,\n })\n if (!fallbackResult.ok) parseError = true\n }\n return { source: 'text', ...(parseError && { parseError: true }) }\n }\n\n if (prosemirrorText !== text) {\n const prefer = options?.prefer ?? 'text'\n if (prefer === 'prosemirror') {\n replaceSharedText(sharedText, prosemirrorText, ORIGIN_INIT, normalize)\n lastBridgedText = normalize(prosemirrorText)\n return { source: 'prosemirror' }\n } else {\n const ok = syncTextToProsemirror(ORIGIN_INIT)\n return { source: 'text', ...(!ok && { parseError: true }) }\n }\n } else {\n lastBridgedText = text\n return { source: 'both-match' }\n }\n }\n\n const textObserver = (\n _: unknown,\n transaction: { origin: unknown },\n ) => {\n if (transaction.origin === ORIGIN_PM_TO_TEXT || transaction.origin === ORIGIN_INIT) {\n return\n }\n\n syncTextToProsemirror(ORIGIN_TEXT_TO_PM)\n }\n\n // Run bootstrap synchronously before installing the observer so that\n // an exception during bootstrap cannot leave a dangling observer.\n const bootstrapResult = bootstrap()\n\n sharedText.observe(textObserver)\n\n return {\n bootstrapResult,\n syncToSharedText(doc: Node): ReplaceTextResult {\n const text = serialize(doc)\n const result = replaceSharedText(sharedText, text, ORIGIN_PM_TO_TEXT, normalize)\n // Always update lastBridgedText unless truly failed (detached).\n // 'unchanged' means Y.Text already has this content — still need to\n // record it so the reverse observer doesn't trigger a redundant sync.\n if (result.ok || result.reason === 'unchanged') {\n lastBridgedText = normalize(text)\n }\n return result\n },\n isYjsSyncChange(tr: Transaction): boolean {\n // Internal meta shape from y-prosemirror's ySyncPlugin (tested against ^1.3.x).\n const meta = tr.getMeta(ySyncPluginKey)\n return (\n typeof meta === 'object' &&\n meta !== null &&\n 'isChangeOrigin' in meta &&\n (meta as Record<string, unknown>).isChangeOrigin === true\n )\n },\n dispose() {\n sharedText.unobserve(textObserver)\n },\n }\n}\n","import type { Node, Schema } from 'prosemirror-model'\nimport type { Transaction } from 'prosemirror-state'\nimport type { Serialize, Parse, Normalize, OnError } from '@pm-cm/core'\nimport type { Doc, Text as YText, XmlFragment as YXmlFragment } from 'yjs'\nimport type { ReplaceTextResult } from './bridge.js'\n\n/** Known warning codes emitted by the yjs bridge and plugins. */\nexport type WarningCode = 'bridge-already-wired' | 'sync-failed' | 'ysync-plugin-missing' | 'cursor-sync-not-installed'\n\n/** Structured warning event for non-fatal warnings. */\nexport type WarningEvent = {\n code: WarningCode\n message: string\n}\n\n/**\n * Warning handler callback for non-fatal warnings.\n *\n * Known codes:\n * - `'bridge-already-wired'` — the same bridge handle is wired to multiple plugin instances.\n * - `'sync-failed'` — `syncToSharedText` failed (e.g. Y.Text detached).\n * - `'ysync-plugin-missing'` — ySyncPlugin state is not available; cursor broadcast skipped.\n * - `'cursor-sync-not-installed'` — cursor sync plugin is not installed on the EditorView.\n */\nexport type OnWarning = (event: WarningEvent) => void\n\n/** Yjs transaction origin: text → ProseMirror direction. */\nexport const ORIGIN_TEXT_TO_PM = 'bridge:text-to-prosemirror'\n\n/** Yjs transaction origin: ProseMirror → text direction. */\nexport const ORIGIN_PM_TO_TEXT = 'bridge:prosemirror-to-text'\n\n/** Yjs transaction origin: bootstrap initialization. */\nexport const ORIGIN_INIT = 'bridge:init'\n\n/** Configuration for {@link createYjsBridge}. */\nexport type YjsBridgeConfig = {\n doc: Doc\n sharedText: YText\n sharedProseMirror: YXmlFragment\n schema: Schema\n serialize: Serialize\n parse: Parse\n normalize?: Normalize\n /** Called on non-fatal errors (e.g. parse failures). Defaults to `console.error`. */\n onError?: OnError\n}\n\n/**\n * Result of the bootstrap phase in {@link createYjsBridge}.\n * Indicates which source was used to initialize the shared types.\n */\nexport type BootstrapResult = {\n source: 'text' | 'prosemirror' | 'both-match' | 'empty' | 'initial'\n /** `true` when format conversion (parse or serialize) failed during bootstrap. The bridge is still usable but the affected shared type may be stale. */\n parseError?: boolean\n}\n\n/** Handle returned by {@link createYjsBridge}. */\nexport type YjsBridgeHandle = {\n /** Result of the synchronous bootstrap phase. */\n readonly bootstrapResult: BootstrapResult\n /** Serialize `doc` and push to `Y.Text` using minimal diff. */\n syncToSharedText(doc: Node): ReplaceTextResult\n /** Returns `true` if the transaction originated from `y-prosemirror` sync. */\n isYjsSyncChange(tr: Transaction): boolean\n /** Remove the Y.Text observer. Call when tearing down. */\n dispose(): void\n}\n","import type { Awareness } from 'y-protocols/awareness'\n\n/**\n * Create a Proxy around a Yjs {@link Awareness} that suppresses the specified\n * cursor field. This prevents y-prosemirror's built-in cursor management from\n * conflicting with the PM↔CM cursor sync plugin.\n *\n * Other `setLocalStateField` calls are passed through unchanged.\n */\nexport function createAwarenessProxy(awareness: Awareness, cursorField = 'pmCursor'): Awareness {\n return new Proxy(awareness, {\n get(target, prop, receiver) {\n if (prop === 'getLocalState') {\n return () => {\n const state = target.getLocalState()\n return state ? { ...state, [cursorField]: null } : state\n }\n }\n if (prop === 'setLocalStateField') {\n return (field: string, value: unknown) => {\n // Only suppress the cursor field; pass through other fields\n if (field === cursorField) return\n target.setLocalStateField(field, value)\n }\n }\n const value = Reflect.get(target, prop, receiver) as unknown\n return typeof value === 'function' ? (value as Function).bind(target) : value\n },\n }) as Awareness\n}\n","import type { Node, Schema } from 'prosemirror-model'\nimport type { EditorState, Plugin } from 'prosemirror-state'\nimport type { DecorationAttrs } from 'prosemirror-view'\nimport type { Awareness } from 'y-protocols/awareness'\nimport type { Serialize, LocateText } from '@pm-cm/core'\nimport { initProseMirrorDoc, yCursorPlugin, ySyncPlugin, yUndoPlugin } from 'y-prosemirror'\nimport type { AbstractType, Text as YText, UndoManager } from 'yjs'\nimport type { XmlFragment as YXmlFragment } from 'yjs'\nimport { createAwarenessProxy } from './awareness-proxy.js'\nimport { createBridgeSyncPlugin } from './bridge-sync-plugin.js'\nimport { createCursorSyncPlugin } from './cursor-sync-plugin.js'\nimport type { YjsBridgeHandle, OnWarning } from './types.js'\n\n/** Yjs ↔ ProseMirror node mapping used by `y-prosemirror`. */\nexport type ProseMirrorMapping = Map<AbstractType<unknown>, Node | Node[]>\n\n/** Options forwarded to `yCursorPlugin` from y-prosemirror. */\nexport type YCursorPluginOpts = {\n awarenessStateFilter?: (currentClientId: number, userClientId: number, user: unknown) => boolean\n cursorBuilder?: (user: unknown, clientId: number) => HTMLElement\n selectionBuilder?: (user: unknown, clientId: number) => DecorationAttrs\n getSelection?: (state: EditorState) => unknown\n}\n\n/** Options forwarded to `yUndoPlugin` from y-prosemirror. */\nexport type YUndoPluginOpts = {\n protectedNodes?: Set<string>\n trackedOrigins?: unknown[]\n undoManager?: UndoManager | null\n}\n\n/** Options for {@link createCollabPlugins}. */\nexport type CollabPluginsOptions = {\n /** Shared ProseMirror document in Yjs. */\n sharedProseMirror: YXmlFragment\n awareness: Awareness\n cursorFieldName?: string\n serialize?: Serialize\n /** Awareness field used for CM/Y.Text cursor payloads. Default `'cursor'`. */\n cmCursorFieldName?: string\n locate?: LocateText\n /**\n * Enable PM↔CM cursor sync. Default `false`.\n *\n * When enabled, an {@link createAwarenessProxy | awareness proxy} is applied\n * to suppress y-prosemirror's built-in cursor management.\n */\n cursorSync?: boolean\n /**\n * The shared `Y.Text` instance. When provided, the cursor sync plugin also\n * broadcasts CM-format cursor positions so remote `yCollab` instances render them.\n */\n sharedText?: YText\n /**\n * When provided, a bridge sync plugin is inserted before the cursor sync plugin\n * to ensure Y.Text is synced before cursor positions are computed. This guarantees\n * that serialize-based offsets match Y.Text indices.\n */\n bridge?: YjsBridgeHandle\n /** Extra options forwarded to `yCursorPlugin`. */\n yCursorPluginOpts?: YCursorPluginOpts\n /** Extra options forwarded to `yUndoPlugin`. */\n yUndoPluginOpts?: YUndoPluginOpts\n /** Called for non-fatal warnings. Propagated to child plugins. Default `console.warn`. */\n onWarning?: OnWarning\n}\n\n/**\n * Bundle `ySyncPlugin`, `yCursorPlugin`, `yUndoPlugin` from y-prosemirror,\n * plus an optional PM↔CM cursor sync plugin.\n *\n * @throws If `cursorSync: true` but `serialize` is not provided.\n */\nexport function createCollabPlugins(\n schema: Schema,\n options: CollabPluginsOptions,\n): { plugins: Plugin[]; doc: Node; mapping: ProseMirrorMapping } {\n const cursorFieldName = options.cursorFieldName ?? 'pmCursor'\n const enableCursorSync = options.cursorSync ?? false\n const { sharedProseMirror } = options\n\n if (enableCursorSync && !options.serialize) {\n throw new Error('createCollabPlugins: cursorSync requires serialize to be provided')\n }\n const { doc, mapping: rawMapping } = initProseMirrorDoc(sharedProseMirror, schema)\n const mapping = rawMapping as ProseMirrorMapping\n const pmAwareness = enableCursorSync\n ? createAwarenessProxy(options.awareness, cursorFieldName)\n : options.awareness\n\n const plugins: Plugin[] = [\n ySyncPlugin(sharedProseMirror, { mapping: rawMapping }),\n yCursorPlugin(pmAwareness, options.yCursorPluginOpts ?? {}, cursorFieldName),\n yUndoPlugin(options.yUndoPluginOpts),\n ]\n\n // Bridge sync plugin must run before cursor sync plugin so that\n // Y.Text is updated before cursor positions are computed.\n if (options.bridge) {\n plugins.push(createBridgeSyncPlugin(options.bridge, { onWarning: options.onWarning }))\n }\n\n if (enableCursorSync && options.serialize) {\n plugins.push(\n createCursorSyncPlugin({\n awareness: options.awareness,\n serialize: options.serialize,\n cursorFieldName,\n cmCursorFieldName: options.cmCursorFieldName,\n locate: options.locate,\n sharedText: options.sharedText,\n onWarning: options.onWarning,\n }),\n )\n }\n\n return { plugins, doc, mapping }\n}\n","import { Plugin, PluginKey } from 'prosemirror-state'\nimport type { EditorView } from 'prosemirror-view'\nimport type { YjsBridgeHandle, OnWarning } from './types.js'\n\ntype BridgeSyncState = { needsSync: boolean }\n\ntype BridgeSyncFailure = { ok: false; reason: 'detached' }\n\n/** Options for {@link createBridgeSyncPlugin}. */\nexport type BridgeSyncPluginOptions = {\n /** Called when `syncToSharedText` fails (excludes `reason: 'unchanged'`). */\n onSyncFailure?: (result: BridgeSyncFailure, view: EditorView) => void\n /** Called for non-fatal warnings. Default `console.warn`. */\n onWarning?: OnWarning\n}\n\n/** ProseMirror plugin key for {@link createBridgeSyncPlugin}. Use to read the plugin state. */\nexport const bridgeSyncPluginKey = new PluginKey<BridgeSyncState>('pm-cm-bridge-sync')\n\nconst wiredBridges = new WeakSet<YjsBridgeHandle>()\n\nconst defaultOnWarning: OnWarning = (event) => console.warn(`[pm-cm] ${event.code}: ${event.message}`)\n\n/**\n * ProseMirror plugin that automatically syncs PM doc changes to Y.Text\n * via the bridge handle. Skips Yjs-originated changes to avoid loops.\n *\n * A warning is logged if the same bridge handle is wired more than once.\n * The guard is cleaned up when the plugin is destroyed.\n */\nexport function createBridgeSyncPlugin(\n bridge: YjsBridgeHandle,\n options: BridgeSyncPluginOptions = {},\n): Plugin {\n const warn = options.onWarning ?? defaultOnWarning\n if (wiredBridges.has(bridge)) {\n warn({ code: 'bridge-already-wired', message: 'this bridge is already wired to another plugin instance' })\n }\n wiredBridges.add(bridge)\n\n return new Plugin<BridgeSyncState>({\n key: bridgeSyncPluginKey,\n\n state: {\n init(): BridgeSyncState {\n return { needsSync: false }\n },\n apply(tr, _prev): BridgeSyncState {\n if (!tr.docChanged) return { needsSync: false }\n if (bridge.isYjsSyncChange(tr)) return { needsSync: false }\n return { needsSync: true }\n },\n },\n\n view() {\n return {\n update(view) {\n const state = bridgeSyncPluginKey.getState(view.state)\n if (state?.needsSync) {\n const result = bridge.syncToSharedText(view.state.doc)\n if (!result.ok) {\n if (result.reason === 'detached') {\n options.onSyncFailure?.(result, view)\n warn({ code: 'sync-failed', message: `bridge sync failed: ${result.reason}` })\n }\n }\n }\n },\n destroy() {\n wiredBridges.delete(bridge)\n },\n }\n },\n })\n}\n","import { Plugin, PluginKey } from 'prosemirror-state'\nimport type { Node } from 'prosemirror-model'\nimport type { EditorView } from 'prosemirror-view'\nimport type { Awareness } from 'y-protocols/awareness'\nimport { absolutePositionToRelativePosition, ySyncPluginKey } from 'y-prosemirror'\nimport { createRelativePositionFromTypeIndex } from 'yjs'\nimport type { Text as YText, XmlFragment as YXmlFragment } from 'yjs'\nimport type { Serialize, LocateText, CursorMap } from '@pm-cm/core'\nimport { buildCursorMap, cursorMapLookup, reverseCursorMapLookup } from '@pm-cm/core'\nimport type { OnWarning } from './types.js'\n\n/** Plugin state for the cursor sync plugin. Read via {@link cursorSyncPluginKey}. */\nexport type CursorSyncState = {\n /** Pending CodeMirror cursor to broadcast. Set by {@link syncCmCursor}. */\n pendingCm: { anchor: number; head: number } | null\n /** Text offset mapped from the current PM selection anchor. `null` when no mapping is available. */\n mappedTextOffset: number | null\n}\n\n/** ProseMirror plugin key for {@link createCursorSyncPlugin}. Use to read the plugin state. */\nexport const cursorSyncPluginKey = new PluginKey<CursorSyncState>('pm-cm-cursor-sync')\n\n/**\n * Internal shape of `ySyncPluginKey` state from y-prosemirror.\n * Not exported by upstream — kept here for explicit tracking.\n * Tested against y-prosemirror ^1.3.x.\n */\ntype YSyncPluginState = { type: YXmlFragment; binding: { mapping: Map<unknown, unknown> } }\n\nfunction getYSyncState(view: EditorView): YSyncPluginState | null {\n const raw = ySyncPluginKey.getState(view.state) as Record<string, unknown> | undefined\n if (!raw) return null\n if (\n typeof raw === 'object' &&\n 'type' in raw && raw.type &&\n 'binding' in raw && raw.binding &&\n typeof raw.binding === 'object' &&\n 'mapping' in (raw.binding as Record<string, unknown>) &&\n (raw.binding as Record<string, unknown>).mapping instanceof Map\n ) {\n return raw as unknown as YSyncPluginState\n }\n return null\n}\n\nfunction toRelativePosition(\n view: EditorView,\n pmPos: number,\n): unknown | null {\n const ySyncState = getYSyncState(view)\n if (!ySyncState) return null\n\n return absolutePositionToRelativePosition(\n pmPos,\n ySyncState.type,\n ySyncState.binding.mapping as any, // eslint-disable-line @typescript-eslint/no-explicit-any -- y-prosemirror internal mapping type\n )\n}\n\n/** Returns `false` when ySyncPlugin state is unavailable (plugin not installed). */\nfunction broadcastPmCursor(\n awareness: Awareness,\n cursorFieldName: string,\n view: EditorView,\n pmAnchor: number,\n pmHead: number,\n): boolean {\n const relAnchor = toRelativePosition(view, pmAnchor)\n const relHead = toRelativePosition(view, pmHead)\n if (relAnchor === null || relHead === null) return false\n\n awareness.setLocalStateField(cursorFieldName, { anchor: relAnchor, head: relHead })\n return true\n}\n\nfunction broadcastTextCursor(\n awareness: Awareness,\n cmCursorFieldName: string,\n sharedText: YText,\n textAnchor: number,\n textHead: number,\n): void {\n const len = sharedText.length\n const clamp = (v: number) => Math.max(0, Math.min(v, len))\n const relAnchor = createRelativePositionFromTypeIndex(sharedText, clamp(textAnchor))\n const relHead = createRelativePositionFromTypeIndex(sharedText, clamp(textHead))\n awareness.setLocalStateField(cmCursorFieldName, { anchor: relAnchor, head: relHead })\n}\n\nconst defaultOnWarning: OnWarning = (event) => console.warn(`[pm-cm] ${event.code}: ${event.message}`)\n\n/** Options for {@link createCursorSyncPlugin}. */\nexport type CursorSyncPluginOptions = {\n awareness: Awareness\n serialize: Serialize\n cursorFieldName?: string\n /** Awareness field used for CM/Y.Text cursor payloads. Default `'cursor'`. */\n cmCursorFieldName?: string\n locate?: LocateText\n /**\n * When provided, the plugin also broadcasts CM-format cursor positions\n * (Y.Text relative positions) to the awareness field specified by\n * `cmCursorFieldName`, so that remote `yCollab` instances can render the cursor.\n */\n sharedText?: YText\n /** Called for non-fatal warnings. Default `console.warn`. */\n onWarning?: OnWarning\n}\n\n/**\n * ProseMirror plugin that synchronizes cursor positions between PM and CM via Yjs awareness.\n *\n * - PM → awareness: automatically broadcasts when the PM view is focused and selection changes.\n * - CM → awareness: triggered by dispatching {@link syncCmCursor}.\n */\nexport function createCursorSyncPlugin(options: CursorSyncPluginOptions): Plugin {\n const { awareness, serialize, locate, sharedText } = options\n const warn = options.onWarning ?? defaultOnWarning\n const cursorFieldName = options.cursorFieldName ?? 'pmCursor'\n const cmCursorFieldName = options.cmCursorFieldName ?? 'cursor'\n\n let warnedSyncPluginMissing = false\n\n // Cached cursor map (serialize-based) — rebuilt only when doc changes\n let cachedMap: CursorMap | null = null\n let cachedMapDoc: Node | null = null\n\n function getOrBuildMap(doc: Node): CursorMap {\n if (cachedMapDoc !== doc || !cachedMap) {\n cachedMap = buildCursorMap(doc, serialize, locate)\n cachedMapDoc = doc\n }\n return cachedMap\n }\n\n return new Plugin<CursorSyncState>({\n key: cursorSyncPluginKey,\n\n state: {\n init(): CursorSyncState {\n return { pendingCm: null, mappedTextOffset: null }\n },\n apply(tr, prev, _oldState, newState): CursorSyncState {\n const cmMeta = tr.getMeta(cursorSyncPluginKey) as\n | { anchor: number; head: number }\n | undefined\n if (cmMeta) {\n return { pendingCm: cmMeta, mappedTextOffset: prev.mappedTextOffset }\n }\n\n // Compute PM → text offset when selection or doc changes\n let mappedTextOffset = prev.mappedTextOffset\n if (tr.selectionSet || tr.docChanged) {\n const map = getOrBuildMap(newState.doc)\n mappedTextOffset = cursorMapLookup(map, newState.selection.anchor)\n }\n\n return {\n pendingCm: prev.pendingCm !== null ? null : prev.pendingCm,\n mappedTextOffset,\n }\n },\n },\n\n view() {\n return {\n update(view, prevState) {\n const pluginState = cursorSyncPluginKey.getState(view.state)\n const prevPluginState = cursorSyncPluginKey.getState(prevState)\n\n // CM → awareness: broadcast when pendingCm is newly set\n if (\n pluginState?.pendingCm != null &&\n pluginState.pendingCm !== prevPluginState?.pendingCm\n ) {\n const map = getOrBuildMap(view.state.doc)\n const pmAnchor = reverseCursorMapLookup(map, pluginState.pendingCm.anchor)\n const pmHead = reverseCursorMapLookup(map, pluginState.pendingCm.head)\n if (pmAnchor !== null && pmHead !== null) {\n const ok = broadcastPmCursor(awareness, cursorFieldName, view, pmAnchor, pmHead)\n if (!ok && !warnedSyncPluginMissing) {\n warnedSyncPluginMissing = true\n warn({ code: 'ysync-plugin-missing', message: 'ySyncPlugin state not available — cursor broadcast skipped' })\n }\n }\n // Also broadcast CM-format cursor so remote yCollab can render it\n if (sharedText) {\n broadcastTextCursor(\n awareness,\n cmCursorFieldName,\n sharedText,\n pluginState.pendingCm.anchor,\n pluginState.pendingCm.head,\n )\n }\n return\n }\n\n // PM → awareness: auto-broadcast on selection/doc change when focused\n if (\n view.hasFocus() &&\n (view.state.selection !== prevState.selection ||\n view.state.doc !== prevState.doc)\n ) {\n const { anchor, head } = view.state.selection\n const ok = broadcastPmCursor(awareness, cursorFieldName, view, anchor, head)\n if (!ok && !warnedSyncPluginMissing) {\n warnedSyncPluginMissing = true\n warn({ code: 'ysync-plugin-missing', message: 'ySyncPlugin state not available — cursor broadcast skipped' })\n }\n // Also broadcast CM-format cursor so remote yCollab can render it.\n // When bridgeSyncPlugin runs before this plugin, Y.Text is already\n // synced so serialize-based offsets match Y.Text indices.\n if (sharedText) {\n const map = getOrBuildMap(view.state.doc)\n const textAnchor = cursorMapLookup(map, anchor)\n const textHead = cursorMapLookup(map, head)\n if (textAnchor !== null && textHead !== null) {\n broadcastTextCursor(awareness, cmCursorFieldName, sharedText, textAnchor, textHead)\n }\n }\n }\n },\n }\n },\n })\n}\n\n/**\n * Dispatch a CodeMirror cursor offset (or range) to the cursor sync plugin.\n * The plugin will convert it to a ProseMirror position and broadcast via awareness.\n *\n * @param view - The ProseMirror EditorView that has the cursor sync plugin installed.\n * @param anchor - CodeMirror text offset for the anchor.\n * @param head - CodeMirror text offset for the head (defaults to `anchor` for a collapsed cursor).\n * @param onWarning - Optional warning callback. Default `console.warn`.\n */\nexport function syncCmCursor(view: EditorView, anchor: number, head?: number, onWarning?: OnWarning): void {\n if (!cursorSyncPluginKey.getState(view.state)) {\n (onWarning ?? defaultOnWarning)({ code: 'cursor-sync-not-installed', message: 'cursor sync plugin is not installed on this EditorView' })\n return\n }\n const sanitize = (v: number) => Math.max(0, Math.floor(v))\n view.dispatch(\n view.state.tr.setMeta(cursorSyncPluginKey, {\n anchor: sanitize(anchor),\n head: sanitize(head ?? anchor),\n }),\n )\n}\n","export { createYjsBridge, replaceSharedText, replaceSharedProseMirror } from './bridge.js'\nexport type { YjsBridgeOptions, ReplaceResult, ReplaceTextResult, ReplaceProseMirrorResult } from './bridge.js'\nexport { createAwarenessProxy } from './awareness-proxy.js'\nexport { createCollabPlugins } from './collab-plugins.js'\nexport type { CollabPluginsOptions, ProseMirrorMapping, YCursorPluginOpts, YUndoPluginOpts } from './collab-plugins.js'\nexport {\n ORIGIN_TEXT_TO_PM,\n ORIGIN_PM_TO_TEXT,\n ORIGIN_INIT,\n} from './types.js'\nexport type {\n BootstrapResult,\n YjsBridgeConfig,\n YjsBridgeHandle,\n WarningCode,\n WarningEvent,\n OnWarning,\n} from './types.js'\n\n// Cursor mapping re-exported from @pm-cm/core\nexport { buildCursorMap, cursorMapLookup, reverseCursorMapLookup } from '@pm-cm/core'\nexport type { TextSegment, CursorMap, LocateText } from '@pm-cm/core'\n\n// Bridge sync plugin (auto PM→Y.Text wiring)\nexport { createBridgeSyncPlugin, bridgeSyncPluginKey } from './bridge-sync-plugin.js'\nexport type { BridgeSyncPluginOptions } from './bridge-sync-plugin.js'\n\n// Cursor sync plugin\nexport { createCursorSyncPlugin, cursorSyncPluginKey, syncCmCursor } from './cursor-sync-plugin.js'\nexport type { CursorSyncState, CursorSyncPluginOptions } from './cursor-sync-plugin.js'\n\n// Re-export types from @pm-cm/core\nexport type { Serialize, Parse, Normalize, OnError, ErrorCode, ErrorEvent } from '@pm-cm/core'\n"],"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kBAAAC,iBAAgB,mBAAAC,kBAAiB,0BAAAC,+BAA8B;","names":["doc","value","Plugin","PluginKey","ySyncPluginKey","defaultOnWarning","buildCursorMap","cursorMapLookup","reverseCursorMapLookup"]}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@pm-cm/yjs",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist/index.*"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup"
20
+ },
21
+ "dependencies": {
22
+ "@pm-cm/core": "*"
23
+ },
24
+ "peerDependencies": {
25
+ "prosemirror-model": "^1.19.0",
26
+ "prosemirror-state": "^1.4.0",
27
+ "prosemirror-view": "^1.28.0",
28
+ "y-prosemirror": "^1.3.7",
29
+ "y-protocols": "^1.0.0",
30
+ "yjs": "^13.6.0"
31
+ },
32
+ "description": "Yjs bridge for syncing ProseMirror and text editors via shared types",
33
+ "license": "MIT",
34
+ "author": "munenick",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/MuNeNICK/prosemirror-codemirror-sync.git",
38
+ "directory": "packages/yjs"
39
+ },
40
+ "keywords": ["prosemirror", "codemirror", "yjs", "sync", "bridge"],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "devDependencies": {
45
+ "prosemirror-model": "^1.25.4",
46
+ "prosemirror-state": "^1.4.4",
47
+ "y-prosemirror": "^1.3.7",
48
+ "y-protocols": "^1.0.7",
49
+ "yjs": "^13.6.29",
50
+ "tsup": "^8.0.0",
51
+ "typescript": "~5.9.3"
52
+ }
53
+ }