@dxos/react-ui-editor 0.8.4-main.c1de068 → 0.8.4-main.e098934

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 (231) hide show
  1. package/dist/lib/browser/index.mjs +2074 -996
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/browser/testing/index.mjs +71 -1
  5. package/dist/lib/browser/testing/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/index.mjs +2074 -996
  7. package/dist/lib/node-esm/index.mjs.map +4 -4
  8. package/dist/lib/node-esm/meta.json +1 -1
  9. package/dist/lib/node-esm/testing/index.mjs +71 -1
  10. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  11. package/dist/types/src/components/{Popover → CommandMenu}/CommandMenu.d.ts +10 -6
  12. package/dist/types/src/components/CommandMenu/CommandMenu.d.ts.map +1 -0
  13. package/dist/types/src/components/CommandMenu/index.d.ts +2 -0
  14. package/dist/types/src/components/CommandMenu/index.d.ts.map +1 -0
  15. package/dist/types/src/components/Editor/Editor.d.ts.map +1 -1
  16. package/dist/types/src/components/EditorToolbar/EditorToolbar.d.ts.map +1 -1
  17. package/dist/types/src/components/EditorToolbar/blocks.d.ts.map +1 -1
  18. package/dist/types/src/components/EditorToolbar/formatting.d.ts.map +1 -1
  19. package/dist/types/src/components/EditorToolbar/headings.d.ts.map +1 -1
  20. package/dist/types/src/components/EditorToolbar/image.d.ts.map +1 -1
  21. package/dist/types/src/components/EditorToolbar/lists.d.ts.map +1 -1
  22. package/dist/types/src/components/EditorToolbar/search.d.ts.map +1 -1
  23. package/dist/types/src/components/EditorToolbar/util.d.ts +2 -2
  24. package/dist/types/src/components/EditorToolbar/util.d.ts.map +1 -1
  25. package/dist/types/src/components/EditorToolbar/view-mode.d.ts +1 -1
  26. package/dist/types/src/components/EditorToolbar/view-mode.d.ts.map +1 -1
  27. package/dist/types/src/components/index.d.ts +1 -1
  28. package/dist/types/src/components/index.d.ts.map +1 -1
  29. package/dist/types/src/defaults.d.ts.map +1 -1
  30. package/dist/types/src/extensions/autocomplete.d.ts +20 -7
  31. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  32. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  33. package/dist/types/src/extensions/automerge/automerge.stories.d.ts +9 -18
  34. package/dist/types/src/extensions/automerge/automerge.stories.d.ts.map +1 -1
  35. package/dist/types/src/extensions/automerge/defs.d.ts +1 -1
  36. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  37. package/dist/types/src/extensions/automerge/sync.d.ts.map +1 -1
  38. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  39. package/dist/types/src/extensions/autoscroll.d.ts +10 -0
  40. package/dist/types/src/extensions/autoscroll.d.ts.map +1 -0
  41. package/dist/types/src/extensions/command/action.d.ts +1 -1
  42. package/dist/types/src/extensions/command/action.d.ts.map +1 -1
  43. package/dist/types/src/extensions/command/command-menu.d.ts +1 -1
  44. package/dist/types/src/extensions/command/command-menu.d.ts.map +1 -1
  45. package/dist/types/src/extensions/command/command.d.ts.map +1 -1
  46. package/dist/types/src/extensions/command/floating-menu.d.ts.map +1 -1
  47. package/dist/types/src/extensions/command/hint.d.ts +2 -7
  48. package/dist/types/src/extensions/command/hint.d.ts.map +1 -1
  49. package/dist/types/src/extensions/command/state.d.ts +1 -1
  50. package/dist/types/src/extensions/command/state.d.ts.map +1 -1
  51. package/dist/types/src/extensions/command/typeahead.d.ts.map +1 -1
  52. package/dist/types/src/extensions/command/useCommandMenu.d.ts +3 -4
  53. package/dist/types/src/extensions/command/useCommandMenu.d.ts.map +1 -1
  54. package/dist/types/src/extensions/comments.d.ts +1 -1
  55. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  56. package/dist/types/src/extensions/dnd.d.ts.map +1 -1
  57. package/dist/types/src/extensions/factories.d.ts +2 -7
  58. package/dist/types/src/extensions/factories.d.ts.map +1 -1
  59. package/dist/types/src/extensions/index.d.ts +2 -0
  60. package/dist/types/src/extensions/index.d.ts.map +1 -1
  61. package/dist/types/src/extensions/markdown/action.d.ts.map +1 -1
  62. package/dist/types/src/extensions/markdown/bundle.d.ts +8 -2
  63. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  64. package/dist/types/src/extensions/markdown/changes.d.ts +1 -1
  65. package/dist/types/src/extensions/markdown/changes.d.ts.map +1 -1
  66. package/dist/types/src/extensions/markdown/decorate.d.ts +9 -1
  67. package/dist/types/src/extensions/markdown/decorate.d.ts.map +1 -1
  68. package/dist/types/src/extensions/markdown/formatting.d.ts +1 -1
  69. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
  70. package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -1
  71. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  72. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -1
  73. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -1
  74. package/dist/types/src/extensions/outliner/outliner.d.ts +1 -1
  75. package/dist/types/src/extensions/outliner/outliner.d.ts.map +1 -1
  76. package/dist/types/src/extensions/outliner/selection.d.ts.map +1 -1
  77. package/dist/types/src/extensions/outliner/tree.d.ts +2 -2
  78. package/dist/types/src/extensions/outliner/tree.d.ts.map +1 -1
  79. package/dist/types/src/extensions/preview/preview.d.ts +3 -6
  80. package/dist/types/src/extensions/preview/preview.d.ts.map +1 -1
  81. package/dist/types/src/extensions/tags/extended-markdown.d.ts +10 -0
  82. package/dist/types/src/extensions/tags/extended-markdown.d.ts.map +1 -0
  83. package/dist/types/src/extensions/tags/extended-markdown.test.d.ts +2 -0
  84. package/dist/types/src/extensions/tags/extended-markdown.test.d.ts.map +1 -0
  85. package/dist/types/src/extensions/tags/index.d.ts +4 -0
  86. package/dist/types/src/extensions/tags/index.d.ts.map +1 -0
  87. package/dist/types/src/extensions/tags/streamer.d.ts +12 -0
  88. package/dist/types/src/extensions/tags/streamer.d.ts.map +1 -0
  89. package/dist/types/src/extensions/tags/xml-tags.d.ts +71 -0
  90. package/dist/types/src/extensions/tags/xml-tags.d.ts.map +1 -0
  91. package/dist/types/src/extensions/tags/xml-util.d.ts +10 -0
  92. package/dist/types/src/extensions/tags/xml-util.d.ts.map +1 -0
  93. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  94. package/dist/types/src/stories/Command.stories.d.ts +12 -4
  95. package/dist/types/src/stories/Command.stories.d.ts.map +1 -1
  96. package/dist/types/src/stories/CommandMenu.stories.d.ts +10 -3
  97. package/dist/types/src/stories/CommandMenu.stories.d.ts.map +1 -1
  98. package/dist/types/src/stories/Comments.stories.d.ts +21 -9
  99. package/dist/types/src/stories/Comments.stories.d.ts.map +1 -1
  100. package/dist/types/src/stories/EditorToolbar.stories.d.ts +39 -2
  101. package/dist/types/src/stories/EditorToolbar.stories.d.ts.map +1 -1
  102. package/dist/types/src/stories/Experimental.stories.d.ts +22 -12
  103. package/dist/types/src/stories/Experimental.stories.d.ts.map +1 -1
  104. package/dist/types/src/stories/Markdown.stories.d.ts +32 -42
  105. package/dist/types/src/stories/Markdown.stories.d.ts.map +1 -1
  106. package/dist/types/src/stories/Outliner.stories.d.ts +15 -20
  107. package/dist/types/src/stories/Outliner.stories.d.ts.map +1 -1
  108. package/dist/types/src/stories/Preview.stories.d.ts +21 -6
  109. package/dist/types/src/stories/Preview.stories.d.ts.map +1 -1
  110. package/dist/types/src/stories/Tags.stories.d.ts +17 -0
  111. package/dist/types/src/stories/Tags.stories.d.ts.map +1 -0
  112. package/dist/types/src/stories/TextEditor.stories.d.ts +38 -51
  113. package/dist/types/src/stories/TextEditor.stories.d.ts.map +1 -1
  114. package/dist/types/src/stories/components/EditorStory.d.ts +3 -6
  115. package/dist/types/src/stories/components/EditorStory.d.ts.map +1 -1
  116. package/dist/types/src/styles/theme.d.ts.map +1 -1
  117. package/dist/types/src/testing/PreviewPopover.d.ts +20 -0
  118. package/dist/types/src/testing/PreviewPopover.d.ts.map +1 -0
  119. package/dist/types/src/testing/index.d.ts +1 -0
  120. package/dist/types/src/testing/index.d.ts.map +1 -1
  121. package/dist/types/src/testing/util.d.ts +1 -0
  122. package/dist/types/src/testing/util.d.ts.map +1 -1
  123. package/dist/types/src/translations.d.ts +1 -1
  124. package/dist/types/src/types/types.d.ts +1 -1
  125. package/dist/types/src/util/cursor.d.ts.map +1 -1
  126. package/dist/types/src/util/debug.d.ts +1 -1
  127. package/dist/types/src/util/debug.d.ts.map +1 -1
  128. package/dist/types/src/util/decorations.d.ts +4 -0
  129. package/dist/types/src/util/decorations.d.ts.map +1 -0
  130. package/dist/types/src/util/dom.d.ts +2 -12
  131. package/dist/types/src/util/dom.d.ts.map +1 -1
  132. package/dist/types/src/util/domino.d.ts +18 -0
  133. package/dist/types/src/util/domino.d.ts.map +1 -0
  134. package/dist/types/src/util/index.d.ts +2 -0
  135. package/dist/types/src/util/index.d.ts.map +1 -1
  136. package/dist/types/src/util/react.d.ts +1 -1
  137. package/dist/types/src/util/react.d.ts.map +1 -1
  138. package/dist/types/tsconfig.tsbuildinfo +1 -1
  139. package/package.json +57 -51
  140. package/src/components/{Popover → CommandMenu}/CommandMenu.tsx +93 -26
  141. package/src/components/{Popover → CommandMenu}/index.ts +0 -2
  142. package/src/components/Editor/Editor.tsx +1 -1
  143. package/src/components/EditorToolbar/EditorToolbar.tsx +40 -30
  144. package/src/components/EditorToolbar/blocks.ts +21 -24
  145. package/src/components/EditorToolbar/formatting.ts +22 -25
  146. package/src/components/EditorToolbar/headings.ts +10 -5
  147. package/src/components/EditorToolbar/image.ts +8 -4
  148. package/src/components/EditorToolbar/lists.ts +16 -19
  149. package/src/components/EditorToolbar/search.ts +8 -4
  150. package/src/components/EditorToolbar/util.ts +16 -5
  151. package/src/components/EditorToolbar/view-mode.ts +11 -6
  152. package/src/components/index.ts +1 -1
  153. package/src/defaults.ts +5 -2
  154. package/src/extensions/autocomplete.ts +204 -54
  155. package/src/extensions/automerge/automerge.stories.tsx +25 -18
  156. package/src/extensions/automerge/automerge.ts +4 -3
  157. package/src/extensions/automerge/defs.ts +1 -1
  158. package/src/extensions/automerge/sync.ts +1 -1
  159. package/src/extensions/automerge/update-automerge.ts +1 -1
  160. package/src/extensions/autoscroll.ts +157 -0
  161. package/src/extensions/awareness/awareness.ts +2 -2
  162. package/src/extensions/command/action.ts +1 -2
  163. package/src/extensions/command/command-menu.ts +7 -6
  164. package/src/extensions/command/command.ts +3 -3
  165. package/src/extensions/command/floating-menu.ts +10 -15
  166. package/src/extensions/command/hint.ts +2 -1
  167. package/src/extensions/command/placeholder.ts +1 -1
  168. package/src/extensions/command/state.ts +4 -3
  169. package/src/extensions/command/typeahead.ts +2 -2
  170. package/src/extensions/command/useCommandMenu.ts +6 -9
  171. package/src/extensions/comments.ts +18 -13
  172. package/src/extensions/dnd.ts +1 -1
  173. package/src/extensions/factories.ts +9 -21
  174. package/src/extensions/folding.tsx +2 -2
  175. package/src/extensions/index.ts +2 -0
  176. package/src/extensions/markdown/action.ts +2 -1
  177. package/src/extensions/markdown/bundle.ts +25 -3
  178. package/src/extensions/markdown/changes.ts +1 -1
  179. package/src/extensions/markdown/decorate.ts +23 -14
  180. package/src/extensions/markdown/formatting.test.ts +6 -6
  181. package/src/extensions/markdown/formatting.ts +3 -3
  182. package/src/extensions/markdown/highlight.ts +1 -1
  183. package/src/extensions/markdown/image.ts +3 -4
  184. package/src/extensions/markdown/link.ts +3 -0
  185. package/src/extensions/markdown/table.ts +7 -1
  186. package/src/extensions/mention.ts +1 -1
  187. package/src/extensions/outliner/outliner.test.ts +3 -2
  188. package/src/extensions/outliner/outliner.ts +6 -5
  189. package/src/extensions/outliner/selection.ts +1 -1
  190. package/src/extensions/outliner/tree.test.ts +2 -1
  191. package/src/extensions/outliner/tree.ts +2 -2
  192. package/src/extensions/preview/preview.ts +59 -62
  193. package/src/extensions/tags/extended-markdown.test.ts +261 -0
  194. package/src/extensions/tags/extended-markdown.ts +78 -0
  195. package/src/extensions/tags/index.ts +7 -0
  196. package/src/extensions/tags/streamer.ts +244 -0
  197. package/src/extensions/tags/xml-tags.ts +335 -0
  198. package/src/extensions/tags/xml-util.ts +94 -0
  199. package/src/hooks/useTextEditor.ts +3 -15
  200. package/src/stories/Command.stories.tsx +24 -31
  201. package/src/stories/CommandMenu.stories.tsx +28 -29
  202. package/src/stories/Comments.stories.tsx +10 -6
  203. package/src/stories/EditorToolbar.stories.tsx +8 -8
  204. package/src/stories/Experimental.stories.tsx +12 -8
  205. package/src/stories/Markdown.stories.tsx +21 -17
  206. package/src/stories/Outliner.stories.tsx +42 -30
  207. package/src/stories/Preview.stories.tsx +30 -29
  208. package/src/stories/Tags.stories.tsx +81 -0
  209. package/src/stories/TextEditor.stories.tsx +40 -34
  210. package/src/stories/components/EditorStory.tsx +9 -10
  211. package/src/styles/theme.ts +8 -6
  212. package/src/testing/PreviewPopover.tsx +78 -0
  213. package/src/testing/index.ts +1 -0
  214. package/src/testing/util.ts +2 -0
  215. package/src/translations.ts +1 -1
  216. package/src/util/cursor.ts +2 -1
  217. package/src/util/debug.ts +2 -2
  218. package/src/util/decorations.ts +21 -0
  219. package/src/util/dom.ts +5 -27
  220. package/src/util/domino.ts +51 -0
  221. package/src/util/index.ts +2 -0
  222. package/src/util/react.tsx +1 -1
  223. package/dist/types/src/components/Popover/CommandMenu.d.ts.map +0 -1
  224. package/dist/types/src/components/Popover/RefDropdownMenu.d.ts +0 -21
  225. package/dist/types/src/components/Popover/RefDropdownMenu.d.ts.map +0 -1
  226. package/dist/types/src/components/Popover/RefPopover.d.ts +0 -34
  227. package/dist/types/src/components/Popover/RefPopover.d.ts.map +0 -1
  228. package/dist/types/src/components/Popover/index.d.ts +0 -4
  229. package/dist/types/src/components/Popover/index.d.ts.map +0 -1
  230. package/src/components/Popover/RefDropdownMenu.tsx +0 -85
  231. package/src/components/Popover/RefPopover.tsx +0 -99
