@dxos/react-ui-editor 0.3.11-main.6fccc32 → 0.3.11-main.73ee990
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 +1259 -1060
- 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 +13 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +9 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +14 -0
- package/dist/types/src/components/Toolbar/Toolbar.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/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/automerge/update-automerge.d.ts.map +1 -1
- 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 +35 -18
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +1 -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/formatting.d.ts +11 -0
- package/dist/types/src/extensions/markdown/formatting.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 +8 -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 +3 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- 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 -27
- package/dist/types/src/hooks/useTextModel.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 +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +34 -37
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +72 -80
- package/src/components/TextEditor/TextEditor.tsx +96 -70
- package/src/components/TextEditor/comments.stories.tsx +113 -71
- package/src/components/Toolbar/Toolbar.stories.tsx +86 -0
- package/src/components/Toolbar/Toolbar.tsx +115 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.ts +39 -46
- 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 +14 -8
- package/src/extensions/awareness.ts +15 -14
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/comments.ts +269 -162
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +1 -7
- package/src/extensions/markdown/bundle.ts +20 -30
- package/src/extensions/markdown/code.ts +142 -0
- package/src/extensions/markdown/formatting.ts +159 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -15
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +8 -5
- 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 +4 -2
- package/src/hooks/useActionHandler.ts +45 -0
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +47 -56
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +1 -11
- package/src/util.ts +26 -3
- 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/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -106
- package/src/extensions/hr.ts +0 -73
- package/src/extensions/tasklist.ts +0 -119
- package/src/extensions/yjs/cursor.ts +0 -22
- 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 -156
- 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/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
- /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
|
@@ -6,18 +6,19 @@ import '@dxosTheme';
|
|
|
6
6
|
|
|
7
7
|
import { type EditorView, keymap } from '@codemirror/view';
|
|
8
8
|
import { faker } from '@faker-js/faker';
|
|
9
|
-
import { Check } from '@phosphor-icons/react';
|
|
9
|
+
import { Check, Trash } from '@phosphor-icons/react';
|
|
10
10
|
import React, { type FC, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
11
|
|
|
12
|
-
import { TextObject } from '@dxos/echo-schema';
|
|
12
|
+
import { getTextContent, TextObject } from '@dxos/echo-schema';
|
|
13
13
|
import { PublicKey } from '@dxos/keys';
|
|
14
|
+
import { log } from '@dxos/log';
|
|
14
15
|
import { Button, DensityProvider } from '@dxos/react-ui';
|
|
15
16
|
import { fixedInsetFlexLayout, mx } from '@dxos/react-ui-theme';
|
|
16
17
|
import { withTheme } from '@dxos/storybook-utils';
|
|
17
18
|
|
|
18
19
|
import { MarkdownEditor, TextEditor } from './TextEditor';
|
|
19
|
-
import { comments, type CommentsOptions,
|
|
20
|
-
import { type
|
|
20
|
+
import { comments, type CommentsOptions, setFocus, useComments } from '../../extensions';
|
|
21
|
+
import { type Comment, type Range, useTextModel } from '../../hooks';
|
|
21
22
|
|
|
22
23
|
faker.seed(101);
|
|
23
24
|
|
|
@@ -27,34 +28,41 @@ faker.seed(101);
|
|
|
27
28
|
|
|
28
29
|
const Editor: FC<{
|
|
29
30
|
item: { text: TextObject };
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
comments: Comment[];
|
|
32
|
+
selected?: string;
|
|
32
33
|
onCreateComment: CommentsOptions['onCreate'];
|
|
34
|
+
onDeleteComment: CommentsOptions['onDelete'];
|
|
35
|
+
onUpdateComment: CommentsOptions['onUpdate'];
|
|
33
36
|
onSelectComment: CommentsOptions['onSelect'];
|
|
34
|
-
}> = ({
|
|
37
|
+
}> = ({
|
|
38
|
+
item,
|
|
39
|
+
selected: selectedValue,
|
|
40
|
+
comments: commentRanges,
|
|
41
|
+
onCreateComment,
|
|
42
|
+
onDeleteComment,
|
|
43
|
+
onUpdateComment,
|
|
44
|
+
onSelectComment,
|
|
45
|
+
}) => {
|
|
35
46
|
const model = useTextModel({ text: item.text });
|
|
36
|
-
const
|
|
47
|
+
const view = useRef<EditorView>(null);
|
|
37
48
|
const [selected, setSelected] = useState<string>();
|
|
38
49
|
useEffect(() => {
|
|
39
|
-
if (!
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
const range = Cursor.getRangeFromCursor(editorRef.current!.state.facet(Cursor.converter), cursor);
|
|
44
|
-
if (range) {
|
|
45
|
-
// TODO(burdon): Scroll selection to center of screen?
|
|
46
|
-
editorRef.current?.dispatch({ selection: { anchor: range.from }, scrollIntoView: true });
|
|
47
|
-
}
|
|
50
|
+
if (!view.current?.hasFocus && selectedValue !== selected) {
|
|
51
|
+
setSelected(selectedValue);
|
|
52
|
+
if (selectedValue) {
|
|
53
|
+
setFocus(view.current!, selectedValue);
|
|
48
54
|
}
|
|
49
|
-
|
|
50
|
-
setSelected(commentSelected);
|
|
51
55
|
}
|
|
52
|
-
}, [selected, commentRanges,
|
|
56
|
+
}, [selected, commentRanges, selectedValue]);
|
|
57
|
+
|
|
58
|
+
useComments(view.current, commentRanges);
|
|
53
59
|
|
|
54
60
|
const extensions = useMemo(() => {
|
|
55
61
|
return [
|
|
56
62
|
comments({
|
|
57
63
|
onCreate: onCreateComment,
|
|
64
|
+
onDelete: onDeleteComment,
|
|
65
|
+
onUpdate: onUpdateComment,
|
|
58
66
|
onSelect: onSelectComment,
|
|
59
67
|
}),
|
|
60
68
|
];
|
|
@@ -67,7 +75,7 @@ const Editor: FC<{
|
|
|
67
75
|
// TODO(burdon): Highlight currently selected comment.
|
|
68
76
|
return (
|
|
69
77
|
<div className='flex grow overflow-y-scroll'>
|
|
70
|
-
<MarkdownEditor ref={
|
|
78
|
+
<MarkdownEditor ref={view} model={model} extensions={extensions} />
|
|
71
79
|
</div>
|
|
72
80
|
);
|
|
73
81
|
};
|
|
@@ -78,9 +86,9 @@ const Editor: FC<{
|
|
|
78
86
|
|
|
79
87
|
type CommentThread = {
|
|
80
88
|
id: string;
|
|
81
|
-
|
|
89
|
+
cursor?: string;
|
|
90
|
+
range?: Range;
|
|
82
91
|
yPos?: number;
|
|
83
|
-
selection?: Range;
|
|
84
92
|
messages: TextObject[];
|
|
85
93
|
};
|
|
86
94
|
|
|
@@ -90,23 +98,17 @@ const Thread: FC<{
|
|
|
90
98
|
onSelect: () => void;
|
|
91
99
|
onResolve: () => void;
|
|
92
100
|
}> = ({ thread, selected, onSelect, onResolve }) => {
|
|
101
|
+
const [focus, setFocus] = useState(false);
|
|
93
102
|
const [item, setItem] = useState({ text: new TextObject() });
|
|
94
103
|
const model = useTextModel({ text: item.text });
|
|
95
104
|
const editorRef = useRef<EditorView>(null);
|
|
96
105
|
|
|
97
|
-
// TODO(burdon): Hack to focus (view is recreated when text object changes).
|
|
98
|
-
const focus = () => {
|
|
99
|
-
setTimeout(() => {
|
|
100
|
-
editorRef.current?.focus();
|
|
101
|
-
}, 100);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
106
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
105
107
|
useEffect(() => {
|
|
106
108
|
if (selected) {
|
|
107
109
|
containerRef.current?.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
|
108
110
|
if (thread.messages.length === 0) {
|
|
109
|
-
|
|
111
|
+
setFocus(true);
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
114
|
}, [selected]);
|
|
@@ -116,7 +118,7 @@ const Thread: FC<{
|
|
|
116
118
|
if (text?.length) {
|
|
117
119
|
thread.messages.push(item.text);
|
|
118
120
|
setItem({ text: new TextObject() });
|
|
119
|
-
|
|
121
|
+
setFocus(true);
|
|
120
122
|
}
|
|
121
123
|
};
|
|
122
124
|
|
|
@@ -125,41 +127,49 @@ const Thread: FC<{
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
return (
|
|
128
|
-
<div
|
|
129
|
-
|
|
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'>
|
|
130
138
|
<span>id:{thread.id.slice(0, 4)}</span>
|
|
131
|
-
<span>from:{thread.
|
|
132
|
-
<span>to:{thread.
|
|
139
|
+
<span>from:{thread.range?.from}</span>
|
|
140
|
+
<span>to:{thread.range?.to}</span>
|
|
133
141
|
<span>y:{thread.yPos}</span>
|
|
142
|
+
<span className='grow' />
|
|
143
|
+
{!thread.cursor && <Trash />}
|
|
134
144
|
</div>
|
|
135
145
|
|
|
136
146
|
{thread.messages.map((message, i) => (
|
|
137
147
|
// TODO(burdon): Fix default editor padding so content doesn't jump on creating message.
|
|
138
148
|
<div key={i} className='p-2' onClick={() => onSelect()}>
|
|
139
|
-
{message
|
|
149
|
+
{getTextContent(message)}
|
|
140
150
|
</div>
|
|
141
151
|
))}
|
|
142
152
|
|
|
143
|
-
<div ref={containerRef} onClick={() => onSelect()} className='flex
|
|
144
|
-
<
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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;
|
|
157
167
|
},
|
|
158
|
-
|
|
159
|
-
]
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
<Button variant='ghost' classNames='px-1
|
|
168
|
+
},
|
|
169
|
+
]),
|
|
170
|
+
]}
|
|
171
|
+
/>
|
|
172
|
+
<Button variant='ghost' classNames='px-1' title='Resolve' onClick={onResolve}>
|
|
163
173
|
<Check />
|
|
164
174
|
</Button>
|
|
165
175
|
</div>
|
|
@@ -183,7 +193,7 @@ const Sidebar: FC<{
|
|
|
183
193
|
|
|
184
194
|
return (
|
|
185
195
|
<DensityProvider density='fine'>
|
|
186
|
-
<div className='flex flex-col grow overflow-y-scroll py-4 gap-4
|
|
196
|
+
<div className='flex flex-col grow overflow-y-scroll py-4 gap-4'>
|
|
187
197
|
{sortedThreads.map((thread) => (
|
|
188
198
|
<Thread
|
|
189
199
|
key={thread.id}
|
|
@@ -210,44 +220,74 @@ type StoryProps = {
|
|
|
210
220
|
const Story = ({ text, autoCreate }: StoryProps) => {
|
|
211
221
|
const [item] = useState({ text: new TextObject(text) });
|
|
212
222
|
const [threads, setThreads] = useState<CommentThread[]>([]);
|
|
213
|
-
const commentRanges = useMemo(() => threads.map((thread) => thread.range), [threads]);
|
|
214
223
|
const [selected, setSelected] = useState<string>();
|
|
215
224
|
|
|
225
|
+
const comments = useMemo<Comment[]>(() => threads.map(({ id, cursor }) => ({ id, cursor })), [threads]);
|
|
226
|
+
|
|
216
227
|
// Filter by visibility.
|
|
217
228
|
const visibleThreads = useMemo(() => threads.filter((thread) => thread.yPos !== undefined), [threads]);
|
|
218
229
|
|
|
219
230
|
const handleCreateComment: CommentsOptions['onCreate'] = (cursor, location) => {
|
|
220
231
|
const id = PublicKey.random().toHex();
|
|
232
|
+
log.info('create', { id: id.slice(0, 4), cursor });
|
|
221
233
|
setThreads((threads) => [
|
|
222
234
|
...threads,
|
|
223
235
|
{
|
|
224
236
|
id,
|
|
225
|
-
|
|
237
|
+
cursor,
|
|
226
238
|
yPos: location ? Math.floor(location.top) : undefined,
|
|
227
239
|
messages: autoCreate
|
|
228
|
-
? faker.helpers.multiple(() => new TextObject(faker.lorem.sentence()), { count: { min:
|
|
240
|
+
? faker.helpers.multiple(() => new TextObject(faker.lorem.sentence()), { count: { min: 1, max: 3 } })
|
|
229
241
|
: [],
|
|
230
242
|
},
|
|
231
243
|
]);
|
|
232
|
-
setSelected(id);
|
|
244
|
+
setSelected(id); // TODO(burdon): Not required.
|
|
233
245
|
return id;
|
|
234
246
|
};
|
|
235
247
|
|
|
236
|
-
|
|
237
|
-
|
|
248
|
+
const handleDeleteComment: CommentsOptions['onDelete'] = (id) => {
|
|
249
|
+
log.info('delete', { id: id.slice(0, 4) });
|
|
238
250
|
setThreads((threads) =>
|
|
239
251
|
threads.map((thread) => {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
thread.yPos = range.location ? Math.floor(range.location.top) : undefined;
|
|
243
|
-
thread.selection = { from: range.from, to: range.to };
|
|
252
|
+
if (thread.id === id) {
|
|
253
|
+
thread.cursor = undefined;
|
|
244
254
|
}
|
|
245
255
|
|
|
246
256
|
return thread;
|
|
247
257
|
}),
|
|
248
258
|
);
|
|
249
259
|
|
|
250
|
-
setSelected(
|
|
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);
|
|
251
291
|
};
|
|
252
292
|
|
|
253
293
|
const handleSelectThread = (id: string) => {
|
|
@@ -265,9 +305,11 @@ const Story = ({ text, autoCreate }: StoryProps) => {
|
|
|
265
305
|
<div className='flex flex-col h-full w-[600px]'>
|
|
266
306
|
<Editor
|
|
267
307
|
item={item}
|
|
268
|
-
|
|
269
|
-
|
|
308
|
+
selected={selected}
|
|
309
|
+
comments={comments}
|
|
270
310
|
onCreateComment={handleCreateComment}
|
|
311
|
+
onDeleteComment={handleDeleteComment}
|
|
312
|
+
onUpdateComment={handleUpdateComment}
|
|
271
313
|
onSelectComment={handleSelectComment}
|
|
272
314
|
/>
|
|
273
315
|
</div>
|
|
@@ -307,7 +349,7 @@ export const Default = {
|
|
|
307
349
|
},
|
|
308
350
|
};
|
|
309
351
|
|
|
310
|
-
export const
|
|
352
|
+
export const Testing = {
|
|
311
353
|
args: {
|
|
312
354
|
text: document,
|
|
313
355
|
autoCreate: true,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
7
|
+
import { faker } from '@faker-js/faker';
|
|
8
|
+
import React, { type FC, useMemo, useState } from 'react';
|
|
9
|
+
|
|
10
|
+
import { TextObject } from '@dxos/echo-schema';
|
|
11
|
+
import { PublicKey } from '@dxos/keys';
|
|
12
|
+
import { fixedInsetFlexLayout, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
13
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
14
|
+
|
|
15
|
+
import { Toolbar } from './Toolbar';
|
|
16
|
+
import { code, comments, formatting, heading, image, table, tasklist, useComments } from '../../extensions';
|
|
17
|
+
import { type Comment, useActionHandler, useTextEditor, useTextModel } from '../../hooks';
|
|
18
|
+
import { MarkdownEditor } from '../TextEditor';
|
|
19
|
+
|
|
20
|
+
faker.seed(101);
|
|
21
|
+
|
|
22
|
+
const Story: FC<{ content: string }> = ({ content }) => {
|
|
23
|
+
const [item] = useState({ text: new TextObject(content) });
|
|
24
|
+
const [_comments, setComments] = useState<Comment[]>([]);
|
|
25
|
+
const [editorRef, editorView] = useTextEditor();
|
|
26
|
+
const model = useTextModel({ text: item.text });
|
|
27
|
+
const extensions = useMemo(
|
|
28
|
+
() => [
|
|
29
|
+
code(),
|
|
30
|
+
comments({
|
|
31
|
+
onCreate: (cursor) => {
|
|
32
|
+
const id = PublicKey.random().toHex();
|
|
33
|
+
setComments((comments) => [...comments, { id, cursor }]);
|
|
34
|
+
return id;
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
formatting(),
|
|
38
|
+
heading(),
|
|
39
|
+
image(),
|
|
40
|
+
table(),
|
|
41
|
+
tasklist(),
|
|
42
|
+
],
|
|
43
|
+
[],
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
useComments(editorView, _comments);
|
|
47
|
+
const handleAction = useActionHandler(editorView);
|
|
48
|
+
|
|
49
|
+
if (!model) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
55
|
+
<div className='flex h-full justify-center'>
|
|
56
|
+
<div className='flex flex-col h-full w-[800px]'>
|
|
57
|
+
<Toolbar onAction={handleAction} />
|
|
58
|
+
<MarkdownEditor ref={editorRef} model={model} extensions={extensions} />
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default {
|
|
66
|
+
title: 'react-ui-editor/Toolbar',
|
|
67
|
+
component: Toolbar,
|
|
68
|
+
render: (args: any) => <Story {...args} />,
|
|
69
|
+
decorators: [withTheme],
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const content = [
|
|
73
|
+
'Demo',
|
|
74
|
+
'',
|
|
75
|
+
'The editor supports **Markdown** styles.',
|
|
76
|
+
'',
|
|
77
|
+
faker.lorem.paragraph({ min: 5, max: 8 }),
|
|
78
|
+
'',
|
|
79
|
+
'',
|
|
80
|
+
].join('\n');
|
|
81
|
+
|
|
82
|
+
export const Default = {
|
|
83
|
+
args: {
|
|
84
|
+
content,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type Icon,
|
|
7
|
+
At,
|
|
8
|
+
Brain,
|
|
9
|
+
CaretRight,
|
|
10
|
+
ChatText,
|
|
11
|
+
Code,
|
|
12
|
+
Image,
|
|
13
|
+
Link,
|
|
14
|
+
ListBullets,
|
|
15
|
+
ListChecks,
|
|
16
|
+
ListNumbers,
|
|
17
|
+
TextStrikethrough,
|
|
18
|
+
Table,
|
|
19
|
+
TextB,
|
|
20
|
+
TextItalic,
|
|
21
|
+
} from '@phosphor-icons/react';
|
|
22
|
+
import React, { type FC } from 'react';
|
|
23
|
+
|
|
24
|
+
import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
|
|
25
|
+
import { getSize } from '@dxos/react-ui-theme';
|
|
26
|
+
|
|
27
|
+
export type Action =
|
|
28
|
+
| 'blockquote'
|
|
29
|
+
| 'bold'
|
|
30
|
+
| 'code'
|
|
31
|
+
| 'comment'
|
|
32
|
+
| 'heading'
|
|
33
|
+
| 'image'
|
|
34
|
+
| 'italic'
|
|
35
|
+
| 'link'
|
|
36
|
+
| 'list'
|
|
37
|
+
| 'list-ordered'
|
|
38
|
+
| 'list-tasks'
|
|
39
|
+
| 'mention'
|
|
40
|
+
| 'prompt'
|
|
41
|
+
| 'strikethrough'
|
|
42
|
+
| 'table';
|
|
43
|
+
|
|
44
|
+
export type ToolbarProps = {
|
|
45
|
+
onAction?: (action: { type: Action; data?: any }) => void;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const IconButton: FC<{ Icon: Icon; title: string } & NonNullable<Pick<ButtonProps, 'onClick'>>> = ({
|
|
49
|
+
Icon,
|
|
50
|
+
title,
|
|
51
|
+
onClick,
|
|
52
|
+
}) => (
|
|
53
|
+
<Button variant='ghost' classNames='p-2' onClick={onClick} title={title}>
|
|
54
|
+
<Icon className={getSize(5)} />
|
|
55
|
+
</Button>
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// TODO(burdon): Update state based on current selection?
|
|
59
|
+
export const Toolbar = ({ onAction }: ToolbarProps) => {
|
|
60
|
+
return (
|
|
61
|
+
<DensityProvider density='fine'>
|
|
62
|
+
<div role='toolbar' className='flex w-full p-2 items-center gap-4'>
|
|
63
|
+
<div>
|
|
64
|
+
<Select.Root
|
|
65
|
+
defaultValue='0'
|
|
66
|
+
onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
|
|
67
|
+
>
|
|
68
|
+
<Select.TriggerButton classNames='w-[8rem]' />
|
|
69
|
+
<Select.Portal>
|
|
70
|
+
<Select.Content>
|
|
71
|
+
<Select.Viewport>
|
|
72
|
+
<Select.Option value='0'>Paragraph</Select.Option>
|
|
73
|
+
{[1, 2, 3, 4, 5, 6].map((level) => (
|
|
74
|
+
<Select.Option key={level} value={String(level)}>
|
|
75
|
+
Heading {level}
|
|
76
|
+
</Select.Option>
|
|
77
|
+
))}
|
|
78
|
+
</Select.Viewport>
|
|
79
|
+
</Select.Content>
|
|
80
|
+
</Select.Portal>
|
|
81
|
+
</Select.Root>
|
|
82
|
+
</div>
|
|
83
|
+
<div>
|
|
84
|
+
<IconButton Icon={TextB} title='Bold' onClick={() => onAction?.({ type: 'bold' })} />
|
|
85
|
+
<IconButton Icon={TextItalic} title='Italic' onClick={() => onAction?.({ type: 'italic' })} />
|
|
86
|
+
<IconButton
|
|
87
|
+
Icon={TextStrikethrough}
|
|
88
|
+
title='Strike-through'
|
|
89
|
+
onClick={() => onAction?.({ type: 'strikethrough' })}
|
|
90
|
+
/>
|
|
91
|
+
</div>
|
|
92
|
+
<div>
|
|
93
|
+
<IconButton Icon={ListBullets} title='Bullet list' onClick={() => onAction?.({ type: 'list' })} />
|
|
94
|
+
<IconButton Icon={ListNumbers} title='Numbered list' onClick={() => onAction?.({ type: 'list-ordered' })} />
|
|
95
|
+
<IconButton Icon={ListChecks} title='Task list' onClick={() => onAction?.({ type: 'list-tasks' })} />
|
|
96
|
+
</div>
|
|
97
|
+
<div>
|
|
98
|
+
<IconButton Icon={CaretRight} title='Block quite' onClick={() => onAction?.({ type: 'blockquote' })} />
|
|
99
|
+
<IconButton Icon={Code} title='Code block' onClick={() => onAction?.({ type: 'code' })} />
|
|
100
|
+
<IconButton Icon={Table} title='Table' onClick={() => onAction?.({ type: 'table' })} />
|
|
101
|
+
</div>
|
|
102
|
+
<div>
|
|
103
|
+
<IconButton Icon={Link} title='Link' onClick={() => onAction?.({ type: 'link' })} />
|
|
104
|
+
<IconButton Icon={At} title='Mention' onClick={() => onAction?.({ type: 'mention' })} />
|
|
105
|
+
<IconButton Icon={Image} title='Image' onClick={() => onAction?.({ type: 'image' })} />
|
|
106
|
+
</div>
|
|
107
|
+
<div className='grow' />
|
|
108
|
+
<div>
|
|
109
|
+
<IconButton Icon={Brain} title='AI prompt' onClick={() => onAction?.({ type: 'prompt' })} />
|
|
110
|
+
<IconButton Icon={ChatText} title='Create comment' onClick={() => onAction?.({ type: 'comment' })} />
|
|
111
|
+
</div>
|
|
112
|
+
</div>
|
|
113
|
+
</DensityProvider>
|
|
114
|
+
);
|
|
115
|
+
};
|
|
@@ -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,40 +4,37 @@
|
|
|
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
|
|
11
|
-
import {
|
|
10
|
+
import { type Prop, next as _automerge } 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
16
|
import { Cursor } from '../cursor';
|
|
17
17
|
|
|
18
|
-
const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
19
|
-
combine: (values) => values.at(-1)!, // Take last.
|
|
20
|
-
});
|
|
21
|
-
|
|
22
18
|
export type AutomergeOptions = {
|
|
23
19
|
handle: IDocHandle;
|
|
24
20
|
path: Prop[];
|
|
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: () => ({
|
|
31
|
-
lastHeads: A.getHeads(handle.docSync()!),
|
|
32
|
-
unreconciledTransactions: [],
|
|
33
26
|
path: path.slice(),
|
|
27
|
+
lastHeads: _automerge.getHeads(handle.docSync()!),
|
|
28
|
+
unreconciledTransactions: [],
|
|
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,38 @@ 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
|
+
semaphore.reconcile(view);
|
|
78
83
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
extension: [
|
|
88
|
-
Cursor.converter.of(cursorConverter(handle, path)),
|
|
89
|
-
semaphoreFacet.of(semaphore),
|
|
90
|
-
stateField,
|
|
91
|
-
viewPlugin,
|
|
92
|
-
],
|
|
93
|
-
};
|
|
84
|
+
}),
|
|
85
|
+
state,
|
|
86
|
+
];
|
|
94
87
|
};
|