@djangocfg/ui-tools 2.1.417 → 2.1.418

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 (35) hide show
  1. package/dist/audio-player/index.cjs +1 -2
  2. package/dist/audio-player/index.cjs.map +1 -1
  3. package/dist/audio-player/index.d.cts +3 -11
  4. package/dist/audio-player/index.d.ts +3 -11
  5. package/dist/audio-player/index.mjs +1 -2
  6. package/dist/audio-player/index.mjs.map +1 -1
  7. package/dist/tree/index.cjs +0 -3
  8. package/dist/tree/index.cjs.map +1 -1
  9. package/dist/tree/index.mjs +0 -3
  10. package/dist/tree/index.mjs.map +1 -1
  11. package/package.json +30 -14
  12. package/src/tools/data/Tree/components/TreeRow.tsx +0 -11
  13. package/src/tools/forms/CodeEditor/components/Editor.tsx +19 -0
  14. package/src/tools/forms/CodeEditor/types/index.ts +7 -0
  15. package/src/tools/forms/MarkdownEditor/MarkdownEditor.tsx +40 -0
  16. package/src/tools/forms/MarkdownEditor/styles.css +174 -21
  17. package/src/tools/forms/NotionEditor/CustomKeymap.ts +48 -0
  18. package/src/tools/forms/NotionEditor/LinkDialog.tsx +133 -0
  19. package/src/tools/forms/NotionEditor/NotionEditor.tsx +304 -0
  20. package/src/tools/forms/NotionEditor/SlashExtension.ts +32 -0
  21. package/src/tools/forms/NotionEditor/SlashList.tsx +136 -0
  22. package/src/tools/forms/NotionEditor/TaskItemView.tsx +41 -0
  23. package/src/tools/forms/NotionEditor/createSlashSuggestion.ts +121 -0
  24. package/src/tools/forms/NotionEditor/extensions.ts +105 -0
  25. package/src/tools/forms/NotionEditor/index.ts +1 -0
  26. package/src/tools/forms/NotionEditor/lazy.tsx +44 -0
  27. package/src/tools/forms/NotionEditor/slashItems.ts +159 -0
  28. package/src/tools/forms/NotionEditor/styles.css +478 -0
  29. package/src/tools/forms/NotionEditor/types.ts +28 -0
  30. package/src/tools/media/AudioPlayer/PlayerShell.tsx +3 -11
  31. package/src/tools/media/AudioPlayer/types.ts +4 -11
  32. package/src/tools/media/ImageViewer/components/ImageViewer.tsx +8 -0
  33. package/src/tools/media/ImageViewer/types.ts +4 -0
  34. package/src/tools/media/VideoPlayer/VideoPlayer.tsx +20 -1
  35. package/src/tools/media/VideoPlayer/types.ts +4 -0
