@dxos/react-ui-editor 0.8.4-main.b97322e → 0.8.4-main.dedc0f3

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 (210) hide show
  1. package/dist/lib/browser/index.mjs +2074 -888
  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.map +2 -2
  5. package/dist/lib/node-esm/index.mjs +2074 -888
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  9. package/dist/types/src/components/Editor/Editor.d.ts.map +1 -1
  10. package/dist/types/src/components/EditorToolbar/EditorToolbar.d.ts.map +1 -1
  11. package/dist/types/src/components/EditorToolbar/blocks.d.ts.map +1 -1
  12. package/dist/types/src/components/EditorToolbar/formatting.d.ts.map +1 -1
  13. package/dist/types/src/components/EditorToolbar/headings.d.ts.map +1 -1
  14. package/dist/types/src/components/EditorToolbar/image.d.ts.map +1 -1
  15. package/dist/types/src/components/EditorToolbar/lists.d.ts.map +1 -1
  16. package/dist/types/src/components/EditorToolbar/search.d.ts.map +1 -1
  17. package/dist/types/src/components/EditorToolbar/util.d.ts +2 -2
  18. package/dist/types/src/components/EditorToolbar/util.d.ts.map +1 -1
  19. package/dist/types/src/components/EditorToolbar/view-mode.d.ts +1 -1
  20. package/dist/types/src/components/EditorToolbar/view-mode.d.ts.map +1 -1
  21. package/dist/types/src/components/Popover/CommandMenu.d.ts.map +1 -1
  22. package/dist/types/src/components/Popover/RefDropdownMenu.d.ts +2 -9
  23. package/dist/types/src/components/Popover/RefDropdownMenu.d.ts.map +1 -1
  24. package/dist/types/src/components/Popover/RefPopover.d.ts +20 -17
  25. package/dist/types/src/components/Popover/RefPopover.d.ts.map +1 -1
  26. package/dist/types/src/defaults.d.ts.map +1 -1
  27. package/dist/types/src/extensions/autocomplete.d.ts +20 -7
  28. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  29. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  30. package/dist/types/src/extensions/automerge/automerge.stories.d.ts +9 -18
  31. package/dist/types/src/extensions/automerge/automerge.stories.d.ts.map +1 -1
  32. package/dist/types/src/extensions/automerge/defs.d.ts +1 -1
  33. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  34. package/dist/types/src/extensions/automerge/sync.d.ts.map +1 -1
  35. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  36. package/dist/types/src/extensions/autoscroll.d.ts +10 -0
  37. package/dist/types/src/extensions/autoscroll.d.ts.map +1 -0
  38. package/dist/types/src/extensions/command/action.d.ts +1 -1
  39. package/dist/types/src/extensions/command/action.d.ts.map +1 -1
  40. package/dist/types/src/extensions/command/command-menu.d.ts +1 -1
  41. package/dist/types/src/extensions/command/command-menu.d.ts.map +1 -1
  42. package/dist/types/src/extensions/command/command.d.ts.map +1 -1
  43. package/dist/types/src/extensions/command/hint.d.ts +2 -7
  44. package/dist/types/src/extensions/command/hint.d.ts.map +1 -1
  45. package/dist/types/src/extensions/command/state.d.ts +1 -1
  46. package/dist/types/src/extensions/command/state.d.ts.map +1 -1
  47. package/dist/types/src/extensions/command/typeahead.d.ts.map +1 -1
  48. package/dist/types/src/extensions/command/useCommandMenu.d.ts +4 -4
  49. package/dist/types/src/extensions/command/useCommandMenu.d.ts.map +1 -1
  50. package/dist/types/src/extensions/comments.d.ts +1 -1
  51. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  52. package/dist/types/src/extensions/dnd.d.ts.map +1 -1
  53. package/dist/types/src/extensions/factories.d.ts +2 -7
  54. package/dist/types/src/extensions/factories.d.ts.map +1 -1
  55. package/dist/types/src/extensions/index.d.ts +2 -0
  56. package/dist/types/src/extensions/index.d.ts.map +1 -1
  57. package/dist/types/src/extensions/markdown/action.d.ts.map +1 -1
  58. package/dist/types/src/extensions/markdown/bundle.d.ts +8 -2
  59. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  60. package/dist/types/src/extensions/markdown/changes.d.ts +1 -1
  61. package/dist/types/src/extensions/markdown/changes.d.ts.map +1 -1
  62. package/dist/types/src/extensions/markdown/decorate.d.ts +9 -1
  63. package/dist/types/src/extensions/markdown/decorate.d.ts.map +1 -1
  64. package/dist/types/src/extensions/markdown/formatting.d.ts +1 -1
  65. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
  66. package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -1
  67. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  68. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -1
  69. package/dist/types/src/extensions/outliner/outliner.d.ts +1 -1
  70. package/dist/types/src/extensions/outliner/outliner.d.ts.map +1 -1
  71. package/dist/types/src/extensions/outliner/selection.d.ts.map +1 -1
  72. package/dist/types/src/extensions/outliner/tree.d.ts +2 -2
  73. package/dist/types/src/extensions/outliner/tree.d.ts.map +1 -1
  74. package/dist/types/src/extensions/preview/preview.d.ts +3 -5
  75. package/dist/types/src/extensions/preview/preview.d.ts.map +1 -1
  76. package/dist/types/src/extensions/tags/extended-markdown.d.ts +10 -0
  77. package/dist/types/src/extensions/tags/extended-markdown.d.ts.map +1 -0
  78. package/dist/types/src/extensions/tags/extended-markdown.test.d.ts +2 -0
  79. package/dist/types/src/extensions/tags/extended-markdown.test.d.ts.map +1 -0
  80. package/dist/types/src/extensions/tags/index.d.ts +4 -0
  81. package/dist/types/src/extensions/tags/index.d.ts.map +1 -0
  82. package/dist/types/src/extensions/tags/streamer.d.ts +12 -0
  83. package/dist/types/src/extensions/tags/streamer.d.ts.map +1 -0
  84. package/dist/types/src/extensions/tags/xml-tags.d.ts +71 -0
  85. package/dist/types/src/extensions/tags/xml-tags.d.ts.map +1 -0
  86. package/dist/types/src/extensions/tags/xml-util.d.ts +10 -0
  87. package/dist/types/src/extensions/tags/xml-util.d.ts.map +1 -0
  88. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  89. package/dist/types/src/stories/Command.stories.d.ts +12 -4
  90. package/dist/types/src/stories/Command.stories.d.ts.map +1 -1
  91. package/dist/types/src/stories/CommandMenu.stories.d.ts +10 -3
  92. package/dist/types/src/stories/CommandMenu.stories.d.ts.map +1 -1
  93. package/dist/types/src/stories/Comments.stories.d.ts +21 -9
  94. package/dist/types/src/stories/Comments.stories.d.ts.map +1 -1
  95. package/dist/types/src/stories/EditorToolbar.stories.d.ts +39 -2
  96. package/dist/types/src/stories/EditorToolbar.stories.d.ts.map +1 -1
  97. package/dist/types/src/stories/Experimental.stories.d.ts +22 -12
  98. package/dist/types/src/stories/Experimental.stories.d.ts.map +1 -1
  99. package/dist/types/src/stories/Markdown.stories.d.ts +32 -42
  100. package/dist/types/src/stories/Markdown.stories.d.ts.map +1 -1
  101. package/dist/types/src/stories/Outliner.stories.d.ts +15 -20
  102. package/dist/types/src/stories/Outliner.stories.d.ts.map +1 -1
  103. package/dist/types/src/stories/Preview.stories.d.ts +21 -6
  104. package/dist/types/src/stories/Preview.stories.d.ts.map +1 -1
  105. package/dist/types/src/stories/Tags.stories.d.ts +17 -0
  106. package/dist/types/src/stories/Tags.stories.d.ts.map +1 -0
  107. package/dist/types/src/stories/TextEditor.stories.d.ts +38 -51
  108. package/dist/types/src/stories/TextEditor.stories.d.ts.map +1 -1
  109. package/dist/types/src/stories/components/EditorStory.d.ts +3 -6
  110. package/dist/types/src/stories/components/EditorStory.d.ts.map +1 -1
  111. package/dist/types/src/styles/theme.d.ts.map +1 -1
  112. package/dist/types/src/testing/util.d.ts +1 -0
  113. package/dist/types/src/testing/util.d.ts.map +1 -1
  114. package/dist/types/src/translations.d.ts +1 -1
  115. package/dist/types/src/types/types.d.ts +1 -1
  116. package/dist/types/src/util/cursor.d.ts.map +1 -1
  117. package/dist/types/src/util/debug.d.ts +1 -1
  118. package/dist/types/src/util/debug.d.ts.map +1 -1
  119. package/dist/types/src/util/decorations.d.ts +4 -0
  120. package/dist/types/src/util/decorations.d.ts.map +1 -0
  121. package/dist/types/src/util/dom.d.ts +2 -12
  122. package/dist/types/src/util/dom.d.ts.map +1 -1
  123. package/dist/types/src/util/domino.d.ts +18 -0
  124. package/dist/types/src/util/domino.d.ts.map +1 -0
  125. package/dist/types/src/util/index.d.ts +2 -0
  126. package/dist/types/src/util/index.d.ts.map +1 -1
  127. package/dist/types/src/util/react.d.ts +1 -1
  128. package/dist/types/src/util/react.d.ts.map +1 -1
  129. package/dist/types/tsconfig.tsbuildinfo +1 -1
  130. package/package.json +57 -51
  131. package/src/components/Editor/Editor.tsx +1 -1
  132. package/src/components/EditorToolbar/EditorToolbar.tsx +40 -30
  133. package/src/components/EditorToolbar/blocks.ts +21 -24
  134. package/src/components/EditorToolbar/formatting.ts +22 -25
  135. package/src/components/EditorToolbar/headings.ts +10 -5
  136. package/src/components/EditorToolbar/image.ts +8 -4
  137. package/src/components/EditorToolbar/lists.ts +16 -19
  138. package/src/components/EditorToolbar/search.ts +8 -4
  139. package/src/components/EditorToolbar/util.ts +16 -5
  140. package/src/components/EditorToolbar/view-mode.ts +11 -6
  141. package/src/components/Popover/CommandMenu.tsx +3 -3
  142. package/src/components/Popover/RefDropdownMenu.tsx +20 -16
  143. package/src/components/Popover/RefPopover.tsx +63 -45
  144. package/src/defaults.ts +5 -2
  145. package/src/extensions/autocomplete.ts +204 -54
  146. package/src/extensions/automerge/automerge.stories.tsx +25 -18
  147. package/src/extensions/automerge/automerge.ts +4 -3
  148. package/src/extensions/automerge/defs.ts +1 -1
  149. package/src/extensions/automerge/sync.ts +1 -1
  150. package/src/extensions/automerge/update-automerge.ts +1 -1
  151. package/src/extensions/autoscroll.ts +151 -0
  152. package/src/extensions/awareness/awareness.ts +2 -2
  153. package/src/extensions/command/action.ts +1 -1
  154. package/src/extensions/command/command-menu.ts +6 -5
  155. package/src/extensions/command/command.ts +3 -3
  156. package/src/extensions/command/floating-menu.ts +1 -1
  157. package/src/extensions/command/hint.ts +2 -1
  158. package/src/extensions/command/placeholder.ts +1 -1
  159. package/src/extensions/command/state.ts +4 -3
  160. package/src/extensions/command/typeahead.ts +2 -2
  161. package/src/extensions/command/useCommandMenu.ts +5 -4
  162. package/src/extensions/comments.ts +18 -13
  163. package/src/extensions/dnd.ts +1 -1
  164. package/src/extensions/factories.ts +9 -21
  165. package/src/extensions/folding.tsx +2 -2
  166. package/src/extensions/index.ts +2 -0
  167. package/src/extensions/markdown/action.ts +2 -1
  168. package/src/extensions/markdown/bundle.ts +25 -3
  169. package/src/extensions/markdown/changes.ts +1 -1
  170. package/src/extensions/markdown/decorate.ts +23 -14
  171. package/src/extensions/markdown/formatting.test.ts +6 -6
  172. package/src/extensions/markdown/formatting.ts +3 -3
  173. package/src/extensions/markdown/highlight.ts +1 -1
  174. package/src/extensions/markdown/image.ts +3 -4
  175. package/src/extensions/markdown/table.ts +7 -1
  176. package/src/extensions/mention.ts +1 -1
  177. package/src/extensions/outliner/outliner.test.ts +3 -2
  178. package/src/extensions/outliner/outliner.ts +5 -4
  179. package/src/extensions/outliner/selection.ts +1 -1
  180. package/src/extensions/outliner/tree.test.ts +2 -1
  181. package/src/extensions/outliner/tree.ts +2 -2
  182. package/src/extensions/preview/preview.ts +59 -59
  183. package/src/extensions/tags/extended-markdown.test.ts +261 -0
  184. package/src/extensions/tags/extended-markdown.ts +78 -0
  185. package/src/extensions/tags/index.ts +7 -0
  186. package/src/extensions/tags/streamer.ts +244 -0
  187. package/src/extensions/tags/xml-tags.ts +335 -0
  188. package/src/extensions/tags/xml-util.ts +94 -0
  189. package/src/hooks/useTextEditor.ts +3 -3
  190. package/src/stories/Command.stories.tsx +24 -31
  191. package/src/stories/CommandMenu.stories.tsx +23 -22
  192. package/src/stories/Comments.stories.tsx +10 -6
  193. package/src/stories/EditorToolbar.stories.tsx +8 -8
  194. package/src/stories/Experimental.stories.tsx +12 -8
  195. package/src/stories/Markdown.stories.tsx +21 -17
  196. package/src/stories/Outliner.stories.tsx +17 -14
  197. package/src/stories/Preview.stories.tsx +27 -26
  198. package/src/stories/Tags.stories.tsx +81 -0
  199. package/src/stories/TextEditor.stories.tsx +40 -34
  200. package/src/stories/components/EditorStory.tsx +9 -10
  201. package/src/styles/theme.ts +8 -6
  202. package/src/testing/util.ts +2 -0
  203. package/src/translations.ts +1 -1
  204. package/src/util/cursor.ts +2 -1
  205. package/src/util/debug.ts +2 -2
  206. package/src/util/decorations.ts +21 -0
  207. package/src/util/dom.ts +5 -27
  208. package/src/util/domino.ts +51 -0
  209. package/src/util/index.ts +2 -0
  210. package/src/util/react.tsx +1 -1
