@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
@@ -0,0 +1,335 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { type EditorState, type Extension, RangeSetBuilder, StateEffect, StateField } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, EditorView, WidgetType } from '@codemirror/view';
8
+ import { type ComponentType, type FC } from 'react';
9
+
10
+ import { invariant } from '@dxos/invariant';
11
+ import { log } from '@dxos/log';
12
+
13
+ import { decorationSetToArray } from '../../util';
14
+
15
+ import { nodeToJson } from './xml-util';
16
+
17
+ export type StateDispatch<T> = T | ((state: T) => T);
18
+
19
+ /**
20
+ * Manages widget state.
21
+ */
22
+ export interface XmlWidgetStateManager {
23
+ updateWidget<T>(id: string, props: StateDispatch<T>): void;
24
+ }
25
+
26
+ export type XmlEventHandler<TEvent = any> = (event: TEvent) => void;
27
+
28
+ /**
29
+ * Widget component.
30
+ */
31
+ export type XmlWidgetProps<TContext = any, TProps = any> = TProps & {
32
+ _tag: string;
33
+ context: TContext;
34
+ onEvent?: XmlEventHandler;
35
+ };
36
+
37
+ /**
38
+ * Factory for creating widgets.
39
+ */
40
+ export type XmlWidgetFactory = (props: XmlWidgetProps, onEvent?: XmlEventHandler) => WidgetType | null;
41
+
42
+ /**
43
+ * Widget registry definition.
44
+ */
45
+ export type XmlWidgetDef = {
46
+ block?: boolean;
47
+ /** Native widget. */
48
+ factory?: XmlWidgetFactory;
49
+ /** React widget. */
50
+ Component?: FC<XmlWidgetProps>;
51
+ };
52
+
53
+ export type XmlWidgetRegistry = Record<string, XmlWidgetDef>;
54
+
55
+ /**
56
+ * Update context.
57
+ */
58
+ export const xmlTagContextEffect = StateEffect.define<any>();
59
+
60
+ /**
61
+ * Update widget.
62
+ */
63
+ export const xmlTagUpdateEffect = StateEffect.define<{ id: string; value: any }>();
64
+
65
+ /**
66
+ * Reset all state.
67
+ */
68
+ export const xmlTagResetEffect = StateEffect.define();
69
+
70
+ type WidgetDecorationSet = {
71
+ from: number;
72
+ decorations: DecorationSet;
73
+ };
74
+
75
+ type XmlWidgetStateMap = Record<string, any>;
76
+
77
+ export type XmlWidgetState = {
78
+ id: string;
79
+ props: any;
80
+ root: HTMLElement;
81
+ Component: ComponentType<any>;
82
+ };
83
+
84
+ export interface XmlWidgetNotifier {
85
+ mounted(widget: XmlWidgetState): void;
86
+ unmounted(id: string): void;
87
+ }
88
+
89
+ export type XmlTagsOptions = {
90
+ registry?: XmlWidgetRegistry;
91
+ /**
92
+ * Called when a widget is mounted or unmounted.
93
+ */
94
+ setWidgets?: (widgets: XmlWidgetState[]) => void;
95
+ };
96
+
97
+ /**
98
+ * Extension that adds thread-related functionality including XML tag decorations.
99
+ */
100
+ export const xmlTags = (options: XmlTagsOptions = {}): Extension => {
101
+ //
102
+ // Context state.
103
+ //
104
+ const contextState = StateField.define<any>({
105
+ create: () => undefined,
106
+ update: (value, tr) => {
107
+ for (const effect of tr.effects) {
108
+ if (effect.is(xmlTagContextEffect)) {
109
+ return effect.value;
110
+ }
111
+ }
112
+
113
+ return value;
114
+ },
115
+ });
116
+
117
+ //
118
+ // Active widgets.
119
+ //
120
+ const widgets = new Map<string, XmlWidgetState>();
121
+ const notifier = {
122
+ mounted: (widget: XmlWidgetState) => {
123
+ widgets.set(widget.id, widget);
124
+ options.setWidgets?.([...widgets.values()]);
125
+ },
126
+ unmounted: (id: string) => {
127
+ widgets.delete(id);
128
+ options.setWidgets?.([...widgets.values()]);
129
+ },
130
+ } satisfies XmlWidgetNotifier;
131
+
132
+ //
133
+ // Widget decorations.
134
+ //
135
+ const decorationsState = StateField.define<WidgetDecorationSet>({
136
+ create: (state) => {
137
+ return buildDecorations(
138
+ state,
139
+ 0,
140
+ state.doc.length,
141
+ state.field(contextState),
142
+ state.field(widgetState),
143
+ options,
144
+ notifier,
145
+ );
146
+ },
147
+ update: ({ from, decorations }, tr) => {
148
+ for (const effect of tr.effects) {
149
+ if (effect.is(xmlTagResetEffect)) {
150
+ return { from: 0, decorations: Decoration.none };
151
+ }
152
+ }
153
+
154
+ if (tr.docChanged) {
155
+ const { state } = tr;
156
+
157
+ // Flag if the transaction has modified the head of the document.
158
+ // (i.e., any changes that touch before the current `from` position).
159
+ const reset = tr.changes.touchesRange(0, from);
160
+
161
+ // Since append-only, rebuild decorations from after the last widget.
162
+ const result = buildDecorations(
163
+ state,
164
+ reset ? 0 : from,
165
+ state.doc.length,
166
+ state.field(contextState),
167
+ state.field(widgetState),
168
+ options,
169
+ notifier,
170
+ );
171
+
172
+ // Merge with existing decorations.
173
+ return {
174
+ from: result.from,
175
+ decorations: decorations.update({ add: decorationSetToArray(result.decorations) }),
176
+ };
177
+ }
178
+
179
+ // No document changes: avoid mapping decorations through an empty ChangeSet,
180
+ // which can throw when the decoration set was created for a different base length.
181
+ // Simply return the existing decorations unchanged.
182
+ return { from, decorations };
183
+ },
184
+ provide: (field) => [
185
+ EditorView.decorations.from(field, (v) => v.decorations),
186
+ EditorView.atomicRanges.of((view) => view.state.field(field).decorations || Decoration.none),
187
+ ],
188
+ });
189
+
190
+ //
191
+ // Widget state management.
192
+ //
193
+ const widgetState = StateField.define<XmlWidgetStateMap>({
194
+ create: () => ({}),
195
+ update: (map, tr) => {
196
+ for (const effect of tr.effects) {
197
+ if (effect.is(xmlTagResetEffect)) {
198
+ return {};
199
+ }
200
+
201
+ if (effect.is(xmlTagUpdateEffect)) {
202
+ // Update accumulated widget props by id.
203
+ const { id, value } = effect.value;
204
+ const state = typeof value === 'function' ? value(map[id]) : value;
205
+
206
+ // Find and render widget.
207
+ const { decorations } = tr.state.field(decorationsState);
208
+ for (const range of decorationSetToArray(decorations)) {
209
+ const deco = range.value;
210
+ const widget = deco?.spec?.widget;
211
+ if (widget && widget instanceof PlaceholderWidget && widget.id === effect.value.id && widget.root) {
212
+ const props = { ...widget.props, ...state };
213
+ notifier.mounted({ id: widget.id, props, root: widget.root, Component: widget.Component });
214
+ }
215
+ }
216
+
217
+ return { ...map, [id]: state };
218
+ }
219
+ }
220
+
221
+ return map;
222
+ },
223
+ });
224
+
225
+ return [contextState, decorationsState, widgetState];
226
+ };
227
+
228
+ /**
229
+ * Creates widget decorations for XML tags in the document using the syntax tree.
230
+ */
231
+ const buildDecorations = (
232
+ state: EditorState,
233
+ from: number,
234
+ to: number,
235
+ context: any,
236
+ widgetState: XmlWidgetStateMap,
237
+ options: XmlTagsOptions,
238
+ notifier: XmlWidgetNotifier,
239
+ ): WidgetDecorationSet => {
240
+ const builder = new RangeSetBuilder<Decoration>();
241
+ const tree = syntaxTree(state);
242
+ if (!tree || (tree.type.name === 'Program' && tree.length === 0)) {
243
+ return { from, decorations: Decoration.none };
244
+ }
245
+
246
+ tree.iterate({
247
+ from,
248
+ to,
249
+ enter: (node) => {
250
+ switch (node.type.name) {
251
+ // XML Element.
252
+ case 'Element': {
253
+ try {
254
+ if (options.registry) {
255
+ const props = nodeToJson(state, node.node);
256
+ if (props) {
257
+ const def = options.registry[props._tag];
258
+ if (def) {
259
+ const { block, factory, Component } = def;
260
+ const state = props.id ? widgetState[props.id] : undefined;
261
+ const args = { context, ...props, ...state };
262
+ const widget: WidgetType | undefined = factory
263
+ ? factory(args)
264
+ : Component
265
+ ? props.id && new PlaceholderWidget(props.id, Component, args, notifier)
266
+ : undefined;
267
+
268
+ if (widget) {
269
+ from = node.node.to;
270
+ builder.add(
271
+ node.node.from,
272
+ node.node.to,
273
+ Decoration.replace({
274
+ widget,
275
+ block,
276
+ atomic: true,
277
+ inclusive: true,
278
+ }),
279
+ );
280
+ }
281
+ }
282
+ }
283
+ }
284
+ } catch (err) {
285
+ log.catch(err);
286
+ }
287
+
288
+ return false; // Don't descend into children.
289
+ }
290
+ }
291
+ },
292
+ });
293
+
294
+ return { from, decorations: builder.finish() };
295
+ };
296
+
297
+ /**
298
+ * Placeholder for React widgets.
299
+ */
300
+ class PlaceholderWidget<TProps = {}> extends WidgetType {
301
+ private _root: HTMLElement | null = null;
302
+
303
+ constructor(
304
+ public readonly id: string,
305
+ public readonly Component: FC<TProps>,
306
+ public readonly props: TProps,
307
+ private readonly notifier: XmlWidgetNotifier,
308
+ ) {
309
+ super();
310
+ invariant(id);
311
+ }
312
+
313
+ get root() {
314
+ return this._root;
315
+ }
316
+
317
+ override eq(other: WidgetType): boolean {
318
+ return other instanceof PlaceholderWidget && this.id === other.id;
319
+ }
320
+
321
+ override ignoreEvent() {
322
+ return true;
323
+ }
324
+
325
+ override toDOM(_view: EditorView): HTMLElement {
326
+ this._root = document.createElement('span');
327
+ this.notifier.mounted({ id: this.id, props: this.props, root: this._root, Component: this.Component });
328
+ return this._root;
329
+ }
330
+
331
+ override destroy(_dom: HTMLElement): void {
332
+ this.notifier.unmounted(this.id);
333
+ this._root = null;
334
+ }
335
+ }
@@ -0,0 +1,94 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type EditorState } from '@codemirror/state';
6
+ import { type SyntaxNode } from '@lezer/common';
7
+
8
+ import { invariant } from '@dxos/invariant';
9
+
10
+ export type Tag = Record<string, any> & {
11
+ _tag: string;
12
+ };
13
+
14
+ /**
15
+ * Parse XML Element.
16
+ */
17
+ export const nodeToJson = (state: EditorState, node: SyntaxNode): Tag | undefined => {
18
+ invariant(node.type.name === 'Element', 'Node is not an Element');
19
+
20
+ // Find the opening tag.
21
+ const openTag = node.node.getChild('OpenTag') || node.node.getChild('SelfClosingTag');
22
+ if (openTag) {
23
+ // Extract tag name.
24
+ const tagName = openTag.getChild('TagName');
25
+ if (!tagName) {
26
+ return;
27
+ }
28
+
29
+ const tag: Tag = {
30
+ _tag: state.doc.sliceString(tagName.from, tagName.to),
31
+ };
32
+
33
+ // Extract attributes.
34
+ let attributeNode = openTag.getChild('Attribute');
35
+ while (attributeNode) {
36
+ const attrName = attributeNode.getChild('AttributeName');
37
+ const attrValue = attributeNode.getChild('AttributeValue');
38
+ if (attrName) {
39
+ const attr = state.doc.sliceString(attrName.from, attrName.to);
40
+
41
+ // Default for attributes without values.
42
+ let value: string | boolean = true;
43
+
44
+ if (attrValue) {
45
+ const rawValue = state.doc.sliceString(attrValue.from, attrValue.to);
46
+ // Remove quotes if present.
47
+ if (
48
+ (rawValue.startsWith('"') && rawValue.endsWith('"')) ||
49
+ (rawValue.startsWith("'") && rawValue.endsWith("'"))
50
+ ) {
51
+ value = rawValue.slice(1, -1);
52
+ } else {
53
+ value = rawValue;
54
+ }
55
+ }
56
+
57
+ tag[attr] = value;
58
+ }
59
+
60
+ // Get next sibling attribute.
61
+ attributeNode = attributeNode.nextSibling;
62
+ }
63
+
64
+ // Extract children for non-self-closing tags.
65
+ if (node.type.name === 'Element' && openTag.type.name !== 'SelfClosingTag') {
66
+ const children: any[] = [];
67
+ let child = node.node.firstChild;
68
+
69
+ while (child) {
70
+ // Skip the opening and closing tags.
71
+ if (child.type.name !== 'OpenTag' && child.type.name !== 'CloseTag') {
72
+ if (child.type.name === 'Text') {
73
+ const text = state.doc.sliceString(child.from, child.to).trim();
74
+ if (text) {
75
+ children.push(text);
76
+ }
77
+ } else if (child.type.name === 'Element') {
78
+ const data = nodeToJson(state, child);
79
+ if (data) {
80
+ children.push(data);
81
+ }
82
+ }
83
+ }
84
+ child = child.nextSibling;
85
+ }
86
+
87
+ if (children.length > 0) {
88
+ tag.children = children;
89
+ }
90
+ }
91
+
92
+ return tag;
93
+ }
94
+ };
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { EditorState, type EditorStateConfig, type Text } from '@codemirror/state';
6
6
  import { EditorView } from '@codemirror/view';
