@kerebron/yjs 0.0.0-stage → 0.9.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/LICENSE +23 -0
- package/README.md +4 -2
- package/esm/ExtensionYjs.d.ts +20 -0
- package/esm/ExtensionYjs.d.ts.map +1 -0
- package/esm/ExtensionYjs.js +144 -0
- package/esm/ExtensionYjs.js.map +1 -0
- package/esm/MarkYChange.d.ts +7 -0
- package/esm/MarkYChange.d.ts.map +1 -0
- package/esm/MarkYChange.js +21 -0
- package/esm/MarkYChange.js.map +1 -0
- package/esm/WebsocketProvider.d.ts +70 -0
- package/esm/WebsocketProvider.d.ts.map +1 -0
- package/esm/WebsocketProvider.js +377 -0
- package/esm/WebsocketProvider.js.map +1 -0
- package/esm/YjsEditorKit.d.ts +11 -0
- package/esm/YjsEditorKit.d.ts.map +1 -0
- package/esm/YjsEditorKit.js +44 -0
- package/esm/YjsEditorKit.js.map +1 -0
- package/esm/YjsProvider.d.ts +48 -0
- package/esm/YjsProvider.d.ts.map +1 -0
- package/esm/YjsProvider.js +12 -0
- package/esm/YjsProvider.js.map +1 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +58 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/binding/BindingMetadata.d.ts +6 -0
- package/esm/binding/BindingMetadata.d.ts.map +1 -0
- package/esm/binding/BindingMetadata.js +2 -0
- package/esm/binding/BindingMetadata.js.map +1 -0
- package/esm/binding/DiffViewer.d.ts +17 -0
- package/esm/binding/DiffViewer.d.ts.map +1 -0
- package/esm/binding/DiffViewer.js +95 -0
- package/esm/binding/DiffViewer.js.map +1 -0
- package/esm/binding/PmYjsBinding.d.ts +47 -0
- package/esm/binding/PmYjsBinding.d.ts.map +1 -0
- package/esm/binding/PmYjsBinding.js +246 -0
- package/esm/binding/PmYjsBinding.js.map +1 -0
- package/esm/binding/convertUtils.d.ts +48 -0
- package/esm/binding/convertUtils.d.ts.map +1 -0
- package/esm/binding/convertUtils.js +80 -0
- package/esm/binding/convertUtils.js.map +1 -0
- package/esm/binding/createNodeFromYElement.d.ts +10 -0
- package/esm/binding/createNodeFromYElement.d.ts.map +1 -0
- package/esm/binding/createNodeFromYElement.js +123 -0
- package/esm/binding/createNodeFromYElement.js.map +1 -0
- package/esm/binding/updateYFragment.d.ts +17 -0
- package/esm/binding/updateYFragment.d.ts.map +1 -0
- package/esm/binding/updateYFragment.js +337 -0
- package/esm/binding/updateYFragment.js.map +1 -0
- package/esm/debug.d.ts +13 -0
- package/esm/debug.d.ts.map +1 -0
- package/esm/debug.js +158 -0
- package/esm/debug.js.map +1 -0
- package/esm/keys.d.ts +8 -0
- package/esm/keys.d.ts.map +1 -0
- package/esm/keys.js +5 -0
- package/esm/keys.js.map +1 -0
- package/esm/lib.d.ts +8 -0
- package/esm/lib.d.ts.map +1 -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 +8 -0
- package/esm/position.d.ts.map +1 -0
- package/esm/position.js +165 -0
- package/esm/position.js.map +1 -0
- package/esm/ui/RoomController.d.ts +17 -0
- package/esm/ui/RoomController.d.ts.map +1 -0
- package/esm/ui/RoomController.js +115 -0
- package/esm/ui/RoomController.js.map +1 -0
- package/esm/ui/selection.d.ts +29 -0
- package/esm/ui/selection.d.ts.map +1 -0
- package/esm/ui/selection.js +129 -0
- package/esm/ui/selection.js.map +1 -0
- package/esm/utils.d.ts +4 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +15 -0
- package/esm/utils.js.map +1 -0
- package/esm/yPositionPlugin.d.ts +27 -0
- package/esm/yPositionPlugin.d.ts.map +1 -0
- package/esm/yPositionPlugin.js +216 -0
- package/esm/yPositionPlugin.js.map +1 -0
- package/esm/ySyncPlugin.d.ts +23 -0
- package/esm/ySyncPlugin.d.ts.map +1 -0
- package/esm/ySyncPlugin.js +140 -0
- package/esm/ySyncPlugin.js.map +1 -0
- package/esm/yUndoPlugin.d.ts +23 -0
- package/esm/yUndoPlugin.d.ts.map +1 -0
- package/esm/yUndoPlugin.js +128 -0
- package/esm/yUndoPlugin.js.map +1 -0
- package/package.json +46 -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/_dnt.shims.ts +60 -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 +218 -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,218 @@
|
|
|
1
|
+
import * as dntShim from "../_dnt.shims.js";
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AllSelection,
|
|
6
|
+
EditorState,
|
|
7
|
+
NodeSelection,
|
|
8
|
+
Selection,
|
|
9
|
+
TextSelection,
|
|
10
|
+
Transaction,
|
|
11
|
+
} from 'prosemirror-state';
|
|
12
|
+
|
|
13
|
+
import { YjsData } from '../binding/PmYjsBinding.js';
|
|
14
|
+
import { ProsemirrorMapping } from '../lib.js';
|
|
15
|
+
import {
|
|
16
|
+
absolutePositionToRelativePosition,
|
|
17
|
+
relativePositionToAbsolutePosition,
|
|
18
|
+
} from '../position.js';
|
|
19
|
+
import { CoreEditor } from '@kerebron/editor';
|
|
20
|
+
|
|
21
|
+
interface TransactionSelection {
|
|
22
|
+
type: string;
|
|
23
|
+
anchor: Y.RelativePosition;
|
|
24
|
+
head: Y.RelativePosition;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const restoreRelativeSelection = (
|
|
28
|
+
tr: Transaction,
|
|
29
|
+
relSel: TransactionSelection,
|
|
30
|
+
yjs: YjsData,
|
|
31
|
+
mapping: ProsemirrorMapping,
|
|
32
|
+
) => {
|
|
33
|
+
const { ydoc, xmlFragment } = yjs;
|
|
34
|
+
|
|
35
|
+
if (relSel.anchor !== null && relSel.head !== null) {
|
|
36
|
+
if (relSel.type === 'all') {
|
|
37
|
+
tr.setSelection(new AllSelection(tr.doc));
|
|
38
|
+
} else if (relSel.type === 'node') {
|
|
39
|
+
const anchor = relativePositionToAbsolutePosition(
|
|
40
|
+
ydoc,
|
|
41
|
+
xmlFragment,
|
|
42
|
+
relSel.anchor,
|
|
43
|
+
mapping,
|
|
44
|
+
);
|
|
45
|
+
if (anchor !== null) {
|
|
46
|
+
tr.setSelection(NodeSelection.create(tr.doc, anchor));
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
const anchor = relativePositionToAbsolutePosition(
|
|
50
|
+
ydoc,
|
|
51
|
+
xmlFragment,
|
|
52
|
+
relSel.anchor,
|
|
53
|
+
mapping,
|
|
54
|
+
);
|
|
55
|
+
const head = relativePositionToAbsolutePosition(
|
|
56
|
+
ydoc,
|
|
57
|
+
xmlFragment,
|
|
58
|
+
relSel.head,
|
|
59
|
+
mapping,
|
|
60
|
+
);
|
|
61
|
+
if (anchor !== null && head !== null) {
|
|
62
|
+
const sel = TextSelection.between(
|
|
63
|
+
tr.doc.resolve(anchor),
|
|
64
|
+
tr.doc.resolve(head),
|
|
65
|
+
);
|
|
66
|
+
tr.setSelection(sel);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const getRelativeSelection = (
|
|
73
|
+
xmlFragment: Y.XmlFragment,
|
|
74
|
+
mapping: ProsemirrorMapping,
|
|
75
|
+
state: EditorState,
|
|
76
|
+
): TransactionSelection => ({
|
|
77
|
+
type: getSelectionType(state.selection),
|
|
78
|
+
anchor: absolutePositionToRelativePosition(
|
|
79
|
+
state.selection.anchor,
|
|
80
|
+
xmlFragment,
|
|
81
|
+
mapping,
|
|
82
|
+
),
|
|
83
|
+
head: absolutePositionToRelativePosition(
|
|
84
|
+
state.selection.head,
|
|
85
|
+
xmlFragment,
|
|
86
|
+
mapping,
|
|
87
|
+
),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
function getSelectionType(selection: Selection) {
|
|
91
|
+
if (selection instanceof TextSelection) {
|
|
92
|
+
return 'text';
|
|
93
|
+
}
|
|
94
|
+
if (selection instanceof AllSelection) {
|
|
95
|
+
return 'all';
|
|
96
|
+
}
|
|
97
|
+
if (selection instanceof NodeSelection) {
|
|
98
|
+
return 'node';
|
|
99
|
+
}
|
|
100
|
+
return 'other_selection';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class SelectionStash {
|
|
104
|
+
private readonly beforeAllTransactions: () => void;
|
|
105
|
+
private readonly afterAllTransactions: () => void;
|
|
106
|
+
|
|
107
|
+
private _beforeTransactionSelection: TransactionSelection | null = null;
|
|
108
|
+
private _domSelectionInView: boolean = false;
|
|
109
|
+
|
|
110
|
+
constructor(
|
|
111
|
+
private yjs: YjsData,
|
|
112
|
+
private mapping: ProsemirrorMapping,
|
|
113
|
+
private editor: CoreEditor,
|
|
114
|
+
) {
|
|
115
|
+
this.beforeAllTransactions = () => {
|
|
116
|
+
if (
|
|
117
|
+
!this._beforeTransactionSelection &&
|
|
118
|
+
editor.view
|
|
119
|
+
) {
|
|
120
|
+
this._beforeTransactionSelection = getRelativeSelection(
|
|
121
|
+
yjs.xmlFragment,
|
|
122
|
+
mapping,
|
|
123
|
+
editor.state,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
this.afterAllTransactions = () => {
|
|
128
|
+
this._beforeTransactionSelection = null;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
this.yjs.ydoc.on('beforeAllTransactions', this.beforeAllTransactions);
|
|
132
|
+
this.yjs.ydoc.on('afterAllTransactions', this.afterAllTransactions);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
destroy() {
|
|
136
|
+
this.yjs.ydoc.off('beforeAllTransactions', this.beforeAllTransactions);
|
|
137
|
+
this.yjs.ydoc.off('afterAllTransactions', this.afterAllTransactions);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
store() {
|
|
141
|
+
this._beforeTransactionSelection = getRelativeSelection(
|
|
142
|
+
this.yjs.xmlFragment,
|
|
143
|
+
this.mapping,
|
|
144
|
+
this.editor.state,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
overwrite(transactionSelection: TransactionSelection) {
|
|
149
|
+
this._beforeTransactionSelection = transactionSelection;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
restore(tr: Transaction) {
|
|
153
|
+
if (this._beforeTransactionSelection) {
|
|
154
|
+
restoreRelativeSelection(
|
|
155
|
+
tr,
|
|
156
|
+
this._beforeTransactionSelection,
|
|
157
|
+
this.yjs,
|
|
158
|
+
this.mapping,
|
|
159
|
+
);
|
|
160
|
+
if (this._isLocalCursorInView()) {
|
|
161
|
+
tr.scrollIntoView();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
_isLocalCursorInView(): boolean {
|
|
167
|
+
if (!this.editor.view.hasFocus()) return false;
|
|
168
|
+
|
|
169
|
+
// const isNode = /* @__PURE__ */(() => typeof process !== 'undefined' && process.release && /node|io\.js/.test(process.release.name) && Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]')()
|
|
170
|
+
const isBrowser =
|
|
171
|
+
/* @__PURE__ */ (() =>
|
|
172
|
+
typeof dntShim.dntGlobalThis !== 'undefined' && typeof document !== 'undefined')(); // && !isNode
|
|
173
|
+
|
|
174
|
+
if (isBrowser && this._domSelectionInView === false) {
|
|
175
|
+
// Calculate the domSelectionInView and clear by next tick after all events are finished
|
|
176
|
+
setTimeout(() => {
|
|
177
|
+
this._domSelectionInView = false;
|
|
178
|
+
}, 0);
|
|
179
|
+
this._domSelectionInView = this._isDomSelectionInView();
|
|
180
|
+
}
|
|
181
|
+
return this._domSelectionInView;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
_isDomSelectionInView(): boolean {
|
|
185
|
+
const view = this.editor.view;
|
|
186
|
+
if (!('root' in view)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
const selection = document.getSelection();
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
!selection || selection.anchorNode == null || selection.focusNode == null
|
|
193
|
+
) return false;
|
|
194
|
+
|
|
195
|
+
const range = document.createRange();
|
|
196
|
+
range.setStart(selection.anchorNode, selection.anchorOffset);
|
|
197
|
+
range.setEnd(selection.focusNode, selection.focusOffset);
|
|
198
|
+
|
|
199
|
+
// This is a workaround for an edgecase where getBoundingClientRect will
|
|
200
|
+
// return zero values if the selection is collapsed at the start of a newline
|
|
201
|
+
// see reference here: https://stackoverflow.com/a/59780954
|
|
202
|
+
const rects = range.getClientRects();
|
|
203
|
+
if (rects.length === 0) {
|
|
204
|
+
// probably buggy newline behavior, explicitly select the node contents
|
|
205
|
+
if (range.startContainer && range.collapsed) {
|
|
206
|
+
range.selectNodeContents(range.startContainer);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const bounding = range.getBoundingClientRect();
|
|
211
|
+
const documentElement = document.documentElement;
|
|
212
|
+
|
|
213
|
+
return bounding.bottom >= 0 && bounding.right >= 0 &&
|
|
214
|
+
bounding.left <=
|
|
215
|
+
(globalThis.innerWidth || documentElement.clientWidth || 0) &&
|
|
216
|
+
bounding.top <= (globalThis.innerHeight || documentElement.clientHeight || 0);
|
|
217
|
+
}
|
|
218
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as Y from 'yjs';
|
|
2
|
+
import * as sha256 from 'lib0/hash/sha256';
|
|
3
|
+
import * as buf from 'lib0/buffer';
|
|
4
|
+
|
|
5
|
+
const _convolute = (digest: Uint8Array) => {
|
|
6
|
+
const N = 6;
|
|
7
|
+
for (let i = N; i < digest.length; i++) {
|
|
8
|
+
digest[i % N] = digest[i % N] ^ digest[i];
|
|
9
|
+
}
|
|
10
|
+
return digest.slice(0, N);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const hashOfJSON = (json: any) =>
|
|
14
|
+
buf.toBase64(_convolute(sha256.digest(buf.encodeAny(json))));
|
|
15
|
+
|
|
16
|
+
export const isVisible = (item: Y.Item, snapshot?: Y.Snapshot) =>
|
|
17
|
+
snapshot === undefined ? !item.deleted : (snapshot.sv.has(item.id.client) &&
|
|
18
|
+
(snapshot.sv.get(item.id.client)!) > item.id.clock &&
|
|
19
|
+
!Y.isDeleted(snapshot.ds, item.id));
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { Awareness } from 'y-protocols/awareness';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
|
|
4
|
+
import { EditorState, Plugin, Transaction } from 'prosemirror-state';
|
|
5
|
+
import { EditorView } from 'prosemirror-view';
|
|
6
|
+
|
|
7
|
+
import type { CoreEditor } from '@kerebron/editor';
|
|
8
|
+
import {
|
|
9
|
+
type ColorMapper,
|
|
10
|
+
defaultColorMapper,
|
|
11
|
+
generateBlankUser,
|
|
12
|
+
type User,
|
|
13
|
+
} from '@kerebron/editor/user';
|
|
14
|
+
import type { SelectionState } from '@kerebron/extension-basic-editor/ExtensionRemoteSelection';
|
|
15
|
+
|
|
16
|
+
import { yPositionPluginKey, ySyncPluginKey } from './keys.js';
|
|
17
|
+
import {
|
|
18
|
+
absolutePositionToRelativePosition,
|
|
19
|
+
relativePositionToAbsolutePosition,
|
|
20
|
+
} from './position.js';
|
|
21
|
+
import type { YSyncPluginState } from './ySyncPlugin.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Is null if no timeout is in progress.
|
|
25
|
+
* Is defined if a timeout is in progress.
|
|
26
|
+
* Maps from view
|
|
27
|
+
*/
|
|
28
|
+
let viewsToUpdate: Map<EditorView, Map<any, any>> | null = null;
|
|
29
|
+
|
|
30
|
+
const updateMetas = () => {
|
|
31
|
+
const ups: Map<EditorView, Map<any, any>> | null = viewsToUpdate;
|
|
32
|
+
viewsToUpdate = null;
|
|
33
|
+
if (!ups) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
ups.forEach((metas, view) => {
|
|
37
|
+
const tr = view.state.tr;
|
|
38
|
+
const syncState = ySyncPluginKey.getState(view.state);
|
|
39
|
+
if (syncState && syncState.binding) { // && !syncState.binding.isDestroyed
|
|
40
|
+
metas.forEach((val, key) => {
|
|
41
|
+
tr.setMeta(key, val);
|
|
42
|
+
});
|
|
43
|
+
view.dispatch(tr);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type AwarenessListener = (
|
|
49
|
+
{ added, updated, removed }: {
|
|
50
|
+
added: number[];
|
|
51
|
+
updated: number[];
|
|
52
|
+
removed: number[];
|
|
53
|
+
},
|
|
54
|
+
s: any,
|
|
55
|
+
t: any,
|
|
56
|
+
) => void;
|
|
57
|
+
|
|
58
|
+
interface PositionPluginConfig {
|
|
59
|
+
getSelection?: (arg0: any) => any;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface YPositionPluginState {
|
|
63
|
+
awareness?: Awareness;
|
|
64
|
+
awarenessListener?: AwarenessListener;
|
|
65
|
+
cursorStateField: string;
|
|
66
|
+
userStateField: string;
|
|
67
|
+
me: User;
|
|
68
|
+
colorMapper: ColorMapper;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function destroyAwareness(
|
|
72
|
+
pluginState: YPositionPluginState,
|
|
73
|
+
) {
|
|
74
|
+
if (!pluginState.awareness) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const awareness = pluginState.awareness;
|
|
78
|
+
if (pluginState.awarenessListener) {
|
|
79
|
+
awareness.off('change', pluginState.awarenessListener);
|
|
80
|
+
}
|
|
81
|
+
awareness.setLocalStateField(pluginState.cursorStateField, null);
|
|
82
|
+
pluginState.awareness = undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function initAwareness(state: YPositionPluginState, editor: CoreEditor) {
|
|
86
|
+
if (!state.awareness) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const awareness = state.awareness;
|
|
90
|
+
const view = editor.view as EditorView;
|
|
91
|
+
|
|
92
|
+
state.awarenessListener = (
|
|
93
|
+
{ added, updated, removed },
|
|
94
|
+
) => {
|
|
95
|
+
const ystate: YSyncPluginState = ySyncPluginKey.getState(view.state)!;
|
|
96
|
+
if (!ystate.binding) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const yjs = ystate.binding.getYjs();
|
|
100
|
+
if (!yjs) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const clients = added.concat(updated).concat(removed);
|
|
105
|
+
if (
|
|
106
|
+
clients.findIndex((id: number) => id !== awareness.doc.clientID) ===
|
|
107
|
+
-1
|
|
108
|
+
) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const { ydoc, xmlFragment } = yjs;
|
|
113
|
+
const remoteStates: SelectionState[] = [];
|
|
114
|
+
awareness.getStates().forEach((aw, clientId) => {
|
|
115
|
+
if (!defaultAwarenessStateFilter(ydoc.clientID, clientId, aw)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const cursor = aw[state.cursorStateField];
|
|
120
|
+
const user: User | undefined = aw[state.userStateField];
|
|
121
|
+
|
|
122
|
+
if (!cursor || !user) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const anchor = relativePositionToAbsolutePosition(
|
|
127
|
+
ydoc,
|
|
128
|
+
xmlFragment,
|
|
129
|
+
Y.createRelativePositionFromJSON(cursor.anchor),
|
|
130
|
+
ystate.binding.getMapping(),
|
|
131
|
+
);
|
|
132
|
+
const head = relativePositionToAbsolutePosition(
|
|
133
|
+
ydoc,
|
|
134
|
+
xmlFragment,
|
|
135
|
+
Y.createRelativePositionFromJSON(cursor.head),
|
|
136
|
+
ystate.binding.getMapping(),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
if (anchor !== null && head !== null) {
|
|
140
|
+
remoteStates.push({
|
|
141
|
+
clientId,
|
|
142
|
+
user: user,
|
|
143
|
+
cursor: {
|
|
144
|
+
anchor,
|
|
145
|
+
head,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const tr = editor.state.tr;
|
|
152
|
+
tr.setMeta('remoteSelectionChange', { remoteStates });
|
|
153
|
+
editor.dispatchTransaction(tr);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
awareness.on('change', state.awarenessListener);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Default awareness state filter
|
|
161
|
+
*/
|
|
162
|
+
export const defaultAwarenessStateFilter = (
|
|
163
|
+
currentClientId: number,
|
|
164
|
+
userClientId: number,
|
|
165
|
+
_user: any,
|
|
166
|
+
): boolean => currentClientId !== userClientId;
|
|
167
|
+
|
|
168
|
+
export const yPositionPlugin = (
|
|
169
|
+
editor: CoreEditor,
|
|
170
|
+
{
|
|
171
|
+
getSelection = (state: EditorState) => state.selection,
|
|
172
|
+
}: PositionPluginConfig = {},
|
|
173
|
+
) => {
|
|
174
|
+
return new Plugin<YPositionPluginState>({
|
|
175
|
+
key: yPositionPluginKey,
|
|
176
|
+
state: {
|
|
177
|
+
init: (): YPositionPluginState => {
|
|
178
|
+
return {
|
|
179
|
+
awareness: undefined,
|
|
180
|
+
cursorStateField: 'kerebron:cursor',
|
|
181
|
+
userStateField: 'kerebron:user',
|
|
182
|
+
me: generateBlankUser(),
|
|
183
|
+
colorMapper: defaultColorMapper,
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
apply: (tr: Transaction, pluginState: YPositionPluginState) => {
|
|
187
|
+
const changeUser = tr.getMeta('changeUser');
|
|
188
|
+
if (changeUser) {
|
|
189
|
+
pluginState.me = { ...changeUser.user };
|
|
190
|
+
}
|
|
191
|
+
const setColorMapper = tr.getMeta('setColorMapper');
|
|
192
|
+
if (setColorMapper) {
|
|
193
|
+
pluginState.colorMapper = setColorMapper.colorMapper;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const awareness = tr.getMeta('yjs:setAwareness');
|
|
197
|
+
if (awareness) {
|
|
198
|
+
if (pluginState.awareness) {
|
|
199
|
+
destroyAwareness(pluginState);
|
|
200
|
+
}
|
|
201
|
+
pluginState.awareness = awareness;
|
|
202
|
+
if (pluginState.awareness) {
|
|
203
|
+
initAwareness(pluginState, editor);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (tr.getMeta('yjs:removeAwareness')) {
|
|
208
|
+
if (pluginState.awareness) {
|
|
209
|
+
destroyAwareness(pluginState);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return pluginState;
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
view: (view: EditorView) => {
|
|
216
|
+
const updateAwareness = (
|
|
217
|
+
selectionAnchor: number,
|
|
218
|
+
selectionHead: number,
|
|
219
|
+
) => {
|
|
220
|
+
const state: YPositionPluginState = yPositionPluginKey.getState(
|
|
221
|
+
view.state,
|
|
222
|
+
)!;
|
|
223
|
+
if (!state.awareness) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const awareness = state.awareness;
|
|
227
|
+
const current = awareness.getLocalState() || {};
|
|
228
|
+
|
|
229
|
+
const ystate: YSyncPluginState = ySyncPluginKey.getState(view.state)!;
|
|
230
|
+
const yjs = ystate.binding.getYjs();
|
|
231
|
+
if (!yjs) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const { xmlFragment } = yjs;
|
|
235
|
+
|
|
236
|
+
const anchor: Y.RelativePosition = absolutePositionToRelativePosition(
|
|
237
|
+
selectionAnchor,
|
|
238
|
+
xmlFragment,
|
|
239
|
+
ystate.binding.getMapping(),
|
|
240
|
+
);
|
|
241
|
+
const head: Y.RelativePosition = absolutePositionToRelativePosition(
|
|
242
|
+
selectionHead,
|
|
243
|
+
xmlFragment,
|
|
244
|
+
ystate.binding.getMapping(),
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
const cursor = current[state.cursorStateField];
|
|
248
|
+
|
|
249
|
+
if (
|
|
250
|
+
cursor == null ||
|
|
251
|
+
!Y.compareRelativePositions(
|
|
252
|
+
Y.createRelativePositionFromJSON(cursor.anchor),
|
|
253
|
+
anchor,
|
|
254
|
+
) ||
|
|
255
|
+
!Y.compareRelativePositions(
|
|
256
|
+
Y.createRelativePositionFromJSON(cursor.head),
|
|
257
|
+
head,
|
|
258
|
+
)
|
|
259
|
+
) {
|
|
260
|
+
awareness.setLocalStateField(state.cursorStateField, {
|
|
261
|
+
anchor,
|
|
262
|
+
head,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const clearAwareness = () => {
|
|
268
|
+
const state: YPositionPluginState = yPositionPluginKey.getState(
|
|
269
|
+
view.state,
|
|
270
|
+
)!;
|
|
271
|
+
if (!state.awareness) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const ystate: YSyncPluginState = ySyncPluginKey.getState(view.state)!;
|
|
276
|
+
const yjs = ystate.binding.getYjs();
|
|
277
|
+
if (!yjs) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const awareness = state.awareness;
|
|
282
|
+
const current = awareness.getLocalState() || {};
|
|
283
|
+
|
|
284
|
+
const { ydoc, xmlFragment } = yjs;
|
|
285
|
+
|
|
286
|
+
const cursor = current[state.cursorStateField];
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
cursor &&
|
|
290
|
+
relativePositionToAbsolutePosition(
|
|
291
|
+
ydoc,
|
|
292
|
+
xmlFragment,
|
|
293
|
+
Y.createRelativePositionFromJSON(cursor.anchor),
|
|
294
|
+
ystate.binding.getMapping(),
|
|
295
|
+
) !== null
|
|
296
|
+
) {
|
|
297
|
+
// delete cursor information if current cursor information is owned by this editor binding
|
|
298
|
+
awareness.setLocalStateField(state.cursorStateField, null);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const updateCursorInfo = () => {
|
|
303
|
+
if (view.hasFocus()) {
|
|
304
|
+
const selection = getSelection(view.state);
|
|
305
|
+
updateAwareness(selection.anchor, selection.head);
|
|
306
|
+
} else {
|
|
307
|
+
// clearAwareness();
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const localPositionChangedListener = (event: Event) => {
|
|
312
|
+
if ('detail' in event) {
|
|
313
|
+
const { detail } = event as CustomEvent;
|
|
314
|
+
updateAwareness(detail.anchor, detail.head);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
editor.addEventListener(
|
|
319
|
+
'localPositionChanged',
|
|
320
|
+
localPositionChangedListener,
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
view.dom.addEventListener('focusin', updateCursorInfo);
|
|
324
|
+
view.dom.addEventListener('focusout', updateCursorInfo);
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
update: updateCursorInfo,
|
|
328
|
+
destroy: () => {
|
|
329
|
+
view.dom.removeEventListener('focusin', updateCursorInfo);
|
|
330
|
+
view.dom.removeEventListener('focusout', updateCursorInfo);
|
|
331
|
+
|
|
332
|
+
const pluginState: YPositionPluginState | undefined =
|
|
333
|
+
yPositionPluginKey.getState(
|
|
334
|
+
view.state,
|
|
335
|
+
);
|
|
336
|
+
if (pluginState) {
|
|
337
|
+
destroyAwareness(pluginState);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
editor.removeEventListener(
|
|
341
|
+
'localPositionChanged',
|
|
342
|
+
localPositionChangedListener,
|
|
343
|
+
);
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
},
|
|
347
|
+
updateCursorInfo(state: EditorState) {
|
|
348
|
+
throw new Error('TODO: merge with updateCursorInfo above');
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
};
|