@kerebron/yjs 0.0.0-stage → 0.9.2

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.
Files changed (88) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +4 -2
  3. package/esm/ExtensionYjs.d.ts +19 -0
  4. package/esm/ExtensionYjs.js +144 -0
  5. package/esm/ExtensionYjs.js.map +1 -0
  6. package/esm/MarkYChange.d.ts +6 -0
  7. package/esm/MarkYChange.js +21 -0
  8. package/esm/MarkYChange.js.map +1 -0
  9. package/esm/WebsocketProvider.d.ts +69 -0
  10. package/esm/WebsocketProvider.js +377 -0
  11. package/esm/WebsocketProvider.js.map +1 -0
  12. package/esm/YjsEditorKit.d.ts +10 -0
  13. package/esm/YjsEditorKit.js +44 -0
  14. package/esm/YjsEditorKit.js.map +1 -0
  15. package/esm/YjsProvider.d.ts +47 -0
  16. package/esm/YjsProvider.js +12 -0
  17. package/esm/YjsProvider.js.map +1 -0
  18. package/esm/binding/BindingMetadata.d.ts +5 -0
  19. package/esm/binding/BindingMetadata.js +2 -0
  20. package/esm/binding/BindingMetadata.js.map +1 -0
  21. package/esm/binding/DiffViewer.d.ts +16 -0
  22. package/esm/binding/DiffViewer.js +95 -0
  23. package/esm/binding/DiffViewer.js.map +1 -0
  24. package/esm/binding/PmYjsBinding.d.ts +46 -0
  25. package/esm/binding/PmYjsBinding.js +246 -0
  26. package/esm/binding/PmYjsBinding.js.map +1 -0
  27. package/esm/binding/convertUtils.d.ts +47 -0
  28. package/esm/binding/convertUtils.js +80 -0
  29. package/esm/binding/convertUtils.js.map +1 -0
  30. package/esm/binding/createNodeFromYElement.d.ts +9 -0
  31. package/esm/binding/createNodeFromYElement.js +123 -0
  32. package/esm/binding/createNodeFromYElement.js.map +1 -0
  33. package/esm/binding/updateYFragment.d.ts +16 -0
  34. package/esm/binding/updateYFragment.js +337 -0
  35. package/esm/binding/updateYFragment.js.map +1 -0
  36. package/esm/debug.d.ts +12 -0
  37. package/esm/debug.js +158 -0
  38. package/esm/debug.js.map +1 -0
  39. package/esm/keys.d.ts +7 -0
  40. package/esm/keys.js +5 -0
  41. package/esm/keys.js.map +1 -0
  42. package/esm/lib.d.ts +7 -0
  43. package/esm/lib.js +2 -0
  44. package/esm/lib.js.map +1 -0
  45. package/esm/package.json +3 -0
  46. package/esm/position.d.ts +7 -0
  47. package/esm/position.js +165 -0
  48. package/esm/position.js.map +1 -0
  49. package/esm/ui/RoomController.d.ts +16 -0
  50. package/esm/ui/RoomController.js +115 -0
  51. package/esm/ui/RoomController.js.map +1 -0
  52. package/esm/ui/selection.d.ts +28 -0
  53. package/esm/ui/selection.js +128 -0
  54. package/esm/ui/selection.js.map +1 -0
  55. package/esm/utils.d.ts +3 -0
  56. package/esm/utils.js +15 -0
  57. package/esm/utils.js.map +1 -0
  58. package/esm/yPositionPlugin.d.ts +26 -0
  59. package/esm/yPositionPlugin.js +216 -0
  60. package/esm/yPositionPlugin.js.map +1 -0
  61. package/esm/ySyncPlugin.d.ts +22 -0
  62. package/esm/ySyncPlugin.js +140 -0
  63. package/esm/ySyncPlugin.js.map +1 -0
  64. package/esm/yUndoPlugin.d.ts +22 -0
  65. package/esm/yUndoPlugin.js +128 -0
  66. package/esm/yUndoPlugin.js.map +1 -0
  67. package/package.json +53 -3
  68. package/src/ExtensionYjs.ts +204 -0
  69. package/src/MarkYChange.ts +23 -0
  70. package/src/WebsocketProvider.ts +528 -0
  71. package/src/YjsEditorKit.ts +58 -0
  72. package/src/YjsProvider.ts +75 -0
  73. package/src/binding/BindingMetadata.ts +6 -0
  74. package/src/binding/DiffViewer.ts +138 -0
  75. package/src/binding/PmYjsBinding.ts +383 -0
  76. package/src/binding/convertUtils.ts +124 -0
  77. package/src/binding/createNodeFromYElement.ts +175 -0
  78. package/src/binding/updateYFragment.ts +447 -0
  79. package/src/debug.ts +239 -0
  80. package/src/keys.ts +14 -0
  81. package/src/lib.ts +12 -0
  82. package/src/position.ts +191 -0
  83. package/src/ui/RoomController.ts +131 -0
  84. package/src/ui/selection.ts +217 -0
  85. package/src/utils.ts +19 -0
  86. package/src/yPositionPlugin.ts +351 -0
  87. package/src/ySyncPlugin.ts +211 -0
  88. package/src/yUndoPlugin.ts +175 -0