7
- import { useFocusableGroup, type TabsterTypes } from '@fluentui/react-tabster';
7
+ import { type TabsterTypes, useFocusableGroup } from '@fluentui/react-tabster';
8
8
  import {
9
9
  type DependencyList,
10
10
  type KeyboardEventHandler,
@@ -17,9 +17,9 @@ import {
17
17
  } from 'react';
18
18
 
19
19
  import { log } from '@dxos/log';
20
- import { getProviderValue, isNotFalsy, type MaybeProvider } from '@dxos/util';
20
+ import { type MaybeProvider, getProviderValue, isNotFalsy } from '@dxos/util';
21
21
 
22
- import { type EditorSelection, documentId, createEditorStateTransaction, editorInputMode } from '../extensions';
22
+ import { type EditorSelection, createEditorStateTransaction, documentId, editorInputMode } from '../extensions';
23
23
  import { debugDispatcher } from '../util';
24
24
 
25
25
  let instanceCount = 0;
@@ -96,18 +96,6 @@ export const useTextEditor = (
96
96
  EditorView.exceptionSink.of((err) => {
97
97
  log.catch(err);
98
98
  }),
99
- // TODO(burdon): Factor out debug inspector.
100
- // ViewPlugin.fromClass(
101
- // class {
102
- // constructor(_view: EditorView) {
103
- // log('construct', { id });
104
- // }
105
- //
106
- // destroy() {
107
- // log('destroy', { id });
108
- // }
109
- // },
110
- // ),
111
99
  ].filter(isNotFalsy),
112
100
  });
