mod content;
pub mod document;
pub mod html;
mod layout;
pub mod plain;
mod snapshot;
mod widget;
use std::fmt;
use iced::widget::Id;
use iced::widget::text_input;
use iced::{Element, Length, Padding};
pub use crate::editing::{Shortcut, Shortcuts};
pub use content::{Action, Content, Edit, Format, Formatting, Motion};
pub use document::{
Block, BlockKind, Document, Mark, Position, Run, Selection, Size, Style, normalise_link,
};
use crate::keys::Keymap;
use crate::primitives::input;
use crate::theme::{Tokens, space, text_size};
pub const MIN_HEIGHT: f32 = 96.0;
pub fn default_keymap() -> Keymap<Shortcut> {
crate::editing::formatting_keymap()
}
pub fn shortcut_action(shortcut: Shortcut) -> Option<Action> {
Some(match shortcut {
Shortcut::Undo => Action::Undo,
Shortcut::Redo => Action::Redo,
Shortcut::DeleteWordBackward => Action::Edit(Edit::DeleteWordBackward),
Shortcut::DeleteWordForward => Action::Edit(Edit::DeleteWordForward),
Shortcut::EscapeUnfocus => return None,
Shortcut::Bold => Action::Format(Format::Toggle(Mark::Bold)),
Shortcut::Italic => Action::Format(Format::Toggle(Mark::Italic)),
Shortcut::Underline => Action::Format(Format::Toggle(Mark::Underline)),
Shortcut::Strikethrough => Action::Format(Format::Toggle(Mark::Strikethrough)),
Shortcut::BulletedList => Action::Format(Format::Block(BlockKind::Bullet)),
Shortcut::NumberedList => Action::Format(Format::Block(BlockKind::Numbered)),
Shortcut::Link => Action::RequestLink,
Shortcut::ClearFormatting => Action::Format(Format::Clear),
})
}
pub struct RichTextEditor<'a, Message> {
content: &'a Content,
placeholder: Option<String>,
id: Option<Id>,
on_action: Option<Box<dyn Fn(Action) -> Message + 'a>>,
on_link: Option<Box<dyn Fn(String) -> Message + 'a>>,
invalid: bool,
width: Length,
height: Length,
min_height: f32,
max_height: f32,
text_size: f32,
padding: Padding,
shortcuts: Shortcuts,
keymap: Keymap<Shortcut>,
}
pub fn rich_text_editor<'a, Message>(content: &'a Content) -> RichTextEditor<'a, Message> {
RichTextEditor {
content,
placeholder: None,
id: None,
on_action: None,
on_link: None,
invalid: false,
width: Length::Fill,
height: Length::Shrink,
min_height: MIN_HEIGHT,
max_height: f32::INFINITY,
text_size: text_size::SM,
padding: Padding::from([space::SM, space::MD]),
shortcuts: Shortcuts::all(),
keymap: default_keymap(),
}
}
impl<'a, Message> RichTextEditor<'a, Message> {
pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = Some(placeholder.into());
self
}
pub fn id(mut self, id: impl Into<Id>) -> Self {
self.id = Some(id.into());
self
}
pub fn on_action(mut self, on_action: impl Fn(Action) -> Message + 'a) -> Self {
self.on_action = Some(Box::new(on_action));
self
}
pub fn on_action_maybe(mut self, on_action: Option<impl Fn(Action) -> Message + 'a>) -> Self {
self.on_action = on_action.map(|f| Box::new(f) as Box<dyn Fn(Action) -> Message + 'a>);
self
}
pub fn on_link(mut self, on_link: impl Fn(String) -> Message + 'a) -> Self {
self.on_link = Some(Box::new(on_link));
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
pub fn min_height(mut self, min_height: f32) -> Self {
self.min_height = min_height.max(0.0);
self
}
pub fn max_height(mut self, max_height: f32) -> Self {
self.max_height = max_height.max(0.0);
self
}
pub fn text_size(mut self, size: f32) -> Self {
self.text_size = size.max(1.0);
self
}
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
self.padding = padding.into();
self
}
pub fn shortcuts(mut self, shortcuts: Shortcuts) -> Self {
self.shortcuts = shortcuts;
self
}
pub fn keymap(mut self, keymap: Keymap<Shortcut>) -> Self {
self.keymap = keymap;
self
}
pub fn is_enabled(&self) -> bool {
self.on_action.is_some()
}
}
impl<Message> fmt::Debug for RichTextEditor<'_, Message> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RichTextEditor")
.field("placeholder", &self.placeholder)
.field("id", &self.id)
.field("enabled", &self.is_enabled())
.field("invalid", &self.invalid)
.field("width", &self.width)
.field("height", &self.height)
.field("min_height", &self.min_height)
.field("max_height", &self.max_height)
.field("text_size", &self.text_size)
.field("shortcuts", &self.shortcuts)
.finish_non_exhaustive()
}
}
impl<'a, Message: Clone + 'a> From<RichTextEditor<'a, Message>> for Element<'a, Message> {
fn from(editor: RichTextEditor<'a, Message>) -> Self {
Element::new(widget::Editor::new(editor))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Appearance {
pub field: text_input::Style,
pub link: iced::Color,
pub quote: iced::Color,
pub quote_text: iced::Color,
pub caret: iced::Color,
pub scrollbar: iced::Color,
}
pub fn style(tokens: &Tokens, status: text_input::Status, invalid: bool) -> Appearance {
let field = input::style(tokens, status, invalid);
let disabled = status == text_input::Status::Disabled;
let fade = |colour| {
if disabled {
crate::theme::fade(colour, 0.5)
} else {
colour
}
};
Appearance {
field,
link: fade(tokens.primary),
quote: tokens.border,
quote_text: fade(tokens.muted_foreground),
caret: tokens.foreground,
scrollbar: crate::theme::fade(tokens.foreground, 0.25),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys;
use crate::theme::{dark, light};
const STATES: [text_input::Status; 4] = [
text_input::Status::Active,
text_input::Status::Hovered,
text_input::Status::Focused { is_hovered: false },
text_input::Status::Disabled,
];
#[test]
fn default_builder_fills_shrinks_and_is_disabled() {
let content = Content::new();
let editor: RichTextEditor<'_, ()> = rich_text_editor(&content);
assert_eq!(editor.width, Length::Fill);
assert_eq!(editor.height, Length::Shrink);
assert_eq!(editor.min_height, MIN_HEIGHT);
assert_eq!(editor.text_size, text_size::SM);
assert_eq!(editor.shortcuts, Shortcuts::all());
assert_eq!(editor.keymap, default_keymap());
assert!(!editor.is_enabled());
let enabled = rich_text_editor::<Action>(&content).on_action(|action| action);
assert!(enabled.is_enabled());
let none: Option<fn(Action) -> Action> = None;
assert!(!enabled.on_action_maybe(none).is_enabled());
}
#[test]
fn builder_options_clamp() {
let content = Content::new();
let editor: RichTextEditor<'_, ()> = rich_text_editor(&content)
.min_height(-1.0)
.max_height(-1.0)
.text_size(0.0)
.shortcuts(Shortcuts::none());
assert_eq!(editor.min_height, 0.0);
assert_eq!(editor.max_height, 0.0);
assert_eq!(editor.text_size, 1.0);
assert_eq!(editor.shortcuts, Shortcuts::none());
}
#[test]
fn every_shortcut_maps_to_an_action_except_escape() {
for shortcut in <Shortcut as keys::Action>::ALL {
assert_eq!(
shortcut_action(*shortcut).is_none(),
*shortcut == Shortcut::EscapeUnfocus,
"{shortcut:?}"
);
}
assert_eq!(
shortcut_action(Shortcut::Bold),
Some(Action::Format(Format::Toggle(Mark::Bold)))
);
assert_eq!(shortcut_action(Shortcut::Link), Some(Action::RequestLink));
}
#[test]
fn the_keymap_can_be_overridden_and_unbound() {
use crate::keys::Chord;
let keymap = default_keymap()
.unbind_action(&Shortcut::Link)
.bind(Chord::character('l').command(), Shortcut::Link);
let chord = Chord::character('k').command();
assert_eq!(keymap.resolve(chord.key(), chord.modifiers()), None);
let chord = Chord::character('l').command();
assert_eq!(
keymap.resolve(chord.key(), chord.modifiers()),
Some(Shortcut::Link)
);
}
#[test]
fn style_matches_the_input_and_fades_when_disabled() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for status in STATES {
for invalid in [false, true] {
let style = style(&tokens, status, invalid);
assert_eq!(style.field, input::style(&tokens, status, invalid));
}
}
let active = style(&tokens, text_input::Status::Active, false);
let disabled = style(&tokens, text_input::Status::Disabled, false);
assert_ne!(active.link, disabled.link);
assert_eq!(active.link, tokens.primary);
}
}
}