Upstream: crates/rune/README.md § Rich text editing at
65f8eb43f710, MIT © Anomaly. Bundled by build-rune-apps; links point at this snapshot or at GitHub at that rev.
Rich text editing
TextEdit holds an attributed rich-text model alongside plain edit.text. Formatting keys,
soft breaks and rich paste are opt-in with TextEdit::new(text).rich() (from_html and
from_parts turn it on); without it a field keeps ⌘B/⌘I/⌘U for the app, inserts a plain
newline on Shift+Return and pastes plain text. The mark and block methods work either way.
Inline marks (
Mark::Bold,Italic,Underline,Strike,Code,Link(url)): stored as sorted byte ranges (MarkRange { start, end, mark }) onedit.text. Insertions and deletions shift and split ranges automatically. Typing inherits the marks of the character before the caret (following AppKit's rules: never extending backwards from the start of a link or code mark's run).edit.toggle_mark(mark),set_mark,remove_markandset_link(Some(url))apply or remove marks on a selection, or arm a pending typing style when the selection is empty (cleared when the caret moves). In a rich field⌘B,⌘Iand⌘U(Ctrlon Windows/Linux) toggle bold, italic and underline. Queryedit.has_mark(&Mark::Bold),edit.active_marks()andedit.active_link()to drive a formatting bar's pressed state.Paragraph block attributes (
Block::Paragraph,Bullet(level),Numbered(level),Quote,Code): oneBlockper\n-delimited paragraph (edit.blocks(),edit.paragraphs(),edit.active_block(),edit.set_block(block),edit.toggle_block(block)). Bullet and numbered list markers (0..=3nesting levels), quote bars and code-block backgrounds are drawn as decorations (never inserted intoedit.text), and Parley indents each paragraph's lines byBlock::indent_pt. By default (overridable via.on_input_key):Returnin a non-empty list item starts a new item at the same level;Returnin an empty list item or quote, or on a blank last line of a code block, resets the paragraph toBlock::Paragraph, leaving the list, quote or code block.DownorRightat the very end of a field that ends in a code block or quote adds an empty paragraph after it, so the caret can always get out.- In a rich field,
Shift+Returninserts a soft line break (edit.soft_breaks()) that stays inside the current list item or quote without drawing a second marker. Backspaceat the start of a list item outdents it (level > 0) or converts it toBlock::Paragraph(level == 0).TabandShift+Tabinside a list item indent or outdent its nesting level instead of moving focus.- Inside
Block::Code,Returnelsewhere inserts a newline (never submits), inline marks do not apply, and a typed or pasted\tinserts a literal tab byte that advances to the next 4-column stop (one byte, so offsets and copy/paste keep the tab). - Code blocks and
Mark::Coderuns draw in the mono slot (name a monospaced family inAppSpec::families; the default is Inter) without ligatures.
Inline pills (
Mark::Pill(id), for@file,@agentor attachment mentions): an atomic run whose label is the text under the mark and whose id is the app's. The caret steps over a pill in one move, a selection that reaches into one grows to cover it,Backspace/Deleteremove it whole, typing and IME composition never land inside it, and undo restores it with its id. Pills are only made by the app:edit.insert_pill(id, label)at the caret, oredit.replace_with_pill(range, id, label)to swap a typed@queryfor one (one undo step; wrap multi-part edits likereplace_with_pill+insert(" ")inedit.undo_group(|e| ..)).edit.pills()lists(range, id)for each. The field draws each pill as a rounded chip (.pill_style(|id| PillStyle::default().bg(..).fg(..).icon(..)), or.pill_colors(bg, fg)and.pill_icon(|id| ...)), and a chip never splits across lines. Copy writes the label as plain text and<span data-pill="id">label</span>as HTML; pasting that into a rich field of the same app restores the pill, while HTML from elsewhere never makes one (anon_input_pastehook can callinsert_pillitself).// `@que` typed before the caret becomes a file pill. let (caret, _) = edit.range(); if let Some(at) = edit.text[..caret].rfind('@') { edit.replace_with_pill(at..caret, "file:src/main.rs", "main.rs"); }Per-frame decorations (
.spans(spans)/.decorations(spans)ontext_input,TextInputandTextArea): paint caller-suppliedrune::Spans (such asSyntaxTheme::from_kit(&k).spans(&ranges, offset)fromrune_kit::syntax::highlight) over the editor without mutatingTextEdit. Byte offsets are relative toedit.textand shift automatically across any active IME preedit, so syntax and mention highlights hold through IME composition and undo/redo.HTML paste and export, and data out:
TextEdit::from_html(html)andedit.paste_html(html, plain)parse a safe HTML subset (b/strong,i/em,u,s/del/strike,a[href],code,pre,ul/ol/li,blockquote,p/div/br, andstyleattributes forfont-weight,font-style,text-decoration;<script>/<style>are dropped and links keep onlyhttp,https,mailto,teland scheme-less URLs). A plain field, a code block, or HTML with no formatting the model keeps (a code editor's coloured copy) pastes the plain text.edit.to_html()andedit.selected_html()serialize back to HTML alongside plainedit.text. A focused rich field uses the rich clipboard on its own:⌘C/⌘Xwriteselected_html()beside the text, and paste goes throughpaste_htmlunless anon_input_pastehook takes it. Construct or inspect the model directly withTextEdit::from_parts(text, marks, blocks),edit.marks(),edit.blocks()andedit.paragraphs(), adding soft breaks with.with_soft_breaks(edit.soft_breaks().to_vec())(orset_soft_breaks) and dropping undo/redo stacks withedit.clear_history(). Accessibility roles (textbox) and plain-text values are unchanged.Undo history past a restart:
edit.history_json(max_steps, max_bytes)gives the newest undo and redo steps (each the document before and after it: text, selection, marks, blocks) as JSON for the app to store, andedit.restore_history_json(&v)takes them back when they lead to the field's document; a history stored a few keystrokes early gets one step from where it ends to the document. A secure field gives and takes none.edit.history_len()(undo, redo) changes when the history moves, so an app can store it only then.Allowed formatting:
.allow_marks([Mark::Bold, Mark::Italic, ..])and.allow_blocks([Block::Bullet(0), ..])keep a field to what its app can store (Markdown has no underline, say). The field never holds the rest: its shortcut (⌘U) goes on to the app's key handlers,toggle_mark,set_mark,toggle_blockandset_blockskip it, paste drops it (a pasted disallowed block becomes a paragraph), and marks or blocks the field already had are removed. Kinds match whatever they carry (anyMark::Linkallows links, anyBlock::Bulletlevel allows bullets);Block::Paragraphis always allowed.edit.allows_mark(&mark)andallows_block(block)tell a formatting bar which buttons to show.