113
101
 
@@ -4,19 +4,20 @@
4
4
 
5
5
  import '@dxos-theme';
6
6
 
7
- import React, { useState, type KeyboardEvent } from 'react';
7
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
+ import React, { type KeyboardEvent, useState } from 'react';
8
9
 
9
- import { Button, Icon, Input, DropdownMenu } from '@dxos/react-ui';
10
+ import { Button, Icon, Input } from '@dxos/react-ui';
10
11
  import { mx } from '@dxos/react-ui-theme';
11
- import { withLayout, withTheme, type Meta } from '@dxos/storybook-utils';
12
+ import { withLayout, withTheme } from '@dxos/storybook-utils';
12
13
 
13
- import { EditorStory } from './components';
14
- import { RefDropdownMenu } from '../components';
15
14
  import { editorWidth } from '../defaults';
16
- import { command, type Action, floatingMenu } from '../extensions';
15
+ import { type Action, command, floatingMenu } from '../extensions';
17
16
  import { str } from '../testing';
18
17
  import { createRenderer } from '../util';
19
18
 
19
+ import { EditorStory } from './components';
20
+
20
21
  const CommandDialog = ({ onAction }: { onAction: (action?: Action) => void }) => {
21
22
  const [text, setText] = useState('');
22
23
 
@@ -40,10 +41,10 @@ const CommandDialog = ({ onAction }: { onAction: (action?: Action) => void }) =>
40
41
  };
