@dxos/react-ui-editor 0.3.11-main.73ee990 → 0.3.11-main.764a9c1

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.
Files changed (88) hide show
  1. package/dist/lib/browser/index.mjs +1649 -234
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +1 -2
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  7. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  8. package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
  9. package/dist/types/src/components/TextEditor/{comments.stories.d.ts → hooks.stories.d.ts} +9 -12
  10. package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
  11. package/dist/types/src/components/Toolbar/Toolbar.d.ts +27 -7
  12. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  13. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +12 -1
  14. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
  15. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  16. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  17. package/dist/types/src/components/index.d.ts +1 -0
  18. package/dist/types/src/components/index.d.ts.map +1 -1
  19. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  20. package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
  21. package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
  22. package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
  23. package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
  24. package/dist/types/src/extensions/automerge/defs.d.ts +5 -5
  25. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  26. package/dist/types/src/extensions/automerge/semaphore.d.ts +2 -1
  27. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  28. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  29. package/dist/types/src/extensions/awareness.d.ts +1 -1
  30. package/dist/types/src/extensions/basic.d.ts +1 -1
  31. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  32. package/dist/types/src/extensions/comments.d.ts +6 -2
  33. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  34. package/dist/types/src/extensions/markdown/bundle.d.ts +2 -2
  35. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  36. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -1
  37. package/dist/types/src/extensions/markdown/formatting.d.ts +55 -8
  38. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
  39. package/dist/types/src/extensions/markdown/formatting.test.d.ts +3 -0
  40. package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -0
  41. package/dist/types/src/hooks/index.d.ts +1 -0
  42. package/dist/types/src/hooks/index.d.ts.map +1 -1
  43. package/dist/types/src/hooks/useActionHandler.d.ts +2 -2
  44. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -1
  45. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  46. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  47. package/dist/types/src/hooks/useTextEditor.d.ts +36 -14
  48. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  49. package/dist/types/src/hooks/useTextModel.d.ts +12 -0
  50. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  51. package/dist/types/src/index.d.ts +1 -1
  52. package/dist/types/src/index.d.ts.map +1 -1
  53. package/dist/types/src/themes/default.d.ts.map +1 -1
  54. package/package.json +39 -26
  55. package/src/components/TextEditor/MarkdownEditor.stories.tsx +5 -6
  56. package/src/components/TextEditor/TextEditor.tsx +26 -16
  57. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  58. package/src/components/TextEditor/hooks.stories.tsx +111 -0
  59. package/src/components/Toolbar/Toolbar.stories.tsx +22 -6
  60. package/src/components/Toolbar/Toolbar.tsx +224 -75
  61. package/src/components/Toolbar/index.ts +5 -0
  62. package/src/components/index.ts +1 -0
  63. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  64. package/src/extensions/automerge/automerge.test.ts +13 -0
  65. package/src/extensions/automerge/automerge.ts +27 -11
  66. package/src/extensions/automerge/cursor.ts +3 -3
  67. package/src/extensions/automerge/defs.ts +9 -9
  68. package/src/extensions/automerge/semaphore.ts +17 -19
  69. package/src/extensions/automerge/update-automerge.ts +9 -9
  70. package/src/extensions/automerge/update-codemirror.ts +5 -5
  71. package/src/extensions/awareness.ts +7 -4
  72. package/src/extensions/basic.ts +15 -13
  73. package/src/extensions/blast.ts +4 -1
  74. package/src/extensions/comments.ts +84 -30
  75. package/src/extensions/markdown/bundle.ts +10 -5
  76. package/src/extensions/markdown/code.ts +12 -4
  77. package/src/extensions/markdown/formatting.test.ts +482 -0
  78. package/src/extensions/markdown/formatting.ts +1133 -55
  79. package/src/extensions/markdown/link.ts +8 -2
  80. package/src/hooks/index.ts +1 -0
  81. package/src/hooks/useActionHandler.ts +61 -13
  82. package/src/hooks/useEditorView.ts +29 -0
  83. package/src/hooks/useTextEditor.ts +143 -19
  84. package/src/hooks/useTextModel.ts +28 -9
  85. package/src/index.ts +1 -1
  86. package/src/themes/default.ts +10 -5
  87. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
  88. package/src/components/TextEditor/comments.stories.tsx +0 -357
