@dxos/react-ui-editor 0.3.11-main.674a7f9 → 0.3.11-main.6901f09

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 (103) hide show
  1. package/dist/lib/browser/index.mjs +1994 -433
  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 +29 -0
  12. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  13. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +25 -0
  14. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  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 +11 -3
  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 +57 -0
  38. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
  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/extensions/markdown/heading.d.ts.map +1 -1
  42. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  43. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -1
  44. package/dist/types/src/extensions/markdown/index.d.ts +1 -0
  45. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  46. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -1
  47. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -1
  48. package/dist/types/src/hooks/index.d.ts +2 -0
  49. package/dist/types/src/hooks/index.d.ts.map +1 -1
  50. package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
  51. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
  52. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  53. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  54. package/dist/types/src/hooks/useTextEditor.d.ts +36 -14
  55. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  56. package/dist/types/src/hooks/useTextModel.d.ts +12 -0
  57. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  58. package/dist/types/src/index.d.ts +1 -1
  59. package/dist/types/src/index.d.ts.map +1 -1
  60. package/dist/types/src/styles/markdown.d.ts +2 -2
  61. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  62. package/dist/types/src/themes/default.d.ts.map +1 -1
  63. package/package.json +40 -27
  64. package/src/components/TextEditor/MarkdownEditor.stories.tsx +15 -21
  65. package/src/components/TextEditor/TextEditor.tsx +27 -16
  66. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  67. package/src/components/TextEditor/hooks.stories.tsx +111 -0
  68. package/src/components/Toolbar/Toolbar.stories.tsx +102 -0
  69. package/src/components/Toolbar/Toolbar.tsx +245 -0
  70. package/src/components/Toolbar/index.ts +5 -0
  71. package/src/components/index.ts +1 -0
  72. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  73. package/src/extensions/automerge/automerge.test.ts +13 -0
  74. package/src/extensions/automerge/automerge.ts +26 -11
  75. package/src/extensions/automerge/cursor.ts +3 -3
  76. package/src/extensions/automerge/defs.ts +9 -9
  77. package/src/extensions/automerge/semaphore.ts +17 -19
  78. package/src/extensions/automerge/update-automerge.ts +12 -6
  79. package/src/extensions/automerge/update-codemirror.ts +5 -5
  80. package/src/extensions/awareness.ts +7 -4
  81. package/src/extensions/basic.ts +15 -13
  82. package/src/extensions/blast.ts +4 -1
  83. package/src/extensions/comments.ts +126 -68
  84. package/src/extensions/markdown/bundle.ts +9 -8
  85. package/src/extensions/markdown/code.ts +95 -49
  86. package/src/extensions/markdown/formatting.test.ts +481 -0
  87. package/src/extensions/markdown/formatting.ts +1198 -0
  88. package/src/extensions/markdown/heading.ts +5 -6
  89. package/src/extensions/markdown/highlight.ts +9 -15
  90. package/src/extensions/markdown/hr.ts +2 -3
  91. package/src/extensions/markdown/index.ts +1 -0
  92. package/src/extensions/markdown/link.ts +13 -4
  93. package/src/extensions/markdown/tasklist.ts +1 -2
  94. package/src/hooks/index.ts +2 -0
  95. package/src/hooks/useActionHandler.ts +93 -0
  96. package/src/hooks/useEditorView.ts +29 -0
  97. package/src/hooks/useTextEditor.ts +143 -19
  98. package/src/hooks/useTextModel.ts +28 -9
  99. package/src/index.ts +1 -1
  100. package/src/styles/markdown.ts +9 -10
  101. package/src/themes/default.ts +11 -8
  102. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
  103. package/src/components/TextEditor/comments.stories.tsx +0 -357
@@ -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 { attentionSurface, mx } from '@dxos/react-ui-theme';
23
+ import { focusRing, mx } from '@dxos/react-ui-theme';
24
+ import { isNotFalsy } from '@dxos/util';
24
25
 
