@dxos/react-ui-editor 0.3.11-main.927e4ba → 0.3.11-main.9b81c36
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 +1267 -662
- 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 +1 -2
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- 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/{comments.stories.d.ts → hooks.stories.d.ts} +9 -12
- 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/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 +5 -5
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +2 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- 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/basic.d.ts +1 -1
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +6 -2
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -6
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +2 -2
- 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.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/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 +36 -14
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +12 -0
- 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/package.json +39 -26
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +14 -18
- package/src/components/TextEditor/TextEditor.tsx +27 -16
- 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/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +26 -11
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +9 -9
- package/src/extensions/automerge/semaphore.ts +17 -19
- package/src/extensions/automerge/update-automerge.ts +12 -6
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +7 -4
- package/src/extensions/basic.ts +15 -13
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +113 -65
- package/src/extensions/index.ts +0 -6
- package/src/extensions/markdown/bundle.ts +9 -8
- 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/{hr.ts → markdown/hr.ts} +3 -4
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +13 -4
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +2 -3
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useActionHandler.ts +62 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +143 -19
- package/src/hooks/useTextModel.ts +28 -9
- package/src/index.ts +1 -1
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +9 -6
- package/dist/types/src/components/TextEditor/comments.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/src/components/TextEditor/comments.stories.tsx +0 -357
- package/src/extensions/code.ts +0 -104
- /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
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { EditorState, type Extension, type StateEffect } from '@codemirror/state';
|
|
6
6
|
import { EditorView } from '@codemirror/view';
|
|
7
|
-
import { useFocusableGroup } from '@fluentui/react-tabster';
|
|
7
|
+
// import { useFocusableGroup } from '@fluentui/react-tabster';
|
|
8
8
|
import { vim } from '@replit/codemirror-vim';
|
|
9
9
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
10
10
|
import React, {
|
|
@@ -20,9 +20,10 @@ import React, {
|
|
|
20
20
|
|
|
21
21
|
import { log } from '@dxos/log';
|
|
22
22
|
import { useThemeContext } from '@dxos/react-ui';
|
|
23
|
-
import {
|
|
23
|
+
import { focusRing, mx } from '@dxos/react-ui-theme';
|
|
24
|
+
import { isNotFalsy } from '@dxos/util';
|
|
24
25
|
|
|
25
|
-
import {
|
|
26
|
+
import { createBasicBundle, createMarkdownExtensions } from '../../extensions';
|
|
26
27
|
import { type EditorModel } from '../../hooks';
|
|
27
28
|
import { type ThemeStyles } from '../../styles';
|
|
28
29
|
import { defaultTheme, markdownTheme, textTheme } from '../../themes';
|
|
@@ -86,7 +87,12 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
86
87
|
},
|
|
87
88
|
forwardedRef,
|
|
88
89
|
) => {
|
|
89
|
-
|
|
90
|
+
// TODO(burdon): Hook causes error even if properties are not spread into div.
|
|
91
|
+
// Uncaught TypeError: Cannot read properties of undefined (reading 'relatedTarget')
|
|
92
|
+
// Uses event.detail, which is deprecated (not event.details). At runtime the event has a property `details`.
|
|
93
|
+
// https://github.com/microsoft/tabster/blob/master/src/State/FocusedElement.ts#L348 (e.detail.relatedTarget)
|
|
94
|
+
// https://github.com/microsoft/keyborg/blob/49e49b2c3ba0a5f6cc518ac46825d7551def8109/src/FocusEvent.ts#L58
|
|
95
|
+
// const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
90
96
|
const { themeMode } = useThemeContext();
|
|
91
97
|
|
|
92
98
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
@@ -98,7 +104,12 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
98
104
|
|
|
99
105
|
// Set focus.
|
|
100
106
|
useEffect(() => {
|
|
101
|
-
if (autoFocus) {
|
|
107
|
+
if (autoFocus && !view?.hasFocus) {
|
|
108
|
+
if (view?.state.selection.main?.from === 0) {
|
|
109
|
+
// Start at end of line.
|
|
110
|
+
const { to } = view.state.doc.lineAt(0);
|
|
111
|
+
view?.dispatch({ selection: { anchor: to } });
|
|
112
|
+
}
|
|
102
113
|
view?.focus();
|
|
103
114
|
}
|
|
104
115
|
}, [view, autoFocus]);
|
|
@@ -132,18 +143,19 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
132
143
|
EditorView.contentAttributes.of({ class: slots.content?.className ?? '' }),
|
|
133
144
|
|
|
134
145
|
// State.
|
|
146
|
+
EditorView.editable.of(!readonly),
|
|
135
147
|
EditorState.readOnly.of(!!readonly),
|
|
136
148
|
|
|
137
149
|
// Storage and replication.
|
|
138
150
|
// NOTE: This must come before user extensions.
|
|
139
151
|
model.extension,
|
|
140
152
|
|
|
141
|
-
// TODO(burdon): Factor out (
|
|
153
|
+
// TODO(burdon): Factor out? (Requires special handling for Escape/Enter below).
|
|
142
154
|
editorMode === 'vim' && vim(),
|
|
143
155
|
|
|
144
156
|
// Custom.
|
|
145
157
|
...extensions,
|
|
146
|
-
].filter(
|
|
158
|
+
].filter(isNotFalsy),
|
|
147
159
|
});
|
|
148
160
|
|
|
149
161
|
//
|
|
@@ -175,6 +187,7 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
175
187
|
|
|
176
188
|
// Handles tab/focus.
|
|
177
189
|
// Pressing Escape focuses the outer div (to support tab navigation); pressing Enter refocuses the editor.
|
|
190
|
+
// TODO(burdon): Convert to keymap?
|
|
178
191
|
const handleKeyUp = useCallback(
|
|
179
192
|
(event: KeyboardEvent) => {
|
|
180
193
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
@@ -186,7 +199,7 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
186
199
|
|
|
187
200
|
case 'Escape': {
|
|
188
201
|
if (editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey)) {
|
|
189
|
-
|
|
202
|
+
view?.focus();
|
|
190
203
|
}
|
|
191
204
|
break;
|
|
192
205
|
}
|
|
@@ -200,16 +213,15 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
200
213
|
key={model.id}
|
|
201
214
|
role='none'
|
|
202
215
|
tabIndex={0}
|
|
203
|
-
onKeyUp={handleKeyUp}
|
|
204
216
|
{...slots.root}
|
|
205
|
-
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
217
|
+
// {...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
218
|
+
onKeyUp={handleKeyUp}
|
|
206
219
|
ref={rootRef}
|
|
207
220
|
/>
|
|
208
221
|
);
|
|
209
222
|
},
|
|
210
223
|
);
|
|
211
224
|
|
|
212
|
-
// TODO(burdon): Single-line/scroll.
|
|
213
225
|
export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
214
226
|
({ readonly, placeholder, lineWrapping, theme = textTheme, slots, extensions = [], ...props }, forwardedRef) => {
|
|
215
227
|
const { themeMode } = useThemeContext();
|
|
@@ -218,7 +230,7 @@ export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
218
230
|
<BaseTextEditor
|
|
219
231
|
ref={forwardedRef}
|
|
220
232
|
readonly={readonly}
|
|
221
|
-
extensions={[
|
|
233
|
+
extensions={[createBasicBundle({ themeMode, placeholder, lineWrapping }), ...extensions]}
|
|
222
234
|
theme={theme}
|
|
223
235
|
slots={updatedSlots}
|
|
224
236
|
{...props}
|
|
@@ -235,7 +247,7 @@ export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
235
247
|
<BaseTextEditor
|
|
236
248
|
ref={forwardedRef}
|
|
237
249
|
readonly={readonly}
|
|
238
|
-
extensions={[
|
|
250
|
+
extensions={[createMarkdownExtensions({ themeMode, placeholder }), ...extensions]}
|
|
239
251
|
theme={theme}
|
|
240
252
|
slots={updatedSlots}
|
|
241
253
|
{...props}
|
|
@@ -246,11 +258,10 @@ export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
246
258
|
|
|
247
259
|
export const defaultSlots: TextEditorSlots = {
|
|
248
260
|
root: {
|
|
249
|
-
|
|
250
|
-
className: mx('flex flex-col grow overflow-y-auto', attentionSurface),
|
|
261
|
+
className: mx('grow', focusRing),
|
|
251
262
|
},
|
|
252
263
|
editor: {
|
|
253
|
-
className: '
|
|
264
|
+
className: 'bs-full',
|
|
254
265
|
},
|
|
255
266
|
};
|
|
256
267
|
|
|
@@ -13,7 +13,7 @@ import React, { useEffect, useRef, useState } from 'react';
|
|
|
13
13
|
|
|
14
14
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
15
15
|
import { Repo, type DocHandle } from '@dxos/automerge/automerge-repo';
|
|
16
|
-
import { Filter
|
|
16
|
+
import { Filter } from '@dxos/echo-schema';
|
|
17
17
|
import { type PublicKey } from '@dxos/keys';
|
|
18
18
|
import { Expando, TextObject, useSpace } from '@dxos/react-client/echo';
|
|
19
19
|
import { useIdentity } from '@dxos/react-client/halo';
|
|
@@ -130,7 +130,6 @@ const EchoStory = ({ id, spaceKey }: { id: number; spaceKey: PublicKey }) => {
|
|
|
130
130
|
|
|
131
131
|
export const WithEcho = {
|
|
132
132
|
render: () => {
|
|
133
|
-
setGlobalAutomergePreference(true);
|
|
134
133
|
return (
|
|
135
134
|
<ClientRepeater
|
|
136
135
|
count={2}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
7
|
+
import React from 'react';
|
|
8
|
+
|
|
9
|
+
import { useThemeContext } from '@dxos/react-ui';
|
|
10
|
+
import { fixedInsetFlexLayout, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
11
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
12
|
+
|
|
13
|
+
import { TextEditor } from './TextEditor';
|
|
14
|
+
import {
|
|
15
|
+
code,
|
|
16
|
+
createMarkdownExtensions,
|
|
17
|
+
formatting,
|
|
18
|
+
heading,
|
|
19
|
+
hr,
|
|
20
|
+
image,
|
|
21
|
+
link,
|
|
22
|
+
table,
|
|
23
|
+
tasklist,
|
|
24
|
+
} from '../../extensions';
|
|
25
|
+
import { createDataExtensions, createThemeExtensions, useActionHandler, useTextEditor } from '../../hooks';
|
|
26
|
+
import { markdownTheme } from '../../themes';
|
|
27
|
+
import { Toolbar } from '../Toolbar';
|
|
28
|
+
|
|
29
|
+
// TODO(burdon): Demo toolbar with hooks.
|
|
30
|
+
// TODO(burdon): Build components from hooks and adapters for model/extensions, etc.
|
|
31
|
+
// TODO(burdon): Remove BaseTextEditor.
|
|
32
|
+
// TODO(burdon): Move scrolling container layout/logic into TextEditor/MarkdownEditor components.
|
|
33
|
+
// TODO(burdon): Define keymap in composer framework format.
|
|
34
|
+
|
|
35
|
+
type StoryProps = {
|
|
36
|
+
autoFocus?: boolean;
|
|
37
|
+
placeholder?: string;
|
|
38
|
+
doc?: string;
|
|
39
|
+
readonly?: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const Story = ({ autoFocus, placeholder, doc, readonly }: StoryProps) => {
|
|
43
|
+
const { themeMode } = useThemeContext();
|
|
44
|
+
const { parentRef, view } = useTextEditor({
|
|
45
|
+
autoFocus,
|
|
46
|
+
doc,
|
|
47
|
+
extensions: [
|
|
48
|
+
//
|
|
49
|
+
createDataExtensions({ readonly }),
|
|
50
|
+
createThemeExtensions({
|
|
51
|
+
themeMode,
|
|
52
|
+
theme: markdownTheme,
|
|
53
|
+
slots: {
|
|
54
|
+
// TODO(burdon): Document classes re base theme.
|
|
55
|
+
// Semantic tokens (e.g., replacement of input surface).
|
|
56
|
+
editor: {
|
|
57
|
+
className: 'h-full p-4 bg-white text-black dark:bg-black dark:text-white',
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
// TODO(burdon): Move lineWrapping.
|
|
62
|
+
createMarkdownExtensions({ placeholder, lineWrapping: true }),
|
|
63
|
+
// TODO(burdon): Move into markdown bundle (with React callbacks).
|
|
64
|
+
code(),
|
|
65
|
+
formatting(),
|
|
66
|
+
heading(),
|
|
67
|
+
hr(),
|
|
68
|
+
image(),
|
|
69
|
+
link(),
|
|
70
|
+
table(),
|
|
71
|
+
tasklist(),
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const handleAction = useActionHandler(view);
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
79
|
+
<div className='flex h-full justify-center'>
|
|
80
|
+
<div className='flex flex-col h-full w-[800px]'>
|
|
81
|
+
<Toolbar.Root onAction={handleAction}>
|
|
82
|
+
<Toolbar.Markdown />
|
|
83
|
+
</Toolbar.Root>
|
|
84
|
+
|
|
85
|
+
{/* TODO(burdon): Handle scrolling in component wrapper (like this). */}
|
|
86
|
+
<div role='none' className='h-full overflow-y-auto' ref={parentRef} />
|
|
87
|
+
</div>
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export default {
|
|
94
|
+
title: 'react-ui-editor/useTextEditor',
|
|
95
|
+
component: TextEditor,
|
|
96
|
+
decorators: [withTheme],
|
|
97
|
+
render: (args: StoryProps) => <Story {...args} />,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const Default = {
|
|
101
|
+
args: {
|
|
102
|
+
autoFocus: true,
|
|
103
|
+
placeholder: 'Text...',
|
|
104
|
+
doc: '# Demo\n\nThis is a document.\n\n',
|
|
105
|
+
},
|
|
106
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
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, useEditorView, 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] = useEditorView();
|
|
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.Root onAction={handleAction}>
|
|
58
|
+
<Toolbar.Markdown />
|
|
59
|
+
<Toolbar.Separator />
|
|
60
|
+
<Toolbar.Extended />
|
|
61
|
+
</Toolbar.Root>
|
|
62
|
+
<MarkdownEditor ref={editorRef} model={model} extensions={extensions} />
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default {
|
|
70
|
+
title: 'react-ui-editor/Toolbar',
|
|
71
|
+
component: Toolbar,
|
|
72
|
+
render: (args: any) => <Story {...args} />,
|
|
73
|
+
decorators: [withTheme],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const content = [
|
|
77
|
+
'Demo',
|
|
78
|
+
'',
|
|
79
|
+
'The editor supports **Markdown** styles.',
|
|
80
|
+
'',
|
|
81
|
+
faker.lorem.paragraph({ min: 5, max: 8 }),
|
|
82
|
+
'',
|
|
83
|
+
'',
|
|
84
|
+
].join('\n');
|
|
85
|
+
|
|
86
|
+
export const Default = {
|
|
87
|
+
args: {
|
|
88
|
+
content,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type Icon,
|
|
7
|
+
At,
|
|
8
|
+
CaretRight,
|
|
9
|
+
ChatText,
|
|
10
|
+
Code,
|
|
11
|
+
Image,
|
|
12
|
+
Link,
|
|
13
|
+
ListBullets,
|
|
14
|
+
ListChecks,
|
|
15
|
+
ListNumbers,
|
|
16
|
+
TextStrikethrough,
|
|
17
|
+
Table,
|
|
18
|
+
TextB,
|
|
19
|
+
TextItalic,
|
|
20
|
+
} from '@phosphor-icons/react';
|
|
21
|
+
import { createContext } from '@radix-ui/react-context';
|
|
22
|
+
import React, { type PropsWithChildren } from 'react';
|
|
23
|
+
|
|
24
|
+
import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
|
|
25
|
+
import { getSize } from '@dxos/react-ui-theme';
|
|
26
|
+
|
|
27
|
+
// TODO(burdon): Revert to string to make extensible?
|
|
28
|
+
export type ActionType =
|
|
29
|
+
| 'blockquote'
|
|
30
|
+
| 'bold'
|
|
31
|
+
| 'codeblock'
|
|
32
|
+
| 'comment'
|
|
33
|
+
| 'heading'
|
|
34
|
+
| 'image'
|
|
35
|
+
| 'italic'
|
|
36
|
+
| 'link'
|
|
37
|
+
| 'list'
|
|
38
|
+
| 'list-ordered'
|
|
39
|
+
| 'list-tasks'
|
|
40
|
+
| 'mention'
|
|
41
|
+
| 'prompt'
|
|
42
|
+
| 'strikethrough'
|
|
43
|
+
| 'table';
|
|
44
|
+
|
|
45
|
+
export type Action = {
|
|
46
|
+
type: ActionType;
|
|
47
|
+
data?: any;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type ToolbarProps = PropsWithChildren<{
|
|
51
|
+
onAction?: (action: Action) => void;
|
|
52
|
+
}>;
|
|
53
|
+
|
|
54
|
+
const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
|
|
55
|
+
|
|
56
|
+
const ToolbarRoot = ({ children, onAction }: ToolbarProps) => {
|
|
57
|
+
return (
|
|
58
|
+
<ToolbarContextProvider onAction={onAction}>
|
|
59
|
+
<DensityProvider density='fine'>
|
|
60
|
+
<div role='toolbar' className='flex w-full shrink-0 p-2 gap-4 items-center whitespace-nowrap overflow-hidden'>
|
|
61
|
+
{children}
|
|
62
|
+
</div>
|
|
63
|
+
</DensityProvider>
|
|
64
|
+
</ToolbarContextProvider>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type ToolbarButtonProps = {
|
|
69
|
+
Icon: Icon;
|
|
70
|
+
onClick: () => Action | undefined;
|
|
71
|
+
} & NonNullable<Pick<ButtonProps, 'title'>>;
|
|
72
|
+
|
|
73
|
+
const ToolbarButton = ({ Icon, onClick, title }: ToolbarButtonProps) => {
|
|
74
|
+
const { onAction } = useToolbarContext('ToolbarButton');
|
|
75
|
+
const handleClick = () => {
|
|
76
|
+
const action = onClick();
|
|
77
|
+
if (action) {
|
|
78
|
+
onAction?.(action);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<Button variant='ghost' classNames='p-2' onClick={handleClick} title={title}>
|
|
84
|
+
<Icon className={getSize(5)} />
|
|
85
|
+
</Button>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const ToolbarSeparator = () => <div className='grow' />;
|
|
90
|
+
|
|
91
|
+
const MarkdownHeading = () => {
|
|
92
|
+
const { onAction } = useToolbarContext('MarkdownFormatting');
|
|
93
|
+
return (
|
|
94
|
+
<Select.Root defaultValue='0' onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}>
|
|
95
|
+
<Select.TriggerButton classNames='w-[8rem]' />
|
|
96
|
+
<Select.Portal>
|
|
97
|
+
<Select.Content>
|
|
98
|
+
<Select.Viewport>
|
|
99
|
+
<Select.Option value='0'>Paragraph</Select.Option>
|
|
100
|
+
{[1, 2, 3, 4, 5, 6].map((level) => (
|
|
101
|
+
<Select.Option key={level} value={String(level)}>
|
|
102
|
+
Heading {level}
|
|
103
|
+
</Select.Option>
|
|
104
|
+
))}
|
|
105
|
+
</Select.Viewport>
|
|
106
|
+
</Select.Content>
|
|
107
|
+
</Select.Portal>
|
|
108
|
+
</Select.Root>
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const MarkdownStyles = () => (
|
|
113
|
+
<div role='none'>
|
|
114
|
+
<ToolbarButton Icon={TextB} title='Bold' onClick={() => ({ type: 'bold' })} />
|
|
115
|
+
<ToolbarButton Icon={TextItalic} title='Italic' onClick={() => ({ type: 'italic' })} />
|
|
116
|
+
<ToolbarButton Icon={TextStrikethrough} title='Strike-through' onClick={() => ({ type: 'strikethrough' })} />
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const MarkdownLists = () => (
|
|
121
|
+
<div role='none'>
|
|
122
|
+
<ToolbarButton Icon={ListBullets} title='Bullet list' onClick={() => ({ type: 'list' })} />
|
|
123
|
+
<ToolbarButton Icon={ListNumbers} title='Numbered list' onClick={() => ({ type: 'list-ordered' })} />
|
|
124
|
+
<ToolbarButton Icon={ListChecks} title='Task list' onClick={() => ({ type: 'list-tasks' })} />
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const MarkdownBlocks = () => (
|
|
129
|
+
<div role='none'>
|
|
130
|
+
<ToolbarButton Icon={CaretRight} title='Block quite' onClick={() => ({ type: 'blockquote' })} />
|
|
131
|
+
<ToolbarButton Icon={Code} title='Code block' onClick={() => ({ type: 'codeblock' })} />
|
|
132
|
+
<ToolbarButton Icon={Table} title='Table' onClick={() => ({ type: 'table' })} />
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const MarkdownLinks = () => (
|
|
137
|
+
<div role='none'>
|
|
138
|
+
<ToolbarButton Icon={Link} title='Link' onClick={() => ({ type: 'link' })} />
|
|
139
|
+
<ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} />
|
|
140
|
+
<ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} />
|
|
141
|
+
</div>
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const MarkdownStandard = () => (
|
|
145
|
+
<>
|
|
146
|
+
<MarkdownHeading />
|
|
147
|
+
<MarkdownStyles />
|
|
148
|
+
<MarkdownLists />
|
|
149
|
+
<MarkdownBlocks />
|
|
150
|
+
<MarkdownLinks />
|
|
151
|
+
</>
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const MarkdownExtended = () => (
|
|
155
|
+
<Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
export const Toolbar = {
|
|
159
|
+
Root: ToolbarRoot,
|
|
160
|
+
Button: ToolbarButton,
|
|
161
|
+
Separator: ToolbarSeparator,
|
|
162
|
+
Markdown: MarkdownStandard,
|
|
163
|
+
Extended: MarkdownExtended,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export { useToolbarContext };
|
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { EditorState } from '@codemirror/state';
|
|
6
|
+
import { EditorView } from '@codemirror/view';
|
|
7
|
+
import { render, screen } from '@testing-library/react';
|
|
8
|
+
import chai, { expect } from 'chai';
|
|
9
|
+
import chaiDom from 'chai-dom';
|
|
10
|
+
import get from 'lodash.get';
|
|
11
|
+
import React, { type FC, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { describe, test } from 'vitest';
|
|
13
|
+
|
|
14
|
+
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
15
|
+
|
|
16
|
+
import { automerge } from './automerge';
|
|
17
|
+
|
|
18
|
+
type TestObject = {
|
|
19
|
+
text: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const path = ['text'];
|
|
23
|
+
|
|
24
|
+
class Generator {
|
|
25
|
+
constructor(private readonly _handle: DocHandle<TestObject>) {}
|
|
26
|
+
update(text: string) {
|
|
27
|
+
this._handle.change((doc: TestObject) => {
|
|
28
|
+
doc.text = text;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Test: FC<{ handle: DocHandle<TestObject>; generator: Generator }> = ({ handle, generator }) => {
|
|
34
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
35
|
+
const [view, setView] = useState<EditorView>();
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const extensions = [
|
|
38
|
+
automerge({ handle, path }),
|
|
39
|
+
EditorView.updateListener.of((update) => {
|
|
40
|
+
if (view.state.doc.toString() === 'hello!') {
|
|
41
|
+
// Update editor.
|
|
42
|
+
update.view.dispatch({ changes: { from: view.state.doc.length - 1, insert: ' world' } });
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const view = new EditorView({
|
|
48
|
+
state: EditorState.create({ doc: get(handle.docSync()!, path), extensions }),
|
|
49
|
+
parent: ref.current!,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
setView(view);
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {}, [view]);
|
|
56
|
+
|
|
57
|
+
return <div ref={ref} />;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
chai.use(chaiDom);
|
|
61
|
+
|
|
62
|
+
describe('Automerge', () => {
|
|
63
|
+
test('basic sync', () => {
|
|
64
|
+
const repo = new Repo({ network: [] });
|
|
65
|
+
const handle = repo.create<TestObject>();
|
|
66
|
+
const generator = new Generator(handle);
|
|
67
|
+
render(<Test handle={handle} generator={generator} />);
|
|
68
|
+
expect(screen.getByRole('textbox')).to.have.text('');
|
|
69
|
+
|
|
70
|
+
generator.update('hello!');
|
|
71
|
+
expect(screen.getByRole('textbox')).to.have.text('hello world!');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// TODO(burdon): Test history/undo.
|
|
75
|
+
// TODO(burdon): https://testing-library.com/docs/react-testing-library/example-intro/
|
|
76
|
+
});
|