@@ -0,0 +1,111 @@
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
+ useFormattingState,
25
+ } from '../../extensions';
26
+ import { createDataExtensions, createThemeExtensions, useActionHandler, useTextEditor } from '../../hooks';
27
+ import { markdownTheme } from '../../themes';
28
+ import { Toolbar } from '../Toolbar';
29
+
30
+ // TODO(burdon): Demo toolbar with hooks.
31
+ // TODO(burdon): Build components from hooks and adapters for model/extensions, etc.
32
+ // TODO(burdon): Remove BaseTextEditor.
33
+ // TODO(burdon): Move scrolling container layout/logic into TextEditor/MarkdownEditor components.
34
+ // TODO(burdon): Define keymap in composer framework format.
35
+
36
+ type StoryProps = {
37
+ autoFocus?: boolean;
38
+ placeholder?: string;
39
+ doc?: string;
40
+ readonly?: boolean;
41
+ };
42
+
43
+ const Story = ({ autoFocus, placeholder, doc, readonly }: StoryProps) => {
44
+ const { themeMode } = useThemeContext();
45
+ const [formattingState, trackFormatting] = useFormattingState();
46
+ const { parentRef, view } = useTextEditor({
47
+ autoFocus,
48
+ doc,
49
+ extensions: [
50
+ //
51
+ createDataExtensions({ readonly }),
52
+ createThemeExtensions({
53
+ themeMode,
54
+ theme: markdownTheme,
55
+ slots: {
56
+ // TODO(burdon): Document classes re base theme.
57
+ // Semantic tokens (e.g., replacement of input surface).
58
+ editor: {
59
+ className: 'h-full p-4 bg-white text-black dark:bg-black dark:text-white',
60
+ },
61
+ },
62
+ }),
63
+ // TODO(burdon): Move lineWrapping.
64
+ createMarkdownExtensions({ placeholder, lineWrapping: true }),
65
+ // TODO(burdon): Move into markdown bundle (with React callbacks).
66
+ code(),
67
+ formatting(),
68
+ heading(),
69
+ hr(),
70
+ image(),
71
+ link(),
72
+ table(),
73
+ tasklist(),
74
+ trackFormatting,
75
+ ],
76
+ });
77
+
78
+ const handleAction = useActionHandler(view);
79
+
80
+ // FIXME This doesn't update the state on view changes. Also not
81
+ // sure if view is even guaranteed to exist at this point.
82
+ return (
83
+ <div className={mx(fixedInsetFlexLayout, groupSurface)}>
84
+ <div className='flex h-full justify-center'>
85
+ <div className='flex flex-col h-full w-[800px]'>
86
+ <Toolbar.Root onAction={handleAction} state={formattingState}>
87
+ <Toolbar.Markdown />
88
+ </Toolbar.Root>
89
+
90
+ {/* TODO(burdon): Handle scrolling in component wrapper (like this). */}
91
+ <div role='none' className='h-full overflow-y-auto' ref={parentRef} />
92
+ </div>
93
+ </div>
94
+ </div>
95
+ );
96
+ };
97
+
98
+ export default {
99
+ title: 'react-ui-editor/useTextEditor',
100
+ component: TextEditor,
101
+ decorators: [withTheme],
102
+ render: (args: StoryProps) => <Story {...args} />,
103
+ };
104
+
105
+ export const Default = {
106
+ args: {
107
+ autoFocus: true,
108
+ placeholder: 'Text...',
109
+ doc: '# Demo\n\nThis is a document.\n\n',
110
+ },
111
+ };
@@ -4,17 +4,27 @@
4
4
 
5
5
  import '@dxosTheme';
6
6
 
7
- import { faker } from '@faker-js/faker';
8
7
  import React, { type FC, useMemo, useState } from 'react';
9
8
 
10
9
  import { TextObject } from '@dxos/echo-schema';
11
10
  import { PublicKey } from '@dxos/keys';
11
+ import { faker } from '@dxos/random';
12
12
  import { fixedInsetFlexLayout, groupSurface, mx } from '@dxos/react-ui-theme';