@@ -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;
@@ -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,16 +5,15 @@
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
18
  CommandMenu,
20
19
  type CommandMenuGroup,
@@ -26,9 +25,11 @@ import {
26
25
  insertAtLineStart,
27
26
  linkSlashCommands,
28
27
  } from '../components';
29
- import { useCommandMenu, type UseCommandMenuOptions } from '../extensions';
28
+ import { type UseCommandMenuOptions, useCommandMenu } from '../extensions';
30
29
  import { str } from '../testing';
31
- import { createElement } from '../util';
30
+ import { Domino } from '../util';
31
+
32
+ import { EditorStory, names } from './components';
32
33
 
33
34
  const generator: ValueGenerator = faker as any;
34
35
 
@@ -63,18 +64,18 @@ const groups: CommandMenuGroup[] = [
63
64
  },
64
65
  ];
65
66
 
66
- const meta: Meta<StoryProps> = {
67
+ const meta = {
67
68
  title: 'ui/react-ui-editor/CommandMenu',
69
+ render: DefaultStory,
68
70
  decorators: [withTheme, withLayout({ fullscreen: true })],
69
- render: (args) => <DefaultStory {...args} />,
70
71
  parameters: {
71
72
  layout: 'fullscreen',
72
73
  },
73
- };
74
+ } satisfies Meta<typeof DefaultStory>;
74
75
 