@@ -0,0 +1,304 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * NotionEditor — Notion-style WYSIWYG built on TipTap v3.
5
+ *
6
+ * Composition (vs `MarkdownEditor`):
7
+ * - StarterKit (with H1-H4) + Placeholder + Markdown serialiser
8
+ * - TaskList + TaskItem (GFM `- [ ]`)
9
+ * - Table (+ Row/Header/Cell)
10
+ * - CodeBlockLowlight (syntax highlight via lowlight's `common` pack)
11
+ * - Highlight (`==text==`)
12
+ * - GlobalDragHandle (Notion-style block grabbers)
13
+ * - SlashExtension (own `/` menu — see SlashList.tsx)
14
+ * - BubbleMenu (floating selection toolbar)
15
+ *
16
+ * Why a separate component instead of a variant on MarkdownEditor:
17
+ * - Different baseline (no mentions, no slash-as-chip extension that
18
+ * the chat composer depends on).
19
+ * - ~30KB more deps (tables, lowlight, drag-handle) — would punish
20
+ * every chat composer mount if added to the shared editor.
21
+ * - Lazy chunk boundary stays clean — Skills / chat composer keep
22
+ * their slim TipTap; document-preview pulls the heavy stack.
23
+ */
24
+
25
+ import { useEditor, EditorContent, type Editor } from '@tiptap/react';
26
+ import { BubbleMenu } from '@tiptap/react/menus';
27
+ import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
28
+ import {
29
+ Bold,
30
+ Italic,
31
+ Strikethrough,
32
+ Code as CodeIcon,
33
+ Highlighter,
34
+ Link as LinkIcon,
35
+ Underline as UnderlineIcon,
36
+ type LucideIcon,
37
+ } from 'lucide-react';
38
+ import { Kbd, Tooltip, TooltipContent, TooltipTrigger } from '@djangocfg/ui-core';
39
+ import { useHotkey } from '@djangocfg/ui-core/hooks';
40
+ import { notionExtensions } from './extensions';
41
+ import { LinkDialog } from './LinkDialog';
42
+ import type { NotionEditorHandle, NotionEditorProps } from './types';
43
+ import './styles.css';
44
+
45
+ // Same markdown helper as MarkdownEditor — TipTap v3 augments Editor with
46
+ // `getMarkdown()` when @tiptap/markdown is registered.
47
+ interface MarkdownManager {
48
+ serialize: (json: Record<string, unknown>) => string;
49
+ }
50
+ function getMarkdown(editor: Editor): string {
51
+ const withMd = editor as Editor & { getMarkdown?: () => string };
52
+ if (typeof withMd.getMarkdown === 'function') return withMd.getMarkdown();
53
+ const storage = editor.storage.markdown as { manager?: MarkdownManager } | undefined;
54
+ if (!storage?.manager) return editor.getText();
55
+ return storage.manager.serialize(editor.getJSON());
56
+ }
57
+
58
+ export const NotionEditor = forwardRef<NotionEditorHandle, NotionEditorProps>(
59
+ function NotionEditor(
60
+ {
61
+ value,
62
+ onChange,
63
+ placeholder = "Type '/' for commands…",
64
+ disabled = false,
65
+ autoFocus = false,
66
+ onSave,
67
+ className = '',
68
+ minHeight = 320,
69
+ },
70
+ ref,
71
+ ) {
72
+ const isExternalUpdate = useRef(false);
73
+ const [linkDialogOpen, setLinkDialogOpen] = useState(false);
74
+
75
+ // Build extensions once. Placeholder is captured by closure on first
76
+ // render — same constraint as MarkdownEditor; mentions / slash items
77
+ // don't change at runtime here.
78
+ const extensions = useMemo(() => notionExtensions({ placeholder }), [placeholder]);
79
+
80
+ const editor = useEditor({
81
+ immediatelyRender: false,
82
+ editable: !disabled,
83
+ extensions,
84
+ content: value,
85
+ contentType: 'markdown',
86
+ onUpdate: ({ editor }) => {
87
+ if (isExternalUpdate.current) return;
88
+ onChange(getMarkdown(editor));
89
+ },
90
+ editorProps: {
91
+ attributes: {
92
+ class: 'notion-editor-content focus:outline-none',
93
+ style: `min-height: ${minHeight}px`,
94
+ },
95
+ },
96
+ });
97
+
98
+ // Sync external value → editor without looping back into onChange.
99
+ useEffect(() => {
100
+ if (!editor) return;
101
+ const current = getMarkdown(editor);
102
+ if (current === value) return;
103
+ isExternalUpdate.current = true;
104
+ editor.commands.setContent(value, {
105
+ contentType: 'markdown',
106
+ emitUpdate: false,
107
+ });
108
+ isExternalUpdate.current = false;
109
+ }, [value, editor]);
110
+
111
+ useEffect(() => {
112
+ editor?.setEditable(!disabled);
113
+ }, [editor, disabled]);
114
+
115
+ // Declarative autoFocus.
116
+ useEffect(() => {
117
+ if (!autoFocus || !editor) return;
118
+ editor.commands.focus('end');
119
+ }, [autoFocus, editor]);
120
+
121
+ // Cmd/Ctrl+S → save, scoped to the editor DOM via guard.
122
+ const onSaveRef = useRef(onSave);
123
+ onSaveRef.current = onSave;
124
+ useHotkey(
125
+ 'mod+s',
126
+ () => {
127
+ const h = onSaveRef.current;
128
+ if (!h || !editor) return;
129
+ const dom = editor.view.dom;
130
+ const active = document.activeElement;
131
+ if (!active || !dom.contains(active)) return;
132
+ h(getMarkdown(editor));
133
+ },
134
+ { enabled: !!editor && !!onSave },
135
+ );
136
+
137
+ // Cmd/Ctrl+K → link prompt. Same focus-scope guard so we don't
138
+ // collide with global command palettes higher up the tree.
139
+ useHotkey(
140
+ 'mod+k',
141
+ () => {
142
+ if (!editor) return;
143
+ const dom = editor.view.dom;
144
+ const active = document.activeElement;
145
+ if (!active || !dom.contains(active)) return;
146
+ setLinkDialogOpen(true);
147
+ },
148
+ { enabled: !!editor },
149
+ );
150
+
151
+ useImperativeHandle(
152
+ ref,
153
+ (): NotionEditorHandle => ({
154
+ focus: () => {
155
+ editor?.commands.focus();
156
+ },
157
+ moveCursorToEnd: () => {
158
+ editor?.commands.focus('end');
159
+ },
160
+ getEditor: () => editor ?? null,
161
+ }),
162
+ [editor],
163
+ );
164
+
165
+ return (
166
+ <div className={`notion-editor ${disabled ? 'opacity-60' : ''} ${className}`.trim()}>
167
+ {editor ? (
168
+ <>
169
+ <BubbleSelectionToolbar
170
+ editor={editor}
171
+ onOpenLink={() => setLinkDialogOpen(true)}
172
+ />
173
+ <LinkDialog
174
+ editor={editor}
175
+ open={linkDialogOpen}
176
+ onOpenChange={setLinkDialogOpen}
177
+ />
178
+ </>
179
+ ) : null}
180
+ <EditorContent editor={editor} />
181
+ </div>
182
+ );
183
+ },
184
+ );
185
+
186
+ interface BubbleItem {
187
+ icon: LucideIcon;
188
+ title: string;
189
+ /** Keyboard shortcut to display in the tooltip. macOS-style (⌘ B). */
190
+ shortcut: readonly string[];
191
+ isActive: () => boolean;
192
+ run: () => void;
193
+ }
194
+
195
+ /**
196
+ * Floating selection toolbar. `@tiptap/extension-bubble-menu` anchors
197
+ * itself to the current selection; it auto-hides when the selection
198
+ * collapses, so we only need to declare the buttons. Tooltips show the
199
+ * keyboard chord — Tiptap installs the shortcuts itself (StarterKit).
200
+ */
201
+ function BubbleSelectionToolbar({
202
+ editor,
203
+ onOpenLink,
204
+ }: {
205
+ editor: Editor;
206
+ onOpenLink: () => void;
207
+ }) {
208
+ const items: BubbleItem[] = [
209
+ {
210
+ icon: Bold,
211
+ title: 'Bold',
212
+ shortcut: ['⌘', 'B'],
213
+ isActive: () => editor.isActive('bold'),
214
+ run: () => editor.chain().focus().toggleBold().run(),
215
+ },
216
+ {
217
+ icon: Italic,
218
+ title: 'Italic',
219
+ shortcut: ['⌘', 'I'],
220
+ isActive: () => editor.isActive('italic'),
221
+ run: () => editor.chain().focus().toggleItalic().run(),
222
+ },
223
+ {
224
+ icon: UnderlineIcon,
225
+ title: 'Underline',
226
+ shortcut: ['⌘', 'U'],
227
+ isActive: () => editor.isActive('underline'),
228
+ run: () => editor.chain().focus().toggleUnderline().run(),
229
+ },
230
+ {
231
+ icon: Strikethrough,
232
+ title: 'Strike',
233
+ shortcut: ['⌘', '⇧', 'X'],
234
+ isActive: () => editor.isActive('strike'),
235
+ run: () => editor.chain().focus().toggleStrike().run(),
236
+ },
237
+ {
238
+ icon: CodeIcon,
239
+ title: 'Inline code',
240
+ shortcut: ['⌘', 'E'],
241
+ isActive: () => editor.isActive('code'),
242
+ run: () => editor.chain().focus().toggleCode().run(),
243
+ },
244
+ {
245
+ icon: Highlighter,
246
+ title: 'Highlight',
247
+ shortcut: ['⌘', '⇧', 'H'],
248
+ isActive: () => editor.isActive('highlight'),
249
+ run: () => editor.chain().focus().toggleHighlight().run(),
250
+ },
251
+ {
252
+ icon: LinkIcon,
253
+ title: 'Link',
254
+ shortcut: ['⌘', 'K'],
255
+ isActive: () => editor.isActive('link'),
256
+ run: onOpenLink,
257
+ },
258
+ ];
259
+
260
+ return (
261
+ <BubbleMenu
262
+ editor={editor}
263
+ className="notion-bubble-menu"
264
+ // Hide inside code blocks (Bold/Italic don't apply there) and when
265
+ // the selection is empty/whitespace-only. Default behaviour shows
266
+ // the menu on any non-empty range, which is too eager.
267
+ shouldShow={({ editor, from, to }) => {
268
+ if (from === to) return false;
269
+ if (editor.isActive('codeBlock')) return false;
270
+ const text = editor.state.doc.textBetween(from, to, ' ').trim();
271
+ return text.length > 0;
272
+ }}
273
+ >
274
+ {items.map((item) => {
275
+ const Icon = item.icon;
276
+ const active = item.isActive();
277
+ return (
278
+ <Tooltip key={item.title}>
279
+ <TooltipTrigger asChild>
280
+ <button
281
+ type="button"
282
+ aria-label={item.title}
283
+ aria-pressed={active}
284
+ onMouseDown={(e) => e.preventDefault()}
285
+ onClick={item.run}
286
+ className={`notion-bubble-btn${active ? ' notion-bubble-btn--active' : ''}`}
287
+ >
288
+ <Icon style={{ width: 14, height: 14 }} aria-hidden />
289
+ </button>
290
+ </TooltipTrigger>
291
+ <TooltipContent side="top" sideOffset={6} className="flex items-center gap-1.5">
292
+ <span>{item.title}</span>
293
+ <span className="flex items-center gap-0.5">
294
+ {item.shortcut.map((k) => (
295
+ <Kbd key={k}>{k}</Kbd>
296
+ ))}
297
+ </span>
298
+ </TooltipContent>
299
+ </Tooltip>
300
+ );
301
+ })}
302
+ </BubbleMenu>
303
+ );
304
+ }
@@ -0,0 +1,32 @@
1
+ import { Extension } from '@tiptap/core';
2
+ import Suggestion, { type SuggestionOptions } from '@tiptap/suggestion';
3
+
4
+ /**
5
+ * Tiptap extension that wires `@tiptap/suggestion` for the slash menu.
6
+ * The actual suggestion config (items / render / floating-ui popup) is
7
+ * passed in via options — keeps this file UI-agnostic and lets the
8
+ * popover live in `createSlashSuggestion.ts`.
9
+ */
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ export interface SlashExtensionOptions {
12
+ suggestion: Omit<SuggestionOptions<any>, 'editor'>;
13
+ }
14
+
15
+ export const SlashExtension = Extension.create<SlashExtensionOptions>({
16
+ name: 'notion-slash-command',
17
+
18
+ addOptions() {
19
+ return {
20
+ suggestion: { char: '/' } as Omit<SuggestionOptions<unknown>, 'editor'>,
21
+ };
22
+ },
23
+
24
+ addProseMirrorPlugins() {
25
+ return [
26
+ Suggestion({
27
+ editor: this.editor,
28
+ ...this.options.suggestion,
29
+ }),
30
+ ];
31
+ },
32
+ });
@@ -0,0 +1,136 @@
1
+ 'use client';
2
+
3
+ import {
4
+ forwardRef,
5
+ useCallback,
6
+ useEffect,
7
+ useImperativeHandle,
8
+ useRef,
9
+ useState,
10
+ } from 'react';
11
+ import type { SlashItem } from './slashItems';
12
+
13
+ export interface SlashListRef {
14
+ /** Forwarded from `@tiptap/suggestion.render().onKeyDown`, which fires
15
+ * the underlying native `KeyboardEvent` — typing this as React's
16
+ * synthetic event would be a lie. */
17
+ onKeyDown: (event: KeyboardEvent) => boolean;
18
+ }
19
+
20
+ interface SlashListProps {
21
+ items: SlashItem[];
22
+ command: (item: SlashItem) => void;
23
+ }
24
+
25
+ const LISTBOX_ID = 'notion-slash-menu';
26
+
27
+ /**
28
+ * Slash-command popover content. Mirrors the MentionList pattern —
29
+ * Arrow keys / Enter / Tab keyboard navigation, mouse hover preview,
30
+ * auto-scroll active item into view.
31
+ */
32
+ export const SlashList = forwardRef<SlashListRef, SlashListProps>(
33
+ ({ items, command }, ref) => {
34
+ const [selectedIndex, setSelectedIndex] = useState(0);
35
+ const itemRefs = useRef<(HTMLButtonElement | null)[]>([]);
36
+
37
+ useEffect(() => setSelectedIndex(0), [items]);
38
+
39
+ useEffect(() => {
40
+ itemRefs.current[selectedIndex]?.scrollIntoView({ block: 'nearest' });
41
+ }, [selectedIndex]);
42
+
43
+ const select = useCallback(
44
+ (index: number) => {
45
+ const item = items[index];
46
+ if (item) command(item);
47
+ },
48
+ [items, command],
49
+ );
50
+
51
+ useImperativeHandle(ref, () => ({
52
+ onKeyDown: (event: KeyboardEvent) => {
53
+ if (items.length === 0) return false;
54
+ if (event.key === 'ArrowUp') {
55
+ setSelectedIndex((i) => (i + items.length - 1) % items.length);
56
+ return true;
57
+ }
58
+ if (event.key === 'ArrowDown') {
59
+ setSelectedIndex((i) => (i + 1) % items.length);
60
+ return true;
61
+ }
62
+ if (event.key === 'Enter' || event.key === 'Tab') {
63
+ select(selectedIndex);
64
+ return true;
65
+ }
66
+ return false;
67
+ },
68
+ }));
69
+
70
+ // Empty state — render a "no matches" pill instead of vanishing so
71
+ // the user gets feedback that the popover is alive (not a bug).
72
+ if (items.length === 0) {
73
+ return (
74
+ <div
75
+ id={LISTBOX_ID}
76
+ role="listbox"
77
+ aria-label="Slash commands"
78
+ className="notion-slash-list"
79
+ >
80
+ <div className="notion-slash-empty">No matching blocks</div>
81
+ </div>
82
+ );
83
+ }
84
+
85
+ const activeId = `${LISTBOX_ID}-${selectedIndex}`;
86
+
87
+ return (
88
+ <div
89
+ id={LISTBOX_ID}
90
+ role="listbox"
91
+ aria-label="Slash commands"
92
+ aria-activedescendant={activeId}
93
+ className="notion-slash-list"
94
+ >
95
+ {items.map((item, i) => {
96
+ const Icon = item.icon;
97
+ const isActive = i === selectedIndex;
98
+ return (
99
+ <button
100
+ key={item.title}
101
+ id={`${LISTBOX_ID}-${i}`}
102
+ ref={(el) => {
103
+ itemRefs.current[i] = el;
104
+ }}
105
+ type="button"
106
+ role="option"
107
+ aria-selected={isActive}
108
+ onMouseEnter={() => setSelectedIndex(i)}
109
+ onClick={() => select(i)}
110
+ // Prevent the click from blurring the editor — without this,
111
+ // ProseMirror loses its selection before the slash command
112
+ // can resolve the range.
113
+ onMouseDown={(e) => e.preventDefault()}
114
+ className={`notion-slash-item${isActive ? ' notion-slash-item--active' : ''}`}
115
+ >
116
+ <span className="notion-slash-icon">
117
+ <Icon style={{ width: 16, height: 16 }} aria-hidden />
118
+ </span>
119
+ <span className="notion-slash-meta">
120
+ <span className="notion-slash-title">{item.title}</span>
121
+ <span className="notion-slash-desc">{item.description}</span>
122
+ </span>
123
+ {item.hint ? (
124
+ <span className="notion-slash-hint" aria-hidden>
125
+ {item.hint}
126
+ </span>
127
+ ) : null}
128
+ </button>
129
+ );
130
+ })}
131
+ </div>
132
+ );
133
+ },
134
+ );
135
+
136
+ SlashList.displayName = 'SlashList';
@@ -0,0 +1,41 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * React NodeView for `taskItem`. The default Tiptap renderer mints a
5
+ * raw `<input type="checkbox">`; we swap it for our ui-core `<Checkbox>`
6
+ * so task lists pick up the macOS / app token styling (radius, ring,
7
+ * focus state) and stay theme-aware.
8
+ *
9
+ * Wired via `TaskItem.extend({ addNodeView: () => ReactNodeViewRenderer(TaskItemView) })`
10
+ * — TaskItem.attributes already expose `checked` and `editor.commands`
11
+ * persists the change back into ProseMirror state. We only render the UI.
12
+ */
13
+
14
+ import { NodeViewContent, NodeViewWrapper, type NodeViewProps } from '@tiptap/react';
15
+ import { Checkbox } from '@djangocfg/ui-core';
16
+
17
+ export function TaskItemView({ node, updateAttributes, editor }: NodeViewProps) {
18
+ const checked = node.attrs.checked === true;
19
+ const isEditable = editor.isEditable;
20
+
21
+ return (
22
+ <NodeViewWrapper
23
+ as="li"
24
+ data-type="taskList"
25
+ data-checked={checked || undefined}
26
+ className="notion-task-item"
27
+ >
28
+ <Checkbox
29
+ checked={checked}
30
+ disabled={!isEditable}
31
+ onCheckedChange={(value) => updateAttributes({ checked: value === true })}
32
+ // Stop ProseMirror from intercepting the click — without this
33
+ // the editor steals focus mid-toggle and the state flickers.
34
+ onClick={(event) => event.stopPropagation()}
35
+ aria-label={checked ? 'Mark as not done' : 'Mark as done'}
36
+ className="notion-task-checkbox"
37
+ />
38
+ <NodeViewContent as="div" className="notion-task-text" />
39
+ </NodeViewWrapper>
40
+ );
41
+ }
@@ -0,0 +1,121 @@
1
+ import { ReactRenderer } from '@tiptap/react';
2
+ import type { SuggestionOptions } from '@tiptap/suggestion';
3
+ import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';
4
+ import { SlashList, type SlashListRef } from './SlashList';
5
+ import { filterSlashItems, type SlashItem } from './slashItems';
6
+
7
+ /**
8
+ * `@tiptap/suggestion` config for the slash menu. Mirrors the mention
9
+ * suggestion pattern in `MarkdownEditor/createMentionSuggestion.ts`:
10
+ * floating-ui positioning, virtual element backed by the caret rect,
11
+ * keyboard nav delegated to `SlashList` via an imperative ref.
12
+ *
13
+ * Editor never imports this directly — `notionExtensions()` consumes it.
14
+ */
15
+ export function createSlashSuggestion(): Omit<SuggestionOptions<SlashItem>, 'editor'> {
16
+ return {
17
+ char: '/',
18
+ // Block the menu inside code blocks — `/` is valid syntax there
19
+ // (regex literals, paths) and a floating popover would be noise.
20
+ // Use `editor.isActive('codeBlock')` rather than `state.doc.resolve()`
21
+ // so nested cases (code block in a table cell, in a list item) all
22
+ // resolve correctly — `parent.type.name` only sees the immediate
23
+ // ancestor, which can be the cell/li instead of the code block.
24
+ allow: ({ editor }) => !editor.isActive('codeBlock'),
25
+ items: ({ query }) => filterSlashItems(query),
26
+ command: ({ editor, range, props }) => {
27
+ props.command({ editor, range });
28
+ },
29
+
30
+ render: () => {
31
+ let component: ReactRenderer<SlashListRef> | null = null;
32
+ let popup: HTMLDivElement | null = null;
33
+ let cleanupAutoUpdate: (() => void) | null = null;
34
+ let getReferenceRect: (() => DOMRect | null) | null = null;
35
+
36
+ const buildVirtualElement = () => ({
37
+ getBoundingClientRect: () => getReferenceRect?.() ?? new DOMRect(0, 0, 0, 0),
38
+ });
39
+
40
+ const setPopupVisible = (visible: boolean) => {
41
+ if (popup) popup.style.display = visible ? '' : 'none';
42
+ };
43
+
44
+ const updatePosition = () => {
45
+ if (!popup) return;
46
+ const virtualEl = buildVirtualElement();
47
+ void computePosition(virtualEl, popup, {
48
+ placement: 'bottom-start',
49
+ middleware: [
50
+ offset(6),
51
+ flip({ fallbackPlacements: ['top-start'] }),
52
+ shift({ padding: 8 }),
53
+ ],
54
+ }).then(({ x, y }) => {
55
+ if (!popup) return;
56
+ popup.style.transform = `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)`;
57
+ });
58
+ };
59
+
60
+ const teardown = () => {
61
+ cleanupAutoUpdate?.();
62
+ cleanupAutoUpdate = null;
63
+ popup?.remove();
64
+ popup = null;
65
+ component?.destroy();
66
+ component = null;
67
+ getReferenceRect = null;
68
+ };
69
+
70
+ return {
71
+ onStart: (props) => {
72
+ component = new ReactRenderer(SlashList, {
73
+ props: {
74
+ items: props.items,
75
+ command: (item: SlashItem) => {
76
+ props.command(item);
77
+ },
78
+ },
79
+ editor: props.editor,
80
+ });
81
+
82
+ popup = document.createElement('div');
83
+ popup.style.cssText = 'position: absolute; top: 0; left: 0; z-index: 99999;';
84
+ popup.appendChild(component.element);
85
+ document.body.appendChild(popup);
86
+ setPopupVisible(props.items.length > 0);
87
+
88
+ getReferenceRect = () => props.clientRect?.() ?? null;
89
+ const virtualEl = buildVirtualElement();
90
+ cleanupAutoUpdate = autoUpdate(virtualEl, popup, updatePosition);
91
+ },
92
+
93
+ onUpdate: (props) => {
94
+ component?.updateProps({
95
+ items: props.items,
96
+ command: (item: SlashItem) => {
97
+ props.command(item);
98
+ },
99
+ });
100
+ setPopupVisible(props.items.length > 0);
101
+ getReferenceRect = () => props.clientRect?.() ?? null;
102
+ updatePosition();
103
+ },
104
+
105
+ onKeyDown: (props) => {
106
+ if (props.event.key === 'Escape') {
107
+ teardown();
108
+ return true;
109
+ }
110
+ // `props.event` is a native KeyboardEvent — SlashListRef types
111
+ // it as such, no cast needed.
112
+ return component?.ref?.onKeyDown(props.event) ?? false;
113
+ },
114
+
115
+ onExit: () => {
116
+ teardown();
117
+ },
118
+ };
119
+ },
120
+ };
121
+ }