useTextareaRich
Owns the runtime contract of the rich-text textarea (OrigamTextareaRich). Mounts a contenteditable host, applies formatting commands via document.execCommand, tracks active formatting state at the caret for toolbar UI feedback, and sanitises every emit so the consumer v-model never carries XSS payloads. Registers Cmd/Ctrl keyboard shortcuts for the common formatting commands.
API
Signature
Signature TypeScript complète extraite du fichier du composable.
useTextareaRich uses document.execCommand, which is marked deprecated by spec but remains the only first-party contenteditable API without a 100 KB library. Limitations: no IME-safe undo stack, no granular collaborative cursor. Every value emitted through onUpdate is sanitised by sanitizeHtml to strip <script> tags and javascript: hrefs.
Interfaces consommées
API
Paramètres
Paramètres d'entrée acceptés par ce composable.
- optionsrequisIUseTextareaRichOptions
- Configuration object. value: () => string — getter for the initial HTML content (bound to the parent v-model). onUpdate: (html: string) => void — called with sanitised HTML on every change. onFormat: (command, value?) => void — called after each format command (lets the host open a link popover on Cmd+K). disabled: () => boolean — suppresses all commands and keyboard shortcuts when true.
API
Valeurs de retour
Propriétés et méthodes exposées par l'objet retourné par le composable.
- hostRef
- Template ref to assign to the contenteditable element. The composable uses it to read and write innerHTML, attach the selectionchange listener, and focus the element before applying a format command.
- active
- Reactive object tracking the formatting state at the current caret position. Properties: bold, italic, underline, code, link (booleans), listBullet, listOrdered (booleans), heading (0 | 1 | 2 | 3). Bind to toolbar button :active props.
- format
- Apply a formatting command. Delegates to document.execCommand for bold, italic, underline, lists, and headings. Handles link creation (value = URL) and inline code toggling (insertHTML). Calls onFormat and onUpdate after every change.
- isFormatActive
- Returns whether the given command is active at the current caret position. Reads from the reactive active object — a convenience wrapper for toolbar binding.
- setValue
- Imperatively update the contenteditable innerHTML with sanitised HTML. No-ops when hostRef is unset or when the content is already identical (avoids cursor jump).
- onInput / onKeydown / onPaste
- Event handlers to bind on the host element. onInput emits sanitised HTML. onKeydown intercepts Cmd/Ctrl+B/I/U/E/K and Cmd+Shift+7/8 for list shortcuts. onPaste strips HTML to sanitised form (or escapes plain text) before inserting.
Usage
Exemples
Extraits de code prêts à coller.
Minimal rich textarea with toolbar