75
76
  export default meta;
76
77
 
77
- type Story = StoryObj<StoryProps>;
78
+ type Story = StoryObj<typeof meta>;
78
79
 
79
80
  // TODO(burdon): Not working.
80
81
  export const Slash: Story = {
@@ -82,13 +83,12 @@ export const Slash: Story = {
82
83
  text: str('# Slash', '', names.join(' '), ''),
83
84
  trigger: '/',
84
85
  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
- },
86
+ content: () =>
87
+ Domino.of('div')
88
+ .child(Domino.of('span').text('Press'))
89
+ .child(Domino.of('span').text('/').classNames('border border-separator rounded-sm mx-1 px-1'))
90
+ .child(Domino.of('span').text('for commands'))
91
+ .build(),
92
92
  },
93
93
  getMenu: (text) => {
94
94
  return filterItems(groups, (item) =>
@@ -99,7 +99,7 @@ export const Slash: Story = {
99
99
  };
100
100
 
101
101
  export const Link: Story = {
102
- render: (args) => {
102
+ render: (args: StoryProps) => {
103
103
  const { space } = useClientProvider();
104
104
  const getMenu = useCallback(
105
105
  async (trigger: string, query?: string): Promise<CommandMenuGroup[]> => {
@@ -139,10 +139,6 @@ export const Link: Story = {
139
139
 
140
140
  return <DefaultStory {...args} getMenu={getMenu} />;
141
141
  },
142
- args: {
143
- text: str('# Link', '', names.join(' '), ''),
144
- trigger: ['/', '@'],
145
- },
146
142
  decorators: [
147
143
  withClientProvider({
148
144
  createSpace: true,
@@ -156,4 +152,9 @@ export const Link: Story = {
156
152
  },
157
153
  }),
158
154
  ],
155
+ args: {
156
+ text: str('# Link', '', names.join(' '), ''),
157
+ trigger: ['/', '@'],
158
+ getMenu: () => [],
159
+ },
159
160
  };
@@ -5,33 +5,37 @@
5
5
  import '@dxos-theme';
6
6
 
7
7
  import { effect, useSignal } from '@preact/signals-react';
8
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
9
  import React, { type FC } from 'react';
9
10
 
10
11
  import { keySymbols, parseShortcut } from '@dxos/keyboard';
11
12
  import { PublicKey } from '@dxos/keys';
12
13
  import { log } from '@dxos/log';
13
- import { withLayout, withTheme, type Meta } from '@dxos/storybook-utils';
14
+ import { withLayout, withTheme } from '@dxos/storybook-utils';
14
15
 
15
- import { EditorStory, content, longText } from './components';
16
16
  import { annotations, comments, createExternalCommentSync } from '../extensions';
17
17
  import { str } from '../testing';
18
18
  import { type Comment } from '../types';
19
19
  import { createRenderer } from '../util';
20
20
 
21
- const meta: Meta<typeof EditorStory> = {
21
+ import { EditorStory, content, longText } from './components';
22
+
23
+ const meta = {
22
24
  title: 'ui/react-ui-editor/Comments',
23
25
  component: EditorStory,
24
26
  decorators: [withTheme, withLayout({ fullscreen: true })],
25
27
  parameters: { layout: 'fullscreen' },
26
- };
28
+ } satisfies Meta<typeof EditorStory>;
27
29
 
28
30
  export default meta;
29
31
 
32
+ type Story = StoryObj<typeof meta>;
33
+
30
34
  //
31
35
  // Comments
32
36
  //
33
37
 
34
- export const Comments = {
38
+ export const Comments: Story = {
35
39
  render: () => {
36
40
  const _comments = useSignal<Comment[]>([]);
37
41
  return (
@@ -91,7 +95,7 @@ const CommentTooltip: FC<{ shortcut: string }> = ({ shortcut }) => {
91
95
  // Annotations
92
96
  //
93
97
 
94
- export const Annotations = {
98
+ export const Annotations: Story = {
95
99
  render: () => (
96
100
  <EditorStory text={str('# Annotations', '', longText)} extensions={[annotations({ match: /volup/gi })]} />
97
101
  ),