41
42
 
42
43
  return (
43
- <div className='flex w-full justify-center'>
44
+ <div className='flex is-full justify-center'>
44
45
  <div
45
46
  className={mx(
46
- 'flex w-full p-2 gap-2 items-center bg-modalSurface border border-separator rounded-md',
47
+ 'flex is-full p-2 gap-2 items-center bg-modalSurface border border-separator rounded-md',
47
48
  editorWidth,
48
49
  )}
49
50
  >
@@ -64,34 +65,26 @@ const CommandDialog = ({ onAction }: { onAction: (action?: Action) => void }) =>
64
65
  );
65
66
  };
66
67
 
67
- const meta: Meta<typeof EditorStory> = {
68
+ const meta = {
68
69
  title: 'ui/react-ui-editor/Command',
69
70
  decorators: [withTheme, withLayout({ fullscreen: true })],
70
71
  render: () => (
71
- <RefDropdownMenu.Provider>
72
- <EditorStory
73
- text={str('# Command', '', '', '')}
74
- extensions={[
75
- floatingMenu(),
76
- command({
77
- renderDialog: createRenderer(CommandDialog),
78
- onHint: () => "Press '/' for commands",
79
- }),
80
- ]}
81
- />
82
- <DropdownMenu.Portal>
83
- <DropdownMenu.Content>
84
- <DropdownMenu.Viewport>
85
- <DropdownMenu.Item onClick={() => console.log('!')}>Test</DropdownMenu.Item>
86
- </DropdownMenu.Viewport>
87
- <DropdownMenu.Arrow />
88
- </DropdownMenu.Content>
89
- </DropdownMenu.Portal>
90
- </RefDropdownMenu.Provider>
72
+ <EditorStory
73
+ text={str('# Command', '', '', '')}
74
+ extensions={[
75
+ floatingMenu(),
76
+ command({
77
+ renderDialog: createRenderer(CommandDialog),
78
+ onHint: () => "Press '/' for commands",
79
+ }),
80
+ ]}
81
+ />
91
82
  ),
92
83
  parameters: { layout: 'fullscreen' },
93
- };
84
+ } satisfies Meta<typeof Button>;
94
85
 
