@dxos/react-ui-editor 0.3.11-main.24b414e → 0.3.11-main.27967cf
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 +1676 -1068
- 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 -15
- 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/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +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/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
- 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 +8 -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 +2 -2
- 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 +40 -19
- 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 +13 -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 +3 -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 +4 -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/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -27
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- 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 +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +47 -37
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +74 -83
- package/src/components/TextEditor/TextEditor.tsx +111 -75
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- package/src/components/TextEditor/hooks.stories.tsx +106 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
- package/src/components/Toolbar/Toolbar.tsx +166 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +54 -45
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +13 -34
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +33 -32
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +22 -18
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +328 -167
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +1 -7
- package/src/extensions/markdown/bundle.ts +23 -28
- package/src/extensions/markdown/code.ts +150 -0
- package/src/extensions/markdown/formatting.ts +188 -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} +16 -7
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -33
- 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 +5 -2
- package/src/hooks/useActionHandler.ts +62 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +153 -0
- package/src/hooks/useTextModel.ts +66 -56
- package/src/index.ts +1 -1
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +11 -8
- 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/comments.stories.d.ts +0 -26
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
- 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 +0 -7
- 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/comments.stories.tsx +0 -315
- 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/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/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
|
@@ -1,315 +0,0 @@
|
|
|
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 } from '@phosphor-icons/react';
|
|
10
|
-
import React, { type FC, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
|
-
|
|
12
|
-
import { TextObject } from '@dxos/echo-schema';
|
|
13
|
-
import { PublicKey } from '@dxos/keys';
|
|
14
|
-
import { Button, DensityProvider } from '@dxos/react-ui';
|
|
15
|
-
import { fixedInsetFlexLayout, mx } from '@dxos/react-ui-theme';
|
|
16
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
17
|
-
|
|
18
|
-
import { MarkdownEditor, TextEditor } from './TextEditor';
|
|
19
|
-
import { comments, type CommentsOptions, Cursor } from '../../extensions';
|
|
20
|
-
import { type CommentRange, type Range, useTextModel } from '../../hooks';
|
|
21
|
-
|
|
22
|
-
faker.seed(101);
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
// Editor
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
const Editor: FC<{
|
|
29
|
-
item: { text: TextObject };
|
|
30
|
-
commentSelected?: string;
|
|
31
|
-
commentRanges: CommentRange[];
|
|
32
|
-
onCreateComment: CommentsOptions['onCreate'];
|
|
33
|
-
onSelectComment: CommentsOptions['onSelect'];
|
|
34
|
-
}> = ({ item, commentSelected, commentRanges, onCreateComment, onSelectComment }) => {
|
|
35
|
-
const model = useTextModel({ text: item.text });
|
|
36
|
-
const editorRef = useRef<EditorView>(null);
|
|
37
|
-
const [selected, setSelected] = useState<string>();
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
if (!editorRef.current?.hasFocus && commentSelected !== selected) {
|
|
40
|
-
const thread = commentRanges.find((range) => range.id === commentSelected);
|
|
41
|
-
if (thread) {
|
|
42
|
-
const { cursor } = thread;
|
|
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
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
setSelected(commentSelected);
|
|
51
|
-
}
|
|
52
|
-
}, [selected, commentRanges, commentSelected]);
|
|
53
|
-
|
|
54
|
-
const extensions = useMemo(() => {
|
|
55
|
-
return [
|
|
56
|
-
comments({
|
|
57
|
-
onCreate: onCreateComment,
|
|
58
|
-
onSelect: onSelectComment,
|
|
59
|
-
}),
|
|
60
|
-
];
|
|
61
|
-
}, []);
|
|
62
|
-
|
|
63
|
-
if (!model) {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// TODO(burdon): Highlight currently selected comment.
|
|
68
|
-
return (
|
|
69
|
-
<div className='flex grow overflow-y-scroll'>
|
|
70
|
-
<MarkdownEditor ref={editorRef} model={model} comments={commentRanges} extensions={extensions} />
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
// Comments thread
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
type CommentThread = {
|
|
80
|
-
id: string;
|
|
81
|
-
range: CommentRange;
|
|
82
|
-
yPos?: number;
|
|
83
|
-
selection?: Range;
|
|
84
|
-
messages: TextObject[];
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const Thread: FC<{
|
|
88
|
-
thread: CommentThread;
|
|
89
|
-
selected: boolean;
|
|
90
|
-
onSelect: () => void;
|
|
91
|
-
onResolve: () => void;
|
|
92
|
-
}> = ({ thread, selected, onSelect, onResolve }) => {
|
|
93
|
-
const [item, setItem] = useState({ text: new TextObject() });
|
|
94
|
-
const model = useTextModel({ text: item.text });
|
|
95
|
-
const editorRef = useRef<EditorView>(null);
|
|
96
|
-
|
|
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
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
105
|
-
useEffect(() => {
|
|
106
|
-
if (selected) {
|
|
107
|
-
containerRef.current?.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
|
108
|
-
if (thread.messages.length === 0) {
|
|
109
|
-
focus();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}, [selected]);
|
|
113
|
-
|
|
114
|
-
const handleCreateMessage = () => {
|
|
115
|
-
const text = model?.text().trim();
|
|
116
|
-
if (text?.length) {
|
|
117
|
-
thread.messages.push(item.text);
|
|
118
|
-
setItem({ text: new TextObject() });
|
|
119
|
-
focus();
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
if (!model) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return (
|
|
128
|
-
<div className={mx('flex flex-col m-1 rounded shadow divide-y bg-white', selected && 'ring')}>
|
|
129
|
-
<div className='flex p-2 text-xs font-mono gap-2 text-neutral-500 font-thin'>
|
|
130
|
-
<span>id:{thread.id.slice(0, 4)}</span>
|
|
131
|
-
<span>from:{thread.selection?.from}</span>
|
|
132
|
-
<span>to:{thread.selection?.to}</span>
|
|
133
|
-
<span>y:{thread.yPos}</span>
|
|
134
|
-
</div>
|
|
135
|
-
|
|
136
|
-
{thread.messages.map((message, i) => (
|
|
137
|
-
// TODO(burdon): Fix default editor padding so content doesn't jump on creating message.
|
|
138
|
-
<div key={i} className='p-2' onClick={() => onSelect()}>
|
|
139
|
-
{message.text}
|
|
140
|
-
</div>
|
|
141
|
-
))}
|
|
142
|
-
|
|
143
|
-
<div ref={containerRef} onClick={() => onSelect()} className='flex py-1'>
|
|
144
|
-
<div className='grow'>
|
|
145
|
-
<TextEditor
|
|
146
|
-
ref={editorRef}
|
|
147
|
-
model={model}
|
|
148
|
-
placeholder={'Enter comment...'}
|
|
149
|
-
extensions={[
|
|
150
|
-
keymap.of([
|
|
151
|
-
{
|
|
152
|
-
key: 'Enter',
|
|
153
|
-
run: () => {
|
|
154
|
-
handleCreateMessage();
|
|
155
|
-
return true;
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
]),
|
|
159
|
-
]}
|
|
160
|
-
/>
|
|
161
|
-
</div>
|
|
162
|
-
<Button variant='ghost' classNames='px-1 mr-1' title='Resolve' onClick={onResolve}>
|
|
163
|
-
<Check />
|
|
164
|
-
</Button>
|
|
165
|
-
</div>
|
|
166
|
-
</div>
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const Sidebar: FC<{
|
|
171
|
-
threads: CommentThread[];
|
|
172
|
-
selected?: string;
|
|
173
|
-
onSelect: (thread: string) => void;
|
|
174
|
-
onResolve: (thread: string) => void;
|
|
175
|
-
}> = ({ threads, selected, onSelect, onResolve }) => {
|
|
176
|
-
// Sort by y-position.
|
|
177
|
-
const sortedThreads = useMemo(() => {
|
|
178
|
-
const sorted = [...threads];
|
|
179
|
-
return sorted.sort(({ yPos: a = Infinity }, { yPos: b = Infinity }) => {
|
|
180
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
181
|
-
});
|
|
182
|
-
}, [threads]);
|
|
183
|
-
|
|
184
|
-
return (
|
|
185
|
-
<DensityProvider density='fine'>
|
|
186
|
-
<div className='flex flex-col grow overflow-y-scroll py-4 gap-4 pr-4'>
|
|
187
|
-
{sortedThreads.map((thread) => (
|
|
188
|
-
<Thread
|
|
189
|
-
key={thread.id}
|
|
190
|
-
thread={thread}
|
|
191
|
-
selected={thread.id === selected}
|
|
192
|
-
onSelect={() => onSelect(thread.id)}
|
|
193
|
-
onResolve={() => onResolve(thread.id)}
|
|
194
|
-
/>
|
|
195
|
-
))}
|
|
196
|
-
</div>
|
|
197
|
-
</DensityProvider>
|
|
198
|
-
);
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
//
|
|
202
|
-
// Story container.
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
type StoryProps = {
|
|
206
|
-
text?: string;
|
|
207
|
-
autoCreate?: boolean;
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
const Story = ({ text, autoCreate }: StoryProps) => {
|
|
211
|
-
const [item] = useState({ text: new TextObject(text) });
|
|
212
|
-
const [threads, setThreads] = useState<CommentThread[]>([]);
|
|
213
|
-
const commentRanges = useMemo(() => threads.map((thread) => thread.range), [threads]);
|
|
214
|
-
const [selected, setSelected] = useState<string>();
|
|
215
|
-
|
|
216
|
-
// Filter by visibility.
|
|
217
|
-
const visibleThreads = useMemo(() => threads.filter((thread) => thread.yPos !== undefined), [threads]);
|
|
218
|
-
|
|
219
|
-
const handleCreateComment: CommentsOptions['onCreate'] = (cursor, location) => {
|
|
220
|
-
const id = PublicKey.random().toHex();
|
|
221
|
-
setThreads((threads) => [
|
|
222
|
-
...threads,
|
|
223
|
-
{
|
|
224
|
-
id,
|
|
225
|
-
range: { id, cursor },
|
|
226
|
-
yPos: location ? Math.floor(location.top) : undefined,
|
|
227
|
-
messages: autoCreate
|
|
228
|
-
? faker.helpers.multiple(() => new TextObject(faker.lorem.sentence()), { count: { min: 2, max: 5 } })
|
|
229
|
-
: [],
|
|
230
|
-
},
|
|
231
|
-
]);
|
|
232
|
-
setSelected(id);
|
|
233
|
-
return id;
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
// TODO(burdon): Scroll sidebar on select.
|
|
237
|
-
const handleSelectComment: CommentsOptions['onSelect'] = ({ active, closest, ranges }) => {
|
|
238
|
-
setThreads((threads) =>
|
|
239
|
-
threads.map((thread) => {
|
|
240
|
-
const range = ranges.find((range) => range.id === thread.range.id);
|
|
241
|
-
if (range) {
|
|
242
|
-
thread.yPos = range.location ? Math.floor(range.location.top) : undefined;
|
|
243
|
-
thread.selection = { from: range.from, to: range.to };
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return thread;
|
|
247
|
-
}),
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
setSelected(active ?? closest);
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
const handleSelectThread = (id: string) => {
|
|
254
|
-
setSelected(id);
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
const handleResolveThread = (id: string) => {
|
|
258
|
-
setThreads((threads) => [...threads.filter((thread) => thread.id !== id)]);
|
|
259
|
-
setSelected(undefined);
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
return (
|
|
263
|
-
<div className={mx(fixedInsetFlexLayout, 'bg-neutral-100')}>
|
|
264
|
-
<div className='flex justify-center h-full gap-8'>
|
|
265
|
-
<div className='flex flex-col h-full w-[600px]'>
|
|
266
|
-
<Editor
|
|
267
|
-
item={item}
|
|
268
|
-
commentSelected={selected}
|
|
269
|
-
commentRanges={commentRanges}
|
|
270
|
-
onCreateComment={handleCreateComment}
|
|
271
|
-
onSelectComment={handleSelectComment}
|
|
272
|
-
/>
|
|
273
|
-
</div>
|
|
274
|
-
|
|
275
|
-
<div className='flex flex-col h-full w-[300px]'>
|
|
276
|
-
<Sidebar
|
|
277
|
-
threads={visibleThreads}
|
|
278
|
-
selected={selected}
|
|
279
|
-
onSelect={handleSelectThread}
|
|
280
|
-
onResolve={handleResolveThread}
|
|
281
|
-
/>
|
|
282
|
-
</div>
|
|
283
|
-
</div>
|
|
284
|
-
</div>
|
|
285
|
-
);
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
export default {
|
|
289
|
-
title: 'react-ui-editor/comments',
|
|
290
|
-
component: MarkdownEditor,
|
|
291
|
-
decorators: [withTheme],
|
|
292
|
-
render: (args: StoryProps) => <Story {...args} />,
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
const str = (...lines: string[]) => lines.join('\n');
|
|
296
|
-
|
|
297
|
-
const document = str(
|
|
298
|
-
'# Comments',
|
|
299
|
-
'',
|
|
300
|
-
str(...faker.helpers.multiple(() => [faker.lorem.paragraph({ min: 5, max: 10 }), ''], { count: 20 }).flat()),
|
|
301
|
-
'',
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
export const Default = {
|
|
305
|
-
args: {
|
|
306
|
-
text: document,
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
export const Demo = {
|
|
311
|
-
args: {
|
|
312
|
-
text: document,
|
|
313
|
-
autoCreate: true,
|
|
314
|
-
},
|
|
315
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxosTheme';
|
|
6
|
-
|
|
7
|
-
import React, { useState } from 'react';
|
|
8
|
-
|
|
9
|
-
import { PublicKey } from '@dxos/react-client';
|
|
10
|
-
import { TextKind } from '@dxos/react-client/echo';
|
|
11
|
-
import { ClientRepeater, textGenerator, useDataGenerator } from '@dxos/react-client/testing';
|
|
12
|
-
import { useId, Input } from '@dxos/react-ui';
|
|
13
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
14
|
-
|
|
15
|
-
import { MarkdownEditor } from './TextEditor';
|
|
16
|
-
import { Replicator, useYjsModel } from '../../testing';
|
|
17
|
-
|
|
18
|
-
export default {
|
|
19
|
-
title: 'react-ui-editor/YJS',
|
|
20
|
-
component: MarkdownEditor,
|
|
21
|
-
decorators: [withTheme],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const replicator = new Replicator(TextKind.PLAIN);
|
|
25
|
-
|
|
26
|
-
const Story = () => {
|
|
27
|
-
const [generate, setGenerate] = useState(false);
|
|
28
|
-
const generateId = useId('generate');
|
|
29
|
-
|
|
30
|
-
const [id] = useState(PublicKey.random().toHex());
|
|
31
|
-
const model = useYjsModel({ id, replicator });
|
|
32
|
-
|
|
33
|
-
useDataGenerator({
|
|
34
|
-
generator: generate ? textGenerator : undefined,
|
|
35
|
-
options: { text: typeof model?.content !== 'string' ? (model?.content as any) : undefined },
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<main className='flex flex-col flex-1 p-4 gap-4'>
|
|
40
|
-
<div id={generateId} className='flex gap-2'>
|
|
41
|
-
<Input.Root>
|
|
42
|
-
<Input.Checkbox checked={generate} onCheckedChange={(checked) => setGenerate(!!checked)} />
|
|
43
|
-
<Input.Label>Start</Input.Label>
|
|
44
|
-
</Input.Root>
|
|
45
|
-
</div>
|
|
46
|
-
<div className='border'>
|
|
47
|
-
<MarkdownEditor model={model} />
|
|
48
|
-
</div>
|
|
49
|
-
</main>
|
|
50
|
-
);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export const Default = {
|
|
54
|
-
// TODO(wittjosiah): Decorator for doing this without clients being initialized?
|
|
55
|
-
render: () => <ClientRepeater count={2} Component={Story} />,
|
|
56
|
-
};
|
package/src/extensions/code.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Range } from '@codemirror/state';
|
|
6
|
-
import {
|
|
7
|
-
ViewPlugin,
|
|
8
|
-
type DecorationSet,
|
|
9
|
-
EditorView,
|
|
10
|
-
type ViewUpdate,
|
|
11
|
-
Decoration,
|
|
12
|
-
type BlockInfo,
|
|
13
|
-
} from '@codemirror/view';
|
|
14
|
-
|
|
15
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
16
|
-
|
|
17
|
-
import { getToken } from '../styles';
|
|
18
|
-
|
|
19
|
-
const CODE_REGEX = /```[\s\S]*?```/gs;
|
|
20
|
-
|
|
21
|
-
// TODO(burdon): Reconcile with theme.
|
|
22
|
-
const styles = EditorView.baseTheme({
|
|
23
|
-
'& .cm-codeblock': {
|
|
24
|
-
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
25
|
-
},
|
|
26
|
-
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
27
|
-
background: getToken('extend.colors.neutral.50'),
|
|
28
|
-
},
|
|
29
|
-
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
30
|
-
background: getToken('extend.colors.neutral.850'),
|
|
31
|
-
},
|
|
32
|
-
'& [aria-readonly="true"] .cm-codeblock': {
|
|
33
|
-
display: 'block',
|
|
34
|
-
paddingInline: '4px !important',
|
|
35
|
-
},
|
|
36
|
-
'& [aria-readonly="true"] .cm-codeblock-first': {
|
|
37
|
-
paddingTop: '4px !important',
|
|
38
|
-
borderTopLeftRadius: '4px',
|
|
39
|
-
borderTopRightRadius: '4px',
|
|
40
|
-
},
|
|
41
|
-
'& [aria-readonly="true"] .cm-codeblock-last': {
|
|
42
|
-
paddingBottom: '4px !important',
|
|
43
|
-
borderBottomLeftRadius: '4px',
|
|
44
|
-
borderBottomRightRadius: '4px',
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
49
|
-
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
50
|
-
const start = lines.findIndex((line) => line.from >= from);
|
|
51
|
-
const end = lines.findIndex((line) => line.to >= to);
|
|
52
|
-
return [start, end];
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const code = () => {
|
|
56
|
-
const getDecorations = (view: EditorView) => {
|
|
57
|
-
const decorations: Range<Decoration>[] = [];
|
|
58
|
-
if (!view.hasFocus || view.state.readOnly) {
|
|
59
|
-
const text = view.state.doc.sliceString(0);
|
|
60
|
-
const matches = text.matchAll(CODE_REGEX);
|
|
61
|
-
const blocks = view.viewportLineBlocks;
|
|
62
|
-
for (const match of matches) {
|
|
63
|
-
const range = getLineRange(blocks, match.index!, match.index! + match[0].length);
|
|
64
|
-
for (let i = range[0]; i <= range[1]; i++) {
|
|
65
|
-
const block = blocks[i];
|
|
66
|
-
decorations.push(
|
|
67
|
-
Decoration.line({
|
|
68
|
-
class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
|
|
69
|
-
}).range(block.from),
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return Decoration.set(decorations);
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
return [
|
|
79
|
-
ViewPlugin.fromClass(
|
|
80
|
-
class {
|
|
81
|
-
hasFocus = false;
|
|
82
|
-
decorations: DecorationSet;
|
|
83
|
-
constructor(view: EditorView) {
|
|
84
|
-
this.decorations = getDecorations(view);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
update(update: ViewUpdate) {
|
|
88
|
-
if (
|
|
89
|
-
// TODO(burdon): Generalize for other extensions.
|
|
90
|
-
this.hasFocus !== update.view.hasFocus ||
|
|
91
|
-
update.startState.readOnly !== update.view.state.readOnly ||
|
|
92
|
-
update.docChanged ||
|
|
93
|
-
update.viewportChanged
|
|
94
|
-
) {
|
|
95
|
-
this.hasFocus = update.view.hasFocus;
|
|
96
|
-
this.decorations = getDecorations(update.view);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
decorations: (value) => value.decorations,
|
|
102
|
-
},
|
|
103
|
-
),
|
|
104
|
-
styles,
|
|
105
|
-
];
|
|
106
|
-
};
|
package/src/extensions/hr.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
Decoration,
|
|
7
|
-
type DecorationSet,
|
|
8
|
-
EditorView,
|
|
9
|
-
MatchDecorator,
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type ViewUpdate,
|
|
12
|
-
WidgetType,
|
|
13
|
-
} from '@codemirror/view';
|
|
14
|
-
|
|
15
|
-
import { getToken } from '../styles';
|
|
16
|
-
|
|
17
|
-
// TODO(burdon): Reconcile with theme.
|
|
18
|
-
const styles = EditorView.baseTheme({
|
|
19
|
-
'& .cm-hr': {
|
|
20
|
-
// TODO(burdon): ???
|
|
21
|
-
// Note that block-level decorations should not have vertical margins,
|
|
22
|
-
borderBottom: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
class HorizontalRuleWidget extends WidgetType {
|
|
27
|
-
constructor(readonly _pos: number) {
|
|
28
|
-
super();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
override eq(other: this) {
|
|
32
|
-
return this._pos === (other as any as HorizontalRuleWidget)._pos;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
override toDOM(view: EditorView) {
|
|
36
|
-
// TODO(burdon): Create <hr> element? Does this clash with markdown parser?
|
|
37
|
-
const el = document.createElement('div');
|
|
38
|
-
el.className = 'cm-hr';
|
|
39
|
-
return el;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const placeholderMatcher = new MatchDecorator({
|
|
44
|
-
regexp: /^---$/g,
|
|
45
|
-
decoration: (match, view, pos) =>
|
|
46
|
-
Decoration.replace({
|
|
47
|
-
widget: new HorizontalRuleWidget(pos),
|
|
48
|
-
}),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
export const hr = () => [
|
|
52
|
-
styles,
|
|
53
|
-
ViewPlugin.fromClass(
|
|
54
|
-
class {
|
|
55
|
-
rules: DecorationSet;
|
|
56
|
-
constructor(view: EditorView) {
|
|
57
|
-
this.rules = placeholderMatcher.createDeco(view);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
update(update: ViewUpdate) {
|
|
61
|
-
this.rules = placeholderMatcher.updateDeco(update, this.rules);
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
decorations: (instance) => instance.rules,
|
|
66
|
-
// TODO(burdon): Is this really atomic (cursor can move into ---).
|
|
67
|
-
provide: (plugin) =>
|
|
68
|
-
EditorView.atomicRanges.of((view) => {
|
|
69
|
-
return view.plugin(plugin)?.rules || Decoration.none;
|
|
70
|
-
}),
|
|
71
|
-
},
|
|
72
|
-
),
|
|
73
|
-
];
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Y from 'yjs';
|
|
6
|
-
|
|
7
|
-
import { arrayToString, stringToArray } from '@dxos/util';
|
|
8
|
-
|
|
9
|
-
import { type CursorConverter } from '../cursor';
|
|
10
|
-
|
|
11
|
-
export const cursorConverter = (text: Y.Text): CursorConverter => ({
|
|
12
|
-
toCursor: (index, assoc) => {
|
|
13
|
-
return arrayToString(Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text, index, assoc)));
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
fromCursor: (cursor) => {
|
|
17
|
-
return (
|
|
18
|
-
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
19
|
-
0
|
|
20
|
-
);
|
|
21
|
-
},
|
|
22
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { expect } from 'chai';
|
|
6
|
-
import * as Y from 'yjs';
|
|
7
|
-
|
|
8
|
-
import { TextObject } from '@dxos/echo-schema';
|
|
9
|
-
import { describe, test } from '@dxos/test';
|
|
10
|
-
import { type YText } from '@dxos/text-model';
|
|
11
|
-
|
|
12
|
-
describe('YJS', () => {
|
|
13
|
-
// https://docs.yjs.dev/api/shared-types/y.text
|
|
14
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
15
|
-
test.skip('absolute position', () => {
|
|
16
|
-
const obj = new TextObject('hello world');
|
|
17
|
-
const index = obj.text.indexOf('world');
|
|
18
|
-
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
19
|
-
|
|
20
|
-
{
|
|
21
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
22
|
-
expect(cursor!.index).to.equal(index);
|
|
23
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
(obj.content as YText).insert(index, 'cruel ');
|
|
27
|
-
expect(obj.text).to.equal('hello cruel world');
|
|
28
|
-
|
|
29
|
-
{
|
|
30
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
31
|
-
expect(cursor!.index).not.to.equal(index);
|
|
32
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { yCollab } from 'y-codemirror.next';
|
|
6
|
-
import type * as YP from 'y-protocols/awareness';
|
|
7
|
-
|
|
8
|
-
import type { YText } from '@dxos/text-model';
|
|
9
|
-
|
|
10
|
-
import { cursorConverter } from './cursor';
|
|
11
|
-
import { Cursor } from '../cursor';
|
|
12
|
-
|
|
13
|
-
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
-
Cursor.converter.of(cursorConverter(content)),
|
|
15
|
-
yCollab(content, awareness),
|
|
16
|
-
];
|
package/src/hooks/automerge.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import get from 'lodash.get';
|
|
6
|
-
|
|
7
|
-
import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
8
|
-
import { isNotNullOrUndefined } from '@dxos/util';
|
|
9
|
-
|
|
10
|
-
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
11
|
-
import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
|
|
12
|
-
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
13
|
-
import { cursorColor } from '../styles';
|
|
14
|
-
|
|
15
|
-
export const createAutomergeModel = ({ space, identity, text }: UseTextModelProps): EditorModel => {
|
|
16
|
-
const obj = text as any as AutomergeTextCompat;
|
|
17
|
-
const doc = getRawDoc(obj, [obj.field]);
|
|
18
|
-
|
|
19
|
-
const awarenessProvider =
|
|
20
|
-
space &&
|
|
21
|
-
new SpaceAwarenessProvider({
|
|
22
|
-
space,
|
|
23
|
-
channel: `automerge.awareness.${obj.id}`,
|
|
24
|
-
info: {
|
|
25
|
-
displayName: identity?.profile?.displayName ?? '',
|
|
26
|
-
color: cursorColor.color,
|
|
27
|
-
lightColor: cursorColor.light,
|
|
28
|
-
},
|
|
29
|
-
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const model: EditorModel = {
|
|
33
|
-
id: obj.id,
|
|
34
|
-
content: doc,
|
|
35
|
-
text: () => get(doc.handle.docSync(), doc.path),
|
|
36
|
-
// TODO(burdon): https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
37
|
-
extension: [
|
|
38
|
-
modelState.init(() => model),
|
|
39
|
-
automerge({ handle: doc.handle, path: doc.path }),
|
|
40
|
-
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
41
|
-
awareness(),
|
|
42
|
-
].filter(isNotNullOrUndefined),
|
|
43
|
-
peer: identity
|
|
44
|
-
? {
|
|
45
|
-
id: identity.identityKey.toHex(),
|
|
46
|
-
name: identity.profile?.displayName,
|
|
47
|
-
}
|
|
48
|
-
: undefined,
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return model;
|
|
52
|
-
};
|