25
- import { basicBundle, markdownBundle } from '../../extensions';
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
- const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
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 (requires special handling for Escape/focus).
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(Boolean) as Extension[],
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
- rootRef.current?.focus();
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={[basicBundle({ themeMode, placeholder, lineWrapping }), ...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={[markdownBundle({ themeMode, readonly, placeholder }), ...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
- // TODO(burdon): Add focusRing by default/as property?
250
- className: mx('flex flex-col grow overflow-y-auto', attentionSurface),
261
+ className: mx('grow', focusRing),
251
262
  },
252
263
  editor: {
253
- className: 'h-full p-2',
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, setGlobalAutomergePreference } from '@dxos/echo-schema';
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,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
+ };
@@ -0,0 +1,102 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import React, { type FC, useMemo, useState } from 'react';
8
+
9
+ import { TextObject } from '@dxos/echo-schema';
10
+ import { PublicKey } from '@dxos/keys';
11
+ import { faker } from '@dxos/random';
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 {
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';
28
+ import { MarkdownEditor } from '../TextEditor';
29
+
30
+ faker.seed(101);
31
+
32
+ const Story: FC<{ content: string }> = ({ content }) => {
33
+ const [item] = useState({ text: new TextObject(content) });
34
+ const [_comments, setComments] = useState<Comment[]>([]);
35
+ const [editorRef, editorView] = useEditorView();
36
+ const model = useTextModel({ text: item.text });
37
+ const [formattingState, formattingObserver] = useFormattingState();
38
+ const extensions = useMemo(
39
+ () => [
40
+ code(),
41
+ comments({
42
+ onCreate: ({ cursor }) => {
43
+ const id = PublicKey.random().toHex();
44
+ setComments((comments) => [...comments, { id, cursor }]);
45
+ return id;
46
+ },
47
+ }),
48
+ formatting(),
49
+ heading(),
50
+ image(),
51
+ table(),
52
+ tasklist(),
53
+ formattingObserver,
54
+ ],
55
+ [],
56
+ );
57
+
58
+ useComments(editorView, _comments);
59
+ const handleAction = useActionHandler(editorView);
60
+
61
+ if (!model) {
62
+ return null;
63
+ }
64
+
65
+ return (
66
+ <div className={mx(fixedInsetFlexLayout, groupSurface)}>
67
+ <div className='flex h-full justify-center'>
68
+ <div className='flex flex-col h-full w-[800px]'>
69
+ <Toolbar.Root onAction={handleAction} state={formattingState}>
70
+ <Toolbar.Markdown />
71
+ <Toolbar.Separator />
72
+ <Toolbar.Extended />
73
+ </Toolbar.Root>
74
+ <MarkdownEditor ref={editorRef} model={model} extensions={extensions} />
75
+ </div>
76
+ </div>
77
+ </div>
78
+ );
79
+ };
80
+
81
+ export default {
82
+ title: 'react-ui-editor/Toolbar',
83
+ component: Toolbar,
84
+ render: (args: any) => <Story {...args} />,
85
+ decorators: [withTheme],
86
+ };
87
+
88
+ const content = [
89
+ 'Demo',
90
+ '',
91
+ 'The editor supports **Markdown** styles.',
92
+ '',
93
+ faker.lorem.paragraph({ min: 5, max: 8 }),
94
+ '',
95
+ '',
96
+ ].join('\n');
97
+
98
+ export const Default = {
99
+ args: {
100
+ content,
101
+ },
102
+ };
@@ -0,0 +1,245 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import {
6
+ type Icon,
7
+ CaretRight,
8
+ ChatText,
9
+ Code,
10
+ Link,
11
+ ListBullets,
12
+ ListChecks,
13
+ ListNumbers,
14
+ Paragraph,
15
+ TextStrikethrough,
16
+ Table,
17
+ TextB,
18
+ TextItalic,
19
+ } from '@phosphor-icons/react';
20
+ import { createContext } from '@radix-ui/react-context';
21
+ import React, { type PropsWithChildren } from 'react';
22
+
23
+ import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
24
+ import { getSize, mx } from '@dxos/react-ui-theme';
25
+
26
+ import { type Formatting } from '../../extensions';
27
+
28
+ // TODO(burdon): Revert to string to make extensible?
29
+ export type ActionType =
30
+ | 'blockquote'
31
+ | 'strong'
32
+ | 'codeblock'
33
+ | 'comment'
34
+ | 'heading'
35
+ | 'image'
36
+ | 'emphasis'
37
+ | 'code'
38
+ | 'link'
39
+ | 'list-bullet'
40
+ | 'list-ordered'
41
+ | 'list-tasks'
42
+ | 'mention'
43
+ | 'prompt'
44
+ | 'strikethrough'
45
+ | 'table';
46
+
47
+ export type Action = {
48
+ type: ActionType;
49
+ data?: any;
50
+ };
51
+
52
+ export type ToolbarProps = PropsWithChildren<{
53
+ state: Formatting | null;
54
+ onAction?: (action: Action) => void;
55
+ }>;
56
+
57
+ const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
58
+
59
+ const ToolbarRoot = ({ children, onAction, state }: ToolbarProps) => {
60
+ return (
61
+ <ToolbarContextProvider onAction={onAction} state={state}>
62
+ <DensityProvider density='fine'>
63
+ <div role='toolbar' className='flex w-full shrink-0 p-1 gap-2 items-center whitespace-nowrap overflow-hidden'>
64
+ {children}
65
+ </div>
66
+ </DensityProvider>
67
+ </ToolbarContextProvider>
68
+ );
69
+ };
70
+
71
+ type ToolbarButtonProps = {
72
+ Icon: Icon;
73
+ disable?: (state: Formatting) => boolean;
74
+ getState?: (state: Formatting) => boolean;
75
+ onClick: (state: Formatting | null) => Action | undefined;
76
+ } & NonNullable<Pick<ButtonProps, 'title'>>;
77
+
78
+ const ToolbarButton = ({ Icon, onClick, title, getState, disable }: ToolbarButtonProps) => {
79
+ const { onAction, state } = useToolbarContext('ToolbarButton');
80
+ const active = getState && state ? getState(state) : false;
81
+ const disabled = disable && state ? disable(state) : false;
82
+
83
+ const handleClick = (event: React.MouseEvent) => {
84
+ const action = onClick(state);
85
+ if (action) {
86
+ onAction?.(action);
87
+ event.preventDefault();
88
+ }
89
+ };
90
+
91
+ return (
92
+ <Button
93
+ variant='ghost'
94
+ classNames={mx('p-2', active && 'ring-[1px]')}
95
+ onMouseDown={handleClick}
96
+ title={title}
97
+ disabled={disabled}
98
+ >
99
+ <Icon className={getSize(5)} />
100
+ </Button>
101
+ );
102
+ };
103
+
104
+ const ToolbarSeparator = () => <div className='grow' />;
105
+
106
+ const MarkdownHeading = () => {
107
+ const { onAction, state } = useToolbarContext('MarkdownFormatting');
108
+ const blockType = state ? state.blockType : 'paragraph';
109
+ const header = blockType && /heading(\d)/.exec(blockType);
110
+ const value = header ? header[1] : blockType === 'paragraph' || !blockType ? '0' : null;
111
+ return (
112
+ <Select.Root
113
+ disabled={value === null}
114
+ value={value ?? '0'}
115
+ onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
116
+ >
117
+ <Select.TriggerButton>
118
+ <Paragraph className={getSize(5)} />
119
+ </Select.TriggerButton>
120
+ <Select.Portal>
121
+ <Select.Content>
122
+ <Select.Viewport>
123
+ <Select.Option value='0'>Paragraph</Select.Option>
124
+ {[1, 2, 3, 4, 5, 6].map((level) => (
125
+ <Select.Option key={level} value={String(level)}>
126
+ Heading {level}
127
+ </Select.Option>
128
+ ))}
129
+ </Select.Viewport>
130
+ </Select.Content>
131
+ </Select.Portal>
132
+ </Select.Root>
133
+ );
134
+ };
135
+
136
+ const MarkdownStyles = () => (
137
+ <div role='none'>
138
+ <ToolbarButton
139
+ Icon={TextB}
140
+ title='String'
141
+ disable={(s) => s.blockType === 'codeblock'}
142
+ getState={(s) => s.strong}
143
+ onClick={(s) => ({ type: 'strong', data: s ? !s.strong : null })}
144
+ />
145
+ <ToolbarButton
146
+ Icon={TextItalic}
147
+ title='Emphasis'
148
+ disable={(s) => s.blockType === 'codeblock'}
149
+ getState={(s) => s.emphasis}
150
+ onClick={(s) => ({ type: 'emphasis', data: s ? !s.emphasis : null })}
151
+ />
152
+ <ToolbarButton
153
+ Icon={TextStrikethrough}
154
+ title='Strike-through'
155
+ disable={(s) => s.blockType === 'codeblock'}
156
+ getState={(s) => s.strikethrough}
157
+ onClick={(s) => ({ type: 'strikethrough', data: s ? !s.strikethrough : null })}
158
+ />
159
+ <ToolbarButton
160
+ Icon={Code}
161
+ title='Inline code'
162
+ disable={(s) => s.blockType === 'codeblock'}
163
+ getState={(s) => s.code}
164
+ onClick={(s) => ({ type: 'code', data: s ? !s.code : null })}
165
+ />
166
+ </div>
167
+ );
168
+
169
+ const MarkdownLists = () => (
170
+ <div role='none'>
171
+ <ToolbarButton
172
+ Icon={ListBullets}
173
+ title='Bullet list'
174
+ getState={(s) => s.listStyle === 'bullet'}
175
+ onClick={(s) => ({ type: 'list-bullet', data: s ? s.listStyle !== 'bullet' : null })}
176
+ />
177
+ <ToolbarButton
178
+ Icon={ListNumbers}
179
+ title='Numbered list'
180
+ getState={(s) => s.listStyle === 'ordered'}
181
+ onClick={(s) => ({ type: 'list-ordered', data: s ? s.listStyle !== 'ordered' : null })}
182
+ />
183
+ <ToolbarButton
184
+ Icon={ListChecks}
185
+ title='Task list'
186
+ getState={(s) => s.listStyle === 'task'}
187
+ onClick={(s) => ({ type: 'list-tasks', data: s ? s.listStyle !== 'task' : null })}
188
+ />
189
+ </div>
190
+ );
191
+
192
+ const MarkdownBlocks = () => (
193
+ <div role='none'>
194
+ <ToolbarButton
195
+ Icon={CaretRight}
196
+ title='Block quote'
197
+ getState={(s) => s.blockquote}
198
+ onClick={(s) => ({ type: 'blockquote', data: s ? !s.blockquote : null })}
199
+ />
200
+ <ToolbarButton
201
+ Icon={Code}
202
+ title='Code block'
203
+ getState={(s) => s.blockType === 'codeblock'}
204
+ onClick={(s) => ({ type: 'codeblock', data: s ? s.blockType !== 'codeblock' : null })}
205
+ />
206
+ <ToolbarButton Icon={Table} title='Table' onClick={() => ({ type: 'table' })} />
207
+ </div>
208
+ );
209
+
210
+ const MarkdownLinks = () => (
211
+ <div role='none'>
212
+ <ToolbarButton
213
+ Icon={Link}
214
+ title='Link'
215
+ getState={(s) => s.link}
216
+ onClick={(s) => ({ type: 'link', data: s ? !s.link : null })}
217
+ />
218
+ {/* <ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} /> */}
219
+ {/* <ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} /> */}
220
+ </div>
221
+ );
222
+
223
+ const MarkdownStandard = () => (
224
+ <>
225
+ <MarkdownHeading />
226
+ <MarkdownStyles />
227
+ <MarkdownLists />
228
+ <MarkdownBlocks />
229
+ <MarkdownLinks />
230
+ </>
231
+ );
232
+
233
+ const MarkdownExtended = () => (
234
+ <Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
235
+ );
236
+
237
+ export const Toolbar = {
238
+ Root: ToolbarRoot,
239
+ Button: ToolbarButton,
240
+ Separator: ToolbarSeparator,
241
+ Markdown: MarkdownStandard,
242
+ Extended: MarkdownExtended,
243
+ };
244
+
245
+ 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';