95
86
  export default meta;
96
87
 
97
- export const Default = {};
88
+ type Story = StoryObj<typeof meta>;
89
+
90
+ export const Default: Story = {};
@@ -5,30 +5,30 @@
5
5
  import '@dxos-theme';
6
6
 
7
7
  import { type EditorView } from '@codemirror/view';
8
- import { type StoryObj } from '@storybook/react-vite';
8
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
9
9
  import React, { useCallback, useRef } from 'react';
10
10
 
11
11
  import { Obj, Query } from '@dxos/echo';
12
12
  import { faker } from '@dxos/random';
13
13
  import { useClientProvider, withClientProvider } from '@dxos/react-client/testing';
14
- import { createObjectFactory, Testing, type ValueGenerator } from '@dxos/schema/testing';
15
- import { withLayout, withTheme, type Meta } from '@dxos/storybook-utils';
14
+ import { Testing, type ValueGenerator, createObjectFactory } from '@dxos/schema/testing';
15
+ import { withLayout, withTheme } from '@dxos/storybook-utils';
16
16
 
17
- import { EditorStory, names } from './components';
18
17
  import {
19
- CommandMenu,
20
18
  type CommandMenuGroup,
21
19
  type CommandMenuItem,
22
- RefPopover,
20
+ CommandMenuProvider,
23
21
  coreSlashCommands,
24
22
  filterItems,
25
23
  insertAtCursor,
26
24
  insertAtLineStart,
27
25
  linkSlashCommands,
28
26
  } from '../components';
29
- import { useCommandMenu, type UseCommandMenuOptions } from '../extensions';
27
+ import { type UseCommandMenuOptions, useCommandMenu } from '../extensions';
30
28
  import { str } from '../testing';
31
- import { createElement } from '../util';
29
+ import { Domino } from '../util';
30
+
31
+ import { EditorStory, names } from './components';
32
32
 
33
33
  const generator: ValueGenerator = faker as any;
34
34
 
@@ -36,13 +36,12 @@ type StoryProps = Omit<UseCommandMenuOptions, 'viewRef'> & { text: string };
36
36
 
