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