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