@vev/silke 1.1.9 → 1.2.0
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.
- package/.turbo/turbo-build.log +162 -77
- package/CHANGELOG.md +40 -0
- package/dist/index.js +47 -40
- package/dist/index.js.LEGAL.txt +1 -1
- package/dist/index.js.map +7 -0
- package/dist/styles.css +1596 -1013
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-autocomplete-field/autocomplete-item.d.ts +1 -1
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +5 -0
- package/dist/types/components/silke-box/silke-box.d.ts +2 -0
- package/dist/types/components/silke-button/silke-dropdown-button.d.ts +3 -1
- package/dist/types/components/silke-card/silke-card-grid.d.ts +1 -0
- package/dist/types/components/silke-card/silke-card.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +1 -1
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +16 -0
- package/dist/types/components/silke-command-text-field/utils.d.ts +25 -0
- package/dist/types/components/silke-css-number-field/silke-css-number-field.spec.d.ts +1 -0
- package/dist/types/components/silke-expression-input/index.d.ts +3 -0
- package/dist/types/components/silke-expression-input/silke-expression-input-tag.d.ts +6 -0
- package/dist/types/components/silke-expression-input/silke-expression-input.d.ts +29 -0
- package/dist/types/components/silke-expression-input/utils.d.ts +18 -0
- package/dist/types/components/silke-form-schema/fields/array-field-inline.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field-modal.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/group-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/layout-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/object-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/popover-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/string-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/index.d.ts +1 -0
- package/dist/types/components/silke-form-schema/silke-form-schema.d.ts +4 -1
- package/dist/types/components/silke-form-schema/silke-schema-decorator.d.ts +17 -0
- package/dist/types/components/silke-form-schema/silke-schema-field.d.ts +2 -1
- package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +2 -0
- package/dist/types/components/silke-form-schema/silke-schema-path.spec.d.ts +1 -0
- package/dist/types/components/silke-gradient-picker/silke-gradient-utils.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/color-swatch-section.d.ts +6 -0
- package/dist/types/components/silke-icon-picker/emoji-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/icons-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/silke-icon-picker.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/utils.d.ts +2 -0
- package/dist/types/components/silke-list/silke-list-item.d.ts +5 -1
- package/dist/types/components/silke-list/types.d.ts +1 -0
- package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +15 -0
- package/dist/types/components/silke-popover/popover-context.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/blockquote.d.ts +21 -0
- package/dist/types/components/silke-rich-text-field/blockquote.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/dropcap.d.ts +27 -0
- package/dist/types/components/silke-rich-text-field/dropcap.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/floating-layer.d.ts +12 -0
- package/dist/types/components/silke-rich-text-field/font-size.d.ts +2 -0
- package/dist/types/components/silke-rich-text-field/formatting-toolbar.d.ts +9 -0
- package/dist/types/components/silke-rich-text-field/index.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/jsdom-rects.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/link-menu.d.ts +26 -0
- package/dist/types/components/silke-rich-text-field/link-menu.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.d.ts +48 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-tooltip.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-item.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-link-fontsize.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/plugins.d.ts +18 -0
- package/dist/types/components/silke-rich-text-field/plugins.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/silke-rich-text-field.d.ts +60 -0
- package/dist/types/components/silke-rich-text-field/slash-menu-items.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/slash-menu.d.ts +11 -0
- package/dist/types/components/silke-rich-text-field/table-content-sync.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.d.ts +17 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/use-link-hover.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/use-slash-menu.d.ts +13 -0
- package/dist/types/components/silke-rich-text-field/utils.d.ts +13 -0
- package/dist/types/components/silke-sortable-list/index.d.ts +1 -1
- package/dist/types/components/silke-sortable-list/silke-nested-sortable-list.d.ts +8 -1
- package/dist/types/components/silke-theme-card/silke-theme-card.d.ts +13 -6
- package/dist/types/components/silke-theme-swatch/silke-theme-swatch.d.ts +6 -1
- package/dist/types/icons.d.ts +18 -0
- package/esbuild.config.mjs +9 -4
- package/package.json +22 -9
- package/src/components/index.ts +2 -0
- package/src/components/silke-alert-banner/silke-alert-banner.tsx +1 -1
- package/src/components/silke-autocomplete-field/autocomplete-item.tsx +47 -61
- package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +13 -2
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +8 -5
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +2 -1
- package/src/components/silke-box/silke-box.scss +4 -0
- package/src/components/silke-box/silke-box.tsx +4 -0
- package/src/components/silke-button/silke-dropdown-button.tsx +4 -1
- package/src/components/silke-card/silke-card.scss +4 -0
- package/src/components/silke-card/silke-card.tsx +3 -0
- package/src/components/silke-command-text-field/index.ts +1 -0
- package/src/components/silke-command-text-field/silke-command-parser.spec.ts +55 -0
- package/src/components/silke-command-text-field/silke-command-parser.ts +33 -2
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +3 -0
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +53 -48
- package/src/components/silke-command-text-field/utils.spec.ts +192 -0
- package/src/components/silke-command-text-field/utils.ts +99 -16
- package/src/components/silke-css-number-field/silke-css-number-field.spec.tsx +115 -0
- package/src/components/silke-css-number-field/silke-css-number-field.tsx +33 -0
- package/src/components/silke-expression-input/index.ts +3 -0
- package/src/components/silke-expression-input/silke-expression-input-tag.tsx +37 -0
- package/src/components/silke-expression-input/silke-expression-input.mdx +40 -0
- package/src/components/silke-expression-input/silke-expression-input.scss +24 -0
- package/src/components/silke-expression-input/silke-expression-input.tsx +343 -0
- package/src/components/silke-expression-input/utils.ts +144 -0
- package/src/components/silke-form-schema/fields/array-field-inline.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field.tsx +6 -11
- package/src/components/silke-form-schema/fields/group-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/layout-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/number-field.tsx +114 -4
- package/src/components/silke-form-schema/fields/object-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/popover-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/string-field.tsx +27 -1
- package/src/components/silke-form-schema/index.ts +1 -0
- package/src/components/silke-form-schema/silke-form-schema.mdx +30 -1
- package/src/components/silke-form-schema/silke-form-schema.tsx +12 -6
- package/src/components/silke-form-schema/silke-schema-decorator.tsx +32 -0
- package/src/components/silke-form-schema/silke-schema-field.tsx +19 -2
- package/src/components/silke-form-schema/silke-schema-fields.tsx +30 -4
- package/src/components/silke-form-schema/silke-schema-path.spec.tsx +160 -0
- package/src/components/silke-gradient-picker/silke-gradient-picker.tsx +3 -1
- package/src/components/silke-gradient-picker/silke-gradient-utils.ts +23 -3
- package/src/components/silke-icon/silke-shape.tsx +1 -1
- package/src/components/silke-icon-picker/color-swatch-section.tsx +61 -0
- package/src/components/silke-icon-picker/emoji-tab.tsx +8 -6
- package/src/components/silke-icon-picker/icons-tab.tsx +8 -7
- package/src/components/silke-icon-picker/silke-icon-picker.tsx +52 -25
- package/src/components/silke-icon-picker/styles.scss +66 -1
- package/src/components/silke-icon-picker/utils.ts +2 -0
- package/src/components/silke-list/silke-list-item.scss +10 -0
- package/src/components/silke-list/silke-list-item.tsx +16 -3
- package/src/components/silke-list/silke-list.mdx +18 -0
- package/src/components/silke-list/types.ts +1 -0
- package/src/components/silke-navigation-link/silke-navigation-link.scss +162 -42
- package/src/components/silke-navigation-link/silke-navigation-link.tsx +99 -21
- package/src/components/silke-popover/popover-context.spec.tsx +44 -0
- package/src/components/silke-popover-native/silke-popover-native.scss +5 -3
- package/src/components/silke-rich-text-field/blockquote.spec.ts +151 -0
- package/src/components/silke-rich-text-field/blockquote.ts +90 -0
- package/src/components/silke-rich-text-field/dropcap.spec.ts +67 -0
- package/src/components/silke-rich-text-field/dropcap.ts +59 -0
- package/src/components/silke-rich-text-field/floating-layer.tsx +15 -0
- package/src/components/silke-rich-text-field/font-size.ts +62 -0
- package/src/components/silke-rich-text-field/formatting-toolbar.tsx +145 -0
- package/src/components/silke-rich-text-field/index.ts +1 -0
- package/src/components/silke-rich-text-field/jsdom-rects.ts +15 -0
- package/src/components/silke-rich-text-field/link-menu.spec.tsx +502 -0
- package/src/components/silke-rich-text-field/link-menu.tsx +553 -0
- package/src/components/silke-rich-text-field/link-suggestions.spec.ts +203 -0
- package/src/components/silke-rich-text-field/link-suggestions.ts +188 -0
- package/src/components/silke-rich-text-field/link-tooltip.tsx +60 -0
- package/src/components/silke-rich-text-field/list-item.ts +64 -0
- package/src/components/silke-rich-text-field/list-link-fontsize.spec.ts +135 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.spec.ts +131 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.ts +51 -0
- package/src/components/silke-rich-text-field/paste.spec.ts +93 -0
- package/src/components/silke-rich-text-field/paste.ts +39 -0
- package/src/components/silke-rich-text-field/plugins.spec.ts +143 -0
- package/src/components/silke-rich-text-field/plugins.ts +230 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.mdx +180 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.scss +343 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.tsx +505 -0
- package/src/components/silke-rich-text-field/slash-menu-items.ts +112 -0
- package/src/components/silke-rich-text-field/slash-menu.tsx +50 -0
- package/src/components/silke-rich-text-field/table-content-sync.spec.tsx +61 -0
- package/src/components/silke-rich-text-field/table-toolbar.spec.tsx +168 -0
- package/src/components/silke-rich-text-field/table-toolbar.tsx +121 -0
- package/src/components/silke-rich-text-field/table.spec.ts +87 -0
- package/src/components/silke-rich-text-field/use-link-hover.ts +63 -0
- package/src/components/silke-rich-text-field/use-slash-menu.ts +130 -0
- package/src/components/silke-rich-text-field/utils.ts +55 -0
- package/src/components/silke-sortable-list/index.ts +1 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +11 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.tsx +9 -0
- package/src/components/silke-sortable-list/silke-sortable-list.mdx +13 -0
- package/src/components/silke-tag/silke-tag.scss +12 -3
- package/src/components/silke-theme-card/silke-theme-card.module.scss +141 -45
- package/src/components/silke-theme-card/silke-theme-card.tsx +76 -47
- package/src/components/silke-theme-swatch/silke-theme-swatch.mdx +56 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.module.scss +53 -63
- package/src/components/silke-theme-swatch/silke-theme-swatch.tsx +99 -14
- package/src/icons.tsx +65 -4
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { EditorContent, useEditor } from '@tiptap/react';
|
|
2
|
+
import classnames from 'classnames/bind';
|
|
3
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import type { Entry } from '../silke-autocomplete-field/autocomplete-types';
|
|
5
|
+
import { SilkeInputSize } from '../silke-text-field/silke-text-field';
|
|
6
|
+
import { SilkeTextFieldOutline } from '../silke-text-field/silke-text-field-outline';
|
|
7
|
+
import { FloatingLayer } from './floating-layer';
|
|
8
|
+
import { FormattingToolbar } from './formatting-toolbar';
|
|
9
|
+
import { LinkMenu, type LinkAttributes } from './link-menu';
|
|
10
|
+
import { LinkTooltip } from './link-tooltip';
|
|
11
|
+
import { buildExtensions, selectionHighlightKey, selectionHighlightPlugin } from './plugins';
|
|
12
|
+
import styles from './silke-rich-text-field.scss';
|
|
13
|
+
import { SlashMenu } from './slash-menu';
|
|
14
|
+
import { TableToolbar } from './table-toolbar';
|
|
15
|
+
import { useLinkHover } from './use-link-hover';
|
|
16
|
+
import { useSlashMenu } from './use-slash-menu';
|
|
17
|
+
import { getCursorPosition, isInTable, useLatest } from './utils';
|
|
18
|
+
|
|
19
|
+
export { PluginKey, Plugin as PmPlugin } from '@tiptap/pm/state';
|
|
20
|
+
export { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
21
|
+
export { Editor } from '@tiptap/react';
|
|
22
|
+
|
|
23
|
+
const cl = classnames.bind(styles);
|
|
24
|
+
|
|
25
|
+
// ─── Props ──────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
export interface SilkeRichTextFieldProps {
|
|
28
|
+
/** HTML content value */
|
|
29
|
+
value?: string;
|
|
30
|
+
/** Called when content changes with HTML string */
|
|
31
|
+
onChange?: (value: string) => void;
|
|
32
|
+
/** Placeholder text shown when editor is empty and unfocused */
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
/** Placeholder shown on focused empty blocks */
|
|
35
|
+
commandPlaceholder?: string;
|
|
36
|
+
/** Label displayed above the editor */
|
|
37
|
+
label?: string;
|
|
38
|
+
/** Help text displayed below the editor */
|
|
39
|
+
help?: string;
|
|
40
|
+
/** Error message */
|
|
41
|
+
error?: string;
|
|
42
|
+
/** Field style variant. 'plain' renders just the editor with no outline or label. */
|
|
43
|
+
kind?: 'default' | 'ghost' | 'zombie' | 'plain';
|
|
44
|
+
/** Field size */
|
|
45
|
+
size?: SilkeInputSize;
|
|
46
|
+
/** Minimum height of the editor area */
|
|
47
|
+
minHeight?: number | string;
|
|
48
|
+
/** Maximum height (enables scrolling) */
|
|
49
|
+
maxHeight?: number | string;
|
|
50
|
+
/** Whether the field is disabled */
|
|
51
|
+
disabled?: boolean;
|
|
52
|
+
/** Whether the field is read-only */
|
|
53
|
+
readOnly?: boolean;
|
|
54
|
+
/** Auto focus on mount */
|
|
55
|
+
autoFocus?: boolean;
|
|
56
|
+
/** Makes text field flex */
|
|
57
|
+
flex?: boolean;
|
|
58
|
+
/** Called on blur */
|
|
59
|
+
onBlur?: () => void;
|
|
60
|
+
/** Called on focus */
|
|
61
|
+
onFocus?: () => void;
|
|
62
|
+
/** Autocomplete items for the link picker popover */
|
|
63
|
+
linkItems?: Entry<string>[];
|
|
64
|
+
/** Called when user creates a new link not in the list */
|
|
65
|
+
onAddLink?: (value: string) => void;
|
|
66
|
+
/** Called when space is pressed on an empty block */
|
|
67
|
+
onSpaceCommand?: () => void;
|
|
68
|
+
/** Ref to access the underlying TipTap editor instance */
|
|
69
|
+
editorRef?: React.MutableRefObject<any>;
|
|
70
|
+
/** Custom CSS class to override default typography styles. When not provided, built-in typography is applied. */
|
|
71
|
+
typographyClassName?: string;
|
|
72
|
+
/** Called when user clicks the AI button in the toolbar. Receives the currently selected text. Only shows the AI button when provided. */
|
|
73
|
+
onSelectionAI?: (selectedText: string) => void;
|
|
74
|
+
/** Additional class name */
|
|
75
|
+
className?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ─── Component ──────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
export function SilkeRichTextField({
|
|
81
|
+
value,
|
|
82
|
+
onChange,
|
|
83
|
+
placeholder = '',
|
|
84
|
+
commandPlaceholder = "Press '/' for commands",
|
|
85
|
+
label,
|
|
86
|
+
help,
|
|
87
|
+
error,
|
|
88
|
+
kind,
|
|
89
|
+
size,
|
|
90
|
+
minHeight = 120,
|
|
91
|
+
maxHeight,
|
|
92
|
+
disabled = false,
|
|
93
|
+
readOnly = false,
|
|
94
|
+
autoFocus = false,
|
|
95
|
+
flex,
|
|
96
|
+
onBlur,
|
|
97
|
+
onFocus,
|
|
98
|
+
linkItems,
|
|
99
|
+
onAddLink,
|
|
100
|
+
onSpaceCommand,
|
|
101
|
+
onSelectionAI,
|
|
102
|
+
editorRef,
|
|
103
|
+
typographyClassName,
|
|
104
|
+
className,
|
|
105
|
+
}: SilkeRichTextFieldProps) {
|
|
106
|
+
// ── Refs ──────────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
const editorAreaRef = useRef<HTMLDivElement>(null);
|
|
109
|
+
const linkMenuRef = useRef<HTMLDivElement>(null);
|
|
110
|
+
const cmdHeldRef = useRef(false);
|
|
111
|
+
|
|
112
|
+
const onChangeRef = useLatest(onChange);
|
|
113
|
+
const onBlurRef = useLatest(onBlur);
|
|
114
|
+
const onFocusRef = useLatest(onFocus);
|
|
115
|
+
const onSpaceCommandRef = useLatest(onSpaceCommand);
|
|
116
|
+
const slashMenuRef = useRef<ReturnType<typeof useSlashMenu>>(null as any);
|
|
117
|
+
|
|
118
|
+
// ── State ─────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
121
|
+
const [linkMenuOpen, setLinkMenuOpen] = useState(false);
|
|
122
|
+
const skipFocusCloseRef = useRef(false);
|
|
123
|
+
const isEditable = !disabled && !readOnly;
|
|
124
|
+
|
|
125
|
+
// ── Editor ────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
const commandPlaceholderRef = useLatest(commandPlaceholder);
|
|
128
|
+
const extensions = useMemo(() => buildExtensions(commandPlaceholderRef), []);
|
|
129
|
+
|
|
130
|
+
const editor = useEditor({
|
|
131
|
+
extensions,
|
|
132
|
+
content: value || '',
|
|
133
|
+
editable: isEditable,
|
|
134
|
+
autofocus: autoFocus,
|
|
135
|
+
onUpdate: ({ editor: e }) => onChangeRef.current?.(e.getHTML()),
|
|
136
|
+
onFocus: ({ editor: e }) => {
|
|
137
|
+
setIsFocused(true);
|
|
138
|
+
if (skipFocusCloseRef.current) {
|
|
139
|
+
skipFocusCloseRef.current = false;
|
|
140
|
+
} else {
|
|
141
|
+
setLinkMenuOpen(false);
|
|
142
|
+
e.view.dispatch(e.state.tr.setMeta(selectionHighlightKey, 'clear'));
|
|
143
|
+
}
|
|
144
|
+
onFocusRef.current?.();
|
|
145
|
+
},
|
|
146
|
+
onBlur: ({ event }) => {
|
|
147
|
+
const related = (event as FocusEvent)?.relatedTarget as HTMLElement | null;
|
|
148
|
+
if (linkMenuRef.current?.contains(related)) return;
|
|
149
|
+
if (editorAreaRef.current?.contains(related)) return;
|
|
150
|
+
// The menus render in the floating layer, so they are no longer descendants
|
|
151
|
+
// of the editor area — match them by their marker attribute instead.
|
|
152
|
+
if (related?.closest('[data-rich-text-menu]')) return;
|
|
153
|
+
setIsFocused(false);
|
|
154
|
+
slashMenuRef.current?.close();
|
|
155
|
+
setLinkMenuOpen(false);
|
|
156
|
+
onBlurRef.current?.();
|
|
157
|
+
},
|
|
158
|
+
editorProps: {
|
|
159
|
+
attributes: { 'data-input': 'true' },
|
|
160
|
+
handleKeyDown: (view, event) => {
|
|
161
|
+
const sm = slashMenuRef.current;
|
|
162
|
+
|
|
163
|
+
if (sm?.open) {
|
|
164
|
+
if (event.key === 'ArrowDown') {
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
sm.navigateDown();
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
if (event.key === 'ArrowUp') {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
sm.navigateUp();
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
if (event.key === 'Enter') {
|
|
175
|
+
event.preventDefault();
|
|
176
|
+
const item = sm.filteredItems[sm.index];
|
|
177
|
+
if (item) sm.selectItem(item);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
if (event.key === 'Escape') {
|
|
181
|
+
sm.close();
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (event.key === ' ' && !sm?.open && onSpaceCommandRef.current) {
|
|
187
|
+
const { $from } = view.state.selection;
|
|
188
|
+
// Every cell of a fresh table is an empty block, so without the table guard
|
|
189
|
+
// the first space typed in one opens the space command instead of a space.
|
|
190
|
+
if ($from.parent.textContent.length === 0 && !isInTable($from)) {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
onSpaceCommandRef.current();
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return false;
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// ── Feature hooks ─────────────────────────────────────────────
|
|
203
|
+
|
|
204
|
+
const slashMenu = useSlashMenu(editor, isFocused);
|
|
205
|
+
slashMenuRef.current = slashMenu;
|
|
206
|
+
if (editorRef) editorRef.current = editor;
|
|
207
|
+
|
|
208
|
+
const linkHover = useLinkHover(editorAreaRef);
|
|
209
|
+
|
|
210
|
+
const linkLabelMap = useMemo(() => {
|
|
211
|
+
const map = new Map<string, string>();
|
|
212
|
+
if (linkItems) {
|
|
213
|
+
for (const item of linkItems) {
|
|
214
|
+
if (!item.divider && !item.disabled && item.value) {
|
|
215
|
+
map.set(item.value, item.label);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return map;
|
|
220
|
+
}, [linkItems]);
|
|
221
|
+
|
|
222
|
+
// ── Plugins ───────────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
if (!editor) return;
|
|
226
|
+
editor.registerPlugin(selectionHighlightPlugin);
|
|
227
|
+
return () => {
|
|
228
|
+
editor.unregisterPlugin(selectionHighlightKey);
|
|
229
|
+
};
|
|
230
|
+
}, [editor]);
|
|
231
|
+
|
|
232
|
+
// ── Sync props → editor ───────────────────────────────────────
|
|
233
|
+
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
if (!editor || value === undefined) return;
|
|
236
|
+
// While the editor has focus the user is the source of truth. Hosts round-trip
|
|
237
|
+
// onUpdate through their own state (the Vev editor writes it to the content
|
|
238
|
+
// store), so `value` comes back a keystroke or more behind what has been typed
|
|
239
|
+
// since — and setContent with that stale HTML rewinds the document, eating the
|
|
240
|
+
// characters in between. External updates land on the next blur.
|
|
241
|
+
if (editor.isFocused) return;
|
|
242
|
+
if (editor.getHTML() === value) return;
|
|
243
|
+
// emitUpdate:false — loading content must never save. setContent emits an update
|
|
244
|
+
// by default, so anything the schema normalizes on parse (an unsupported element,
|
|
245
|
+
// a reordered attribute) went straight back out through onChange and overwrote the
|
|
246
|
+
// host's content without the user having typed. That is how tables were destroyed
|
|
247
|
+
// simply by opening a text element. Edits still persist through onUpdate.
|
|
248
|
+
editor.commands.setContent(value, { emitUpdate: false });
|
|
249
|
+
}, [value, editor]);
|
|
250
|
+
|
|
251
|
+
useEffect(() => {
|
|
252
|
+
// Second argument is emitUpdate, and it defaults to true: toggling editability
|
|
253
|
+
// fired an update carrying whatever the schema had normalized the document into,
|
|
254
|
+
// which onChange then saved. This runs on every mount, so it saved over the
|
|
255
|
+
// host's content before the user had touched the field.
|
|
256
|
+
editor?.setEditable(isEditable, false);
|
|
257
|
+
}, [isEditable, editor]);
|
|
258
|
+
|
|
259
|
+
// ── Cmd/Ctrl tracking (ref + classList — no re-renders) ───────
|
|
260
|
+
|
|
261
|
+
useEffect(() => {
|
|
262
|
+
const el = editorAreaRef.current;
|
|
263
|
+
if (!el) return;
|
|
264
|
+
const cmdClass = cl('cmdHeld');
|
|
265
|
+
|
|
266
|
+
const setCmd = (held: boolean) => {
|
|
267
|
+
if (held === cmdHeldRef.current) return;
|
|
268
|
+
cmdHeldRef.current = held;
|
|
269
|
+
el.classList.toggle(cmdClass, held);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
273
|
+
if (e.metaKey || e.ctrlKey) setCmd(true);
|
|
274
|
+
};
|
|
275
|
+
const onKeyUp = (e: KeyboardEvent) => {
|
|
276
|
+
if (!e.metaKey && !e.ctrlKey) setCmd(false);
|
|
277
|
+
};
|
|
278
|
+
const onBlur = () => setCmd(false);
|
|
279
|
+
|
|
280
|
+
window.addEventListener('keydown', onKeyDown);
|
|
281
|
+
window.addEventListener('keyup', onKeyUp);
|
|
282
|
+
window.addEventListener('blur', onBlur);
|
|
283
|
+
return () => {
|
|
284
|
+
window.removeEventListener('keydown', onKeyDown);
|
|
285
|
+
window.removeEventListener('keyup', onKeyUp);
|
|
286
|
+
window.removeEventListener('blur', onBlur);
|
|
287
|
+
};
|
|
288
|
+
}, []);
|
|
289
|
+
|
|
290
|
+
// ── AI + Link callbacks ───────────────────────────────────────
|
|
291
|
+
|
|
292
|
+
const onSelectionAIRef = useLatest(onSelectionAI);
|
|
293
|
+
|
|
294
|
+
const handleAIClick = useCallback(() => {
|
|
295
|
+
if (!editor || !onSelectionAIRef.current) return;
|
|
296
|
+
const { from, to } = editor.state.selection;
|
|
297
|
+
const selectedText = editor.state.doc.textBetween(from, to, ' ');
|
|
298
|
+
editor.view.dispatch(editor.state.tr.setMeta(selectionHighlightKey, 'show'));
|
|
299
|
+
onSelectionAIRef.current(selectedText);
|
|
300
|
+
}, [editor]);
|
|
301
|
+
|
|
302
|
+
const openLinkMenu = useCallback(() => {
|
|
303
|
+
skipFocusCloseRef.current = true;
|
|
304
|
+
if (editor) {
|
|
305
|
+
editor.view.dispatch(editor.state.tr.setMeta(selectionHighlightKey, 'show'));
|
|
306
|
+
}
|
|
307
|
+
setLinkMenuOpen(true);
|
|
308
|
+
}, [editor]);
|
|
309
|
+
|
|
310
|
+
const closeLinkMenu = useCallback(
|
|
311
|
+
(refocusEditor?: boolean) => {
|
|
312
|
+
setLinkMenuOpen(false);
|
|
313
|
+
if (editor) {
|
|
314
|
+
editor.view.dispatch(editor.state.tr.setMeta(selectionHighlightKey, 'clear'));
|
|
315
|
+
}
|
|
316
|
+
if (refocusEditor) editor?.chain().focus().run();
|
|
317
|
+
},
|
|
318
|
+
[editor],
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const selectLink = useCallback(
|
|
322
|
+
(attrs: LinkAttributes) => {
|
|
323
|
+
if (!editor) return;
|
|
324
|
+
// extendMarkRange('link') so picking a menu item / re-submitting rewrites an
|
|
325
|
+
// existing link even when the cursor is collapsed inside it. It's a no-op when
|
|
326
|
+
// there's no link mark, so selected text still gets a fresh link.
|
|
327
|
+
editor.chain().focus().extendMarkRange('link').setLink(attrs).run();
|
|
328
|
+
closeLinkMenu();
|
|
329
|
+
},
|
|
330
|
+
[editor, closeLinkMenu],
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const updateLink = useCallback(
|
|
334
|
+
(attrs: LinkAttributes) => {
|
|
335
|
+
if (!editor) return;
|
|
336
|
+
// Must focus + extendMarkRange so setLink rewrites the whole existing link mark.
|
|
337
|
+
// Without focus the selection is collapsed and setMark only sets a *stored mark*
|
|
338
|
+
// (applies to the next keystroke), so target/rel toggles never persist.
|
|
339
|
+
// skipFocusCloseRef keeps the menu open when this refocus fires editor.onFocus.
|
|
340
|
+
skipFocusCloseRef.current = true;
|
|
341
|
+
editor.chain().focus().extendMarkRange('link').setLink(attrs).run();
|
|
342
|
+
},
|
|
343
|
+
[editor],
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
const unlinkAndClose = useCallback(() => {
|
|
347
|
+
if (!editor) return;
|
|
348
|
+
editor.chain().focus().unsetLink().run();
|
|
349
|
+
closeLinkMenu();
|
|
350
|
+
}, [editor, closeLinkMenu]);
|
|
351
|
+
|
|
352
|
+
useEffect(() => {
|
|
353
|
+
if (!linkMenuOpen) return;
|
|
354
|
+
const onMouseDown = (e: MouseEvent) => {
|
|
355
|
+
if (linkMenuRef.current?.contains(e.target as HTMLElement)) return;
|
|
356
|
+
setLinkMenuOpen(false);
|
|
357
|
+
};
|
|
358
|
+
// Capture phase: the rich text inspector stops mousedown propagation on its root
|
|
359
|
+
// (to keep canvas clicks from deselecting), which hides every click inside the
|
|
360
|
+
// editor from a bubble-phase listener here — so the menu never closed.
|
|
361
|
+
document.addEventListener('mousedown', onMouseDown, true);
|
|
362
|
+
return () => document.removeEventListener('mousedown', onMouseDown, true);
|
|
363
|
+
}, [linkMenuOpen]);
|
|
364
|
+
|
|
365
|
+
// ── Link tooltip handlers ─────────────────────────────────────
|
|
366
|
+
|
|
367
|
+
const handleLinkEdit = useCallback(() => {
|
|
368
|
+
if (!editor || !linkHover.hoveredAnchor.current) return;
|
|
369
|
+
const pos = editor.view.posAtDOM(linkHover.hoveredAnchor.current, 0);
|
|
370
|
+
editor.chain().focus().setTextSelection(pos).extendMarkRange('link').run();
|
|
371
|
+
linkHover.dismiss();
|
|
372
|
+
openLinkMenu();
|
|
373
|
+
}, [editor, linkHover, openLinkMenu]);
|
|
374
|
+
|
|
375
|
+
const handleLinkUnlink = useCallback(() => {
|
|
376
|
+
if (!editor || !linkHover.hoveredAnchor.current) return;
|
|
377
|
+
const pos = editor.view.posAtDOM(linkHover.hoveredAnchor.current, 0);
|
|
378
|
+
editor.chain().focus().setTextSelection(pos).extendMarkRange('link').unsetLink().run();
|
|
379
|
+
linkHover.dismiss();
|
|
380
|
+
}, [editor, linkHover]);
|
|
381
|
+
|
|
382
|
+
const handleLinkOpen = useCallback(() => {
|
|
383
|
+
if (linkHover.hoveredLink) window.open(linkHover.hoveredLink.href, '_blank', 'noopener');
|
|
384
|
+
}, [linkHover.hoveredLink]);
|
|
385
|
+
|
|
386
|
+
// ── Memoized class names & styles ─────────────────────────────
|
|
387
|
+
|
|
388
|
+
const editorContentClass = useMemo(
|
|
389
|
+
() =>
|
|
390
|
+
cl('editorContent', { defaultTypography: !typographyClassName }) +
|
|
391
|
+
(typographyClassName ? ' ' + typographyClassName : ''),
|
|
392
|
+
[typographyClassName],
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
const editorAreaClass = useMemo(
|
|
396
|
+
() =>
|
|
397
|
+
cl('editorArea', { empty: editor?.isEmpty, focused: isFocused, padded: kind !== 'plain' }),
|
|
398
|
+
[editor?.isEmpty, isFocused, kind],
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
const editorAreaStyle = useMemo(
|
|
402
|
+
() => ({
|
|
403
|
+
minHeight,
|
|
404
|
+
...(maxHeight ? { maxHeight, overflowY: 'auto' as const } : {}),
|
|
405
|
+
}),
|
|
406
|
+
[minHeight, maxHeight],
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const cursorPosition = useMemo(() => {
|
|
410
|
+
if (!slashMenu.open && !linkMenuOpen) return { left: 0, top: 0 };
|
|
411
|
+
return getCursorPosition(editor);
|
|
412
|
+
}, [slashMenu.open, linkMenuOpen, editor?.state.selection]);
|
|
413
|
+
|
|
414
|
+
const linkMenuCurrent = useMemo(
|
|
415
|
+
() =>
|
|
416
|
+
editor?.isActive('link')
|
|
417
|
+
? (editor.getAttributes('link') as LinkAttributes)
|
|
418
|
+
: null,
|
|
419
|
+
[editor?.state.selection],
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
// ── Render ────────────────────────────────────────────────────
|
|
423
|
+
|
|
424
|
+
const editorArea = (
|
|
425
|
+
<div
|
|
426
|
+
ref={editorAreaRef}
|
|
427
|
+
className={editorAreaClass}
|
|
428
|
+
data-placeholder={placeholder || undefined}
|
|
429
|
+
style={editorAreaStyle}
|
|
430
|
+
>
|
|
431
|
+
{editor && isEditable && (
|
|
432
|
+
<FormattingToolbar
|
|
433
|
+
editor={editor}
|
|
434
|
+
onLinkClick={openLinkMenu}
|
|
435
|
+
onBlockTypeClick={slashMenu.openFromToolbar}
|
|
436
|
+
onAIClick={onSelectionAI ? handleAIClick : undefined}
|
|
437
|
+
/>
|
|
438
|
+
)}
|
|
439
|
+
|
|
440
|
+
{editor && isEditable && (
|
|
441
|
+
<TableToolbar editor={editor} suppressed={slashMenu.open || linkMenuOpen} />
|
|
442
|
+
)}
|
|
443
|
+
|
|
444
|
+
<EditorContent editor={editor} className={editorContentClass} />
|
|
445
|
+
|
|
446
|
+
<FloatingLayer>
|
|
447
|
+
{linkHover.hoveredLink && !linkMenuOpen && editor && (
|
|
448
|
+
<LinkTooltip
|
|
449
|
+
href={linkHover.hoveredLink.href}
|
|
450
|
+
label={linkLabelMap.get(linkHover.hoveredLink.href)}
|
|
451
|
+
position={linkHover.hoveredLink.rect}
|
|
452
|
+
onMouseEnter={linkHover.cancelDismiss}
|
|
453
|
+
onMouseLeave={linkHover.dismiss}
|
|
454
|
+
onOpen={handleLinkOpen}
|
|
455
|
+
onEdit={handleLinkEdit}
|
|
456
|
+
onUnlink={handleLinkUnlink}
|
|
457
|
+
/>
|
|
458
|
+
)}
|
|
459
|
+
|
|
460
|
+
{slashMenu.open && slashMenu.filteredItems.length > 0 && (
|
|
461
|
+
<SlashMenu
|
|
462
|
+
items={slashMenu.filteredItems}
|
|
463
|
+
activeIndex={slashMenu.index}
|
|
464
|
+
position={cursorPosition}
|
|
465
|
+
onSelect={slashMenu.selectItem}
|
|
466
|
+
onHover={slashMenu.setIndex}
|
|
467
|
+
/>
|
|
468
|
+
)}
|
|
469
|
+
|
|
470
|
+
{linkMenuOpen && editor && (
|
|
471
|
+
<LinkMenu
|
|
472
|
+
items={linkItems || []}
|
|
473
|
+
current={linkMenuCurrent}
|
|
474
|
+
position={cursorPosition}
|
|
475
|
+
menuRef={linkMenuRef}
|
|
476
|
+
onSelect={selectLink}
|
|
477
|
+
onUpdate={updateLink}
|
|
478
|
+
onAdd={onAddLink}
|
|
479
|
+
onUnlink={editor.isActive('link') ? unlinkAndClose : undefined}
|
|
480
|
+
onClose={() => closeLinkMenu(true)}
|
|
481
|
+
/>
|
|
482
|
+
)}
|
|
483
|
+
</FloatingLayer>
|
|
484
|
+
</div>
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
if (kind === 'plain') return editorArea;
|
|
488
|
+
|
|
489
|
+
return (
|
|
490
|
+
<SilkeTextFieldOutline
|
|
491
|
+
label={label}
|
|
492
|
+
error={error}
|
|
493
|
+
help={help}
|
|
494
|
+
kind={kind}
|
|
495
|
+
size={size}
|
|
496
|
+
flex={flex}
|
|
497
|
+
disabled={disabled}
|
|
498
|
+
className={className}
|
|
499
|
+
>
|
|
500
|
+
{editorArea}
|
|
501
|
+
</SilkeTextFieldOutline>
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
SilkeRichTextField.displayName = 'SilkeRichTextField';
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/react';
|
|
2
|
+
import type { SilkeIcons } from '../silke-icon';
|
|
3
|
+
|
|
4
|
+
export interface SlashMenuItem {
|
|
5
|
+
label: string;
|
|
6
|
+
description: string;
|
|
7
|
+
icon: SilkeIcons;
|
|
8
|
+
keywords: string[];
|
|
9
|
+
command: (editor: Editor) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const SLASH_MENU_ITEMS: SlashMenuItem[] = [
|
|
13
|
+
{
|
|
14
|
+
label: 'Text',
|
|
15
|
+
description: 'Plain text',
|
|
16
|
+
icon: 'text.p',
|
|
17
|
+
keywords: ['p', 'paragraph', 'body', 'plain'],
|
|
18
|
+
command: (editor) => editor.chain().focus().setParagraph().run(),
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
label: 'Heading 1',
|
|
22
|
+
description: 'Large heading',
|
|
23
|
+
icon: 'text.h1',
|
|
24
|
+
keywords: ['h1', 'title', 'large'],
|
|
25
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 1 }).run(),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
label: 'Heading 2',
|
|
29
|
+
description: 'Medium heading',
|
|
30
|
+
icon: 'text.h2',
|
|
31
|
+
keywords: ['h2', 'subtitle', 'medium'],
|
|
32
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 2 }).run(),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
label: 'Heading 3',
|
|
36
|
+
description: 'Small heading',
|
|
37
|
+
icon: 'text.h3',
|
|
38
|
+
keywords: ['h3', 'small'],
|
|
39
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 3 }).run(),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
label: 'Heading 4',
|
|
43
|
+
description: 'Smaller heading',
|
|
44
|
+
icon: 'text.h4',
|
|
45
|
+
keywords: ['h4'],
|
|
46
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 4 }).run(),
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
label: 'Heading 5',
|
|
50
|
+
description: 'Tiny heading',
|
|
51
|
+
icon: 'text.h5',
|
|
52
|
+
keywords: ['h5'],
|
|
53
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 5 }).run(),
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
label: 'Heading 6',
|
|
57
|
+
description: 'Smallest heading',
|
|
58
|
+
icon: 'text.h6',
|
|
59
|
+
keywords: ['h6'],
|
|
60
|
+
command: (editor) => editor.chain().focus().setHeading({ level: 6 }).run(),
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
label: 'Bullet list',
|
|
64
|
+
description: 'Unordered list',
|
|
65
|
+
icon: 'list.check',
|
|
66
|
+
keywords: ['ul', 'unordered', 'bullets', 'dots'],
|
|
67
|
+
command: (editor) => {
|
|
68
|
+
// Heading → paragraph first so the list item cannot keep an h1 and
|
|
69
|
+
// render at the theme heading size (~48px) instead of the element size.
|
|
70
|
+
const chain = editor.chain().focus();
|
|
71
|
+
if (editor.isActive('heading')) chain.setParagraph();
|
|
72
|
+
chain.toggleBulletList().run();
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
label: 'Numbered list',
|
|
77
|
+
description: 'Ordered list',
|
|
78
|
+
icon: 'list.ol',
|
|
79
|
+
keywords: ['ol', 'ordered', 'numbers'],
|
|
80
|
+
command: (editor) => {
|
|
81
|
+
const chain = editor.chain().focus();
|
|
82
|
+
if (editor.isActive('heading')) chain.setParagraph();
|
|
83
|
+
chain.toggleOrderedList().run();
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
label: 'Quote',
|
|
88
|
+
description: 'Block quote',
|
|
89
|
+
icon: 'text.blockquote',
|
|
90
|
+
keywords: ['blockquote', 'cite', 'pullquote'],
|
|
91
|
+
// Toggle, like the two list items above — `setBlockquote` could only ever add
|
|
92
|
+
// a wrapper, so there was no way back out of a quote from this menu. The
|
|
93
|
+
// heading→paragraph demotion lives in blockquoteExtension so the `Mod-Shift-b`
|
|
94
|
+
// shortcut and the `> ` input rule get it too.
|
|
95
|
+
command: (editor) => editor.chain().focus().toggleBlockquote().run(),
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
label: 'Table',
|
|
99
|
+
description: '3×3 table with a header row',
|
|
100
|
+
icon: 'table',
|
|
101
|
+
keywords: ['table', 'grid', 'rows', 'columns', 'cells'],
|
|
102
|
+
command: (editor) =>
|
|
103
|
+
editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
label: 'Divider',
|
|
107
|
+
description: 'Horizontal rule',
|
|
108
|
+
icon: 'dots.horizontal',
|
|
109
|
+
keywords: ['hr', 'separator', 'line', 'rule'],
|
|
110
|
+
command: (editor) => editor.chain().focus().setHorizontalRule().run(),
|
|
111
|
+
},
|
|
112
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import classnames from 'classnames/bind';
|
|
3
|
+
import { SilkeBox } from '../silke-box';
|
|
4
|
+
import { SilkeList } from '../silke-list';
|
|
5
|
+
import type { SilkeListItemType } from '../silke-list/types';
|
|
6
|
+
import { SlashMenuItem } from './slash-menu-items';
|
|
7
|
+
import styles from './silke-rich-text-field.scss';
|
|
8
|
+
|
|
9
|
+
const cl = classnames.bind(styles);
|
|
10
|
+
|
|
11
|
+
interface SlashMenuProps {
|
|
12
|
+
items: SlashMenuItem[];
|
|
13
|
+
activeIndex: number;
|
|
14
|
+
position: React.CSSProperties;
|
|
15
|
+
onSelect: (item: SlashMenuItem) => void;
|
|
16
|
+
onHover: (index: number) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function SlashMenu({ items, activeIndex, position, onSelect, onHover }: SlashMenuProps) {
|
|
20
|
+
const listItems: SilkeListItemType[] = useMemo(
|
|
21
|
+
() =>
|
|
22
|
+
items.map((item, index) => ({
|
|
23
|
+
label: item.label,
|
|
24
|
+
subLabel: item.description,
|
|
25
|
+
icon: item.icon,
|
|
26
|
+
active: index === activeIndex,
|
|
27
|
+
onClick: () => onSelect(item),
|
|
28
|
+
onMouseEnter: () => onHover(index),
|
|
29
|
+
})),
|
|
30
|
+
[items, activeIndex, onSelect, onHover],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<SilkeBox
|
|
35
|
+
data-rich-text-menu
|
|
36
|
+
// The caret stays in the editor while this menu is open, so arrow keys are dispatched
|
|
37
|
+
// at the editor DOM and hosts that bind their own arrow navigation there will steal
|
|
38
|
+
// them. This marks "arrow keys belong to a menu right now" — and unlike the toolbars,
|
|
39
|
+
// which are body-appended BubbleMenus that stay mounted and merely hide, this element
|
|
40
|
+
// only exists while the menu is open, so its presence is the signal.
|
|
41
|
+
data-rich-text-keynav
|
|
42
|
+
column
|
|
43
|
+
className={cl('slashMenu')}
|
|
44
|
+
style={position}
|
|
45
|
+
onMouseDown={(e: React.MouseEvent) => e.preventDefault()}
|
|
46
|
+
>
|
|
47
|
+
<SilkeList items={listItems} />
|
|
48
|
+
</SilkeBox>
|
|
49
|
+
);
|
|
50
|
+
}
|