37
37
  const DefaultStory = ({ text, ...options }: StoryProps) => {
38
38
  const viewRef = useRef<EditorView>();
39
- const { commandMenu, groupsRef, currentItem, onSelect, ...props } = useCommandMenu({ viewRef, ...options });
39
+ const { commandMenu, groupsRef, ...commandMenuProps } = useCommandMenu({ viewRef, ...options });
40
40
 
41
41
  return (
42
- <RefPopover modal={false} {...props}>
42
+ <CommandMenuProvider groups={groupsRef.current} {...commandMenuProps}>
43
43
  <EditorStory ref={viewRef} text={text} placeholder={''} extensions={commandMenu} />
44
- <CommandMenu groups={groupsRef.current} currentItem={currentItem} onSelect={onSelect} />
45
- </RefPopover>
44
+ </CommandMenuProvider>
46
45
  );
47
46
  };
48
47
 
@@ -63,18 +62,18 @@ const groups: CommandMenuGroup[] = [
63
62
  },
64
63
  ];
65
64
 
66
- const meta: Meta<StoryProps> = {
65
+ const meta = {
67
66
  title: 'ui/react-ui-editor/CommandMenu',
67
+ render: DefaultStory,
68
68
  decorators: [withTheme, withLayout({ fullscreen: true })],
69
- render: (args) => <DefaultStory {...args} />,
70
69
  parameters: {
71
70
  layout: 'fullscreen',
72
71
  },
73
- };
72
+ } satisfies Meta<typeof DefaultStory>;
74
73
 
75
74
  export default meta;
76
75
 
77
- type Story = StoryObj<StoryProps>;
76
+ type Story = StoryObj<typeof meta>;
78
77
 
79
78
  // TODO(burdon): Not working.
80
79
  export const Slash: Story = {
@@ -82,13 +81,12 @@ export const Slash: Story = {
82
81
  text: str('# Slash', '', names.join(' '), ''),
83
82
  trigger: '/',
84
83
  placeholder: {
85
- content: () => {
86
- return createElement('div', undefined, [
87
- createElement('span', { text: 'Press' }),
88
- createElement('span', { className: 'border border-separator rounded-sm mx-1 px-1', text: '/' }),
89
- createElement('span', { text: 'for commands' }),
90
- ]);
91
- },
84
+ content: () =>
85
+ Domino.of('div')
86
+ .child(Domino.of('span').text('Press'))
87
+ .child(Domino.of('span').text('/').classNames('border border-separator rounded-sm mx-1 px-1'))
88
+ .child(Domino.of('span').text('for commands'))
89
+ .build(),
92
90
  },
93
91
  getMenu: (text) => {
94
92
  return filterItems(groups, (item) =>
@@ -99,7 +97,7 @@ export const Slash: Story = {
99
97
  };
100
98
 
101
99
  export const Link: Story = {
102
- render: (args) => {
100
+ render: (args: StoryProps) => {
103
101
  const { space } = useClientProvider();
104
102
  const getMenu = useCallback(
105
103
  async (trigger: string, query?: string): Promise<CommandMenuGroup[]> => {
@@ -123,7 +121,7 @@ export const Link: Story = {
123
121
  label: object.name,
124
122
  icon: 'ph--user--regular',
125
123
  onSelect: (view, head) => {
126
- const link = `[${object.name}][${Obj.getDXN(object)}]`;
124
+ const link = `[${object.name}](${Obj.getDXN(object)})`;
127
125
  if (query?.startsWith('@')) {
128
126
  insertAtLineStart(view, head, `!${link}\n`);
129
127
  } else {
@@ -139,10 +137,6 @@ export const Link: Story = {
139
137
 
140
138
  return <DefaultStory {...args} getMenu={getMenu} />;
141
139
  },
142
- args: {
143
- text: str('# Link', '', names.join(' '), ''),
144
- trigger: ['/', '@'],
145
- },
146
140
  decorators: [
147
141
  withClientProvider({
148
142
  createSpace: true,
@@ -156,4 +150,9 @@ export const Link: Story = {
156
150
  },
157
151
  }),
158
152
  ],
153
+ args: {
154
+ text: str('# Link', '', names.join(' '), ''),
155
+ trigger: ['/', '@'],
156
+ getMenu: () => [],
157
+ },
159
158
  };