Skip to main content

Module rich_text_editor

Module rich_text_editor 

Source
Available on crate feature rich-text-editor only.
Expand description

A rich text editor: bold, italic, underline, strikethrough, links, text sizes, bulleted and numbered lists, drawn as they will look.

The app owns a Content and applies every Action the editor sends in update, as with iced’s own text editor. The content keeps the Document, the selection and the undo history, so formatting from a toolbar, a shortcut or the app is undone like typing. Read it back as HTML with Content::html and as the plain-text alternative with Content::plain_text, and load a draft with Content::with_html.

The editor handles the usual keys: arrows, Home, End, Page Up and Page Down (with Shift to select and Ctrl, or Alt on macOS, to move by word), select all, copy, cut and paste. Undo, redo, word deletion, Escape and the formatting chords are Shortcuts from the same set inputs and textareas take, bound by default_keymap; pass a Shortcuts set to RichTextEditor::shortcuts to switch some or all of them off, and their keys reach the app instead.

Copying puts plain text on the clipboard. Pasting that same text back into the editor keeps its formatting.

Bold uses theme::semibold and italic the italic face of theme::font, so the app must load an italic face of its font for italic text to slant.

use iced::Element;
use iced_cube::forms::rich_text_editor::{self, Action, Content, rich_text_editor};

#[derive(Debug, Clone)]
enum Message {
    Edit(Action),
}

struct Compose {
    body: Content,
}

impl Compose {
    fn update(&mut self, message: Message) {
        let Message::Edit(action) = message;
        self.body.perform(action);
    }

    fn view(&self) -> Element<'_, Message> {
        rich_text_editor(&self.body)
            .placeholder("Write your message")
            .on_action(Message::Edit)
            .into()
    }

    fn send(&self) -> (String, String) {
        (self.body.html(), self.body.plain_text())
    }
}

Re-exports§

pub use document::Block;
pub use document::BlockKind;
pub use document::Document;
pub use document::Mark;
pub use document::Position;
pub use document::Run;
pub use document::Selection;
pub use document::Size;
pub use document::Style;

Modules§

document
The document model: blocks of styled runs, and the edits made on them.
html
A document to and from a small, safe subset of HTML.
plain
The plain-text alternative of a document, for a mail’s text part.

Structs§

Appearance
How the editor looks in a status.
Content
A rich text document with its selection and undo history.
Formatting
The formatting at the caret or across the selection, for a toolbar.
RichTextEditor
A rich text editor builder. Convert it into an Element to render.
Shortcutsinput
The set of Shortcuts a text field handles. Every shortcut is on by default. A shortcut that is off is ignored by the field, so its keys reach the app instead.

Enums§

Action
What an editor asks of its Content.
Edit
A change to the text.
Format
A change to the formatting of the selection, or of what is typed next when nothing is selected.
Motion
Where a motion moves the caret.
Shortcutinput
A shortcut that inputs, textareas and the search fields inside other components add to iced’s own editing keys. Each can be switched off in a Shortcuts set.

Constants§

MIN_HEIGHT
Minimum height while the height is Length::Shrink, roughly four lines.

Functions§

default_keymap
The default shortcuts: undo, redo, word deletion and Escape as in input::default_keymap, plus the formatting chords. Mod is Cmd on macOS and Ctrl elsewhere.
rich_text_editor
Creates a rich text editor for content.
shortcut_action
The Action a shortcut sends, or None for Escape, which the editor handles itself.
style
The editor’s look, matching the input’s for the field itself.