# KptRichText (kpt-rich-text) A lightweight rich text editor (contenteditable) with a configurable, sectioned toolbar. The value is HTML. No external dependencies. Signal Forms. The color popover sits on CDK Overlay - requires `import '@angular/cdk/overlay-prebuilt.css';` Import: `import { KptRichText, KptRichTool, KptRichToolbar } from '@konce-pt/angular';` ## Inputs - `value`: model (HTML) - `toolbar`: KptRichToolbar = KptRichTool[] | KptRichTool[][] (a flat list, or groups = sections). Leaving a tool or a group out disables it; groups are separated by a divider. The full toolbar in 5 sections by default. - `placeholder`: string; `disabled`/`invalid`/`touched`: boolean; `touch`: output - `maxListLevel`: number = 6 - the maximum list nesting depth. The limit applies to interaction (Tab and the `indent`/`outdent` tools); pasted content is not flattened, so that no data is lost. - `mentions`: `readonly KptMentionItem[]` - suggestions for `@`; an empty list turns the mechanism off, so `@` stays an ordinary character. `mentionLimit`: number = 8; `(mentioned)` fires with the inserted entry - the moment to notify a person or attach a link. ## Mentions (`@`) With `mentions` set, typing `@` opens a suggestion list anchored at the caret: arrows move through it, Enter or Tab inserts, Escape closes. The query is read from the **text node** at the caret, not from the HTML, and recognised by `mentionQuery()` from `@konce-pt/mention` - the same rules as in a plain text field. An inserted mention is `@Name` plus a non-breaking space. The id survives a rename and the round trip through the value; `contenteditable="false"` keeps the mention in one piece, and the space is what gives the caret a place to stand after a block it cannot enter. Both attributes are on the sanitizer's allowlist - they are inert, so they open no new door. ## Tools (KptRichTool) - Clipboard: `copy`, `paste` (with formatting, sanitized), `pasteText` (without formatting) - Style: `heading` (a select: Normal/paragraph + Heading 1-6) - Format: `bold`, `italic`, `underline`, `strike`, `ul`, `ol` - List indentation: `indent` (add an indent), `outdent` (remove one) - they work only inside an `li`, nesting per the specification (`li > ul/ol > li`, a sublist inherits the parent list's type). They are only active where they would do something (outside a list and at the limit the button is dimmed). Level markers cycle every 3 (as in word processors): `ol` 1. -> a. -> i., `ul` - -> o -> -. The `list-style-type` rules apply in the editor view (`.kpt-rich-text__editor`) down to level 6. The saved HTML carries no classes - when rendering `value` outside the editor, wrap it in `.kpt-rich-content` from `@konce-pt/styles` so the lists look the same. - Colors: `foreColor` (text color), `backColor` (background color) - a popover with swatches + an `input type=color` - Alignment: `alignLeft`, `alignCenter`, `alignRight`, `alignJustify` - `table` (insert a table: a popover with an NxM grid). Inside a cell, right-click opens a context menu (insert/remove a row and a column, a row/column header - toggling td<->th, cell background color and alignment, table/cell borders - color + thickness (thin/medium/thick) or none, delete the table). Dragging across cells makes a rectangular selection (highlighted); merging/splitting cells (colspan/rowspan) from the menu; cell operations (background, alignment, borders) apply to the whole selection. Dragging a column's right edge changes its width. Deferred: irregular selection, Ctrl+Backspace, bulk formatting from the top toolbar. - `link` (a URL prompt), `clear` (remove formatting), `html` (a toggle for the HTML source view - editing raw HTML) - `dictate` needs `provideKptSpeech()` in the app; without it the tool is not shown. - `dictate` - dictation at the caret (`kpt-speech-button`, Web Speech API); not in the default toolbar. Final text goes in through `insertText` (native undo, `input` -> value); interim text is an overlay next to the caret (`.kpt-speech-ghost`) and never enters `innerHTML`. Hidden without browser support, stopped by the HTML source view and by `disabled`/`readonly`. Language: `speechLang` (BCP 47, default from `KptI18n.locale`), listening colour: `speechColor` (theme colour by default). Example: `[toolbar]="[['bold','italic'],['dictate']]"`. ## Keyboard - The toolbar is a named `role="toolbar"` ("Formatting", `richText.toolbar`) with one Tab stop: `ArrowLeft`/`ArrowRight` (mirrored in RTL) and `Home`/`End` move between tools, skipping disabled ones; the stop remembers the last tool, so Tab from the toolbar goes straight to the editor. - Tool panels open in the overlay container at the end of ``, so focus moves into them: the paragraph style list is a `listbox` (`ArrowUp`/`ArrowDown`, `Home`/`End`, `Enter`/`Space` picks, `Escape`/`Tab` close); the table grid is one stop (arrows change the size 1-8, the size is announced, `Enter` inserts); the colour palette starts on the first swatch (`ArrowLeft`/`ArrowRight` between swatches, `Tab` cycles inside the panel). `Escape` closes any of them and returns focus to the tool. - Tab / Shift+Tab inside a list item = indent / one level up (the same as `indent`/`outdent`). - Tab / Shift+Tab inside a table cell = the next / previous cell (with its content selected). A list inside a cell takes precedence - only once it can no longer be indented does Tab move to another cell. - Everywhere else - including on the first list item, at the top level, at `maxListLevel` and in the last table cell - Tab stays **native** and moves focus out of the editor. That is deliberate: an editor must not be a keyboard trap (WCAG 2.1.2), so `preventDefault()` is called only when the operation actually changed the content. ## Example ## i18n Every label (the tools' aria text, the "Normal/Heading N" styles, the link prompt, the table context menu, the border popover, "Custom...") comes from i18n (the `richText` namespace, including `richText.menu.*`, `richText.border.*`). English by default - see `i18n/llms.txt`. ## Notes Formatting goes through document.execCommand; active tools are highlighted (queryCommandState). Pasting (both native and through the buttons) is sanitized (a tag/style allowlist; it strips script/on*/javascript:). Sanitization covers **every** write into the editor's DOM, not just paste - including a `value` set from outside (`[(value)]`/`formField` from the application, the HTML source mode). Treat an incoming `value` as potentially untrusted (a database, another user, a CMS); on the way out of the component it is sanitized. Links keep only relative addresses and `http(s):`, `mailto:`, `tel:` - the same allowlist as the link tool, so `data:`, `vbscript:` or `blob:` from a paste are dropped. `class` keeps only the editor's own classes (`kpt-rt-*`, `kpt-mention`), a link with `target` always gets `rel="noopener noreferrer"`, and when the sanitizer removed something, the cleaned HTML is written back to `value` - the model never holds the unsafe original (a pure formatting difference does not touch it, so a clean value does not dirty a form). The "paste"/"paste without formatting" clipboard tools use the async Clipboard API (which needs browser permission). An exception to execCommand: list indentation is done with our own DOM code - `execCommand('indent')` produces invalid `ul > ul` in Chrome (or a `blockquote` with a margin, which the sanitizer would strip anyway). ## Screen reader and keyboard `link` opens a small dialog instead of the browser's `prompt()`: a labelled URL field (prefilled with the current link), `Enter` inserts, `Escape` returns to the text with the selection restored. The table context menu is a `role="menu"` of `menuitem`s: it also opens from the keyboard (the menu key or `Shift+F10` in a cell), the arrows and `Home`/`End` move, `Escape`/`Tab` close it, and focus goes back to the cell. Colour swatches are named ("Dark grey", `richText.swatches.*`) instead of their hex codes. The table size grid is still mouse-only. ## Label and description `id` goes to the focusable element - the host never carries it; without one it is generated. A `div` with a role is not named by `label[for]`, so inside `kpt-form-field` it gets `aria-labelledby` pointing at the wrapper's label (unless `ariaLabel` is set), and the hint or error is added to `aria-describedby`. No `controlId` needed. New: `id`, `ariaLabel` and `required` (-> `aria-required`) on the `role="textbox"` area. ## Read-only and required `readonly` keeps the content selectable and copyable but not editable (`aria-readonly`); the toolbar is disabled.