@@ -0,0 +1,211 @@
1
+ import * as Y from 'yjs';
2
+ import { Plugin } from 'prosemirror-state';
3
+
4
+ import { ySyncPluginKey, yUndoPluginKey } from './keys.js';
5
+ import type { CreateYjsProvider } from './YjsProvider.js';
6
+ import { PmYjsBinding } from './binding/PmYjsBinding.js';
7
+ import { CoreEditor } from '@kerebron/editor';
8
+
9
+ interface YSyncOpts {
10
+ permanentUserData?: Y.PermanentUserData;
11
+ onFirstRender?: () => void;
12
+ roomId?: string;
13
+ }
14
+
15
+ export interface YSyncPluginState {
16
+ binding: PmYjsBinding;
17
+ isChangeOrigin: boolean;
18
+ isUndoRedoOperation: boolean; // Used in y-history.test.ts
19
+ permanentUserData?: Y.PermanentUserData;
20
+ }
21
+
22
+ interface YSyncMeta {
23
+ getYDoc?: {
24
+ resolve: (doc: Y.Doc) => void;
25
+ reject: (reason: any) => void;
26
+ };
27
+ changeRoom?: {
28
+ roomId: string;
29
+ };
30
+ leaveRoom?: boolean;
31
+ isChangeOrigin?: boolean;
32
+ isUndoRedoOperation?: boolean;
33
+ getYSnapshot?: {
34
+ resolve: (snapshot: Uint8Array) => void;
35
+ reject: (reason: any) => void;
36
+ };
37
+ setYSnapshot?: {
38
+ prevSnapshot?: Uint8Array;
39
+ snapshot: Uint8Array;
40
+ };
41
+ resetYSnapshot?: boolean;
42
+ }
43
+
44
+ /**
45
+ * This plugin listens to changes in prosemirror view and keeps yXmlState and view in sync.
46
+ *
47
+ * This plugin also keeps references to the type and the shared document so other plugins can access it.
48
+ */
49
+ export const ySyncPlugin = (
50
+ editor: CoreEditor,
51
+ createYjsProvider: CreateYjsProvider,
52
+ {
53
+ onFirstRender = () => {
54
+ },
55
+ }: YSyncOpts = {},
56
+ ): any => {
57
+ let initialContentChanged = false;
58
+ const plugin: Plugin<YSyncPluginState> = new Plugin<YSyncPluginState>({
59
+ props: {
60
+ editable: (state) => {
61
+ const syncState = ySyncPluginKey.getState(state)!;
62
+ return syncState.binding.isEditable();
63
+ },
64
+ },
65
+ key: ySyncPluginKey,
66
+ state: {
67
+ init: (_initargs, state): YSyncPluginState => {
68
+ return {
69
+ binding: new PmYjsBinding(editor),
70
+ isChangeOrigin: false,
71
+ isUndoRedoOperation: false,
72
+ permanentUserData: undefined,
73
+ };
74
+ },
75
+ apply: (tr, pluginState: YSyncPluginState) => {
76
+ const pluginMeta: Partial<YSyncMeta> = tr.getMeta(ySyncPluginKey);
77
+
78
+ const changeUser = tr.getMeta('changeUser');
79
+ if (changeUser) {
80
+ pluginState.binding.changeUser(changeUser.user);
81
+ }
82
+
83
+ if (pluginMeta?.getYDoc) {
84
+ const yjs = pluginState.binding.getYjs();
85
+ if (yjs) {
86
+ pluginMeta.getYDoc.resolve(yjs.ydoc);
87
+ } else {
88
+ if (pluginMeta.getYDoc.reject) {
89
+ pluginMeta.getYDoc.reject(new Error('No yjs'));
90
+ } else {
91
+ throw new Error('No yjs');
92
+ }
93
+ }
94
+ return pluginState;
95
+ }
96
+
97
+ if (pluginMeta?.leaveRoom) {
98
+ pluginState.isChangeOrigin = false;
99
+ pluginState.isUndoRedoOperation = false;
100
+
101
+ initialContentChanged = false;
102
+
103
+ pluginState.binding.leaveRoom(tr);
104
+
105
+ return pluginState;
106
+ }
107
+
108
+ if (pluginMeta?.changeRoom) {
109
+ pluginState.isChangeOrigin = false;
110
+ pluginState.isUndoRedoOperation = false;
111
+
112
+ initialContentChanged = false;
113
+
114
+ const roomId = pluginMeta.changeRoom.roomId;
115
+ pluginState.binding.changeRoom(
116
+ roomId,
117
+ createYjsProvider,
118
+ tr,
119
+ );
120
+
121
+ return pluginState;
122
+ }
123
+
124
+ pluginState.binding.addToYjsHistory =
125
+ tr.getMeta('addToYjsHistory') !== false;
126
+ // always set isChangeOrigin. If undefined, this is not change origin.
127
+ pluginState.isChangeOrigin = !!pluginMeta?.isChangeOrigin;
128
+ pluginState.isUndoRedoOperation = !!pluginMeta?.isChangeOrigin &&
129
+ !!pluginMeta?.isUndoRedoOperation;
130
+
131
+ if (pluginMeta?.getYSnapshot) {
132
+ const yjs = pluginState.binding.getYjs();
133
+ if (yjs) {
134
+ const snapshot = Y.snapshot(yjs.ydoc);
135
+ pluginMeta.getYSnapshot.resolve(Y.encodeSnapshotV2(snapshot));
136
+ } else {
137
+ if (pluginMeta.getYSnapshot.reject) {
138
+ pluginMeta.getYSnapshot.reject(new Error('No yjs'));
139
+ } else {
140
+ throw new Error('No yjs');
141
+ }
142
+ }
143
+ return pluginState;
144
+ }
145
+
146
+ if (pluginMeta?.resetYSnapshot) {
147
+ pluginState.binding.diffViewer.reset();
148
+ return pluginState;
149
+ }
150
+
151
+ if (pluginMeta?.setYSnapshot) {
152
+ const { prevSnapshot, snapshot } = pluginMeta?.setYSnapshot;
153
+ setTimeout(() => { // Prevent from snapshot being overwritten by current tr
154
+ pluginState.binding.setSnapshot(snapshot, prevSnapshot);
155
+ }, 0);
156
+ return pluginState;
157
+ }
158
+
159
+ return pluginState;
160
+ },
161
+ },
162
+ view: (view) => {
163
+ const pluginState: YSyncPluginState = ySyncPluginKey.getState(
164
+ view.state,
165
+ )!;
166
+ const binding = pluginState.binding;
167
+
168
+ onFirstRender();
169
+ return {
170
+ update: () => {
171
+ const pluginState: YSyncPluginState | undefined = plugin.getState(
172
+ view.state,
173
+ );
174
+ if (!pluginState) {
175
+ return;
176
+ }
177
+
178
+ const binding = pluginState.binding;
179
+ if (binding.isEditable()) {
180
+ if (
181
+ // If the content doesn't change initially, we don't render anything to Yjs
182
+ // If the content was cleared by a user action, we want to catch the change and
183
+ // represent it in Yjs
184
+ initialContentChanged ||
185
+ view.state.doc.content.findDiffStart(
186
+ view.state.doc.type.createAndFill()!.content,
187
+ ) !== null
188
+ ) {
189
+ initialContentChanged = true;
190
+ if (
191
+ pluginState.binding.addToYjsHistory === false &&
192
+ !pluginState.isChangeOrigin
193
+ ) {
194
+ const yUndoPluginState = yUndoPluginKey.getState(view.state);
195
+ if (yUndoPluginState?.undoManager) {
196
+ yUndoPluginState.undoManager.stopCapturing();
197
+ }
198
+ }
199
+
200
+ binding.pmChanged();
201
+ }
202
+ }
203
+ },
204
+ destroy: () => {
205
+ binding.destroy();
206
+ },
207
+ };
208
+ },
209
+ });
210
+ return plugin;
211
+ };
@@ -0,0 +1,175 @@
1
+ import { EditorState, Plugin } from 'prosemirror-state';
2
+ import { ContentType, Item, Text, UndoManager, XmlElement } from 'yjs';
3
+ import * as Y from 'yjs';
4
+
5
+ import type { Command } from '@kerebron/editor/commands';
6
+
7
+ import { ySyncPluginKey, yUndoPluginKey } from './keys.js';
8
+ import { getRelativeSelection } from './ui/selection.js';
9
+ import { YjsData } from './binding/PmYjsBinding.js';
10
+ import { YSyncPluginState } from './ySyncPlugin.js';
11
+
12
+ export interface UndoPluginState {
13
+ xmlFragment?: Y.XmlFragment;
14
+ undoManager?: UndoManager;
15
+ prevSel?: ReturnType<typeof getRelativeSelection>;
16
+ hasUndoOps: boolean;
17
+ hasRedoOps: boolean;
18
+ }
19
+
20
+ export const undo: Command = (state: EditorState): boolean => {
21
+ const tr = state.tr;
22
+ tr.setMeta('preventDispatch', true);
23
+ return !!(yUndoPluginKey.getState(state)?.undoManager?.undo());
24
+ };
25
+
26
+ export const redo: Command = (state: EditorState): boolean => {
27
+ const tr = state.tr;
28
+ tr.setMeta('preventDispatch', true);
29
+ return !!(yUndoPluginKey.getState(state)?.undoManager?.redo());
30
+ };
31
+
32
+ export const undoCommand: Command = (state, dispatch) =>
33
+ !dispatch
34
+ ? !!(yUndoPluginKey.getState(state)?.undoManager?.canUndo())
35
+ : undo(state);
36
+
37
+ export const redoCommand: Command = (state, dispatch) =>
38
+ !dispatch
39
+ ? !!(yUndoPluginKey.getState(state)?.undoManager?.canRedo())
40
+ : redo(state);
41
+
42
+ export const defaultProtectedNodes = new Set(['paragraph']);
43
+
44
+ export const defaultDeleteFilter = (
45
+ item: Y.Item,
46
+ protectedNodes: Set<string>,
47
+ ): boolean =>
48
+ !(item instanceof Item) ||
49
+ !(item.content instanceof ContentType) ||
50
+ !(item.content.type instanceof Text ||
51
+ (item.content.type instanceof XmlElement &&
52
+ protectedNodes.has(item.content.type.nodeName))) ||
53
+ item.content.type._length === 0;
54
+
55
+ export const yUndoPlugin = ({
56
+ protectedNodes = defaultProtectedNodes,
57
+ trackedOrigins = [],
58
+ }: {
59
+ protectedNodes?: Set<string>;
60
+ trackedOrigins?: any[];
61
+ } = {}) =>
62
+ new Plugin<UndoPluginState>({
63
+ key: yUndoPluginKey,
64
+ state: {
65
+ init: (_initargs, _state) => {
66
+ return {
67
+ xmlFragment: undefined,
68
+ undoManager: undefined,
69
+ prevSel: undefined,
70
+ hasUndoOps: false,
71
+ hasRedoOps: false,
72
+ };
73
+ },
74
+ apply: (tr, val: UndoPluginState, oldState, state) => {
75
+ const clearYjs = tr.getMeta('clearYjs');
76
+ if (clearYjs) {
77
+ const undoManager = val.undoManager;
78
+ if (undoManager) {
79
+ undoManager?.destroy();
80
+ }
81
+
82
+ return {
83
+ xmlFragment: undefined,
84
+ undoManager: undefined,
85
+ prevSel: undefined,
86
+ hasUndoOps: false,
87
+ hasRedoOps: false,
88
+ };
89
+ }
90
+
91
+ const setYjs: YjsData | undefined = tr.getMeta('setYjs');
92
+ if (setYjs) {
93
+ const { xmlFragment } = setYjs;
94
+ const undoManager = new UndoManager(xmlFragment, {
95
+ trackedOrigins: new Set([ySyncPluginKey].concat(trackedOrigins)),
96
+ deleteFilter: (item) => defaultDeleteFilter(item, protectedNodes),
97
+ captureTransaction: (ytr) => {
98
+ return ytr.meta.get('addToYjsHistory') !== false;
99
+ },
100
+ });
101
+
102
+ const ystate: YSyncPluginState = ySyncPluginKey.getState(oldState)!;
103
+ undoManager.on('stack-item-added', ({ stackItem }) => {
104
+ const yUndoPluginState = yUndoPluginKey.getState(oldState);
105
+ const binding = ystate.binding;
106
+ if (binding && yUndoPluginState) {
107
+ stackItem.meta.set(
108
+ binding,
109
+ yUndoPluginState.prevSel,
110
+ );
111
+ }
112
+ });
113
+ undoManager.on('stack-item-popped', ({ stackItem }) => {
114
+ const binding = ystate.binding;
115
+ if (binding) {
116
+ const beforeTransactionSelection = stackItem.meta.get(binding);
117
+ if (beforeTransactionSelection) {
118
+ const selectionStash = binding.getSelectionStash();
119
+ if (selectionStash) {
120
+ selectionStash.overwrite(beforeTransactionSelection);
121
+ }
122
+ }
123
+ }
124
+ });
125
+
126
+ return {
127
+ undoManager,
128
+ xmlFragment,
129
+ prevSel: undefined,
130
+ hasUndoOps: undoManager.undoStack.length > 0,
131
+ hasRedoOps: undoManager.redoStack.length > 0,
132
+ };
133
+ }
134
+
135
+ if (val.undoManager && val.xmlFragment) {
136
+ const undoManager = val.undoManager;
137
+ const hasUndoOps = undoManager.undoStack.length > 0;
138
+ const hasRedoOps = undoManager.redoStack.length > 0;
139
+
140
+ const ystate = ySyncPluginKey.getState(state)!;
141
+ const binding = ystate.binding;
142
+ if (binding) {
143
+ return {
144
+ ...val,
145
+ prevSel: getRelativeSelection(
146
+ val.xmlFragment,
147
+ binding.getMapping(),
148
+ oldState,
149
+ ),
150
+ hasUndoOps,
151
+ hasRedoOps,
152
+ };
153
+ }
154
+
155
+ if (hasUndoOps !== val.hasUndoOps || hasRedoOps !== val.hasRedoOps) {
156
+ return {
157
+ ...val,
158
+ hasUndoOps: undoManager.undoStack.length > 0,
159
+ hasRedoOps: undoManager.redoStack.length > 0,
160
+ };
161
+ }
162
+ }
163
+ return val;
164
+ },
165
+ },
166
+ view: (view) => {
167
+ return {
168
+ destroy: () => {
169
+ const yUndoPluginState = yUndoPluginKey.getState(view.state);
170
+ const undoManager = yUndoPluginState?.undoManager;
171
+ undoManager?.destroy();
172
+ },
173
+ };
174
+ },
175
+ });