13
13
  import { withTheme } from '@dxos/storybook-utils';
14
14
 
15
15
  import { Toolbar } from './Toolbar';
16
- import { code, comments, formatting, heading, image, table, tasklist, useComments } from '../../extensions';
17
- import { type Comment, useActionHandler, useTextEditor, useTextModel } from '../../hooks';
16
+ import {
17
+ code,
18
+ comments,
19
+ formatting,
20
+ heading,
21
+ image,
22
+ table,
23
+ tasklist,
24
+ useComments,
25
+ useFormattingState,
26
+ } from '../../extensions';
27
+ import { type Comment, useActionHandler, useEditorView, useTextModel } from '../../hooks';
18
28
  import { MarkdownEditor } from '../TextEditor';
19
29
 
20
30
  faker.seed(101);
@@ -22,13 +32,14 @@ faker.seed(101);
22
32
  const Story: FC<{ content: string }> = ({ content }) => {
23
33
  const [item] = useState({ text: new TextObject(content) });
24
34
  const [_comments, setComments] = useState<Comment[]>([]);
25
- const [editorRef, editorView] = useTextEditor();
35
+ const [editorRef, editorView] = useEditorView();
26
36
  const model = useTextModel({ text: item.text });
37
+ const [formattingState, formattingObserver] = useFormattingState();
27
38
  const extensions = useMemo(
28
39
  () => [
29
40
  code(),
30
41
  comments({
31
- onCreate: (cursor) => {
42
+ onCreate: ({ cursor }) => {
32
43
  const id = PublicKey.random().toHex();
33
44
  setComments((comments) => [...comments, { id, cursor }]);
34
45
  return id;
@@ -39,6 +50,7 @@ const Story: FC<{ content: string }> = ({ content }) => {
39
50
  image(),
40
51
  table(),
41
52
  tasklist(),
53
+ formattingObserver,
42
54
  ],
43
55
  [],
44
56
  );
@@ -54,7 +66,11 @@ const Story: FC<{ content: string }> = ({ content }) => {
54
66
  <div className={mx(fixedInsetFlexLayout, groupSurface)}>
55
67
  <div className='flex h-full justify-center'>
56
68
  <div className='flex flex-col h-full w-[800px]'>
57
- <Toolbar onAction={handleAction} />
69
+ <Toolbar.Root onAction={handleAction} state={formattingState}>
70
+ <Toolbar.Markdown />
71
+ <Toolbar.Separator />
72
+ <Toolbar.Extended />
73
+ </Toolbar.Root>
58
74
  <MarkdownEditor ref={editorRef} model={model} extensions={extensions} />
59
75
  </div>
60
76
  </div>
@@ -4,36 +4,56 @@
4
4
 
5
5
  import {
6
6
  type Icon,
7
- At,
8
- Brain,
9
7
  CaretRight,
10
8
  ChatText,
11
9
  Code,
12
- Image,
10
+ CodeBlock,
13
11
  Link,
14
12
  ListBullets,
15
13
  ListChecks,
16
14
  ListNumbers,
15
+ Paragraph,
17
16
  TextStrikethrough,
18
17
  Table,
19
18
  TextB,
19
+ TextHOne,
20
+ TextHTwo,
21
+ TextHThree,
22
+ TextHFour,
23
+ TextHFive,
24
+ TextHSix,
20
25
  TextItalic,
21
26
  } from '@phosphor-icons/react';
22
- import React, { type FC } from 'react';
27
+ import { createContext } from '@radix-ui/react-context';
28
+ import React, { type PropsWithChildren } from 'react';
23
29
 
24
30
  import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
25
- import { getSize } from '@dxos/react-ui-theme';
31
+ import { getSize, mx } from '@dxos/react-ui-theme';
26
32
 
27
- export type Action =
33
+ import { type Formatting } from '../../extensions';
34
+
35
+ const HeadingIcons: { [key: string]: Icon } = {
36
+ '0': Paragraph,
37
+ '1': TextHOne,
38
+ '2': TextHTwo,
39
+ '3': TextHThree,
40
+ '4': TextHFour,
41
+ '5': TextHFive,
42
+ '6': TextHSix,
43
+ };
44
+
45
+ // TODO(burdon): Revert to string to make extensible?
46
+ export type ActionType =
28
47
  | 'blockquote'
29
- | 'bold'
30
- | 'code'
48
+ | 'strong'
49
+ | 'codeblock'
31
50
  | 'comment'
32
51
  | 'heading'
33
52
  | 'image'
34
- | 'italic'
53
+ | 'emphasis'
54
+ | 'code'
35
55
  | 'link'
36
- | 'list'
56
+ | 'list-bullet'
37
57
  | 'list-ordered'
38
58
  | 'list-tasks'
39
59
  | 'mention'
@@ -41,75 +61,204 @@ export type Action =
41
61
  | 'strikethrough'
42
62
  | 'table';
43
63
 
44
- export type ToolbarProps = {
45
- onAction?: (action: { type: Action; data?: any }) => void;
64
+ export type Action = {
65
+ type: ActionType;
66
+ data?: any;
46
67
  };
47
68
 
48
- const IconButton: FC<{ Icon: Icon; title: string } & NonNullable<Pick<ButtonProps, 'onClick'>>> = ({
49
- Icon,
50
- title,
51
- onClick,
52
- }) => (
53
- <Button variant='ghost' classNames='p-2' onClick={onClick} title={title}>
54
- <Icon className={getSize(5)} />
55
- </Button>
56
- );
69
+ export type ToolbarProps = PropsWithChildren<{
70
+ state: Formatting | null;
71
+ onAction?: (action: Action) => void;
72
+ }>;
73
+
74
+ const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
57
75
 
58
- // TODO(burdon): Update state based on current selection?
59
- export const Toolbar = ({ onAction }: ToolbarProps) => {
76
+ const ToolbarRoot = ({ children, onAction, state }: ToolbarProps) => {
60
77
  return (
61
- <DensityProvider density='fine'>
62
- <div role='toolbar' className='flex w-full p-2 items-center gap-4'>
63
- <div>
64
- <Select.Root
65
- defaultValue='0'
66
- onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
67
- >
68
- <Select.TriggerButton classNames='w-[8rem]' />
69
- <Select.Portal>
70
- <Select.Content>
71
- <Select.Viewport>
72
- <Select.Option value='0'>Paragraph</Select.Option>
73
- {[1, 2, 3, 4, 5, 6].map((level) => (
74
- <Select.Option key={level} value={String(level)}>
75
- Heading {level}
76
- </Select.Option>
77
- ))}
78
- </Select.Viewport>
79
- </Select.Content>
80
- </Select.Portal>
81
- </Select.Root>
82
- </div>
83
- <div>
84
- <IconButton Icon={TextB} title='Bold' onClick={() => onAction?.({ type: 'bold' })} />
85
- <IconButton Icon={TextItalic} title='Italic' onClick={() => onAction?.({ type: 'italic' })} />
86
- <IconButton
87
- Icon={TextStrikethrough}
88
- title='Strike-through'
89
- onClick={() => onAction?.({ type: 'strikethrough' })}
90
- />
91
- </div>
92
- <div>
93
- <IconButton Icon={ListBullets} title='Bullet list' onClick={() => onAction?.({ type: 'list' })} />
94
- <IconButton Icon={ListNumbers} title='Numbered list' onClick={() => onAction?.({ type: 'list-ordered' })} />
95
- <IconButton Icon={ListChecks} title='Task list' onClick={() => onAction?.({ type: 'list-tasks' })} />
96
- </div>
97
- <div>
98
- <IconButton Icon={CaretRight} title='Block quite' onClick={() => onAction?.({ type: 'blockquote' })} />
99
- <IconButton Icon={Code} title='Code block' onClick={() => onAction?.({ type: 'code' })} />
100
- <IconButton Icon={Table} title='Table' onClick={() => onAction?.({ type: 'table' })} />
78
+ <ToolbarContextProvider onAction={onAction} state={state}>
79
+ <DensityProvider density='fine'>
80
+ <div role='toolbar' className='flex w-full shrink-0 p-1 gap-4 items-center whitespace-nowrap overflow-hidden'>
81
+ {children}
101
82
  </div>
102
- <div>
103
- <IconButton Icon={Link} title='Link' onClick={() => onAction?.({ type: 'link' })} />
104
- <IconButton Icon={At} title='Mention' onClick={() => onAction?.({ type: 'mention' })} />
105
- <IconButton Icon={Image} title='Image' onClick={() => onAction?.({ type: 'image' })} />
106
- </div>
107
- <div className='grow' />
108
- <div>
109
- <IconButton Icon={Brain} title='AI prompt' onClick={() => onAction?.({ type: 'prompt' })} />
110
- <IconButton Icon={ChatText} title='Create comment' onClick={() => onAction?.({ type: 'comment' })} />
111
- </div>
112
- </div>
113
- </DensityProvider>
83
+ </DensityProvider>
84
+ </ToolbarContextProvider>
85
+ );
86
+ };
87
+
88
+ type ToolbarButtonProps = {
89
+ Icon: Icon;
90
+ disable?: (state: Formatting) => boolean;
91
+ getState?: (state: Formatting) => boolean;
92
+ onClick: (state: Formatting | null) => Action | undefined;
93
+ } & NonNullable<Pick<ButtonProps, 'title'>>;
94
+
95
+ const ToolbarButton = ({ Icon, onClick, title, getState, disable }: ToolbarButtonProps) => {
96
+ const { onAction, state } = useToolbarContext('ToolbarButton');
97
+ const active = getState && state ? getState(state) : false;
98
+ const disabled = disable && state ? disable(state) : false;
99
+
100
+ const handleClick = (event: React.MouseEvent) => {
101
+ const action = onClick(state);
102
+ if (action) {
103
+ onAction?.(action);
104
+ event.preventDefault();
105
+ }
106
+ };
107
+
108
+ return (
109
+ <Button
110
+ variant='ghost'
111
+ classNames={mx('p-2', active && 'ring-[1px]')}
112
+ onMouseDown={handleClick}
113
+ title={title}
114
+ disabled={disabled}
115
+ >
116
+ <Icon className={getSize(5)} />
117
+ </Button>
114
118
  );
115
119
  };
120
+
121
+ const ToolbarSeparator = () => <div className='grow' />;
122
+
123
+ const MarkdownHeading = () => {
124
+ const { onAction, state } = useToolbarContext('MarkdownFormatting');
125
+ const blockType = state ? state.blockType : 'paragraph';
126
+ const header = blockType && /heading(\d)/.exec(blockType);
127
+ const value = header ? header[1] : blockType === 'paragraph' || !blockType ? '0' : undefined;
128
+ const HeadingIcon = HeadingIcons[value ?? '0'];
129
+ return (
130
+ <Select.Root
131
+ disabled={value === null}
132
+ value={value ?? '0'}
133
+ onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
134
+ >
135
+ <Select.TriggerButton>
136
+ <HeadingIcon className={getSize(5)} />
137
+ </Select.TriggerButton>
138
+ <Select.Portal>
139
+ <Select.Content>
140
+ <Select.Viewport>
141
+ <Select.Option value='0'>Paragraph</Select.Option>
142
+ {[1, 2, 3, 4, 5, 6].map((level) => (
143
+ <Select.Option key={level} value={String(level)}>
144
+ Heading {level}
145
+ </Select.Option>
146
+ ))}
147
+ </Select.Viewport>
148
+ </Select.Content>
149
+ </Select.Portal>
150
+ </Select.Root>
151
+ );
152
+ };
153
+
154
+ const MarkdownStyles = () => (
155
+ <div role='none'>
156
+ <ToolbarButton
157
+ Icon={TextB}
158
+ title='String'
159
+ disable={(s) => s.blockType === 'codeblock'}
160
+ getState={(s) => s.strong}
161
+ onClick={(s) => ({ type: 'strong', data: s ? !s.strong : null })}
162
+ />
163
+ <ToolbarButton
164
+ Icon={TextItalic}
165
+ title='Emphasis'
166
+ disable={(s) => s.blockType === 'codeblock'}
167
+ getState={(s) => s.emphasis}
168
+ onClick={(s) => ({ type: 'emphasis', data: s ? !s.emphasis : null })}
169
+ />
170
+ <ToolbarButton
171
+ Icon={TextStrikethrough}
172
+ title='Strike-through'
173
+ disable={(s) => s.blockType === 'codeblock'}
174
+ getState={(s) => s.strikethrough}
175
+ onClick={(s) => ({ type: 'strikethrough', data: s ? !s.strikethrough : null })}
176
+ />
177
+ <ToolbarButton
178
+ Icon={Code}
179
+ title='Inline code'
180
+ disable={(s) => s.blockType === 'codeblock'}
181
+ getState={(s) => s.code}
182
+ onClick={(s) => ({ type: 'code', data: s ? !s.code : null })}
183
+ />
184
+ </div>
185
+ );
186
+
187
+ const MarkdownLists = () => (
188
+ <div role='none'>
189
+ <ToolbarButton
190
+ Icon={ListBullets}
191
+ title='Bullet list'
192
+ getState={(s) => s.listStyle === 'bullet'}
193
+ onClick={(s) => ({ type: 'list-bullet', data: s ? s.listStyle !== 'bullet' : null })}
194
+ />
195
+ <ToolbarButton
196
+ Icon={ListNumbers}
197
+ title='Numbered list'
198
+ getState={(s) => s.listStyle === 'ordered'}
199
+ onClick={(s) => ({ type: 'list-ordered', data: s ? s.listStyle !== 'ordered' : null })}
200
+ />
201
+ <ToolbarButton
202
+ Icon={ListChecks}
203
+ title='Task list'
204
+ getState={(s) => s.listStyle === 'task'}
205
+ onClick={(s) => ({ type: 'list-tasks', data: s ? s.listStyle !== 'task' : null })}
206
+ />
207
+ </div>
208
+ );
209
+
210
+ const MarkdownBlocks = () => (
211
+ <div role='none'>
212
+ <ToolbarButton
213
+ Icon={CaretRight}
214
+ title='Block quote'
215
+ getState={(s) => s.blockQuote}
216
+ onClick={(s) => ({ type: 'blockquote', data: s ? !s.blockQuote : null })}
217
+ />
218
+ <ToolbarButton
219
+ Icon={CodeBlock}
220
+ title='Code block'
221
+ disable={(s) => !s.blankLine}
222
+ getState={(s) => s.blockType === 'codeblock'}
223
+ onClick={(s) => ({ type: 'codeblock', data: s ? s.blockType !== 'codeblock' : null })}
224
+ />
225
+ <ToolbarButton Icon={Table} title='Table' disable={(s) => !s.blankLine} onClick={() => ({ type: 'table' })} />
226
+ </div>
227
+ );
228
+
229
+ const MarkdownLinks = () => (
230
+ <div role='none'>
231
+ <ToolbarButton
232
+ Icon={Link}
233
+ title='Link'
234
+ getState={(s) => s.link}
235
+ onClick={(s) => ({ type: 'link', data: s ? !s.link : null })}
236
+ />
237
+ {/* <ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} /> */}
238
+ {/* <ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} /> */}
239
+ </div>
240
+ );
241
+
242
+ const MarkdownStandard = () => (
243
+ <>
244
+ <MarkdownHeading />
245
+ <MarkdownStyles />
246
+ <MarkdownLists />
247
+ <MarkdownBlocks />
248
+ <MarkdownLinks />
249
+ </>
250
+ );
251
+
252
+ const MarkdownExtended = () => (
253
+ <Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
254
+ );
255
+
256
+ export const Toolbar = {
257
+ Root: ToolbarRoot,
258
+ Button: ToolbarButton,
259
+ Separator: ToolbarSeparator,
260
+ Markdown: MarkdownStandard,
261
+ Extended: MarkdownExtended,
262
+ };
263
+
264
+ export { useToolbarContext };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './Toolbar';
@@ -3,3 +3,4 @@
3
3
  //
4
4
 
5
5
  export * from './TextEditor';
6
+ export * from './Toolbar';
@@ -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
+ });
@@ -0,0 +1,13 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { describe, test } from '@dxos/test';
8
+
9
+ describe('Test', () => {
10
+ test('checks sanity', async () => {
11
+ expect(true).to.be.true;
12
+ });
13
+ });