@dxos/react-ui-editor 0.3.11-main.870164f → 0.3.11-main.8d54a30
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/lib/browser/index.mjs +1405 -960
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/yjs → components/TextEditor}/yjs.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +3 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{hooks → extensions}/automerge/semaphore.d.ts +4 -4
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/extensions/awareness.d.ts +46 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -0
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +3 -0
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +3 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +8 -7
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +8 -8
- package/dist/types/src/hooks/yjs.d.ts.map +1 -0
- package/dist/types/src/styles/cursors.d.ts.map +1 -0
- package/dist/types/src/styles/index.d.ts +1 -0
- package/dist/types/src/styles/index.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts +2 -2
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/dist/types/src/util/cursor.d.ts +17 -0
- package/dist/types/src/util/cursor.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/package.json +22 -21
- package/src/components/TextEditor/TextEditor.tsx +1 -1
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/{hooks/automerge/plugin.ts → extensions/automerge/automerge.ts} +20 -58
- package/src/extensions/automerge/cursor.ts +51 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +6 -0
- package/src/{hooks → extensions}/automerge/semaphore.ts +14 -12
- package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
- package/src/extensions/awareness.ts +316 -0
- package/src/extensions/comments.ts +27 -11
- package/src/extensions/index.ts +3 -0
- package/src/extensions/yjs/cursor.ts +21 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +1 -25
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTextModel.ts +19 -93
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +71 -19
- package/src/styles/index.ts +1 -0
- package/src/testing/replicator.ts +10 -15
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -0
- package/dist/types/src/extensions/util.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
- package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/semaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/index.d.ts +0 -3
- package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/yjs/yjs.test.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/dist/types/src/{extensions → util}/util.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{extensions → util}/util.ts +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
// TODO(burdon): Additional copyright?
|
|
6
|
-
|
|
7
7
|
import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
|
|
8
8
|
import { type EditorView } from '@codemirror/view';
|
|
9
9
|
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
type SpliceTextPatch,
|
|
17
17
|
} from '@dxos/automerge/automerge';
|
|
18
18
|
|
|
19
|
-
import { reconcileAnnotationType } from './
|
|
19
|
+
import { reconcileAnnotationType } from './defs';
|
|
20
20
|
|
|
21
21
|
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
22
22
|
for (const patch of patches) {
|
|
@@ -56,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
56
56
|
if (index == null) {
|
|
57
57
|
return [];
|
|
58
58
|
}
|
|
59
|
+
|
|
59
60
|
const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
|
|
60
61
|
return [{ from: index, to: index, insert: text }];
|
|
61
62
|
};
|
|
@@ -65,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
|
|
|
65
66
|
if (index == null) {
|
|
66
67
|
return [];
|
|
67
68
|
}
|
|
69
|
+
|
|
68
70
|
return [{ from: index, insert: patch.value }];
|
|
69
71
|
};
|
|
70
72
|
|
|
@@ -73,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
|
|
|
73
75
|
if (index == null) {
|
|
74
76
|
return [];
|
|
75
77
|
}
|
|
78
|
+
|
|
76
79
|
const length = patch.length || 1;
|
|
77
80
|
return [{ from: index, to: index + length }];
|
|
78
81
|
};
|
|
@@ -82,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
82
85
|
if (index == null) {
|
|
83
86
|
return [];
|
|
84
87
|
}
|
|
88
|
+
|
|
85
89
|
const length = state.doc.length;
|
|
86
90
|
if (typeof patch.value !== 'string') {
|
|
87
91
|
return []; // TODO(dmaretskyi): How to handle non string values?
|
|
88
92
|
}
|
|
93
|
+
|
|
89
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
90
95
|
};
|
|
91
96
|
|
|
92
|
-
// If the path of the patch is of the form [path, <index>] then we know this is
|
|
93
|
-
//
|
|
97
|
+
// If the path of the patch is of the form [path, <index>] then we know this is a path to a character
|
|
98
|
+
// within the sequence given by path.
|
|
94
99
|
const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
|
|
95
100
|
if (candidatePath.length !== textPath.length + 1) {
|
|
96
101
|
return null;
|
|
97
102
|
}
|
|
103
|
+
|
|
98
104
|
for (let i = 0; i < textPath.length; i++) {
|
|
99
105
|
if (textPath[i] !== candidatePath[i]) {
|
|
100
106
|
return null;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
109
|
+
|
|
103
110
|
const index = candidatePath[candidatePath.length - 1];
|
|
104
111
|
if (typeof index === 'number') {
|
|
105
112
|
return index;
|
|
106
113
|
}
|
|
114
|
+
|
|
107
115
|
return null;
|
|
108
116
|
};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Annotation, Facet, type Extension, RangeSet, type Range } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
Decoration,
|
|
8
|
+
type DecorationSet,
|
|
9
|
+
EditorView,
|
|
10
|
+
type PluginValue,
|
|
11
|
+
ViewPlugin,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
} from '@codemirror/view';
|
|
15
|
+
|
|
16
|
+
import { Event } from '@dxos/async';
|
|
17
|
+
import { Context } from '@dxos/context';
|
|
18
|
+
|
|
19
|
+
import { CursorConverter } from '../util';
|
|
20
|
+
|
|
21
|
+
export interface AwarenessProvider {
|
|
22
|
+
remoteStateChange: Event<void>;
|
|
23
|
+
|
|
24
|
+
open(): void;
|
|
25
|
+
close(): void;
|
|
26
|
+
|
|
27
|
+
update(position: AwarenessPosition | undefined): void;
|
|
28
|
+
getRemoteStates(): AwarenessState[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const dummyProvider: AwarenessProvider = {
|
|
32
|
+
remoteStateChange: new Event(),
|
|
33
|
+
|
|
34
|
+
open: () => {},
|
|
35
|
+
close: () => {},
|
|
36
|
+
|
|
37
|
+
update: () => {},
|
|
38
|
+
getRemoteStates: () => [],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const AwarenessProvider = Facet.define<AwarenessProvider, AwarenessProvider>({
|
|
42
|
+
combine: (providers) => providers[0] ?? dummyProvider,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// TODO(dmaretskyi): Specify the users that actually changed. Currently, we recalculate positions for every user.
|
|
46
|
+
const RemoteSelectionChangedAnnotation = Annotation.define();
|
|
47
|
+
|
|
48
|
+
export type AwarenessPosition = {
|
|
49
|
+
anchor?: string;
|
|
50
|
+
head?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type AwarenessInfo = {
|
|
54
|
+
displayName?: string;
|
|
55
|
+
// TODO(burdon): Rename light/dark.
|
|
56
|
+
color?: string;
|
|
57
|
+
lightColor?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type AwarenessState = {
|
|
61
|
+
position?: AwarenessPosition;
|
|
62
|
+
peerId: string;
|
|
63
|
+
info: AwarenessInfo;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extension provides presence information about other peers.
|
|
68
|
+
*/
|
|
69
|
+
// TODO(burdon): Why provide default?
|
|
70
|
+
export const awareness = (provider = dummyProvider): Extension => {
|
|
71
|
+
return [
|
|
72
|
+
AwarenessProvider.of(provider),
|
|
73
|
+
ViewPlugin.fromClass(RemoteSelectionsDecorator, {
|
|
74
|
+
decorations: (value) => value.decorations,
|
|
75
|
+
}),
|
|
76
|
+
styles,
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Generates selection decorations from remote peers.
|
|
82
|
+
*/
|
|
83
|
+
export class RemoteSelectionsDecorator implements PluginValue {
|
|
84
|
+
public decorations: DecorationSet = RangeSet.of([]);
|
|
85
|
+
|
|
86
|
+
private readonly _ctx = new Context();
|
|
87
|
+
|
|
88
|
+
private _cursorConverter: CursorConverter;
|
|
89
|
+
private _provider: AwarenessProvider;
|
|
90
|
+
private _lastAnchor?: number = undefined;
|
|
91
|
+
private _lastHead?: number = undefined;
|
|
92
|
+
|
|
93
|
+
constructor(view: EditorView) {
|
|
94
|
+
this._cursorConverter = view.state.facet(CursorConverter);
|
|
95
|
+
this._provider = view.state.facet(AwarenessProvider);
|
|
96
|
+
this._provider.open();
|
|
97
|
+
this._provider.remoteStateChange.on(this._ctx, () => {
|
|
98
|
+
view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
destroy() {
|
|
103
|
+
void this._ctx.dispose();
|
|
104
|
+
this._provider.close();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
update(update: ViewUpdate) {
|
|
108
|
+
this._updateLocalSelection(update);
|
|
109
|
+
this._updateRemoteSelections(update);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private _updateLocalSelection(update: ViewUpdate) {
|
|
113
|
+
const hasFocus = update.view.hasFocus && update.view.dom.ownerDocument.hasFocus();
|
|
114
|
+
const { anchor = undefined, head = undefined } = hasFocus ? update.state.selection.main : {};
|
|
115
|
+
if (this._lastAnchor === anchor && this._lastHead === head) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this._lastAnchor = anchor;
|
|
120
|
+
this._lastHead = head;
|
|
121
|
+
|
|
122
|
+
this._provider.update(
|
|
123
|
+
anchor !== undefined && head !== undefined
|
|
124
|
+
? {
|
|
125
|
+
anchor: this._cursorConverter.toCursor(anchor),
|
|
126
|
+
head: this._cursorConverter.toCursor(head),
|
|
127
|
+
}
|
|
128
|
+
: undefined,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private _updateRemoteSelections(update: ViewUpdate) {
|
|
133
|
+
const decorations: Range<Decoration>[] = [];
|
|
134
|
+
const awarenessStates = this._provider.getRemoteStates();
|
|
135
|
+
for (const state of awarenessStates) {
|
|
136
|
+
const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
|
|
137
|
+
const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
|
|
138
|
+
if (anchor == null || head == null) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const start = Math.min(Math.min(anchor, head), update.view.state.doc.length);
|
|
143
|
+
const end = Math.min(Math.max(anchor, head), update.view.state.doc.length);
|
|
144
|
+
|
|
145
|
+
const startLine = update.view.state.doc.lineAt(start);
|
|
146
|
+
const endLine = update.view.state.doc.lineAt(end);
|
|
147
|
+
|
|
148
|
+
// TODO(burdon): Factor out styles.
|
|
149
|
+
const color = state.info.color ?? '#30bced';
|
|
150
|
+
const lightColor = state.info.lightColor ?? color + '33';
|
|
151
|
+
|
|
152
|
+
if (startLine.number === endLine.number) {
|
|
153
|
+
// Selected content in a single line.
|
|
154
|
+
decorations.push({
|
|
155
|
+
from: start,
|
|
156
|
+
to: end,
|
|
157
|
+
value: Decoration.mark({
|
|
158
|
+
attributes: { style: `background-color: ${lightColor}` },
|
|
159
|
+
class: 'cm-ySelection',
|
|
160
|
+
}),
|
|
161
|
+
});
|
|
162
|
+
} else {
|
|
163
|
+
// Selected content in multiple lines; first, render text-selection in the first line.
|
|
164
|
+
decorations.push({
|
|
165
|
+
from: start,
|
|
166
|
+
to: startLine.from + startLine.length,
|
|
167
|
+
value: Decoration.mark({
|
|
168
|
+
attributes: { style: `background-color: ${color}` },
|
|
169
|
+
class: 'cm-ySelection',
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Render text-selection in the last line.
|
|
174
|
+
decorations.push({
|
|
175
|
+
from: endLine.from,
|
|
176
|
+
to: end,
|
|
177
|
+
value: Decoration.mark({
|
|
178
|
+
attributes: { style: `background-color: ${color}` },
|
|
179
|
+
class: 'cm-ySelection',
|
|
180
|
+
}),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
for (let i = startLine.number + 1; i < endLine.number; i++) {
|
|
184
|
+
const linePos = update.view.state.doc.line(i).from;
|
|
185
|
+
decorations.push({
|
|
186
|
+
from: linePos,
|
|
187
|
+
to: linePos,
|
|
188
|
+
value: Decoration.line({
|
|
189
|
+
attributes: { style: `background-color: ${color}`, class: 'cm-yLineSelection' },
|
|
190
|
+
}),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
decorations.push({
|
|
196
|
+
from: head,
|
|
197
|
+
to: head,
|
|
198
|
+
value: Decoration.widget({
|
|
199
|
+
side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
|
|
200
|
+
block: false,
|
|
201
|
+
widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', color),
|
|
202
|
+
}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.decorations = Decoration.set(decorations, true);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class RemoteCaretWidget extends WidgetType {
|
|
211
|
+
constructor(public name: string, public color: string) {
|
|
212
|
+
super();
|
|
213
|
+
this.name = name;
|
|
214
|
+
this.color = color;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
toDOM(): HTMLElement {
|
|
218
|
+
const span = document.createElement('span');
|
|
219
|
+
span.className = 'cm-ySelectionCaret';
|
|
220
|
+
span.style.backgroundColor = this.color;
|
|
221
|
+
span.style.borderColor = this.color;
|
|
222
|
+
|
|
223
|
+
const dot = document.createElement('div');
|
|
224
|
+
dot.className = 'cm-ySelectionCaretDot';
|
|
225
|
+
|
|
226
|
+
const info = document.createElement('div');
|
|
227
|
+
info.className = 'cm-ySelectionInfo';
|
|
228
|
+
info.innerText = this.name;
|
|
229
|
+
|
|
230
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
231
|
+
span.appendChild(dot);
|
|
232
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
233
|
+
span.appendChild(info);
|
|
234
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
235
|
+
|
|
236
|
+
return span;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
override eq(widget: this) {
|
|
240
|
+
return widget.color === this.color;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
compare(widget: this) {
|
|
244
|
+
return widget.color === this.color;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
override updateDOM() {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override get estimatedHeight() {
|
|
252
|
+
return -1;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
override ignoreEvent() {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// TODO(burdon): Rename prefix (y for yjs?)
|
|
261
|
+
const styles = EditorView.baseTheme({
|
|
262
|
+
'.cm-ySelection': {},
|
|
263
|
+
'.cm-yLineSelection': {
|
|
264
|
+
padding: 0,
|
|
265
|
+
margin: '0px 2px 0px 4px',
|
|
266
|
+
},
|
|
267
|
+
'.cm-ySelectionCaret': {
|
|
268
|
+
position: 'relative',
|
|
269
|
+
borderLeft: '1px solid black',
|
|
270
|
+
borderRight: '1px solid black',
|
|
271
|
+
marginLeft: '-1px',
|
|
272
|
+
marginRight: '-1px',
|
|
273
|
+
boxSizing: 'border-box',
|
|
274
|
+
display: 'inline',
|
|
275
|
+
},
|
|
276
|
+
'.cm-ySelectionCaretDot': {
|
|
277
|
+
borderRadius: '50%',
|
|
278
|
+
position: 'absolute',
|
|
279
|
+
width: '.4em',
|
|
280
|
+
height: '.4em',
|
|
281
|
+
top: '-.2em',
|
|
282
|
+
left: '-.2em',
|
|
283
|
+
backgroundColor: 'inherit',
|
|
284
|
+
transition: 'transform .3s ease-in-out',
|
|
285
|
+
boxSizing: 'border-box',
|
|
286
|
+
},
|
|
287
|
+
'.cm-ySelectionCaret:hover > .cm-ySelectionCaretDot': {
|
|
288
|
+
transformOrigin: 'bottom center',
|
|
289
|
+
transform: 'scale(0)',
|
|
290
|
+
},
|
|
291
|
+
'.cm-ySelectionInfo': {
|
|
292
|
+
position: 'absolute',
|
|
293
|
+
top: '-1.05em',
|
|
294
|
+
left: '-1px',
|
|
295
|
+
fontSize: '.75em',
|
|
296
|
+
fontFamily: 'serif',
|
|
297
|
+
fontStyle: 'normal',
|
|
298
|
+
fontWeight: 'normal',
|
|
299
|
+
lineHeight: 'normal',
|
|
300
|
+
userSelect: 'none',
|
|
301
|
+
color: 'white',
|
|
302
|
+
paddingLeft: '2px',
|
|
303
|
+
paddingRight: '2px',
|
|
304
|
+
zIndex: 101,
|
|
305
|
+
transition: 'opacity .3s ease-in-out',
|
|
306
|
+
backgroundColor: 'inherit',
|
|
307
|
+
// these should be separate
|
|
308
|
+
opacity: 0,
|
|
309
|
+
transitionDelay: '0s',
|
|
310
|
+
whiteSpace: 'nowrap',
|
|
311
|
+
},
|
|
312
|
+
'.cm-ySelectionCaret:hover > .cm-ySelectionInfo': {
|
|
313
|
+
opacity: 1,
|
|
314
|
+
transitionDelay: '0s',
|
|
315
|
+
},
|
|
316
|
+
});
|
|
@@ -24,9 +24,9 @@ import { invariant } from '@dxos/invariant';
|
|
|
24
24
|
import { log } from '@dxos/log';
|
|
25
25
|
import { nonNullable } from '@dxos/util';
|
|
26
26
|
|
|
27
|
-
import {
|
|
28
|
-
import { type CommentRange, type EditorModel, modelState, type Range } from '../hooks';
|
|
27
|
+
import { type CommentRange, type EditorModel, type Range } from '../hooks';
|
|
29
28
|
import { tokens } from '../styles';
|
|
29
|
+
import { callbackWrapper, CursorConverter } from '../util';
|
|
30
30
|
|
|
31
31
|
// TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
|
|
32
32
|
// TODO(burdon): Consider breaking into separate plugin (since not standalone)? Like mermaid?
|
|
@@ -99,6 +99,8 @@ const setCommentState = StateEffect.define<CommentsState>();
|
|
|
99
99
|
const commentsStateField = StateField.define<CommentsState>({
|
|
100
100
|
create: () => ({ ranges: [] }),
|
|
101
101
|
update: (value, tr) => {
|
|
102
|
+
const cursorConverter = tr.state.facet(CursorConverter);
|
|
103
|
+
|
|
102
104
|
for (const effect of tr.effects) {
|
|
103
105
|
// Update selection.
|
|
104
106
|
if (effect.is(setSelection)) {
|
|
@@ -107,10 +109,10 @@ const commentsStateField = StateField.define<CommentsState>({
|
|
|
107
109
|
|
|
108
110
|
// Update range from store.
|
|
109
111
|
if (effect.is(setCommentRange)) {
|
|
110
|
-
const {
|
|
112
|
+
const { comments } = effect.value;
|
|
111
113
|
const ranges: ExtendedCommentRange[] = comments
|
|
112
114
|
.map((comment) => {
|
|
113
|
-
const range =
|
|
115
|
+
const range = getRangeFromCursor(cursorConverter, comment.cursor);
|
|
114
116
|
return range && { ...comment, ...range };
|
|
115
117
|
})
|
|
116
118
|
.filter(nonNullable);
|
|
@@ -218,14 +220,15 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
218
220
|
* Create comment thread action.
|
|
219
221
|
*/
|
|
220
222
|
const createCommentThread: Command = (view) => {
|
|
223
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
224
|
+
|
|
221
225
|
invariant(options.onCreate);
|
|
222
226
|
const { head, from, to } = view.state.selection.main;
|
|
223
227
|
if (from === to) {
|
|
224
228
|
return false;
|
|
225
229
|
}
|
|
226
230
|
|
|
227
|
-
const
|
|
228
|
-
const relPos = model?.getCursorFromRange?.({ from, to });
|
|
231
|
+
const relPos = getCursorFromRange(cursorConverter, { from, to });
|
|
229
232
|
if (relPos) {
|
|
230
233
|
// Create thread via callback.
|
|
231
234
|
const id = callbackWrapper(options.onCreate)(relPos);
|
|
@@ -338,19 +341,19 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
338
341
|
// Monitor cursor movement and text updates.
|
|
339
342
|
// TODO(burdon): Is there a better (finer grained) way to do this?
|
|
340
343
|
//
|
|
341
|
-
EditorView.updateListener.of(({ view, state,
|
|
344
|
+
EditorView.updateListener.of(({ view, state, changes }) => {
|
|
345
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
346
|
+
|
|
342
347
|
//
|
|
343
348
|
// Test if need to recompute indexed range if document changes before the end of the range.
|
|
344
349
|
//
|
|
345
350
|
{
|
|
346
351
|
let mod = false;
|
|
347
|
-
const model = state.field(modelState);
|
|
348
352
|
const { active, ranges } = state.field(commentsStateField);
|
|
349
353
|
changes.iterChanges((from, to, from2, to2) => {
|
|
350
|
-
invariant(model);
|
|
351
354
|
ranges.forEach((range) => {
|
|
352
355
|
if (from2 === to2) {
|
|
353
|
-
const newRange =
|
|
356
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
354
357
|
if (!newRange || newRange.to - newRange.from === 0) {
|
|
355
358
|
// TODO(burdon): Delete range if empty.
|
|
356
359
|
log.info('deleted comment', { thread: range.id });
|
|
@@ -358,7 +361,7 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
358
361
|
}
|
|
359
362
|
|
|
360
363
|
if (from <= range.to) {
|
|
361
|
-
const newRange =
|
|
364
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
362
365
|
Object.assign(range, newRange);
|
|
363
366
|
mod = true;
|
|
364
367
|
}
|
|
@@ -403,3 +406,16 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
403
406
|
}),
|
|
404
407
|
];
|
|
405
408
|
};
|
|
409
|
+
|
|
410
|
+
const getCursorFromRange = (cursorConverter: CursorConverter, range: Range) => {
|
|
411
|
+
const from = cursorConverter.toCursor(range.from);
|
|
412
|
+
const to = cursorConverter.toCursor(range.to, -1);
|
|
413
|
+
return [from, to].join(':');
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) => {
|
|
417
|
+
const parts = cursor.split(':');
|
|
418
|
+
const from = cursorConverter.fromCursor(parts[0]);
|
|
419
|
+
const to = cursorConverter.fromCursor(parts[1]);
|
|
420
|
+
return from && to ? { from, to } : undefined;
|
|
421
|
+
};
|
package/src/extensions/index.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './autocomplete';
|
|
6
|
+
export * from './automerge';
|
|
7
|
+
export * from './awareness';
|
|
6
8
|
export * from './basic';
|
|
7
9
|
export * from './blast';
|
|
8
10
|
export * from './code';
|
|
@@ -17,3 +19,4 @@ export * from './mermaid';
|
|
|
17
19
|
export * from './table';
|
|
18
20
|
export * from './tasklist';
|
|
19
21
|
export * from './typewriter';
|
|
22
|
+
export * from './yjs';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Y from 'yjs';
|
|
6
|
+
|
|
7
|
+
import { arrayToString, stringToArray } from '@dxos/util';
|
|
8
|
+
|
|
9
|
+
import { type CursorConverter } from '../../util';
|
|
10
|
+
|
|
11
|
+
export const cursorConverter = (text: Y.Text): CursorConverter => ({
|
|
12
|
+
toCursor: (index, assoc) => {
|
|
13
|
+
return arrayToString(Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text, index, assoc)));
|
|
14
|
+
},
|
|
15
|
+
fromCursor: (cursor) => {
|
|
16
|
+
return (
|
|
17
|
+
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
18
|
+
0
|
|
19
|
+
);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
@@ -2,13 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { effect } from '@preact/signals-react';
|
|
6
5
|
import { expect } from 'chai';
|
|
7
6
|
import * as Y from 'yjs';
|
|
8
7
|
|
|
9
|
-
import {
|
|
10
|
-
import { Expando, setGlobalAutomergePreference, TextObject } from '@dxos/echo-schema';
|
|
11
|
-
import { registerSignalFactory } from '@dxos/echo-signals';
|
|
8
|
+
import { TextObject } from '@dxos/echo-schema';
|
|
12
9
|
import { describe, test } from '@dxos/test';
|
|
13
10
|
import { type YText } from '@dxos/text-model';
|
|
14
11
|
|
|
@@ -35,25 +32,4 @@ describe('YJS', () => {
|
|
|
35
32
|
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
36
33
|
}
|
|
37
34
|
});
|
|
38
|
-
|
|
39
|
-
// TODO(burdon): Test deleting end character.
|
|
40
|
-
|
|
41
|
-
test.skip('effect', async () => {
|
|
42
|
-
setGlobalAutomergePreference(true);
|
|
43
|
-
registerSignalFactory();
|
|
44
|
-
const obj = new Expando({ comments: [] });
|
|
45
|
-
|
|
46
|
-
const trigger = new Trigger();
|
|
47
|
-
const unsubscribe = effect(() => {
|
|
48
|
-
// TODO(burdon): BUG: doesn't work with YJS.
|
|
49
|
-
console.log(obj.comments);
|
|
50
|
-
if (obj.comments.length > 0) {
|
|
51
|
-
trigger.wake();
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
obj.comments.push({ message: 'hello' });
|
|
56
|
-
await trigger.wait();
|
|
57
|
-
unsubscribe();
|
|
58
|
-
});
|
|
59
35
|
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { yCollab } from 'y-codemirror.next';
|
|
6
|
+
import type * as YP from 'y-protocols/awareness';
|
|
7
|
+
|
|
8
|
+
import type { YText } from '@dxos/text-model';
|
|
9
|
+
|
|
10
|
+
import { cursorConverter } from './cursor';
|
|
11
|
+
import { CursorConverter } from '../../util';
|
|
12
|
+
|
|
13
|
+
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
+
CursorConverter.of(cursorConverter(content)),
|
|
15
|
+
yCollab(content, awareness),
|
|
16
|
+
];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import get from 'lodash.get';
|
|
6
|
+
|
|
7
|
+
import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
8
|
+
import { isNotNullOrUndefined } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
11
|
+
import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
|
|
12
|
+
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
13
|
+
import { cursorColor } from '../styles';
|
|
14
|
+
|
|
15
|
+
export const createAutomergeModel = ({ space, identity, text }: UseTextModelProps): EditorModel => {
|
|
16
|
+
const obj = text as any as AutomergeTextCompat;
|
|
17
|
+
const doc = getRawDoc(obj, [obj.field]);
|
|
18
|
+
|
|
19
|
+
const awarenessProvider =
|
|
20
|
+
space &&
|
|
21
|
+
new SpaceAwarenessProvider({
|
|
22
|
+
space,
|
|
23
|
+
channel: `automerge.awareness.${obj.id}`,
|
|
24
|
+
info: {
|
|
25
|
+
displayName: identity?.profile?.displayName ?? '',
|
|
26
|
+
color: cursorColor.color,
|
|
27
|
+
lightColor: cursorColor.light,
|
|
28
|
+
},
|
|
29
|
+
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const model: EditorModel = {
|
|
33
|
+
id: obj.id,
|
|
34
|
+
content: doc,
|
|
35
|
+
text: () => get(doc.handle.docSync(), doc.path),
|
|
36
|
+
// TODO(burdon): https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
37
|
+
extension: [
|
|
38
|
+
modelState.init(() => model),
|
|
39
|
+
automerge({ handle: doc.handle, path: doc.path }),
|
|
40
|
+
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
41
|
+
awareness(),
|
|
42
|
+
].filter(isNotNullOrUndefined),
|
|
43
|
+
peer: identity
|
|
44
|
+
? {
|
|
45
|
+
id: identity.identityKey.toHex(),
|
|
46
|
+
name: identity.profile?.displayName,
|
|
47
|
+
}
|
|
48
|
+
: undefined,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return model;
|
|
52
|
+
};
|