@dxos/react-ui-editor 0.3.11-main.8be6c8a → 0.3.11-main.8ef1cc6
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 +306 -402
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +3 -0
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +4 -9
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +4 -4
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +4 -4
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +0 -1
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +4 -9
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +14 -5
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/{util → extensions}/cursor.d.ts +9 -1
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +2 -0
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts +5 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +1 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +0 -5
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/yjs.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +25 -24
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +28 -26
- package/src/components/TextEditor/TextEditor.stories.tsx +10 -43
- package/src/components/TextEditor/TextEditor.tsx +31 -41
- package/src/components/TextEditor/comments.stories.tsx +355 -0
- package/src/extensions/automerge/automerge.ts +12 -14
- package/src/extensions/automerge/cursor.ts +2 -1
- package/src/extensions/automerge/defs.ts +13 -9
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +10 -12
- package/src/extensions/awareness.ts +27 -33
- package/src/extensions/code.ts +24 -18
- package/src/extensions/comments.ts +128 -140
- package/src/extensions/cursor.ts +46 -0
- package/src/extensions/hr.ts +8 -9
- package/src/extensions/image.ts +2 -2
- package/src/extensions/index.ts +2 -0
- package/src/extensions/link.ts +7 -7
- package/src/extensions/markdown/highlight.ts +0 -8
- package/src/extensions/outliner.ts +12 -0
- package/src/extensions/table.ts +2 -2
- package/src/extensions/tasklist.ts +54 -77
- package/src/extensions/yjs/cursor.ts +2 -1
- package/src/extensions/yjs/yjs.test.ts +1 -1
- package/src/extensions/yjs/yjs.ts +2 -2
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useTextModel.ts +3 -6
- package/src/hooks/yjs.ts +1 -23
- package/src/styles/markdown.ts +0 -2
- package/src/themes/default.ts +17 -50
- package/dist/types/src/util/cursor.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -3
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- /package/dist/types/src/{util/util.d.ts → util.d.ts} +0 -0
- /package/src/{util/util.ts → util.ts} +0 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
7
|
+
import { type EditorView, keymap } from '@codemirror/view';
|
|
8
|
+
import { faker } from '@faker-js/faker';
|
|
9
|
+
import { Check, Trash } from '@phosphor-icons/react';
|
|
10
|
+
import React, { type FC, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
|
+
|
|
12
|
+
import { getTextContent, TextObject } from '@dxos/echo-schema';
|
|
13
|
+
import { PublicKey } from '@dxos/keys';
|
|
14
|
+
import { log } from '@dxos/log';
|
|
15
|
+
import { Button, DensityProvider } from '@dxos/react-ui';
|
|
16
|
+
import { fixedInsetFlexLayout, mx } from '@dxos/react-ui-theme';
|
|
17
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
18
|
+
|
|
19
|
+
import { MarkdownEditor, TextEditor } from './TextEditor';
|
|
20
|
+
import { comments, type CommentsOptions, Cursor } from '../../extensions';
|
|
21
|
+
import { type CommentRange, type Range, useTextModel } from '../../hooks';
|
|
22
|
+
|
|
23
|
+
faker.seed(101);
|
|
24
|
+
|
|
25
|
+
//
|
|
26
|
+
// Editor
|
|
27
|
+
//
|
|
28
|
+
|
|
29
|
+
const Editor: FC<{
|
|
30
|
+
item: { text: TextObject };
|
|
31
|
+
commentSelected?: string;
|
|
32
|
+
commentRanges: CommentRange[];
|
|
33
|
+
onCreateComment: CommentsOptions['onCreate'];
|
|
34
|
+
onDeleteComment: CommentsOptions['onDelete'];
|
|
35
|
+
onUpdateComment: CommentsOptions['onUpdate'];
|
|
36
|
+
onSelectComment: CommentsOptions['onSelect'];
|
|
37
|
+
}> = ({ item, commentSelected, commentRanges, onCreateComment, onDeleteComment, onUpdateComment, onSelectComment }) => {
|
|
38
|
+
const model = useTextModel({ text: item.text });
|
|
39
|
+
const editorRef = useRef<EditorView>(null);
|
|
40
|
+
const [selected, setSelected] = useState<string>();
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!editorRef.current?.hasFocus && commentSelected !== selected) {
|
|
43
|
+
const thread = commentRanges.find((range) => range.id === commentSelected);
|
|
44
|
+
if (thread) {
|
|
45
|
+
const { cursor } = thread;
|
|
46
|
+
const range = Cursor.getRangeFromCursor(editorRef.current!.state.facet(Cursor.converter), cursor);
|
|
47
|
+
if (range) {
|
|
48
|
+
// TODO(burdon): Scroll selection to center of screen?
|
|
49
|
+
editorRef.current?.dispatch({ selection: { anchor: range.from }, scrollIntoView: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setSelected(commentSelected);
|
|
54
|
+
}
|
|
55
|
+
}, [selected, commentRanges, commentSelected]);
|
|
56
|
+
|
|
57
|
+
const extensions = useMemo(() => {
|
|
58
|
+
return [
|
|
59
|
+
comments({
|
|
60
|
+
onCreate: onCreateComment,
|
|
61
|
+
onDelete: onDeleteComment,
|
|
62
|
+
onUpdate: onUpdateComment,
|
|
63
|
+
onSelect: onSelectComment,
|
|
64
|
+
}),
|
|
65
|
+
];
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
if (!model) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// TODO(burdon): Highlight currently selected comment.
|
|
73
|
+
return (
|
|
74
|
+
<div className='flex grow overflow-y-scroll'>
|
|
75
|
+
<MarkdownEditor ref={editorRef} model={model} comments={commentRanges} extensions={extensions} />
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
//
|
|
81
|
+
// Comments thread
|
|
82
|
+
//
|
|
83
|
+
|
|
84
|
+
type CommentThread = {
|
|
85
|
+
id: string;
|
|
86
|
+
range: CommentRange;
|
|
87
|
+
yPos?: number;
|
|
88
|
+
selection?: Range;
|
|
89
|
+
messages: TextObject[];
|
|
90
|
+
deleted?: boolean;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const Thread: FC<{
|
|
94
|
+
thread: CommentThread;
|
|
95
|
+
selected: boolean;
|
|
96
|
+
onSelect: () => void;
|
|
97
|
+
onResolve: () => void;
|
|
98
|
+
}> = ({ thread, selected, onSelect, onResolve }) => {
|
|
99
|
+
const [focus, setFocus] = useState(false);
|
|
100
|
+
const [item, setItem] = useState({ text: new TextObject() });
|
|
101
|
+
const model = useTextModel({ text: item.text });
|
|
102
|
+
const editorRef = useRef<EditorView>(null);
|
|
103
|
+
|
|
104
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (selected) {
|
|
107
|
+
containerRef.current?.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
|
108
|
+
if (thread.messages.length === 0) {
|
|
109
|
+
setFocus(true);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}, [selected]);
|
|
113
|
+
|
|
114
|
+
const handleCreateMessage = () => {
|
|
115
|
+
const text = model?.text().trim();
|
|
116
|
+
if (text?.length) {
|
|
117
|
+
thread.messages.push(item.text);
|
|
118
|
+
setItem({ text: new TextObject() });
|
|
119
|
+
setFocus(true);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
if (!model) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<div
|
|
129
|
+
className={mx(
|
|
130
|
+
'flex flex-col m-1 rounded shadow divide-y bg-white',
|
|
131
|
+
selected && 'ring',
|
|
132
|
+
thread.deleted && 'opacity-50',
|
|
133
|
+
)}
|
|
134
|
+
>
|
|
135
|
+
<div className='flex p-2 gap-2 items-center text-xs font-mono text-neutral-500 font-thin'>
|
|
136
|
+
<span>id:{thread.id.slice(0, 4)}</span>
|
|
137
|
+
<span>from:{thread.selection?.from}</span>
|
|
138
|
+
<span>to:{thread.selection?.to}</span>
|
|
139
|
+
<span>y:{thread.yPos}</span>
|
|
140
|
+
<span className='grow' />
|
|
141
|
+
{thread.deleted && <Trash />}
|
|
142
|
+
</div>
|
|
143
|
+
|
|
144
|
+
{thread.messages.map((message, i) => (
|
|
145
|
+
// TODO(burdon): Fix default editor padding so content doesn't jump on creating message.
|
|
146
|
+
<div key={i} className='p-2' onClick={() => onSelect()}>
|
|
147
|
+
{getTextContent(message)}
|
|
148
|
+
</div>
|
|
149
|
+
))}
|
|
150
|
+
|
|
151
|
+
<div ref={containerRef} onClick={() => onSelect()} className='flex'>
|
|
152
|
+
<TextEditor
|
|
153
|
+
ref={editorRef}
|
|
154
|
+
autofocus={focus}
|
|
155
|
+
model={model}
|
|
156
|
+
placeholder={'Enter comment...'}
|
|
157
|
+
slots={{ root: { className: 'grow p-2 rounded-b' } }}
|
|
158
|
+
extensions={[
|
|
159
|
+
keymap.of([
|
|
160
|
+
{
|
|
161
|
+
key: 'Enter',
|
|
162
|
+
run: () => {
|
|
163
|
+
handleCreateMessage();
|
|
164
|
+
return true;
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
]),
|
|
168
|
+
]}
|
|
169
|
+
/>
|
|
170
|
+
<Button variant='ghost' classNames='px-1' title='Resolve' onClick={onResolve}>
|
|
171
|
+
<Check />
|
|
172
|
+
</Button>
|
|
173
|
+
</div>
|
|
174
|
+
</div>
|
|
175
|
+
);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const Sidebar: FC<{
|
|
179
|
+
threads: CommentThread[];
|
|
180
|
+
selected?: string;
|
|
181
|
+
onSelect: (thread: string) => void;
|
|
182
|
+
onResolve: (thread: string) => void;
|
|
183
|
+
}> = ({ threads, selected, onSelect, onResolve }) => {
|
|
184
|
+
// Sort by y-position.
|
|
185
|
+
const sortedThreads = useMemo(() => {
|
|
186
|
+
const sorted = [...threads];
|
|
187
|
+
return sorted.sort(({ yPos: a = Infinity }, { yPos: b = Infinity }) => {
|
|
188
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
189
|
+
});
|
|
190
|
+
}, [threads]);
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<DensityProvider density='fine'>
|
|
194
|
+
<div className='flex flex-col grow overflow-y-scroll py-4 gap-4'>
|
|
195
|
+
{sortedThreads.map((thread) => (
|
|
196
|
+
<Thread
|
|
197
|
+
key={thread.id}
|
|
198
|
+
thread={thread}
|
|
199
|
+
selected={thread.id === selected}
|
|
200
|
+
onSelect={() => onSelect(thread.id)}
|
|
201
|
+
onResolve={() => onResolve(thread.id)}
|
|
202
|
+
/>
|
|
203
|
+
))}
|
|
204
|
+
</div>
|
|
205
|
+
</DensityProvider>
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
//
|
|
210
|
+
// Story container.
|
|
211
|
+
//
|
|
212
|
+
|
|
213
|
+
type StoryProps = {
|
|
214
|
+
text?: string;
|
|
215
|
+
autoCreate?: boolean;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const Story = ({ text, autoCreate }: StoryProps) => {
|
|
219
|
+
const [item] = useState({ text: new TextObject(text) });
|
|
220
|
+
const [threads, setThreads] = useState<CommentThread[]>([]);
|
|
221
|
+
const commentRanges = useMemo(() => threads.map((thread) => thread.range), [threads]);
|
|
222
|
+
const [selected, setSelected] = useState<string>();
|
|
223
|
+
|
|
224
|
+
// Filter by visibility.
|
|
225
|
+
const visibleThreads = useMemo(() => threads.filter((thread) => thread.yPos !== undefined), [threads]);
|
|
226
|
+
|
|
227
|
+
const handleCreateComment: CommentsOptions['onCreate'] = (cursor, location) => {
|
|
228
|
+
const id = PublicKey.random().toHex();
|
|
229
|
+
log.info('create', { id: id.slice(0, 4), cursor });
|
|
230
|
+
setThreads((threads) => [
|
|
231
|
+
...threads,
|
|
232
|
+
{
|
|
233
|
+
id,
|
|
234
|
+
range: { id, cursor },
|
|
235
|
+
yPos: location ? Math.floor(location.top) : undefined,
|
|
236
|
+
messages: autoCreate
|
|
237
|
+
? faker.helpers.multiple(() => new TextObject(faker.lorem.sentence()), { count: { min: 1, max: 3 } })
|
|
238
|
+
: [],
|
|
239
|
+
},
|
|
240
|
+
]);
|
|
241
|
+
setSelected(id);
|
|
242
|
+
return id;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const handleDeleteComment: CommentsOptions['onDelete'] = (id) => {
|
|
246
|
+
log.info('delete', { id: id.slice(0, 4) });
|
|
247
|
+
setThreads((threads) =>
|
|
248
|
+
threads.map((thread) => {
|
|
249
|
+
if (thread.id === id) {
|
|
250
|
+
thread.deleted = true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return thread;
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
setSelected((selected) => (selected === id ? undefined : selected));
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const handleUpdateComment: CommentsOptions['onUpdate'] = (id, cursor) => {
|
|
261
|
+
log.info('update', { id: id.slice(0, 4), cursor });
|
|
262
|
+
setThreads((threads) =>
|
|
263
|
+
threads.map((thread) => {
|
|
264
|
+
if (thread.id === id) {
|
|
265
|
+
thread.range.cursor = cursor;
|
|
266
|
+
delete thread.deleted;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return thread;
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const handleSelectComment: CommentsOptions['onSelect'] = ({ active, closest, ranges }) => {
|
|
275
|
+
log.info('select', { active: active?.slice(0, 4), closest: closest?.slice(0, 4) });
|
|
276
|
+
setThreads((threads) =>
|
|
277
|
+
threads.map((thread) => {
|
|
278
|
+
const range = ranges.find((range) => range.id === thread.range.id);
|
|
279
|
+
if (range) {
|
|
280
|
+
thread.yPos = range.location ? Math.floor(range.location.top) : undefined;
|
|
281
|
+
thread.selection = { from: range.from, to: range.to };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return thread;
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
setSelected(active ?? closest);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const handleSelectThread = (id: string) => {
|
|
292
|
+
setSelected(id);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const handleResolveThread = (id: string) => {
|
|
296
|
+
setThreads((threads) => [...threads.filter((thread) => thread.id !== id)]);
|
|
297
|
+
setSelected(undefined);
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
return (
|
|
301
|
+
<div className={mx(fixedInsetFlexLayout, 'bg-neutral-100')}>
|
|
302
|
+
<div className='flex justify-center h-full gap-8'>
|
|
303
|
+
<div className='flex flex-col h-full w-[600px]'>
|
|
304
|
+
<Editor
|
|
305
|
+
item={item}
|
|
306
|
+
commentSelected={selected}
|
|
307
|
+
commentRanges={commentRanges}
|
|
308
|
+
onCreateComment={handleCreateComment}
|
|
309
|
+
onDeleteComment={handleDeleteComment}
|
|
310
|
+
onUpdateComment={handleUpdateComment}
|
|
311
|
+
onSelectComment={handleSelectComment}
|
|
312
|
+
/>
|
|
313
|
+
</div>
|
|
314
|
+
|
|
315
|
+
<div className='flex flex-col h-full w-[300px]'>
|
|
316
|
+
<Sidebar
|
|
317
|
+
threads={visibleThreads}
|
|
318
|
+
selected={selected}
|
|
319
|
+
onSelect={handleSelectThread}
|
|
320
|
+
onResolve={handleResolveThread}
|
|
321
|
+
/>
|
|
322
|
+
</div>
|
|
323
|
+
</div>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export default {
|
|
329
|
+
title: 'react-ui-editor/comments',
|
|
330
|
+
component: MarkdownEditor,
|
|
331
|
+
decorators: [withTheme],
|
|
332
|
+
render: (args: StoryProps) => <Story {...args} />,
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const str = (...lines: string[]) => lines.join('\n');
|
|
336
|
+
|
|
337
|
+
const document = str(
|
|
338
|
+
'# Comments',
|
|
339
|
+
'',
|
|
340
|
+
str(...faker.helpers.multiple(() => [faker.lorem.paragraph({ min: 5, max: 10 }), ''], { count: 20 }).flat()),
|
|
341
|
+
'',
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
export const Default = {
|
|
345
|
+
args: {
|
|
346
|
+
text: document,
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
export const Testing = {
|
|
351
|
+
args: {
|
|
352
|
+
text: document,
|
|
353
|
+
autoCreate: true,
|
|
354
|
+
},
|
|
355
|
+
};
|
|
@@ -4,20 +4,16 @@
|
|
|
4
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
5
|
//
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { StateField, type Extension, type Transaction } from '@codemirror/state';
|
|
8
8
|
import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '@codemirror/view';
|
|
9
9
|
|
|
10
10
|
import { next as A } from '@dxos/automerge/automerge';
|
|
11
11
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
12
12
|
|
|
13
13
|
import { cursorConverter } from './cursor';
|
|
14
|
-
import { effectType, type IDocHandle, isReconcileTx, type Value } from './defs';
|
|
14
|
+
import { effectType, type IDocHandle, isReconcileTx, semaphoreFacet, type Value } from './defs';
|
|
15
15
|
import { PatchSemaphore } from './semaphore';
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
19
|
-
combine: (values) => values.at(-1)!, // Take last.
|
|
20
|
-
});
|
|
16
|
+
import { Cursor } from '../cursor';
|
|
21
17
|
|
|
22
18
|
export type AutomergeOptions = {
|
|
23
19
|
handle: IDocHandle;
|
|
@@ -25,19 +21,20 @@ export type AutomergeOptions = {
|
|
|
25
21
|
};
|
|
26
22
|
|
|
27
23
|
export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
28
|
-
// TODO(burdon): Rename.
|
|
29
24
|
const stateField: StateField<Value> = StateField.define({
|
|
30
25
|
create: () => ({
|
|
31
26
|
lastHeads: A.getHeads(handle.docSync()!),
|
|
32
27
|
unreconciledTransactions: [],
|
|
33
28
|
path: path.slice(),
|
|
34
29
|
}),
|
|
30
|
+
|
|
35
31
|
update: (value: Value, tr: Transaction) => {
|
|
36
32
|
const result: Value = {
|
|
37
33
|
lastHeads: value.lastHeads,
|
|
38
34
|
unreconciledTransactions: value.unreconciledTransactions.slice(),
|
|
39
35
|
path: path.slice(),
|
|
40
36
|
};
|
|
37
|
+
|
|
41
38
|
let clearUnreconciled = false;
|
|
42
39
|
for (const effect of tr.effects) {
|
|
43
40
|
if (effect.is(effectType)) {
|
|
@@ -45,6 +42,7 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
45
42
|
clearUnreconciled = true;
|
|
46
43
|
}
|
|
47
44
|
}
|
|
45
|
+
|
|
48
46
|
if (clearUnreconciled) {
|
|
49
47
|
result.unreconciledTransactions = [];
|
|
50
48
|
} else {
|
|
@@ -52,23 +50,23 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
52
50
|
result.unreconciledTransactions.push(tr);
|
|
53
51
|
}
|
|
54
52
|
}
|
|
53
|
+
|
|
55
54
|
return result;
|
|
56
55
|
},
|
|
57
56
|
});
|
|
58
57
|
|
|
59
|
-
const semaphore = new PatchSemaphore(stateField);
|
|
58
|
+
const semaphore = new PatchSemaphore(handle, stateField);
|
|
60
59
|
|
|
61
60
|
const viewPlugin = ViewPlugin.fromClass(
|
|
62
|
-
class
|
|
61
|
+
class AutomergeViewPlugin implements PluginValue {
|
|
63
62
|
constructor(private readonly _view: EditorView) {
|
|
64
63
|
handle.addListener('change', this._handleChange);
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
update(update: ViewUpdate) {
|
|
68
67
|
if (update.transactions.length > 0 && update.transactions.some((tr) => !isReconcileTx(tr))) {
|
|
69
|
-
// TODO(burdon): This is a hack to ensure that the view is updated after the transaction is applied.
|
|
70
68
|
queueMicrotask(() => {
|
|
71
|
-
this._view.state.facet(semaphoreFacet).reconcile(
|
|
69
|
+
this._view.state.facet(semaphoreFacet).reconcile(this._view);
|
|
72
70
|
});
|
|
73
71
|
}
|
|
74
72
|
}
|
|
@@ -78,14 +76,14 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
private _handleChange = () => {
|
|
81
|
-
this._view.state.facet(semaphoreFacet).reconcile(
|
|
79
|
+
this._view.state.facet(semaphoreFacet).reconcile(this._view);
|
|
82
80
|
};
|
|
83
81
|
},
|
|
84
82
|
);
|
|
85
83
|
|
|
86
84
|
return {
|
|
87
85
|
extension: [
|
|
88
|
-
|
|
86
|
+
Cursor.converter.of(cursorConverter(handle, path)),
|
|
89
87
|
semaphoreFacet.of(semaphore),
|
|
90
88
|
stateField,
|
|
91
89
|
viewPlugin,
|
|
@@ -8,7 +8,7 @@ import type { Prop } from '@dxos/automerge/automerge';
|
|
|
8
8
|
import { next as automerge } from '@dxos/automerge/automerge';
|
|
9
9
|
|
|
10
10
|
import { type IDocHandle } from './defs';
|
|
11
|
-
import { type CursorConverter } from '
|
|
11
|
+
import { type CursorConverter } from '../cursor';
|
|
12
12
|
|
|
13
13
|
export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConverter => ({
|
|
14
14
|
// TODO(burdon): Handle assoc to associate with a previous character.
|
|
@@ -26,6 +26,7 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
|
|
|
26
26
|
// NOTE: Slice is needed because getCursor mutates the array.
|
|
27
27
|
return automerge.getCursor(doc, path.slice(), pos);
|
|
28
28
|
},
|
|
29
|
+
|
|
29
30
|
fromCursor: (cursor) => {
|
|
30
31
|
if (cursor === '') {
|
|
31
32
|
return 0;
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
3
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
4
5
|
//
|
|
5
6
|
|
|
6
|
-
import {
|
|
7
|
-
Annotation,
|
|
8
|
-
StateEffect,
|
|
9
|
-
type StateField,
|
|
10
|
-
type EditorState,
|
|
11
|
-
type Transaction,
|
|
12
|
-
type TransactionSpec,
|
|
13
|
-
} from '@codemirror/state';
|
|
7
|
+
import { Annotation, StateEffect, type StateField, type EditorState, type Transaction, Facet } from '@codemirror/state';
|
|
14
8
|
|
|
15
9
|
import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
16
10
|
|
|
11
|
+
import { type PatchSemaphore } from './semaphore';
|
|
12
|
+
|
|
17
13
|
export type Value = {
|
|
18
14
|
lastHeads: Heads;
|
|
19
15
|
path: Prop[];
|
|
20
16
|
unreconciledTransactions: Transaction[];
|
|
21
17
|
};
|
|
22
18
|
|
|
23
|
-
type UpdateHeads = {
|
|
19
|
+
export type UpdateHeads = {
|
|
24
20
|
newHeads: Heads;
|
|
25
21
|
};
|
|
26
22
|
|
|
@@ -36,6 +32,12 @@ export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
|
36
32
|
|
|
37
33
|
export const isReconcileTx = (tr: Transaction): boolean => !!tr.annotation(reconcileAnnotationType);
|
|
38
34
|
|
|
35
|
+
// TODO(burdon): Rename sync/create generic def (like cursor) to decouple automerge deps.
|
|
36
|
+
export const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
37
|
+
combine: (values) => values.at(-1)!, // Take last.
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/*
|
|
39
41
|
export const makeReconcile = (tr: TransactionSpec) => {
|
|
40
42
|
if (tr.annotations != null) {
|
|
41
43
|
if (tr.annotations instanceof Array) {
|
|
@@ -46,11 +48,13 @@ export const makeReconcile = (tr: TransactionSpec) => {
|
|
|
46
48
|
} else {
|
|
47
49
|
tr.annotations = [reconcileAnnotationType.of({})];
|
|
48
50
|
}
|
|
51
|
+
|
|
49
52
|
// return {
|
|
50
53
|
// ...tr,
|
|
51
54
|
// annotations: reconcileAnnotationType.of({})
|
|
52
55
|
// }
|
|
53
56
|
};
|
|
57
|
+
*/
|
|
54
58
|
|
|
55
59
|
export type IDocHandle<T = any> = {
|
|
56
60
|
docSync(): Doc<T> | undefined;
|
|
@@ -21,27 +21,23 @@ import {
|
|
|
21
21
|
import { updateAutomerge } from './update-automerge';
|
|
22
22
|
import { updateCodeMirror } from './update-codemirror';
|
|
23
23
|
|
|
24
|
-
type Doc<T> = automerge.Doc<T>;
|
|
25
|
-
type Heads = automerge.Heads;
|
|
26
|
-
|
|
27
|
-
type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
|
|
28
|
-
|
|
29
24
|
/**
|
|
30
|
-
*
|
|
25
|
+
* Implements three-way merge (on each mutation/key-stroke).
|
|
31
26
|
*/
|
|
32
27
|
export class PatchSemaphore {
|
|
33
28
|
_inReconcile = false;
|
|
34
|
-
_queue: Array<ChangeFn> = [];
|
|
35
29
|
|
|
36
|
-
constructor(private readonly _field: StateField<Value>) {}
|
|
30
|
+
constructor(private readonly _handle: IDocHandle, private readonly _field: StateField<Value>) {}
|
|
37
31
|
|
|
38
|
-
reconcile = (
|
|
32
|
+
reconcile = (view: EditorView) => {
|
|
39
33
|
if (this._inReconcile) {
|
|
40
34
|
return;
|
|
41
35
|
}
|
|
42
36
|
this._inReconcile = true;
|
|
43
37
|
|
|
44
38
|
const path = getPath(view.state, this._field);
|
|
39
|
+
|
|
40
|
+
// Get the heads before the unreconciled transactions are applied.
|
|
45
41
|
const oldHeads = getLastHeads(view.state, this._field);
|
|
46
42
|
let selection = view.state.selection;
|
|
47
43
|
|
|
@@ -58,16 +54,18 @@ export class PatchSemaphore {
|
|
|
58
54
|
}
|
|
59
55
|
|
|
60
56
|
// Apply the unreconciled transactions to the document.
|
|
61
|
-
let newHeads = updateAutomerge(this._field,
|
|
57
|
+
let newHeads = updateAutomerge(this._field, this._handle, transactions, view.state);
|
|
62
58
|
|
|
63
59
|
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
64
60
|
if (newHeads === null || newHeads === undefined) {
|
|
65
61
|
// TODO: @alexjg this is the call that's resetting the editor state on click
|
|
66
|
-
newHeads = automerge.getHeads(
|
|
62
|
+
newHeads = automerge.getHeads(this._handle.docSync()!);
|
|
67
63
|
}
|
|
68
64
|
|
|
69
65
|
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
70
|
-
const diff = automerge.equals(oldHeads, newHeads)
|
|
66
|
+
const diff = automerge.equals(oldHeads, newHeads)
|
|
67
|
+
? []
|
|
68
|
+
: automerge.diff(this._handle.docSync()!, oldHeads, newHeads);
|
|
71
69
|
updateCodeMirror(view, selection, path, diff);
|
|
72
70
|
|
|
73
71
|
view.dispatch({
|