@dxos/react-ui-editor 0.3.11-main.1caa3af → 0.3.11-main.1d8382a
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 +1492 -989
- 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 +9 -8
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +8 -12
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +5 -2
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/index.d.ts +0 -2
- package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
- package/dist/types/src/{hooks/yjs → components/TextEditor}/yjs.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +4 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{hooks → extensions}/automerge/semaphore.d.ts +4 -4
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/extensions/awareness.d.ts +46 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -0
- package/dist/types/src/extensions/basic.d.ts.map +1 -0
- package/dist/types/src/extensions/blast.d.ts.map +1 -0
- package/dist/types/src/extensions/code.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/comments.d.ts +4 -2
- package/dist/types/src/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/image.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/index.d.ts +3 -2
- package/dist/types/src/extensions/index.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/link.d.ts +1 -0
- package/dist/types/src/extensions/link.d.ts.map +1 -0
- package/dist/types/src/extensions/listener.d.ts +10 -0
- package/dist/types/src/extensions/listener.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -0
- package/dist/types/src/extensions/table.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +3 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +9 -8
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +8 -8
- package/dist/types/src/hooks/yjs.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/cursors.d.ts.map +1 -0
- package/dist/types/src/styles/index.d.ts +1 -0
- package/dist/types/src/styles/index.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +1 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts +2 -2
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/dist/types/src/{components/TextEditor/themes → themes}/default.d.ts +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -0
- package/dist/types/src/themes/index.d.ts.map +1 -0
- package/dist/types/src/util/cursor.d.ts +17 -0
- package/dist/types/src/util/cursor.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/package.json +22 -21
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +24 -24
- package/src/components/TextEditor/TextEditor.stories.tsx +17 -5
- package/src/components/TextEditor/TextEditor.tsx +57 -51
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/index.ts +0 -3
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/{components/TextEditor/extensions → extensions}/autocomplete.ts +18 -17
- package/src/extensions/automerge/automerge.ts +94 -0
- package/src/extensions/automerge/cursor.ts +51 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +7 -0
- package/src/{hooks → extensions}/automerge/semaphore.ts +14 -12
- package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
- package/src/extensions/awareness.ts +316 -0
- package/src/{components/TextEditor/extensions → extensions}/code.ts +4 -5
- package/src/{components/TextEditor/extensions → extensions}/comments.ts +51 -19
- package/src/{components/TextEditor/extensions → extensions}/hr.ts +2 -3
- package/src/extensions/image.ts +87 -0
- package/src/{components/TextEditor/extensions → extensions}/index.ts +3 -2
- package/src/{components/TextEditor/extensions → extensions}/link.ts +112 -58
- package/src/extensions/listener.ts +35 -0
- package/src/{components/TextEditor/extensions → extensions}/markdown/highlight.ts +4 -5
- package/src/{components/TextEditor/extensions → extensions}/table.ts +9 -3
- package/src/{components/TextEditor/extensions → extensions}/tasklist.ts +1 -3
- package/src/{components/TextEditor/extensions → extensions}/typewriter.ts +1 -1
- package/src/extensions/yjs/cursor.ts +21 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +1 -25
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTextModel.ts +21 -94
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +71 -19
- package/src/index.ts +5 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/tokens.ts +3 -0
- package/src/testing/replicator.ts +10 -15
- package/src/{components/TextEditor/themes → themes}/default.ts +40 -34
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -0
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts +0 -9
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts +0 -12
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/typewriter.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/util.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
- package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/semaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/index.d.ts +0 -3
- package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
- package/src/components/TextEditor/extensions/image.ts +0 -69
- package/src/components/TextEditor/extensions/listener.ts +0 -23
- package/src/components/TextEditor/extensions/mermaid.ts +0 -11
- package/src/components/TextEditor/extensions/tooltip.ts +0 -61
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/automerge/plugin.ts +0 -134
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/autocomplete.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/basic.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/blast.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/code.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/hr.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/image.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/bundle.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/highlight.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/index.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/mention.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/table.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/tasklist.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/typewriter.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/yjs/yjs.test.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/themes → themes}/index.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → util}/util.d.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/basic.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/blast.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/bundle.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/mention.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{components/TextEditor/themes → themes}/index.ts +0 -0
- /package/src/{components/TextEditor/extensions → util}/util.ts +0 -0
|
@@ -8,7 +8,7 @@ import { EditorView } from '@codemirror/view';
|
|
|
8
8
|
import { faker } from '@faker-js/faker';
|
|
9
9
|
import { ArrowSquareOut } from '@phosphor-icons/react';
|
|
10
10
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
11
|
-
import React, { type FC, StrictMode,
|
|
11
|
+
import React, { type FC, StrictMode, useState } from 'react';
|
|
12
12
|
import { createRoot } from 'react-dom/client';
|
|
13
13
|
|
|
14
14
|
import { TextObject } from '@dxos/echo-schema';
|
|
@@ -16,7 +16,7 @@ import { PublicKey } from '@dxos/keys';
|
|
|
16
16
|
import { fixedInsetFlexLayout, getSize, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
17
17
|
import { withTheme } from '@dxos/storybook-utils';
|
|
18
18
|
|
|
19
|
-
import { MarkdownEditor, type TextEditorProps
|
|
19
|
+
import { MarkdownEditor, type TextEditorProps } from './TextEditor';
|
|
20
20
|
import {
|
|
21
21
|
autocomplete,
|
|
22
22
|
blast,
|
|
@@ -30,13 +30,10 @@ import {
|
|
|
30
30
|
mention,
|
|
31
31
|
table,
|
|
32
32
|
tasklist,
|
|
33
|
-
tooltip,
|
|
34
33
|
typewriter,
|
|
35
34
|
type CommentsOptions,
|
|
36
|
-
type TooltipOptions,
|
|
37
35
|
type LinkOptions,
|
|
38
|
-
|
|
39
|
-
} from './extensions';
|
|
36
|
+
} from '../../extensions';
|
|
40
37
|
import { type CommentRange, useTextModel } from '../../hooks';
|
|
41
38
|
|
|
42
39
|
// Extensions:
|
|
@@ -125,8 +122,6 @@ const text = {
|
|
|
125
122
|
|
|
126
123
|
paragraphs: str(...faker.helpers.multiple(() => [faker.lorem.paragraph(), ''], { count: 3 }).flat()),
|
|
127
124
|
|
|
128
|
-
mermaid: str('```mermaid', 'graph TD;', 'A-->B;', 'A-->C;', 'B-->D;', 'C-->D;', '```'),
|
|
129
|
-
|
|
130
125
|
footer: str('', '', '', '', '')
|
|
131
126
|
};
|
|
132
127
|
|
|
@@ -169,7 +164,7 @@ const links = [
|
|
|
169
164
|
const hover =
|
|
170
165
|
'rounded-sm text-base text-primary-600 hover:text-primary-500 dark:text-primary-300 hover:dark:text-primary-200';
|
|
171
166
|
|
|
172
|
-
const
|
|
167
|
+
const onHoverLinkTooltip: LinkOptions['onHover'] = (el, url) => {
|
|
173
168
|
const web = new URL(url);
|
|
174
169
|
createRoot(el).render(
|
|
175
170
|
<StrictMode>
|
|
@@ -202,7 +197,7 @@ const onCommentsHover: CommentsOptions['onHover'] = (el) => {
|
|
|
202
197
|
);
|
|
203
198
|
};
|
|
204
199
|
|
|
205
|
-
const
|
|
200
|
+
const onRenderLink: LinkOptions['onRender'] = (el, url) => {
|
|
206
201
|
createRoot(el).render(
|
|
207
202
|
<StrictMode>
|
|
208
203
|
<a href={url} target='_blank' rel='noreferrer' className={hover}>
|
|
@@ -218,7 +213,6 @@ type StoryProps = {
|
|
|
218
213
|
} & Pick<TextEditorProps, 'comments' | 'readonly' | 'extensions' | 'slots'>;
|
|
219
214
|
|
|
220
215
|
const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
221
|
-
const ref = useRef<TextEditorRef>(null);
|
|
222
216
|
const [item] = useState({ text: new TextObject(text, undefined, undefined, { automerge }) });
|
|
223
217
|
const model = useTextModel({ text: item.text });
|
|
224
218
|
if (!model) {
|
|
@@ -229,7 +223,7 @@ const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
|
229
223
|
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
230
224
|
<div className='flex justify-center overflow-y-scroll'>
|
|
231
225
|
<div className='flex flex-col w-[800px] py-16'>
|
|
232
|
-
<MarkdownEditor
|
|
226
|
+
<MarkdownEditor model={model} {...props} />
|
|
233
227
|
<div className='flex shrink-0 h-[300px]'></div>
|
|
234
228
|
</div>
|
|
235
229
|
</div>
|
|
@@ -251,10 +245,9 @@ const extensions = [
|
|
|
251
245
|
code(),
|
|
252
246
|
hr(),
|
|
253
247
|
image(),
|
|
254
|
-
link({ onRender:
|
|
248
|
+
link({ onRender: onRenderLink, onHover: onHoverLinkTooltip }),
|
|
255
249
|
table(),
|
|
256
250
|
tasklist(),
|
|
257
|
-
tooltip({ onHover: onTooltipHover }),
|
|
258
251
|
];
|
|
259
252
|
|
|
260
253
|
export const Default = {
|
|
@@ -265,6 +258,16 @@ export const Readonly = {
|
|
|
265
258
|
render: () => <Story text={document} extensions={extensions} readonly />,
|
|
266
259
|
};
|
|
267
260
|
|
|
261
|
+
const large = faker.helpers.multiple(() => faker.lorem.paragraph({ min: 8, max: 16 }), { count: 20 }).join('\n\n');
|
|
262
|
+
|
|
263
|
+
export const Large = {
|
|
264
|
+
render: () => <Story text={str('# Large Document', '', large)} extensions={[]} />,
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export const Empty = {
|
|
268
|
+
render: () => <Story />,
|
|
269
|
+
};
|
|
270
|
+
|
|
268
271
|
export const NoExtensions = {
|
|
269
272
|
render: () => <Story text={document} />,
|
|
270
273
|
};
|
|
@@ -279,12 +282,13 @@ export const HorizontalRule = {
|
|
|
279
282
|
),
|
|
280
283
|
};
|
|
281
284
|
|
|
282
|
-
export const Tooltips = {
|
|
283
|
-
render: () => <Story text={str(text.links, text.footer)} extensions={[tooltip({ onHover: onTooltipHover })]} />,
|
|
284
|
-
};
|
|
285
|
-
|
|
286
285
|
export const Links = {
|
|
287
|
-
render: () =>
|
|
286
|
+
render: () => (
|
|
287
|
+
<Story
|
|
288
|
+
text={str(text.links, text.footer)}
|
|
289
|
+
extensions={[link({ onHover: onHoverLinkTooltip, onRender: onRenderLink })]}
|
|
290
|
+
/>
|
|
291
|
+
),
|
|
288
292
|
};
|
|
289
293
|
|
|
290
294
|
export const Code = {
|
|
@@ -320,7 +324,7 @@ export const Autocomplete = {
|
|
|
320
324
|
<Story
|
|
321
325
|
text={str('# Autocomplete', '', 'Press CTRL-SPACE', text.footer)}
|
|
322
326
|
extensions={[
|
|
323
|
-
link({ onRender:
|
|
327
|
+
link({ onRender: onRenderLink }),
|
|
324
328
|
autocomplete({
|
|
325
329
|
onSearch: (text) => links.filter(({ label }) => label.toLowerCase().includes(text.toLowerCase())),
|
|
326
330
|
}),
|
|
@@ -344,10 +348,6 @@ export const Mention = {
|
|
|
344
348
|
),
|
|
345
349
|
};
|
|
346
350
|
|
|
347
|
-
export const Mermaid = {
|
|
348
|
-
render: () => <Story text={str('# Mermaid', '', text.mermaid, text.footer)} extensions={[code(), mermaid()]} />,
|
|
349
|
-
};
|
|
350
|
-
|
|
351
351
|
export const Comments = {
|
|
352
352
|
render: () => {
|
|
353
353
|
const [commentsStates, setCommentStates] = useState<CommentRange[]>([]);
|
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|
import '@dxosTheme';
|
|
6
6
|
|
|
7
7
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
8
|
-
import React, {
|
|
8
|
+
import React, { useState } from 'react';
|
|
9
9
|
|
|
10
10
|
import { TextObject } from '@dxos/echo-schema';
|
|
11
11
|
import { fixedInsetFlexLayout, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
12
12
|
import { withTheme } from '@dxos/storybook-utils';
|
|
13
13
|
|
|
14
|
-
import { defaultSlots, TextEditor, type TextEditorProps, type
|
|
15
|
-
import {
|
|
14
|
+
import { defaultSlots, TextEditor, type TextEditorProps, type TextEditorSlots } from './TextEditor';
|
|
15
|
+
import { listener } from '../../extensions';
|
|
16
16
|
import { useTextModel } from '../../hooks';
|
|
17
|
+
import { textTheme } from '../../themes';
|
|
17
18
|
|
|
18
19
|
const initialText = [
|
|
19
20
|
'# TextEditor',
|
|
@@ -30,7 +31,6 @@ const Story = ({
|
|
|
30
31
|
automerge,
|
|
31
32
|
...props
|
|
32
33
|
}: { text?: string; automerge?: boolean } & Pick<TextEditorProps, 'extensions' | 'slots'>) => {
|
|
33
|
-
const ref = useRef<TextEditorRef>(null);
|
|
34
34
|
const [item] = useState({ text: new TextObject(text, undefined, undefined, { automerge }) });
|
|
35
35
|
const model = useTextModel({ text: item.text });
|
|
36
36
|
if (!model) {
|
|
@@ -41,7 +41,7 @@ const Story = ({
|
|
|
41
41
|
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
42
42
|
<div className='flex justify-center overflow-y-scroll'>
|
|
43
43
|
<div className='flex flex-col w-[800px] py-16'>
|
|
44
|
-
<TextEditor
|
|
44
|
+
<TextEditor model={model} {...props} />
|
|
45
45
|
<div className='flex shrink-0 h-[300px]'></div>
|
|
46
46
|
</div>
|
|
47
47
|
</div>
|
|
@@ -67,6 +67,18 @@ export const Default = {
|
|
|
67
67
|
),
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
+
export const Listener = {
|
|
71
|
+
render: () => (
|
|
72
|
+
<Story
|
|
73
|
+
slots={defaultsDeep(
|
|
74
|
+
{ editor: { theme: textTheme, placeholder: 'Enter text...' } } satisfies TextEditorSlots,
|
|
75
|
+
defaultSlots,
|
|
76
|
+
)}
|
|
77
|
+
extensions={[listener({ onChange: (text) => console.log(text) })]}
|
|
78
|
+
/>
|
|
79
|
+
),
|
|
80
|
+
};
|
|
81
|
+
|
|
70
82
|
export const Text = {
|
|
71
83
|
render: () => (
|
|
72
84
|
<Story
|
|
@@ -15,16 +15,17 @@ import React, {
|
|
|
15
15
|
useEffect,
|
|
16
16
|
useImperativeHandle,
|
|
17
17
|
useState,
|
|
18
|
+
useRef,
|
|
18
19
|
} from 'react';
|
|
19
20
|
|
|
20
21
|
import { generateName } from '@dxos/display-name';
|
|
21
22
|
import { useThemeContext } from '@dxos/react-ui';
|
|
22
23
|
import { getColorForValue, inputSurface, mx } from '@dxos/react-ui-theme';
|
|
23
24
|
|
|
24
|
-
import { basicBundle, markdownBundle, setCommentRange } from '
|
|
25
|
-
import { defaultTheme, markdownTheme, textTheme } from './themes';
|
|
25
|
+
import { basicBundle, markdownBundle, setCommentRange } from '../../extensions';
|
|
26
26
|
import { type CommentRange, type EditorModel } from '../../hooks';
|
|
27
27
|
import { type ThemeStyles } from '../../styles';
|
|
28
|
+
import { defaultTheme, markdownTheme, textTheme } from '../../themes';
|
|
28
29
|
|
|
29
30
|
export const EditorModes = ['default', 'vim'] as const;
|
|
30
31
|
export type EditorMode = (typeof EditorModes)[number];
|
|
@@ -34,15 +35,10 @@ export type CursorInfo = {
|
|
|
34
35
|
to: number;
|
|
35
36
|
line: number;
|
|
36
37
|
lines: number;
|
|
38
|
+
length: number;
|
|
37
39
|
after?: string;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
|
-
export type TextEditorRef = {
|
|
41
|
-
root: HTMLDivElement | null;
|
|
42
|
-
state?: EditorState;
|
|
43
|
-
view?: EditorView;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
42
|
export type TextEditorSlots = {
|
|
47
43
|
root?: Omit<ComponentProps<'div'>, 'ref'>;
|
|
48
44
|
editor?: {
|
|
@@ -56,28 +52,32 @@ export type TextEditorSlots = {
|
|
|
56
52
|
|
|
57
53
|
export type TextEditorProps = {
|
|
58
54
|
model: EditorModel;
|
|
59
|
-
|
|
60
|
-
readonly?: boolean;
|
|
61
|
-
|
|
55
|
+
focus?: boolean;
|
|
56
|
+
readonly?: boolean; // TODO(burdon): Move into model.
|
|
57
|
+
comments?: CommentRange[]; // TODO(burdon): Move into extension.
|
|
62
58
|
extensions?: Extension[];
|
|
59
|
+
editorMode?: EditorMode;
|
|
63
60
|
slots?: TextEditorSlots;
|
|
64
61
|
};
|
|
65
62
|
|
|
66
63
|
/**
|
|
67
64
|
* Base text editor.
|
|
68
|
-
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
69
65
|
*/
|
|
70
|
-
export const BaseTextEditor = forwardRef<
|
|
71
|
-
({ model,
|
|
72
|
-
const { themeMode } = useThemeContext();
|
|
66
|
+
export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
67
|
+
({ model, focus, readonly, comments, extensions = [], editorMode, slots = defaultSlots }, forwardedRef) => {
|
|
73
68
|
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
useImperativeHandle(forwardedRef, () =>
|
|
69
|
+
const { themeMode } = useThemeContext();
|
|
70
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
71
|
+
const [view, setView] = useState<EditorView | null>(null);
|
|
72
|
+
// NOTE: This doesn't update useRef.
|
|
73
|
+
useImperativeHandle<EditorView | null, EditorView | null>(forwardedRef, () => view, [view]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (focus && view) {
|
|
76
|
+
view.focus();
|
|
77
|
+
}
|
|
78
|
+
}, [focus, view]);
|
|
79
79
|
|
|
80
|
-
// TODO(burdon): Factor out
|
|
80
|
+
// TODO(burdon): Factor out as extension.
|
|
81
81
|
const { awareness, peer } = model;
|
|
82
82
|
useEffect(() => {
|
|
83
83
|
if (awareness && peer) {
|
|
@@ -99,7 +99,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
99
99
|
}, [view, comments]);
|
|
100
100
|
|
|
101
101
|
useEffect(() => {
|
|
102
|
-
if (!
|
|
102
|
+
if (!model || !rootRef.current) {
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -131,36 +131,35 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
131
131
|
// If the new state is derived from the old state, it will likely not be visible other than the cursor resetting.
|
|
132
132
|
// Ideally this should not happen except when changing between text objects.
|
|
133
133
|
view?.destroy();
|
|
134
|
-
|
|
134
|
+
const newView = new EditorView({
|
|
135
|
+
parent: rootRef.current,
|
|
135
136
|
state,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// dispatch: (transaction, view) => {
|
|
142
|
-
// view.update([transaction]);
|
|
143
|
-
// },
|
|
144
|
-
}),
|
|
137
|
+
// NOTE: Uncomment to spy on all transactions.
|
|
138
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
139
|
+
// dispatch: (transaction, view) => {
|
|
140
|
+
// view.update([transaction]);
|
|
141
|
+
// },
|
|
145
142
|
});
|
|
146
143
|
|
|
144
|
+
setView(newView);
|
|
147
145
|
return () => {
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
newView?.destroy();
|
|
147
|
+
setView(null);
|
|
150
148
|
};
|
|
151
|
-
}, [
|
|
149
|
+
}, [rootRef, model, readonly, editorMode, themeMode]);
|
|
152
150
|
|
|
153
151
|
const handleKeyUp = useCallback(
|
|
154
152
|
(event: KeyboardEvent) => {
|
|
155
153
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
156
154
|
switch (key) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
// TODO(burdon): ???
|
|
156
|
+
// case 'Enter': {
|
|
157
|
+
// view?.contentDOM.focus();
|
|
158
|
+
// break;
|
|
159
|
+
// }
|
|
161
160
|
|
|
162
161
|
case 'Escape': {
|
|
163
|
-
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) &&
|
|
162
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && rootRef.current?.focus();
|
|
164
163
|
break;
|
|
165
164
|
}
|
|
166
165
|
}
|
|
@@ -171,7 +170,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
171
170
|
return (
|
|
172
171
|
<div
|
|
173
172
|
key={model.id}
|
|
174
|
-
ref={
|
|
173
|
+
ref={rootRef}
|
|
175
174
|
tabIndex={0}
|
|
176
175
|
{...slots?.root}
|
|
177
176
|
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
@@ -181,32 +180,39 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
181
180
|
},
|
|
182
181
|
);
|
|
183
182
|
|
|
184
|
-
export const TextEditor = forwardRef<
|
|
185
|
-
({ readonly, extensions = [], slots
|
|
183
|
+
export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
184
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
186
185
|
const { themeMode } = useThemeContext();
|
|
187
|
-
const
|
|
186
|
+
const updatedSlots = defaultsDeep({}, slots, defaultTextSlots);
|
|
188
187
|
return (
|
|
189
188
|
<BaseTextEditor
|
|
190
189
|
ref={forwardedRef}
|
|
191
190
|
readonly={readonly}
|
|
192
|
-
extensions={[
|
|
193
|
-
|
|
191
|
+
extensions={[
|
|
192
|
+
basicBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
193
|
+
...extensions,
|
|
194
|
+
]}
|
|
195
|
+
slots={updatedSlots}
|
|
194
196
|
{...props}
|
|
195
197
|
/>
|
|
196
198
|
);
|
|
197
199
|
},
|
|
198
200
|
);
|
|
199
201
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
// TODO(burdon): Remove (Just provide bundle, slots).
|
|
203
|
+
export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
204
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
202
205
|
const { themeMode } = useThemeContext();
|
|
203
|
-
const
|
|
206
|
+
const updatedSlots = defaultsDeep({}, slots, defaultMarkdownSlots);
|
|
204
207
|
return (
|
|
205
208
|
<BaseTextEditor
|
|
206
209
|
ref={forwardedRef}
|
|
207
210
|
readonly={readonly}
|
|
208
|
-
extensions={[
|
|
209
|
-
|
|
211
|
+
extensions={[
|
|
212
|
+
markdownBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
213
|
+
...extensions,
|
|
214
|
+
]}
|
|
215
|
+
slots={updatedSlots}
|
|
210
216
|
{...props}
|
|
211
217
|
/>
|
|
212
218
|
);
|
|
@@ -2,19 +2,29 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
6
7
|
import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-network-broadcastchannel';
|
|
7
8
|
import { EditorView } from '@codemirror/view';
|
|
9
|
+
import '@preact/signals-react'; // Register react integration
|
|
8
10
|
import { basicSetup } from 'codemirror';
|
|
9
11
|
import get from 'lodash.get';
|
|
10
12
|
import React, { useEffect, useRef, useState } from 'react';
|
|
11
13
|
|
|
12
|
-
// TODO(burdon): Why separate imports?
|
|
13
14
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
14
|
-
import { type DocHandle
|
|
15
|
+
import { Repo, type DocHandle } from '@dxos/automerge/automerge-repo';
|
|
16
|
+
import { Filter, setGlobalAutomergePreference } from '@dxos/echo-schema';
|
|
17
|
+
import { type PublicKey } from '@dxos/keys';
|
|
18
|
+
import { Expando, TextObject, useSpace } from '@dxos/react-client/echo';
|
|
19
|
+
import { useIdentity } from '@dxos/react-client/halo';
|
|
20
|
+
import { ClientRepeater } from '@dxos/react-client/testing';
|
|
21
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
15
22
|
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
23
|
+
import { MarkdownEditor } from './TextEditor';
|
|
24
|
+
import { type IDocHandle, automerge, awareness } from '../../extensions';
|
|
25
|
+
import { useTextModel } from '../../hooks';
|
|
26
|
+
|
|
27
|
+
// TODO(burdon): Move to components.
|
|
18
28
|
|
|
19
29
|
type EditorProps = {
|
|
20
30
|
handle: IDocHandle;
|
|
@@ -28,7 +38,7 @@ const Editor = ({ handle, path }: EditorProps) => {
|
|
|
28
38
|
useEffect(() => {
|
|
29
39
|
const view = (editorRoot.current = new EditorView({
|
|
30
40
|
doc: get(handle.docSync()!, path),
|
|
31
|
-
extensions: [basicSetup,
|
|
41
|
+
extensions: [basicSetup, automerge({ handle, path }), awareness()],
|
|
32
42
|
parent: containerRef.current as any,
|
|
33
43
|
}));
|
|
34
44
|
|
|
@@ -89,3 +99,53 @@ export default {
|
|
|
89
99
|
};
|
|
90
100
|
|
|
91
101
|
export const Default = {};
|
|
102
|
+
|
|
103
|
+
const EchoStory = ({ id, spaceKey }: { id: number; spaceKey: PublicKey }) => {
|
|
104
|
+
const identity = useIdentity();
|
|
105
|
+
const space = useSpace(spaceKey);
|
|
106
|
+
// TODO(dmaretskyi): useQuery doesn't work.
|
|
107
|
+
const [obj] = space?.db.query(Filter.from({ type: 'test' })).objects ?? [];
|
|
108
|
+
|
|
109
|
+
const model = useTextModel({
|
|
110
|
+
text: obj?.content,
|
|
111
|
+
identity,
|
|
112
|
+
space,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!model) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
// <div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
121
|
+
<div className='flex justify-center overflow-y-scroll'>
|
|
122
|
+
<div className='flex flex-col w-[800px] py-16'>
|
|
123
|
+
<MarkdownEditor model={model} />
|
|
124
|
+
<div className='flex shrink-0 h-[300px]'></div>
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
// </div>
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const WithEcho = {
|
|
132
|
+
render: () => {
|
|
133
|
+
setGlobalAutomergePreference(true);
|
|
134
|
+
return (
|
|
135
|
+
<ClientRepeater
|
|
136
|
+
count={2}
|
|
137
|
+
createSpace
|
|
138
|
+
onCreateSpace={async (space) => {
|
|
139
|
+
space.db.add(
|
|
140
|
+
new Expando({
|
|
141
|
+
type: 'test',
|
|
142
|
+
content: new TextObject('Hello world!'),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}}
|
|
146
|
+
Component={EchoStory}
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
},
|
|
150
|
+
decorators: [withTheme],
|
|
151
|
+
};
|
|
@@ -17,7 +17,8 @@ import { type Identity } from '@dxos/react-client/halo';
|
|
|
17
17
|
import { joinCommonSpace, TestBuilder, textGenerator } from '@dxos/react-client/testing';
|
|
18
18
|
import { YText } from '@dxos/text-model';
|
|
19
19
|
|
|
20
|
-
import {
|
|
20
|
+
import { YAwarenessProvider } from '../../hooks';
|
|
21
|
+
import { cursorColor } from '../../styles';
|
|
21
22
|
import { EditorDocument, types as schema } from '../../testing';
|
|
22
23
|
|
|
23
24
|
export default {
|
|
@@ -35,7 +36,7 @@ const updateEditor = async (id: number, editor: HTMLDivElement, identity: Identi
|
|
|
35
36
|
return;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
const { awareness } = new
|
|
39
|
+
const { awareness } = new YAwarenessProvider({ space, doc, channel: `yjs.awareness.${text.id}` });
|
|
39
40
|
awareness.setLocalStateField('user', {
|
|
40
41
|
name: identity.profile?.displayName ?? generateName(identity.identityKey.toHex()),
|
|
41
42
|
// TODO(wittjosiah): Pick colours from theme based on identity key.
|
|
@@ -12,7 +12,7 @@ import { ClientRepeater, textGenerator, useDataGenerator } from '@dxos/react-cli
|
|
|
12
12
|
import { useId, Input } from '@dxos/react-ui';
|
|
13
13
|
import { withTheme } from '@dxos/storybook-utils';
|
|
14
14
|
|
|
15
|
-
import { MarkdownEditor } from '
|
|
15
|
+
import { MarkdownEditor } from './TextEditor';
|
|
16
16
|
import { Replicator, useYjsModel } from '../../testing';
|
|
17
17
|
|
|
18
18
|
export default {
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type CompletionContext,
|
|
14
14
|
type CompletionResult,
|
|
15
15
|
} from '@codemirror/autocomplete';
|
|
16
|
+
import { markdownLanguage } from '@codemirror/lang-markdown';
|
|
16
17
|
import { keymap } from '@codemirror/view';
|
|
17
18
|
|
|
18
19
|
export type AutocompleteResult = Completion;
|
|
@@ -28,32 +29,32 @@ export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
|
|
|
28
29
|
return [
|
|
29
30
|
// https://codemirror.net/docs/ref/#view.keymap
|
|
30
31
|
// https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
|
|
32
|
+
// TODO(burdon): Set custom keymap.
|
|
31
33
|
keymap.of(completionKeymap),
|
|
32
34
|
|
|
33
35
|
// https://codemirror.net/examples/autocompletion
|
|
34
36
|
// https://codemirror.net/docs/ref/#autocomplete.autocompletion
|
|
35
37
|
autocompletion({
|
|
36
|
-
|
|
37
|
-
// defaultKeymap: false,
|
|
38
|
+
defaultKeymap: false,
|
|
38
39
|
|
|
39
40
|
// Don't start unless key press.
|
|
40
41
|
activateOnTyping: false,
|
|
42
|
+
}),
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
],
|
|
44
|
+
// TODO(burdon): Option to create new page?
|
|
45
|
+
// TODO(burdon): Optional decoration via addToOptions
|
|
46
|
+
markdownLanguage.data.of({
|
|
47
|
+
autocomplete: (context: CompletionContext): CompletionResult | null => {
|
|
48
|
+
const match = context.matchBefore(/\w*/);
|
|
49
|
+
if (!match || (match.from === match.to && !context.explicit)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
from: match.from,
|
|
55
|
+
options: onSearch(match.text.toLowerCase()),
|
|
56
|
+
};
|
|
57
|
+
},
|
|
57
58
|
}),
|
|
58
59
|
];
|
|
59
60
|
};
|