@@ -7,8 +7,9 @@ import { type EditorView } from '@codemirror/view';
7
7
  import { type NodeArg } from '@dxos/app-graph';
8
8
  import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
9
9
 
10
- import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
11
- import { addList, List, removeList } from '../../extensions';
10
+ import { List, addList, removeList } from '../../extensions';
11
+
12
+ import { type EditorToolbarState, createEditorAction, createEditorActionGroup } from './util';
12
13
 
13
14
  const listStyles = {
14
15
  bullet: 'ph--list-bullets--regular',
@@ -26,23 +27,19 @@ const createListGroupAction = (value: string) =>
26
27
  const createListActions = (value: string, getView: () => EditorView) =>
27
28
  Object.entries(listStyles).map(([listStyle, icon]) => {
28
29
  const checked = value === listStyle;
29
- return createEditorAction(
30
- `list-${listStyle}`,
31
- () => {
32
- const view = getView();
33
- if (!view) {
34
- return;
35
- }
36
-
37
- const listType = listStyle === 'ordered' ? List.Ordered : listStyle === 'bullet' ? List.Bullet : List.Task;
38
- if (checked) {
39
- removeList(listType)(view);
40
- } else {
41
- addList(listType)(view);
42
- }
43
- },
44
- { checked, icon },
45
- );
30
+ return createEditorAction(`list-${listStyle}`, { checked, icon }, () => {
31
+ const view = getView();
32
+ if (!view) {
33
+ return;
34
+ }
35
+
36
+ const listType = listStyle === 'ordered' ? List.Ordered : listStyle === 'bullet' ? List.Bullet : List.Task;
37
+ if (checked) {
38
+ removeList(listType)(view);
39
+ } else {
40
+ addList(listType)(view);
41
+ }
42
+ });
46
43
  });
47
44
 
48
45
  export const createLists = (state: EditorToolbarState, getView: () => EditorView) => {
@@ -8,10 +8,14 @@ import { type EditorView } from '@codemirror/view';
8
8
  import { createEditorAction } from './util';
9
9
 
10
10
  const createSearchAction = (getView: () => EditorView) =>
11
- createEditorAction('search', () => openSearchPanel(getView()), {
12
- testId: 'editor.toolbar.search',
13
- icon: 'ph--magnifying-glass--regular',
14
- });
11
+ createEditorAction(
12
+ 'search',
13
+ {
14
+ testId: 'editor.toolbar.search',
15
+ icon: 'ph--magnifying-glass--regular',
16
+ },
17
+ () => openSearchPanel(getView()),
18
+ );
15
19
 
16
20
  export const createSearch = (getView: () => EditorView) => ({
17
21
  nodes: [createSearchAction(getView)],
@@ -7,7 +7,7 @@ import { type Rx } from '@effect-rx/rx-react';
7
7
  import { useMemo } from 'react';
8
8
 
9
9
  import { type Action } from '@dxos/app-graph';
10
- import { live, type Live } from '@dxos/live-object';
10
+ import { type Live, live } from '@dxos/live-object';
11
11
  import { type ThemedClassName } from '@dxos/react-ui';
12
12
  import {
13
13
  type ActionGraphProps,
@@ -53,13 +53,24 @@ export type EditorToolbarProps = ThemedClassName<
53
53
 
54
54
  export type EditorToolbarItem = EditorAction | MenuItemGroup | MenuSeparator;
55
55
 
56
- export const createEditorAction = (id: string, invoke: () => void, properties: Partial<MenuActionProperties>) => {
57
- const { label = [`${id} label`, { ns: translationKey }], ...rest } = properties;
58
- return createMenuAction(id, invoke, { label, ...rest }) as Action<MenuActionProperties>;
56
+ export const createEditorAction = (id: string, props: Partial<MenuActionProperties>, invoke: () => void) => {
57
+ const { label = [`${id} label`, { ns: translationKey }], ...rest } = props;
58
+ return createMenuAction(id, invoke, {
59
+ label,
60
+ ...rest,
61
+ }) as Action<MenuActionProperties>;
59
62
  };
60
63
 
61
64
  export const createEditorActionGroup = (
62
65
  id: string,
63
66
  props: Omit<ToolbarMenuActionGroupProperties, 'icon'>,
64
67
  icon?: string,
65
- ) => createMenuItemGroup(id, { icon, iconOnly: true, ...props });
68
+ ) => {
69
+ const { label = [`${id} label`, { ns: translationKey }], ...rest } = props;
70
+ return createMenuItemGroup(id, {
71
+ label,
72
+ icon,
73
+ iconOnly: true,
74
+ ...rest,
75
+ });
76
+ };
@@ -5,10 +5,11 @@
5
5
  import { type NodeArg } from '@dxos/app-graph';
6
6
  import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
7
7
 
8
- import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
9
8
  import { translationKey } from '../../translations';
10
9
  import { type EditorViewMode } from '../../types';
11
10
 
11
+ import { type EditorToolbarState, createEditorAction, createEditorActionGroup } from './util';
12
+
12
13
  const createViewModeGroupAction = (value: string) =>
13
14
  createEditorActionGroup(
14
15
  'viewMode',
@@ -28,11 +29,15 @@ const createViewModeActions = (value: string, onViewModeChange: (mode: EditorVie
28
29
  readonly: 'ph--pencil-slash--regular',
29
30
  }).map(([viewMode, icon]) => {
30
31
  const checked = viewMode === value;
31
- return createEditorAction(`view-mode--${viewMode}`, () => onViewModeChange(viewMode as EditorViewMode), {
32
- label: [`${viewMode} mode label`, { ns: translationKey }],
33
- checked,
34
- icon,
35
- });
32
+ return createEditorAction(
33
+ `view-mode--${viewMode}`,
34
+ {
35
+ label: [`${viewMode} mode label`, { ns: translationKey }],
36
+ checked,
37
+ icon,
38
+ },
39
+ () => onViewModeChange(viewMode as EditorViewMode),
40
+ );
36
41
  });
37
42
 
38
43
  export const createViewMode = (state: EditorToolbarState, onViewModeChange: (mode: EditorViewMode) => void) => {
@@ -4,4 +4,4 @@
4
4
 
5
5
  export * from './Editor';
6
6
  export * from './EditorToolbar';
7
- export * from './Popover';
7
+ export * from './CommandMenu';
package/src/defaults.ts CHANGED
@@ -28,7 +28,9 @@ export const editorSlots: ThemeExtensionsOptions['slots'] = {
28
28
 
29
29
  export const editorGutter = EditorView.theme({
30
30
  '.cm-gutters': {
31
- background: 'var(--dx-baseSurface)',
31
+ // NOTE: Color required to cover content if scrolling horizontally.
32
+ // TODO(burdon): Non-transparent background clips the focus ring.
33
+ background: 'var(--dx-baseSurface) !important',
32
34
  paddingRight: '1rem',
33
35
  },
34
36
  });
@@ -42,8 +44,9 @@ export const editorMonospace = EditorView.theme({
42
44
  export const editorWithToolbarLayout =
43
45
  'grid grid-cols-1 grid-rows-[min-content_1fr] data-[toolbar=disabled]:grid-rows-[1fr] justify-center content-start overflow-hidden';
44
46
 
47
+ // NOTE: Padding is added to the editor to account for the focus ring (since otherwise the CM gutter will clip it)
45
48
  export const stackItemContentEditorClassNames = (role?: string) =>
46
49
  mx(
47
- 'attention-surface dx-focus-ring-inset data-[toolbar=disabled]:pbs-2',
50
+ 'p-0.5 dx-focus-ring-inset attention-surface data-[toolbar=disabled]:pbs-2',
48
51
  role === 'section' ? '[&_.cm-scroller]:overflow-hidden [&_.cm-scroller]:min-bs-24' : 'min-bs-0',
49
52
  );
@@ -1,69 +1,219 @@
1
1
  //
2
- // Copyright 2023 DXOS.org
2
+ // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import { type Extension, Prec } from '@codemirror/state';
5
6
  import {
6
- autocompletion,
7
- completionKeymap,
8
- type CompletionSource,
9
- type Completion,
10
- type CompletionContext,
11
- type CompletionResult,
12
- } from '@codemirror/autocomplete';
13
- import { markdownLanguage } from '@codemirror/lang-markdown';
14
- import { type Extension } from '@codemirror/state';
15
- import { keymap } from '@codemirror/view';
16
-
17
- export type AutocompleteResult = Completion;
7
+ Decoration,
8
+ type DecorationSet,
9
+ EditorView,
10
+ ViewPlugin,
11
+ type ViewUpdate,
12
+ WidgetType,
13
+ keymap,
14
+ } from '@codemirror/view';
18
15
 
19
16
  export type AutocompleteOptions = {
20
- activateOnTyping?: boolean;
21
- override?: CompletionSource[];
22
- onSearch?: (text: string) => Completion[];
23
- };
17
+ fireIfEmpty?: boolean;
18
+
19
+ /**
20
+ * Callback triggered when Enter is pressed.
21
+ * @param text The current text in the editor
22
+ * @returns true if the editor should reset the document.
23
+ */
24
+ onSubmit?: (text: string) => boolean | void;
24
25
 
25
- // https://codemirror.net/examples/autocompletion
26
- // https://codemirror.net/docs/ref/#autocomplete.autocompletion
27
- // https://codemirror.net/docs/ref/#autocomplete.Completion
26
+ /**
27
+ * Function that returns a list of suggestions based on the current text.
28
+ * @param text The current text before the cursor
29
+ * @returns Array of suggestion strings
30
+ */
31
+ onSuggest?: (text: string) => string[];
32
+
33
+ /**
34
+ * ESC pressed.
35
+ */
36
+ onCancel?: () => void;
37
+ };
28
38
 
29
39
  /**
30
- * Autocomplete extension.
40
+ * Creates an autocomplete extension that shows inline suggestions.
41
+ * Pressing Tab will complete the suggestion.
31
42
  */
32
- export const autocomplete = ({ activateOnTyping, override, onSearch }: AutocompleteOptions = {}): Extension => {
33
- const extensions: Extension[] = [
34
- // https://codemirror.net/docs/ref/#view.keymap
35
- // https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
36
- // TODO(burdon): Set custom keymap.
37
- keymap.of(completionKeymap),
38
-
39
- // https://codemirror.net/examples/autocompletion
40
- // https://codemirror.net/docs/ref/#autocomplete.autocompletion
41
- autocompletion({
42
- override,
43
- activateOnTyping,
44
- // closeOnBlur: false,
45
- // tooltipClass: () => 'rounded-be pbe-1 border-separator',
43
+ export const autocomplete = ({ fireIfEmpty, onSubmit, onSuggest, onCancel }: AutocompleteOptions): Extension => {
44
+ const suggest = ViewPlugin.fromClass(
45
+ class {
46
+ _decorations: DecorationSet;
47
+ _currentSuggestion: string | null = null;
48
+
49
+ constructor(view: EditorView) {
50
+ this._decorations = this.computeDecorations(view);
51
+ }
52
+
53
+ update(update: ViewUpdate) {
54
+ if (update.docChanged || update.selectionSet) {
55
+ this._decorations = this.computeDecorations(update.view);
56
+ }
57
+ }
58
+
59
+ private computeDecorations(view: EditorView): DecorationSet {
60
+ const text = view.state.doc.toString();
61
+ const suggestions = onSuggest?.(text) ?? [];
62
+ if (!suggestions.length) {
63
+ this._currentSuggestion = null;
64
+ return Decoration.none;
65
+ }
66
+
67
+ // Get the first suggestion.
68
+ this._currentSuggestion = suggestions[0];
69
+ const suffix = this._currentSuggestion.slice(text.length);
70
+ if (!suffix) {
71
+ return Decoration.none;
72
+ }
73
+
74
+ // Always show ghost text at the end of the document.
75
+ return Decoration.set([
76
+ Decoration.widget({
77
+ widget: new InlineSuggestionWidget(suffix),
78
+ side: 1,
79
+ }).range(view.state.doc.length),
80
+ ]);
81
+ }
82
+
83
+ completeSuggestion(view: EditorView): boolean {
84
+ if (!this._currentSuggestion) {
85
+ return false;
86
+ }
87
+
88
+ const text = view.state.doc.toString();
89
+ const suffix = this._currentSuggestion.slice(text.length);
90
+ if (!suffix) {
91
+ return false;
92
+ }
93
+
94
+ view.dispatch({
95
+ changes: {
96
+ from: view.state.doc.length,
97
+ insert: suffix,
98
+ },
99
+ selection: {
100
+ anchor: view.state.doc.length + suffix.length,
101
+ },
102
+ });
103
+
104
+ return true;
105
+ }
106
+ },
107
+ {
108
+ decorations: (v) => v._decorations,
109
+ },
110
+ );
111
+
112
+ return [
113
+ suggest,
114
+ EditorView.theme({
115
+ '.cm-inline-suggestion': {
116
+ opacity: 0.4,
117
+ },
46
118
  }),
47
- ];
48
119
 
49
- if (onSearch) {
50
- extensions.push(
51
- // TODO(burdon): Optional decoration via addToOptions.
52
- markdownLanguage.data.of({
53
- autocomplete: (context: CompletionContext): CompletionResult | null => {
54
- const match = context.matchBefore(/\w*/);
55
- if (!match || (match.from === match.to && !context.explicit)) {
56
- return null;
57
- }
58
-
59
- return {
60
- from: match.from,
61
- options: onSearch(match.text.toLowerCase()),
62
- };
120
+ Prec.highest(
121
+ keymap.of([
122
+ {
123
+ key: 'Tab',
124
+ preventDefault: true,
125
+ run: (view) => {
126
+ const plugin = view.plugin(suggest);
127
+ return plugin?.completeSuggestion(view) ?? false;
128
+ },
63
129
  },
64
- }),
65
- );
66
- }
130
+ {
131
+ key: 'ArrowRight',
132
+ preventDefault: true,
133
+ run: (view) => {
134
+ // Only complete if cursor is at the end
135
+ if (view.state.selection.main.head !== view.state.doc.length) {
136
+ return false;
137
+ }
138
+
139
+ const plugin = view.plugin(suggest);
140
+ return plugin?.completeSuggestion(view) ?? false;
141
+ },
142
+ },
143
+ {
144
+ key: 'Enter',
145
+ preventDefault: true,
146
+ run: (view) => {
147
+ const text = view.state.doc.toString().trim();
148
+ if (onSubmit && (fireIfEmpty || text.length > 0)) {
149
+ const reset = onSubmit(text);
150
+
151
+ // Clear the document after calling onEnter.
152
+ if (reset) {
153
+ view.dispatch({
154
+ changes: {
155
+ from: 0,
156
+ to: view.state.doc.length,
157
+ insert: '',
158
+ },
159
+ });
160
+ }
161
+ }
67
162
 
68
- return extensions;
163
+ return true;
164
+ },
165
+ },
166
+ {
167
+ key: 'Shift-Enter',
168
+ preventDefault: true,
169
+ run: (view) => {
170
+ view.dispatch({
171
+ changes: {
172
+ from: view.state.selection.main.head,
173
+ insert: '\n',
174
+ },
175
+ selection: {
176
+ anchor: view.state.selection.main.head + 1,
177
+ head: view.state.selection.main.head + 1,
178
+ },
179
+ });
180
+ return true;
181
+ },
182
+ },
183
+ {
184
+ key: 'Escape',
185
+ preventDefault: true,
186
+ run: (view) => {
187
+ // Clear the entire document.
188
+ view.dispatch({
189
+ changes: {
190
+ from: 0,
191
+ to: view.state.doc.length,
192
+ insert: '',
193
+ },
194
+ });
195
+ onCancel?.();
196
+ return true;
197
+ },
198
+ },
199
+ ]),
200
+ ),
201
+ ];
69
202
  };
203
+
204
+ class InlineSuggestionWidget extends WidgetType {
205
+ constructor(private suffix: string) {
206
+ super();
207
+ }
208
+
209
+ override toDOM(): HTMLSpanElement {
210
+ const span = document.createElement('span');
211
+ span.textContent = this.suffix;
212
+ span.className = 'cm-inline-suggestion';
213
+ return span;
214
+ }
215
+
216
+ override eq(other: InlineSuggestionWidget): boolean {
217
+ return other.suffix === this.suffix;
218
+ }
219
+ }
@@ -8,14 +8,15 @@ import '@preact/signals-react';
8
8
 
9
9
  import { Repo } from '@automerge/automerge-repo';
10
10
  import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-network-broadcastchannel';
11
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
11
12
  import React, { useEffect, useState } from 'react';
12
13
 
13
14
  import { Obj, Ref, Type } from '@dxos/echo';
14
- import { DocAccessor, createDocAccessor, useQuery, useSpace, type Space, Query } from '@dxos/react-client/echo';
15
- import { useIdentity, type Identity } from '@dxos/react-client/halo';
16
- import { ClientRepeater, type ClientRepeatedComponentProps } from '@dxos/react-client/testing';
15
+ import { DocAccessor, Query, type Space, createDocAccessor, useQuery, useSpace } from '@dxos/react-client/echo';
16
+ import { type Identity, useIdentity } from '@dxos/react-client/halo';
17
+ import { type ClientRepeatedComponentProps, ClientRepeater } from '@dxos/react-client/testing';
17
18
  import { useThemeContext } from '@dxos/react-ui';
18
- import { withLayout, withTheme } from '@dxos/storybook-utils';
19
+ import { render, withLayout, withTheme } from '@dxos/storybook-utils';
19
20
 
20
21
  import { editorSlots } from '../../defaults';
21
22
  import { useTextEditor } from '../../hooks';
@@ -41,7 +42,7 @@ const Editor = ({ source, autoFocus, space, identity }: EditorProps) => {
41
42
  () => ({
42
43
  initialValue: DocAccessor.getValue(source),
43
44
  extensions: [
44
- createBasicExtensions({ placeholder: 'Type here...' }),
45
+ createBasicExtensions({ placeholder: 'Type here...', search: true }),
45
46
  createThemeExtensions({ themeMode, slots: editorSlots }),
46
47
  createDataExtensions({ id: 'test', text: source, space, identity }),
47
48
  ],
@@ -53,7 +54,7 @@ const Editor = ({ source, autoFocus, space, identity }: EditorProps) => {
53
54
  return <div ref={parentRef} className='flex w-full' />;
54
55
  };
55
56
 
56
- const Story = () => {
57
+ const DefaultStory = () => {
57
58
  const [object1, setObject1] = useState<DocAccessor<TestObject>>();
58
59
  const [object2, setObject2] = useState<DocAccessor<TestObject>>();
59
60
 
@@ -88,16 +89,6 @@ const Story = () => {
88
89
  );
89
90
  };
90
91
 
91
- export default {
92
- title: 'ui/react-ui-editor/Automerge',
93
- component: Editor,
94
- decorators: [withTheme, withLayout({ fullscreen: true })],
95
- render: () => <Story />,
96
- parameters: {
97
- translations,
98
- },
99
- };
100
-
101
92
  const EchoStory = ({ spaceKey }: ClientRepeatedComponentProps) => {
102
93
  const identity = useIdentity();
103
94
  const space = useSpace(spaceKey);
@@ -118,9 +109,25 @@ const EchoStory = ({ spaceKey }: ClientRepeatedComponentProps) => {
118
109
  return <Editor source={source} space={space} identity={identity ?? undefined} />;
119
110
  };
120
111
 
121
- export const Default = {};
112
+ const meta = {
113
+ title: 'ui/react-ui-editor/Automerge',
114
+ component: Editor as any,
115
+ render: render(DefaultStory),
116
+ decorators: [withTheme, withLayout({ fullscreen: true })],
117
+ parameters: {
118
+ translations,
119
+ },
120
+ } satisfies Meta<typeof DefaultStory>;
121
+
122
+ export default meta;
123
+
124
+ type Story = StoryObj<typeof meta>;
125
+
126
+ export const Default: Story = {
127
+ args: {},
128
+ };
122
129
 
123
- export const WithEcho = {
130
+ export const WithEcho: Story = {
124
131
  decorators: [withTheme],
125
132
  render: () => {
126
133
  return (
@@ -5,15 +5,16 @@
5
5
  //
6
6
 
7
7
  import { next as A } from '@automerge/automerge';
8
- import { StateField, type Extension } from '@codemirror/state';
8
+ import { type Extension, StateField } from '@codemirror/state';
9
9
  import { EditorView, ViewPlugin } from '@codemirror/view';
10
10
 
11
11
  import { type DocAccessor } from '@dxos/react-client/echo';
12
12
 
13
+ import { Cursor } from '../../util';
14
+
13
15
  import { cursorConverter } from './cursor';
14
- import { updateHeadsEffect, isReconcile, type State } from './defs';
16
+ import { type State, isReconcile, updateHeadsEffect } from './defs';
15
17
  import { Syncer } from './sync';
16
- import { Cursor } from '../../util';
17
18
 
18
19
  export const automerge = (accessor: DocAccessor): Extension => {
19
20
  const syncState = StateField.define<State>({
@@ -5,7 +5,7 @@
5
5
  //
6
6
 
7
7
  import { type Heads, type Prop } from '@automerge/automerge';
8
- import { Annotation, StateEffect, type StateField, type EditorState, type Transaction } from '@codemirror/state';
8
+ import { Annotation, type EditorState, StateEffect, type StateField, type Transaction } from '@codemirror/state';
9
9
 
10
10
  export type State = {
11
11
  path: Prop[];
@@ -10,7 +10,7 @@ import { type EditorView } from '@codemirror/view';
10
10
 
11
11
  import { type IDocHandle } from '@dxos/react-client/echo';
12
12
 
13
- import { getLastHeads, getPath, isReconcile, reconcileAnnotation, type State, updateHeads } from './defs';
13
+ import { type State, getLastHeads, getPath, isReconcile, reconcileAnnotation, updateHeads } from './defs';
14
14
  import { updateAutomerge } from './update-automerge';
15
15
  import { updateCodeMirror } from './update-codemirror';
16
16
 
@@ -5,7 +5,7 @@
5
5
  //
6
6
 
7
7
  import { next as A, type Heads } from '@automerge/automerge';
8
- import { type EditorState, type StateField, type Transaction, type Text } from '@codemirror/state';
8
+ import { type EditorState, type StateField, type Text, type Transaction } from '@codemirror/state';
9
9
 
10
10
  import { type IDocHandle } from '@dxos/react-client/echo';
11
11