@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.
- package/LICENSE +23 -0
- package/README.md +4 -2
- package/esm/ExtensionYjs.d.ts +19 -0
- package/esm/ExtensionYjs.js +144 -0
- package/esm/ExtensionYjs.js.map +1 -0
- package/esm/MarkYChange.d.ts +6 -0
- package/esm/MarkYChange.js +21 -0
- package/esm/MarkYChange.js.map +1 -0
- package/esm/WebsocketProvider.d.ts +69 -0
- package/esm/WebsocketProvider.js +377 -0
- package/esm/WebsocketProvider.js.map +1 -0
- package/esm/YjsEditorKit.d.ts +10 -0
- package/esm/YjsEditorKit.js +44 -0
- package/esm/YjsEditorKit.js.map +1 -0
- package/esm/YjsProvider.d.ts +47 -0
- package/esm/YjsProvider.js +12 -0
- package/esm/YjsProvider.js.map +1 -0
- package/esm/binding/BindingMetadata.d.ts +5 -0
- package/esm/binding/BindingMetadata.js +2 -0
- package/esm/binding/BindingMetadata.js.map +1 -0
- package/esm/binding/DiffViewer.d.ts +16 -0
- package/esm/binding/DiffViewer.js +95 -0
- package/esm/binding/DiffViewer.js.map +1 -0
- package/esm/binding/PmYjsBinding.d.ts +46 -0
- package/esm/binding/PmYjsBinding.js +246 -0
- package/esm/binding/PmYjsBinding.js.map +1 -0
- package/esm/binding/convertUtils.d.ts +47 -0
- package/esm/binding/convertUtils.js +80 -0
- package/esm/binding/convertUtils.js.map +1 -0
- package/esm/binding/createNodeFromYElement.d.ts +9 -0
- package/esm/binding/createNodeFromYElement.js +123 -0
- package/esm/binding/createNodeFromYElement.js.map +1 -0
- package/esm/binding/updateYFragment.d.ts +16 -0
- package/esm/binding/updateYFragment.js +337 -0
- package/esm/binding/updateYFragment.js.map +1 -0
- package/esm/debug.d.ts +12 -0
- package/esm/debug.js +158 -0
- package/esm/debug.js.map +1 -0
- package/esm/keys.d.ts +7 -0
- package/esm/keys.js +5 -0
- package/esm/keys.js.map +1 -0
- package/esm/lib.d.ts +7 -0
- package/esm/lib.js +2 -0
- package/esm/lib.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/position.d.ts +7 -0
- package/esm/position.js +165 -0
- package/esm/position.js.map +1 -0
- package/esm/ui/RoomController.d.ts +16 -0
- package/esm/ui/RoomController.js +115 -0
- package/esm/ui/RoomController.js.map +1 -0
- package/esm/ui/selection.d.ts +28 -0
- package/esm/ui/selection.js +128 -0
- package/esm/ui/selection.js.map +1 -0
- package/esm/utils.d.ts +3 -0
- package/esm/utils.js +15 -0
- package/esm/utils.js.map +1 -0
- package/esm/yPositionPlugin.d.ts +26 -0
- package/esm/yPositionPlugin.js +216 -0
- package/esm/yPositionPlugin.js.map +1 -0
- package/esm/ySyncPlugin.d.ts +22 -0
- package/esm/ySyncPlugin.js +140 -0
- package/esm/ySyncPlugin.js.map +1 -0
- package/esm/yUndoPlugin.d.ts +22 -0
- package/esm/yUndoPlugin.js +128 -0
- package/esm/yUndoPlugin.js.map +1 -0
- package/package.json +53 -3
- package/src/ExtensionYjs.ts +204 -0
- package/src/MarkYChange.ts +23 -0
- package/src/WebsocketProvider.ts +528 -0
- package/src/YjsEditorKit.ts +58 -0
- package/src/YjsProvider.ts +75 -0
- package/src/binding/BindingMetadata.ts +6 -0
- package/src/binding/DiffViewer.ts +138 -0
- package/src/binding/PmYjsBinding.ts +383 -0
- package/src/binding/convertUtils.ts +124 -0
- package/src/binding/createNodeFromYElement.ts +175 -0
- package/src/binding/updateYFragment.ts +447 -0
- package/src/debug.ts +239 -0
- package/src/keys.ts +14 -0
- package/src/lib.ts +12 -0
- package/src/position.ts +191 -0
- package/src/ui/RoomController.ts +131 -0
- package/src/ui/selection.ts +217 -0
- package/src/utils.ts +19 -0
- package/src/yPositionPlugin.ts +351 -0
- package/src/ySyncPlugin.ts +211 -0
- package/src/yUndoPlugin.ts +175 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { EditorState, Plugin, Transaction } from 'prosemirror-state';
|
|
2
|
+
|
|
3
|
+
import * as Y from 'yjs';
|
|
4
|
+
|
|
5
|
+
import { Extension } from '@kerebron/editor';
|
|
6
|
+
import type {
|
|
7
|
+
CommandFactories,
|
|
8
|
+
CommandFactory,
|
|
9
|
+
CommandShortcuts,
|
|
10
|
+
} from '@kerebron/editor/commands';
|
|
11
|
+
|
|
12
|
+
import { icons } from '@kerebron/ui/icons';
|
|
13
|
+
import { Ui } from '@kerebron/ui';
|
|
14
|
+
|
|
15
|
+
import { ySyncPlugin } from './ySyncPlugin.js';
|
|
16
|
+
import { yPositionPlugin } from './yPositionPlugin.js';
|
|
17
|
+
import { redoCommand, undoCommand, yUndoPlugin } from './yUndoPlugin.js';
|
|
18
|
+
import { ySyncPluginKey } from './keys.js';
|
|
19
|
+
|
|
20
|
+
import type { CreateYjsProvider } from './YjsProvider.js';
|
|
21
|
+
import { TemplatesRegistry } from '@kerebron/ui/templates';
|
|
22
|
+
import { RoomController } from './ui/RoomController.js';
|
|
23
|
+
|
|
24
|
+
export interface YjsConfig {
|
|
25
|
+
createYjsProvider?: CreateYjsProvider;
|
|
26
|
+
addMenu?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class ExtensionYjs extends Extension {
|
|
30
|
+
name = 'yjs';
|
|
31
|
+
|
|
32
|
+
override overrides = ['history'];
|
|
33
|
+
requires = ['remote-selection'];
|
|
34
|
+
|
|
35
|
+
constructor(public override config: YjsConfig) {
|
|
36
|
+
super();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override getCommandFactories(): Partial<CommandFactories> {
|
|
40
|
+
const changeRoom: CommandFactory = (roomId: string) => {
|
|
41
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
42
|
+
const tr = state.tr;
|
|
43
|
+
tr.setMeta(ySyncPluginKey, { changeRoom: { roomId } });
|
|
44
|
+
|
|
45
|
+
if (dispatch) {
|
|
46
|
+
dispatch(tr);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const leaveRoom: CommandFactory = () => {
|
|
54
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
55
|
+
const tr = state.tr;
|
|
56
|
+
tr.setMeta(ySyncPluginKey, { leaveRoom: true });
|
|
57
|
+
|
|
58
|
+
if (dispatch) {
|
|
59
|
+
dispatch(tr);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const getYDoc: CommandFactory = (
|
|
67
|
+
{ resolve, reject }: {
|
|
68
|
+
resolve: (doc: Y.Doc) => void;
|
|
69
|
+
reject: (reason: any) => void;
|
|
70
|
+
},
|
|
71
|
+
) => {
|
|
72
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
73
|
+
const tr = state.tr;
|
|
74
|
+
tr.setMeta(ySyncPluginKey, { getYDoc: { resolve, reject } });
|
|
75
|
+
if (dispatch) {
|
|
76
|
+
dispatch(tr);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const getYSnapshot: CommandFactory = (
|
|
83
|
+
{ resolve, reject }: {
|
|
84
|
+
resolve: (snapshot: Uint8Array) => void;
|
|
85
|
+
reject: (reason: any) => void;
|
|
86
|
+
},
|
|
87
|
+
) => {
|
|
88
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
89
|
+
const tr = state.tr;
|
|
90
|
+
tr.setMeta(ySyncPluginKey, { getYSnapshot: { resolve, reject } });
|
|
91
|
+
if (dispatch) {
|
|
92
|
+
dispatch(tr);
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const setYSnapshot: CommandFactory = (
|
|
99
|
+
opts: { prevSnapshot: Uint8Array; snapshot: Uint8Array },
|
|
100
|
+
) => {
|
|
101
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
102
|
+
const tr = state.tr;
|
|
103
|
+
tr.setMeta(ySyncPluginKey, { setYSnapshot: opts });
|
|
104
|
+
if (dispatch) {
|
|
105
|
+
dispatch(tr);
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const resetYSnapshot: CommandFactory = () => {
|
|
112
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
113
|
+
const tr = state.tr;
|
|
114
|
+
tr.setMeta(ySyncPluginKey, { resetYSnapshot: true });
|
|
115
|
+
if (dispatch) {
|
|
116
|
+
dispatch(tr);
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
getYDoc,
|
|
124
|
+
changeRoom,
|
|
125
|
+
leaveRoom,
|
|
126
|
+
'undo': () => undoCommand,
|
|
127
|
+
'redo': () => redoCommand,
|
|
128
|
+
getYSnapshot,
|
|
129
|
+
setYSnapshot,
|
|
130
|
+
resetYSnapshot,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
override getKeyboardShortcuts(): Partial<CommandShortcuts> {
|
|
135
|
+
return {
|
|
136
|
+
'Mod-z': 'undo',
|
|
137
|
+
'Mod-y': 'redo',
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
override getProseMirrorPlugins(): Plugin[] {
|
|
142
|
+
return [
|
|
143
|
+
ySyncPlugin(this.editor, this.config.createYjsProvider),
|
|
144
|
+
yPositionPlugin(this.editor),
|
|
145
|
+
yUndoPlugin(),
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
override created(): void {
|
|
150
|
+
if (this.config.addMenu) {
|
|
151
|
+
const ui: Ui = this.editor.ci.resolve('ui');
|
|
152
|
+
|
|
153
|
+
const controller = new RoomController();
|
|
154
|
+
|
|
155
|
+
this.editor.addEventListener('ready', () => {
|
|
156
|
+
const menuFile = {
|
|
157
|
+
type: 'item',
|
|
158
|
+
id: 'yjs-room',
|
|
159
|
+
title: 'YJS Room',
|
|
160
|
+
icon: icons.user,
|
|
161
|
+
action: async (_editor) => {
|
|
162
|
+
const popup = ui.createPopup({
|
|
163
|
+
anchorId: '--menuitem-more-tools',
|
|
164
|
+
layer: 'toplayer',
|
|
165
|
+
group: 'menu',
|
|
166
|
+
});
|
|
167
|
+
popup.addEventListener('remove', () => {
|
|
168
|
+
// popup = undefined;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
controller.bindEvents(popup);
|
|
172
|
+
await controller.init();
|
|
173
|
+
|
|
174
|
+
const templates: TemplatesRegistry = this.editor.ci.resolve(
|
|
175
|
+
'templates',
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
const renderForm = () => {
|
|
179
|
+
ui.setPopupContent(
|
|
180
|
+
popup,
|
|
181
|
+
templates['room-selector'](controller),
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
renderForm();
|
|
186
|
+
|
|
187
|
+
controller.addEventListener('change-room', (event) => {
|
|
188
|
+
renderForm();
|
|
189
|
+
popup.remove();
|
|
190
|
+
});
|
|
191
|
+
controller.addEventListener('change-user', (event) => {
|
|
192
|
+
renderForm();
|
|
193
|
+
popup.remove();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
return true;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
this.editor.run.addMenuItem(menuFile, 0, 'tray');
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type MarkSpec } from 'prosemirror-model';
|
|
2
|
+
import { Mark } from '@kerebron/editor';
|
|
3
|
+
|
|
4
|
+
export class MarkYChange extends Mark {
|
|
5
|
+
override name = 'ychange';
|
|
6
|
+
|
|
7
|
+
override getMarkSpec(): MarkSpec {
|
|
8
|
+
return {
|
|
9
|
+
attrs: {
|
|
10
|
+
user: { default: null },
|
|
11
|
+
type: { default: null },
|
|
12
|
+
},
|
|
13
|
+
inclusive: false,
|
|
14
|
+
parseDOM: [{ tag: 'ychange' }],
|
|
15
|
+
toDOM(node) {
|
|
16
|
+
return ['ychange', {
|
|
17
|
+
ychange_user: node.attrs.user,
|
|
18
|
+
ychange_type: node.attrs.type,
|
|
19
|
+
}];
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|