export type RichTextToolbarOption = 'bold' | 'italic' | 'underline' | 'strike' | 'heading1' | 'heading2' | 'heading3' | 'bulletList' | 'orderedList' | 'blockquote' | 'code' | 'codeBlock' | 'link' | 'image' | 'attachment' | 'undo' | 'redo' | 'clear'; /** * Uploads an image and resolves with its http(s) URL */ export type RichTextImageUpload = (file: File) => Promise; /** * Why an image was not inserted: * - `'upload'`: the upload failed or resolved with an unsafe URL */ export type RichTextImageUploadError = 'upload'; /** * Uploads an attached file and resolves with its http(s) URL */ export type RichTextFileUpload = (file: File) => Promise; /** * Why an attached file was not inserted: * - `'type'`: it doesn't match `fileAttachmentAccept`, or it isn't an image and * no `onFileUpload` was given * - `'size'`: it is larger than `maxFileSize` * - `'upload'`: the upload failed or resolved with an unsafe URL */ export type RichTextFileAttachmentError = 'type' | 'size' | 'upload'; /** * Format of the editor `value` and of the content passed to `onChange`, `onFocus` and `onBlur` */ export type RichTextOutputFormat = 'html' | 'text'; export interface RichTextDynamicValue { /** * Text shown in the suggestion list */ label: string; /** * Identifier stored on the token as `data-id` and shown as `{{value}}` */ value: string; } export interface RichTextEditorProps { /** * Content of the editor, in the format set by `outputFormat` */ value?: string; /** * Format of `value` and of the content passed to `onChange`, `onFocus` and `onBlur`. * - `'html'`: the editor HTML, with formatting * - `'text'`: plain text, one line per block. Formatting, links and images are * dropped and dynamic values are written as `{{value}}` * @default 'html' */ outputFormat?: RichTextOutputFormat; /** * Called with the editor content (see `outputFormat`) and its text character count * whenever the content changes */ onChange?: (value: string, charCount: number) => void; /** * Called with the text character count on mount and whenever it changes * (including external `value` updates). Use it to run min/max length * validation in the parent form and pass the result back via `error`/`helperText` */ onCharCountChange?: (charCount: number) => void; /** * Label shown above the editor */ label?: string; /** * Shows a required asterisk next to the label */ required?: boolean; /** * Placeholder shown when the editor is empty */ placeholder?: string; /** * Makes the editor non-editable and hides the toolbar */ readOnly?: boolean; /** * Hides the toolbar while keeping the editor editable */ toolbarHidden?: boolean; /** * Toolbar buttons to show, in order. Defaults to all options */ toolbarOptions?: RichTextToolbarOption[]; /** * Values offered when the user types `dynamicValueTrigger`. Picking one inserts it * as a token: ``. * The feature is disabled (typing the trigger does nothing) when this is empty or omitted */ dynamicValues?: RichTextDynamicValue[]; /** * Called with the dynamic values present in the content (document order, no duplicates) * on mount and whenever they change, including external `value` updates */ onDynamicValuesChange?: (dynamicValues: RichTextDynamicValue[]) => void; /** * Character that opens the dynamic value list * @default '<' */ dynamicValueTrigger?: string; /** * Enables inserting images via the toolbar `image` button, paste and drop. * When false, the `image` toolbar option is hidden and pasted/dropped images are ignored. * * **Pass `onImageUpload` with this** for content that is sent as email or stored: * without it, images are embedded as base64 (see `onImageUpload`) * @default false */ allowImageUpload?: boolean; /** * Uploads images picked, pasted or dropped into the editor and resolves with their URL. * * **Strongly recommended when `allowImageUpload` is true.** When omitted, images * are embedded in the HTML as base64 data URLs, which: * - are blocked or stripped by email clients such as Gmail and Outlook * - make the HTML (and every `onChange` value) as large as the images, often several MB * * The fallback is only meant for demos and small, local-only content */ onImageUpload?: RichTextImageUpload; /** * Called for each image that was not inserted, with the reason. The other * images still upload */ onImageUploadError?: (file: File, reason: RichTextImageUploadError) => void; /** * Enables attaching local files via the toolbar `attachment` button, and via * paste and drop for files that aren't inserted as images. Each file is inserted * as a download chip: * ``. * When false, the `attachment` toolbar option is hidden. * * **Pass `onFileUpload` with this**: without it only image files can be * attached, and they are embedded as base64 (see `onFileUpload`) * @default false */ allowFileAttachment?: boolean; /** * Uploads attached files and resolves with their URL. * * **Required to attach anything other than images.** When omitted: * - non-image files are rejected with reason `'type'` (a base64 link is only * allowed for images, so they can't be inserted safely) * - image files are embedded in the HTML as base64 data URLs, which email * clients such as Gmail and Outlook block or strip, and which make the HTML * (and every `onChange` value) as large as the files */ onFileUpload?: RichTextFileUpload; /** * File types that can be attached, in `accept` attribute syntax * (e.g. `'image/*'`, `'.pdf,.docx'`) * @default 'image/*' */ fileAttachmentAccept?: string; /** * Maximum size of an attached file, in bytes */ maxFileSize?: number; /** * Called for each attached file that was not inserted, with the reason */ onFileAttachmentError?: (file: File, reason: RichTextFileAttachmentError) => void; /** * Applies error styling */ error?: boolean; /** * Text shown below the editor (error message when `error` is true) */ helperText?: string; /** * Minimum height of the editable area */ minHeight?: number | string; /** * Maximum height of the editable area, scrolls beyond this */ maxHeight?: number | string; /** * Minimum number of text characters. Shows an error after blur when the * content is non-empty but shorter than this */ minLength?: number; /** * Maximum number of text characters. Input beyond this is blocked and a * `count/maxLength` counter is shown below the editor */ maxLength?: number; /** * Focuses the editor on mount */ autoFocus?: boolean; onFocus?: (value: string) => void; onBlur?: (value: string) => void; className?: string; }