use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use parking_lot::Mutex;
use super::{
button::{Button, ButtonPart, ButtonSize, ButtonVariant},
field::{FieldControl, InField},
icons,
input::{Input, InputSize},
names,
part_style::{FontFamily, Length, PartStyle},
picture::Picture,
popover::{Popover, PopoverPart},
recipe::{Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
select::{Select, SelectPart},
text_box::{self, BoxStates, Judgment, TextBoxState},
theme::{Themed, current_theme},
toggle::{Toggle, TogglePart},
toolbar::{Toolbar, ToolbarItem, ToolbarPart},
};
use crate::{
app::{
component::Component,
ctx::{Ctx, Placement},
events::{ClipboardImage, EditCommand, KeyboardEvent, MouseEvent, PasteEvent},
theme::{BorderSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
},
components::{
Column, InlineObject, LinkClick, MarkPresence, ParagraphKind, ParagraphMarker, ParagraphStyle, ParagraphStyles,
RichText, Row, SelectionStyle, StyledChange, StyledSpan, StyledText, TextInput, TextInputController, TextMark,
},
core::{ElementRef, Signal},
layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
node::{CursorIcon, Element, TextColor, border::Border},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RichTextEditorPart {
Root,
Toolbar,
Tool,
ToolOn,
ToolIcon,
Style,
StyleChevron,
StyleList,
Content,
Placeholder,
Heading1,
Heading2,
Heading3,
Bullet,
BulletMark,
Ordered,
OrderedMark,
Quote,
QuoteMark,
CodeBlock,
Footer,
FooterText,
LinkPanel,
}
names!(RichTextEditorPart {
Root => "root",
Toolbar => "toolbar",
Tool => "tool",
ToolOn => "tool_on",
ToolIcon => "tool_icon",
Style => "style",
StyleChevron => "style_chevron",
StyleList => "style_list",
Content => "content",
Placeholder => "placeholder",
Heading1 => "heading1",
Heading2 => "heading2",
Heading3 => "heading3",
Bullet => "bullet",
BulletMark => "bullet_mark",
Ordered => "ordered",
OrderedMark => "ordered_mark",
Quote => "quote",
QuoteMark => "quote_mark",
CodeBlock => "code_block",
Footer => "footer",
FooterText => "footer_text",
LinkPanel => "link_panel",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RichTextEditorState {
Focused,
Invalid,
Warning,
Valid,
ReadOnly,
Disabled,
FullWidth,
}
names!(RichTextEditorState {
Focused => "focused",
Invalid => "invalid",
Warning => "warning",
Valid => "valid",
ReadOnly => "read_only",
Disabled => "disabled",
FullWidth => "full_width",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RichTextTool {
Style,
Bold,
Italic,
Underline,
Strikethrough,
Bullet,
Ordered,
Quote,
Code,
Link,
Image,
Undo,
Redo,
}
impl RichTextTool {
pub const DEFAULT: [Self; 12] = [
Self::Style,
Self::Bold,
Self::Italic,
Self::Underline,
Self::Strikethrough,
Self::Bullet,
Self::Ordered,
Self::Quote,
Self::Code,
Self::Link,
Self::Undo,
Self::Redo,
];
fn group(self) -> u8 {
match self {
Self::Style => 0,
Self::Bold | Self::Italic | Self::Underline | Self::Strikethrough => 1,
Self::Bullet | Self::Ordered | Self::Quote | Self::Code => 2,
Self::Link | Self::Image => 3,
Self::Undo | Self::Redo => 4,
}
}
}
pub type RichTextEditorRenderer = Arc<dyn Fn(RichTextEditorView) -> Element + Send + Sync>;
type Run = Arc<dyn Fn() + Send + Sync>;
type ChangeHandler = Arc<dyn Fn(&RichText) + Send + Sync>;
type LinkHandler = Arc<dyn Fn(&str) + Send + Sync>;
type PictureBuilder = Arc<dyn Fn(&InlineObject) -> Element + Send + Sync>;
type ImageKeeper = Arc<dyn Fn(&ClipboardImage) -> Option<String> + Send + Sync>;
type CountText = Arc<dyn Fn(usize) -> String + Send + Sync>;
type HeadingText = Arc<dyn Fn(u8) -> String + Send + Sync>;
#[derive(Clone)]
enum Held {
Bound(Signal<RichText>),
Controlled(RichText),
}
#[derive(Clone)]
struct Words {
label: Option<Arc<str>>,
toolbar: Arc<str>,
style: Arc<str>,
paragraph: Arc<str>,
heading: HeadingText,
code_block: Arc<str>,
bold: Arc<str>,
italic: Arc<str>,
underline: Arc<str>,
strikethrough: Arc<str>,
bullet: Arc<str>,
ordered: Arc<str>,
quote: Arc<str>,
code: Arc<str>,
link: Arc<str>,
image: Arc<str>,
undo: Arc<str>,
redo: Arc<str>,
more: Arc<str>,
link_placeholder: Arc<str>,
apply: Arc<str>,
remove_link: Arc<str>,
shortcuts: Arc<str>,
count: CountText,
}
impl Default for Words {
fn default() -> Self {
let locale = super::current_locale();
let words = &locale.editing;
Self {
label: None,
toolbar: words.formatting.clone(),
style: words.text_style.clone(),
paragraph: words.paragraph.clone(),
heading: {
let heading = words.heading.clone();
Arc::new(move |level| crate::core::fill(&heading, &[("level", &level.to_string())]))
},
code_block: words.code_block.clone(),
bold: words.bold.clone(),
italic: words.italic.clone(),
underline: words.underline.clone(),
strikethrough: words.strikethrough.clone(),
bullet: words.bulleted_list.clone(),
ordered: words.numbered_list.clone(),
quote: words.quote.clone(),
code: words.code.clone(),
link: words.link.clone(),
image: words.image.clone(),
undo: words.undo.clone(),
redo: words.redo.clone(),
more: locale.common.more.clone(),
link_placeholder: words.paste_link.clone(),
apply: locale.common.apply.clone(),
remove_link: words.remove_link.clone(),
shortcuts: words.markdown_shortcuts.clone(),
count: {
let locale = locale.clone();
Arc::new(move |count| locale.count(&locale.editing.words, count))
},
}
}
}
#[derive(Clone)]
pub struct RichTextController {
input: TextInputController,
tick: Signal<u64>,
}
impl Default for RichTextController {
fn default() -> Self {
Self::new()
}
}
impl RichTextController {
pub fn new() -> Self {
Self {
input: TextInputController::new(),
tick: Signal::new(0),
}
}
pub fn input(&self) -> TextInputController {
self.input.clone()
}
fn redraw(&self) {
self.tick.update(|tick| *tick = tick.wrapping_add(1));
}
pub fn toggle_mark(&self, mark: TextMark) -> bool {
let done = self.input.toggle_mark(mark).unwrap_or(false);
self.redraw();
done
}
pub fn set_paragraphs(&self, kind: ParagraphKind) -> bool {
let done = self.input.set_paragraphs(kind).unwrap_or(false);
self.redraw();
done
}
pub fn toggle_paragraphs(&self, kind: ParagraphKind) -> bool {
let all = self
.input
.selected_paragraphs()
.ok()
.flatten()
.is_some_and(|kinds| kinds.iter().all(|held| *held == kind));
self.set_paragraphs(if all { ParagraphKind::Body } else { kind })
}
pub fn set_link(&self, target: Option<&str>) -> bool {
let done = self.input.set_link(target).unwrap_or(false);
self.redraw();
done
}
pub fn select(&self, anchor: usize, caret: usize) -> bool {
let done = self.input.select(anchor, caret).is_ok();
self.redraw();
done
}
pub fn insert_text(&self, text: &str) -> bool {
let done = self.input.replace_selection(text).unwrap_or(false);
self.redraw();
done
}
pub fn insert_picture(&self, source: &str, label: &str) -> bool {
self.insert_object(&InlineObject::new(0, source).label(label))
}
pub fn insert_object(&self, object: &InlineObject) -> bool {
let in_code = self
.input
.caret_paragraph()
.ok()
.flatten()
.is_some_and(|(kind, _)| kind == ParagraphKind::Code);
let done = !in_code && self.input.insert_object(object).unwrap_or(false);
self.redraw();
done
}
pub fn insert_link(&self, text: &str, target: &str) -> bool {
let style = crate::components::SpanStyle::new().link(target);
let linked = StyledText::new(text, vec![StyledSpan::new(0..text.len(), style)]);
let done = self.input.replace_selection_styled(&linked).unwrap_or(false);
self.redraw();
done
}
pub fn undo(&self) -> bool {
let done = self.input.undo().unwrap_or(false);
self.redraw();
done
}
pub fn redo(&self) -> bool {
let done = self.input.redo().unwrap_or(false);
self.redraw();
done
}
pub fn value(&self) -> Option<RichText> {
self.input.rich_text().ok().flatten()
}
}
#[derive(Clone)]
pub struct RichTextEditor {
value: Held,
on_change: Vec<ChangeHandler>,
on_link_click: Vec<LinkHandler>,
on_image: Vec<Run>,
picture: Option<PictureBuilder>,
on_paste_image: Option<ImageKeeper>,
controller: Option<RichTextController>,
placeholder: Option<Arc<str>>,
min_rows: usize,
max_rows: usize,
tools: Vec<RichTextTool>,
toolbar: bool,
footer: bool,
markdown_shortcuts: bool,
boxed: BoxStates,
full_width: bool,
words: Words,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
name: Option<Arc<str>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
fn picture_of(object: &InlineObject) -> Element {
let source = object.source.as_ref();
let mut picture = if source.starts_with("http://") || source.starts_with("https://") {
Picture::from_url(source)
} else {
Picture::from_file(source)
};
if !object.label.is_empty() {
picture = picture.label(&object.label);
}
if let Some(width) = object.width {
picture = picture.width(width as f32);
}
picture.into()
}
pub const RICH_TEXT_MIN_ROWS: usize = 6;
pub const RICH_TEXT_MAX_ROWS: usize = 16;
macro_rules! words {
($($(#[$doc:meta])* $method:ident => $field:ident),* $(,)?) => {
$(
$(#[$doc])*
pub fn $method(mut self, text: &str) -> Self {
self.words.$field = Arc::from(text);
self
}
)*
};
}
impl RichTextEditor {
fn of(value: Held) -> Self {
Self {
value,
on_change: Vec::new(),
on_link_click: Vec::new(),
on_image: Vec::new(),
picture: None,
on_paste_image: None,
controller: None,
placeholder: None,
min_rows: RICH_TEXT_MIN_ROWS,
max_rows: RICH_TEXT_MAX_ROWS,
tools: RichTextTool::DEFAULT.to_vec(),
toolbar: true,
footer: true,
markdown_shortcuts: true,
boxed: BoxStates::default(),
full_width: false,
words: Words::default(),
states: Vec::new(),
rules: Recipe::new(),
forced: None,
name: None,
id: None,
key: None,
}
}
pub fn new(value: Signal<RichText>) -> Self {
Self::of(Held::Bound(value))
}
pub fn controlled(value: RichText, on_change: impl Fn(&RichText) + Send + Sync + 'static) -> Self {
Self::of(Held::Controlled(value)).on_change(on_change)
}
pub fn on_change(mut self, handler: impl Fn(&RichText) + Send + Sync + 'static) -> Self {
self.on_change.push(Arc::new(handler));
self
}
pub fn on_link_click(mut self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
self.on_link_click.push(Arc::new(handler));
self
}
pub fn on_image(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
if self.on_image.is_empty() && !self.tools.contains(&RichTextTool::Image) {
let after = self
.tools
.iter()
.rposition(|tool| tool.group() <= RichTextTool::Image.group())
.map_or(0, |at| at + 1);
self.tools.insert(after, RichTextTool::Image);
}
self.on_image.push(Arc::new(handler));
self
}
pub fn picture(mut self, element: impl Fn(&InlineObject) -> Element + Send + Sync + 'static) -> Self {
self.picture = Some(Arc::new(element));
self
}
pub fn on_paste_image(mut self, keep: impl Fn(&ClipboardImage) -> Option<String> + Send + Sync + 'static) -> Self {
self.on_paste_image = Some(Arc::new(keep));
self
}
pub fn controller(mut self, controller: &RichTextController) -> Self {
self.controller = Some(controller.clone());
self
}
pub fn placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = Some(Arc::from(placeholder));
self
}
pub fn label(mut self, label: &str) -> Self {
self.words.label = Some(Arc::from(label));
self.boxed.label = Some(Arc::from(label));
self
}
pub fn rows(mut self, min: usize, max: usize) -> Self {
self.min_rows = min.max(1);
self.max_rows = max.max(self.min_rows);
self
}
pub fn tools(mut self, tools: impl IntoIterator<Item = RichTextTool>) -> Self {
self.tools = tools.into_iter().collect();
self
}
pub fn toolbar(mut self, toolbar: bool) -> Self {
self.toolbar = toolbar;
self
}
pub fn footer(mut self, footer: bool) -> Self {
self.footer = footer;
self
}
pub fn markdown_shortcuts(mut self, shortcuts: bool) -> Self {
self.markdown_shortcuts = shortcuts;
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.boxed.invalid = invalid;
self
}
pub fn warning(mut self, warning: bool) -> Self {
self.boxed.warning = warning;
self
}
pub fn valid(mut self, valid: bool) -> Self {
self.boxed.valid = valid;
self
}
pub fn read_only(mut self, read_only: bool) -> Self {
self.boxed.read_only = read_only;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.boxed.disabled = disabled;
self
}
pub fn full_width(mut self, full_width: bool) -> Self {
self.full_width = full_width;
self
}
words! {
toolbar_label => toolbar,
style_label => style,
paragraph_text => paragraph,
code_block_text => code_block,
bold_label => bold,
italic_label => italic,
underline_label => underline,
strikethrough_label => strikethrough,
bullet_label => bullet,
ordered_label => ordered,
quote_label => quote,
code_label => code,
link_label => link,
image_label => image,
undo_label => undo,
redo_label => redo,
more_label => more,
link_placeholder => link_placeholder,
apply_label => apply,
remove_link_label => remove_link,
shortcuts_text => shortcuts,
}
pub fn heading_text(mut self, text: impl Fn(u8) -> String + Send + Sync + 'static) -> Self {
self.words.heading = Arc::new(text);
self
}
pub fn count_text(mut self, text: impl Fn(usize) -> String + Send + Sync + 'static) -> Self {
self.words.count = Arc::new(text);
self
}
#[cfg(feature = "form")]
pub fn name(mut self, name: impl Into<Arc<str>>) -> Self {
self.name = Some(name.into());
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let key = self.key.clone();
let mut element = ctx.mount::<RichTextEditorHost>(RichTextEditorProps { editor: self });
if let Some(key) = key {
element = element.key(key);
}
element
}
pub fn build(self, theme: &Theme) -> Element {
let document = match &self.value {
Held::Bound(signal) => signal.get(),
Held::Controlled(value) => value.clone(),
};
let document = RichText::new(document.text, document.spans, document.paragraphs).with_objects(document.objects);
let kept = Kept {
text: Signal::new(document.text),
spans: Signal::new(document.spans),
kinds: Signal::new(document.paragraphs),
objects: Signal::new(document.objects),
controller: self.controller.clone().unwrap_or_default(),
last: Arc::default(),
link_open: Signal::new(false),
link_target: Signal::new(String::new()),
link_anchor: ElementRef::new(),
link_field: ElementRef::new(),
refocus: Arc::default(),
};
self.view(theme, kept, None)
}
fn view(self, theme: &Theme, kept: Kept, ctx: Option<&mut Ctx>) -> Element {
let focused = kept.controller.input.element_ref().focus_signal().get();
let case = self
.boxed
.case(self.states.clone())
.state(RichTextEditorState::Focused, focused)
.state(RichTextEditorState::FullWidth, self.full_width);
let recipe = theme
.resolve::<RichTextEditor>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let input = &kept.controller.input;
let editable = !self.boxed.read_only && !self.boxed.disabled;
let format = RichTextFormat {
marks: input.selection_style().ok().flatten().unwrap_or_default(),
paragraphs: input.selected_paragraphs().ok().flatten().unwrap_or_default(),
can_undo: editable && input.can_undo(),
can_redo: editable && input.can_redo(),
};
let words = kept.objects.with_untracked(|objects| {
kept.text.with_untracked(|text| {
if objects.is_empty() {
text.split_whitespace().count()
} else {
RichText::plain(text.clone()).with_objects(objects.clone()).word_count()
}
})
});
let mut view = RichTextEditorView {
placeholder: self.placeholder,
min_rows: self.min_rows,
max_rows: self.max_rows,
tools: self.tools,
has_toolbar: self.toolbar,
has_footer: self.footer,
markdown_shortcuts: self.markdown_shortcuts,
read_only: self.boxed.read_only,
disabled: self.boxed.disabled,
invalid: self.boxed.judgment() == Some(Judgment::Invalid),
focused,
format,
word_count: words,
recipe,
toolbar: None,
held: self.value,
on_change: self.on_change,
on_link_click: self.on_link_click,
on_image: self.on_image,
picture: self.picture,
on_paste_image: self.on_paste_image,
boxed: self.boxed,
words: self.words,
forced: self.forced,
kept,
name: self.name,
id: self.id,
key: self.key,
};
if view.has_toolbar {
let bar = view.bar();
view.toolbar = Some(match ctx {
Some(ctx) => bar.overflow(true).mount(ctx),
None => bar.build(theme),
});
}
match theme.renderer::<RichTextEditor>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<RichTextEditor> for Element {
fn from(editor: RichTextEditor) -> Self {
editor.build(¤t_theme())
}
}
impl FieldControl for RichTextEditor {
fn in_field(mut self, field: &InField) -> Element {
self.boxed.in_field(field);
self.into()
}
fn mount_in_field(mut self, field: &InField, ctx: &mut Ctx) -> Element {
self.boxed.in_field(field);
self.mount(ctx)
}
}
#[derive(Clone)]
struct Kept {
text: Signal<String>,
spans: Signal<Vec<StyledSpan>>,
kinds: Signal<Vec<ParagraphKind>>,
objects: Signal<Vec<InlineObject>>,
controller: RichTextController,
last: Arc<Mutex<Option<RichText>>>,
link_open: Signal<bool>,
link_target: Signal<String>,
link_anchor: ElementRef,
link_field: ElementRef,
refocus: Arc<AtomicBool>,
}
#[derive(Clone)]
struct RichTextEditorProps {
editor: RichTextEditor,
}
impl PartialEq for RichTextEditorProps {
fn eq(&self, _: &Self) -> bool {
false
}
}
impl crate::app::component::DevtoolsInspectable for RichTextEditorProps {}
struct RichTextEditorHost {
kept: Kept,
link_was_open: AtomicBool,
}
impl Component for RichTextEditorHost {
type Props = RichTextEditorProps;
fn create(_: &mut Ctx) -> Self {
Self {
kept: Kept {
text: Signal::new(String::new()),
spans: Signal::new(Vec::new()),
kinds: Signal::new(Vec::new()),
objects: Signal::new(Vec::new()),
controller: RichTextController::new(),
last: Arc::default(),
link_open: Signal::new(false),
link_target: Signal::new(String::new()),
link_anchor: ElementRef::new(),
link_field: ElementRef::new(),
refocus: Arc::default(),
},
link_was_open: AtomicBool::new(false),
}
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let RichTextEditorProps { editor } = ctx.props::<RichTextEditorProps>().clone();
let mut kept = self.kept.clone();
if let Some(controller) = &editor.controller {
kept.controller = controller.clone();
}
kept.controller.tick.get();
let held = match &editor.value {
Held::Bound(signal) => signal.get(),
Held::Controlled(value) => value.clone(),
};
let stale = kept.last.lock().as_ref() != Some(&held);
if stale {
let document = RichText::new(held.text.clone(), held.spans.clone(), held.paragraphs.clone())
.with_objects(held.objects.clone());
if kept.text.with_untracked(|text| *text != document.text) {
kept.text.set(document.text);
}
if kept.objects.with_untracked(|objects| *objects != document.objects) {
kept.objects.set(document.objects);
}
if kept.spans.with_untracked(|spans| *spans != document.spans) {
kept.spans.set(document.spans);
}
if kept.kinds.with_untracked(|kinds| *kinds != document.paragraphs) {
kept.kinds.set(document.paragraphs);
}
*kept.last.lock() = Some(held);
}
let link_open = kept.link_open.get();
if link_open && !self.link_was_open.swap(link_open, Ordering::Relaxed) {
ctx.focus(&kept.link_field);
} else if !link_open {
self.link_was_open.store(false, Ordering::Relaxed);
}
if kept.refocus.swap(false, Ordering::Relaxed) {
kept.controller.input.focus(ctx);
}
editor.view(¤t_theme(), kept, Some(ctx))
}
}
#[derive(Clone, Debug, Default)]
pub struct RichTextFormat {
pub marks: SelectionStyle,
pub paragraphs: Vec<ParagraphKind>,
pub can_undo: bool,
pub can_redo: bool,
}
impl RichTextFormat {
pub fn all(&self, kind: ParagraphKind) -> bool {
!self.paragraphs.is_empty() && self.paragraphs.iter().all(|held| *held == kind)
}
fn style(&self) -> u8 {
match self.paragraphs.first() {
Some(ParagraphKind::Heading(level)) if self.all(ParagraphKind::Heading(*level)) => (*level).min(3),
Some(ParagraphKind::Code) if self.all(ParagraphKind::Code) => CODE_STYLE,
_ => 0,
}
}
}
const CODE_STYLE: u8 = 9;
fn style_kind(style: u8) -> ParagraphKind {
match style {
0 => ParagraphKind::Body,
CODE_STYLE => ParagraphKind::Code,
level => ParagraphKind::Heading(level),
}
}
pub struct RichTextEditorView {
pub placeholder: Option<Arc<str>>,
pub min_rows: usize,
pub max_rows: usize,
pub tools: Vec<RichTextTool>,
pub has_toolbar: bool,
pub has_footer: bool,
pub markdown_shortcuts: bool,
pub read_only: bool,
pub disabled: bool,
pub invalid: bool,
pub focused: bool,
pub format: RichTextFormat,
pub word_count: usize,
pub recipe: ResolvedRecipe,
pub toolbar: Option<Element>,
held: Held,
on_change: Vec<ChangeHandler>,
on_link_click: Vec<LinkHandler>,
on_image: Vec<Run>,
picture: Option<PictureBuilder>,
on_paste_image: Option<ImageKeeper>,
boxed: BoxStates,
words: Words,
forced: Option<Interaction>,
kept: Kept,
#[cfg_attr(not(feature = "form"), allow(dead_code))]
name: Option<Arc<str>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl RichTextEditorView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
fn sub_id(&self, suffix: &str) -> Option<String> {
self.id.as_ref().map(|id| format!("{id}-{suffix}"))
}
fn editable(&self) -> bool {
!self.read_only && !self.disabled
}
fn run(&self, command: impl Fn(&RichTextController) + Send + Sync + 'static) -> Run {
let (controller, refocus, editable) = (self.kept.controller.clone(), self.kept.refocus.clone(), self.editable());
Arc::new(move || {
if !editable {
return;
}
command(&controller);
refocus.store(true, Ordering::Relaxed);
controller.redraw();
})
}
fn open_link(&self) -> Run {
let kept = self.kept.clone();
let editable = self.editable();
Arc::new(move || {
if !editable {
return;
}
let target = kept
.controller
.input
.selection_style()
.ok()
.flatten()
.and_then(|style| style.link)
.map(|target| target.to_string())
.unwrap_or_default();
kept.link_target.set(target);
kept.link_open.set(true);
})
}
fn toggle(&self, label: &str, icon: &'static str, suffix: &str, on: bool, run: Run) -> Toggle {
let lit = self.part(RichTextEditorPart::ToolOn).rest();
let rules = self
.recipe
.rules_for(RichTextEditorPart::ToolIcon, TogglePart::Icon)
.state(super::toggle::ToggleState::On, TogglePart::Root, move |s| {
s.merged(&lit)
});
let mut toggle = Toggle::new(label)
.icon(icon)
.icon_only(true)
.on(on)
.rules(&rules)
.focuses(&self.kept.controller.input.element_ref())
.on_change(move |_| run());
if let Some(id) = self.sub_id(suffix) {
toggle = toggle.id(id);
}
toggle
}
fn button(&self, label: &str, icon: &'static str, suffix: &str, disabled: bool, run: Run) -> Button {
let rules = self.recipe.rules_for(RichTextEditorPart::ToolIcon, ButtonPart::Icon);
let mut button = Button::new(label)
.icon(icon)
.icon_only(true)
.disabled(disabled)
.rules(&rules)
.focuses(&self.kept.controller.input.element_ref())
.on_click(move |_| run());
if let Some(id) = self.sub_id(suffix) {
button = button.id(id);
}
button
}
pub fn style_select(&self) -> Element {
let run = {
let (controller, refocus, editable) = (self.kept.controller.clone(), self.kept.refocus.clone(), self.editable());
move |style: &u8| {
if editable {
controller.set_paragraphs(style_kind(*style));
refocus.store(true, Ordering::Relaxed);
controller.redraw();
}
}
};
let heading = &self.words.heading;
let options = [
(0, self.words.paragraph.to_string()),
(1, heading(1)),
(2, heading(2)),
(3, heading(3)),
(CODE_STYLE, self.words.code_block.to_string()),
];
let rules = self
.recipe
.rules_for(RichTextEditorPart::Style, SelectPart::Trigger)
.extend(
&self
.recipe
.rules_for(RichTextEditorPart::StyleChevron, SelectPart::Chevron),
)
.extend(&self.recipe.rules_for(RichTextEditorPart::StyleList, SelectPart::Panel));
let mut select = Select::controlled(self.format.style(), options, run)
.label(&self.words.style)
.disabled(!self.editable())
.fit_options(true)
.rules(&rules);
if let Some(id) = self.sub_id("style") {
select = select.id(id);
}
select.build(self.theme())
}
fn tool(&self, tool: RichTextTool) -> ToolbarItem {
let all = |presence: MarkPresence| presence == MarkPresence::All;
let marks = &self.format.marks;
let mark = |mark: TextMark| {
self.run(move |editor| {
editor.toggle_mark(mark);
})
};
let block = |kind: ParagraphKind| {
self.run(move |editor| {
editor.toggle_paragraphs(kind);
})
};
let command = if cfg!(target_vendor = "apple") { "Cmd" } else { "Ctrl" };
let keys = |keys: &str| format!("{command}+{keys}");
let edit_keys = |edit: EditCommand| {
let accelerator = edit.accelerator();
let shift = if accelerator.modifiers.shift { "Shift+" } else { "" };
keys(&format!("{shift}{}", accelerator.key.to_uppercase()))
};
let words = &self.words;
match tool {
RichTextTool::Style => ToolbarItem::element(self.style_select()),
RichTextTool::Bold => {
ToolbarItem::from(self.toggle(&words.bold, icons::BOLD, "bold", all(marks.bold), mark(TextMark::Bold)))
.shortcut(&keys("B"))
}
RichTextTool::Italic => ToolbarItem::from(self.toggle(
&words.italic,
icons::ITALIC,
"italic",
all(marks.italic),
mark(TextMark::Italic),
))
.shortcut(&keys("I")),
RichTextTool::Underline => ToolbarItem::from(self.toggle(
&words.underline,
icons::UNDERLINE,
"underline",
all(marks.underline),
mark(TextMark::Underline),
))
.shortcut(&keys("U")),
RichTextTool::Strikethrough => ToolbarItem::from(self.toggle(
&words.strikethrough,
icons::STRIKETHROUGH,
"strikethrough",
all(marks.strikethrough),
mark(TextMark::Strikethrough),
))
.shortcut(&keys("Shift+X")),
RichTextTool::Bullet => ToolbarItem::from(self.toggle(
&words.bullet,
icons::LIST,
"bullet",
self.format.all(ParagraphKind::Bullet),
block(ParagraphKind::Bullet),
))
.shortcut(&keys("Shift+8")),
RichTextTool::Ordered => ToolbarItem::from(self.toggle(
&words.ordered,
icons::LIST_NUMBERED,
"ordered",
self.format.all(ParagraphKind::Ordered),
block(ParagraphKind::Ordered),
))
.shortcut(&keys("Shift+7")),
RichTextTool::Quote => ToolbarItem::from(self.toggle(
&words.quote,
icons::QUOTE,
"quote",
self.format.all(ParagraphKind::Quote),
block(ParagraphKind::Quote),
))
.shortcut(&keys("Shift+9")),
RichTextTool::Code => {
ToolbarItem::from(self.toggle(&words.code, icons::CODE, "code", all(marks.code), mark(TextMark::Code)))
.shortcut(&keys("E"))
}
RichTextTool::Link => {
ToolbarItem::from(self.toggle(&words.link, icons::LINK, "link", marks.has_link, self.open_link()))
.shortcut(&keys("K"))
}
RichTextTool::Image => {
let handlers = self.on_image.clone();
let run = self.run(move |_| {
for handler in &handlers {
handler();
}
});
ToolbarItem::from(self.button(&words.image, icons::IMAGE, "image", false, run))
}
RichTextTool::Undo => ToolbarItem::from(self.button(
&words.undo,
icons::UNDO,
"undo",
!self.format.can_undo,
self.run(|editor| {
editor.undo();
}),
))
.shortcut(&edit_keys(EditCommand::Undo)),
RichTextTool::Redo => ToolbarItem::from(self.button(
&words.redo,
icons::REDO,
"redo",
!self.format.can_redo,
self.run(|editor| {
editor.redo();
}),
))
.shortcut(&edit_keys(EditCommand::Redo)),
}
}
pub fn bar(&self) -> Toolbar {
let rules = self
.recipe
.rules_for(RichTextEditorPart::Toolbar, ToolbarPart::Root)
.extend(&self.recipe.rules_for(RichTextEditorPart::Tool, ToolbarPart::Item))
.extend(&self.recipe.rules_for(RichTextEditorPart::Tool, ToolbarPart::MoreButton));
let mut bar = Toolbar::new()
.label(&self.words.toolbar)
.more_label(&self.words.more)
.size(ButtonSize::Sm)
.disabled(!self.editable())
.rules(&rules);
let history = RichTextTool::Undo.group();
let mut group = None;
for tool in &self.tools {
if tool.group() == history && group != Some(history) {
bar = bar.item(Toolbar::spacer());
} else if group.is_some_and(|before| before != tool.group()) {
bar = bar.item(Toolbar::separator());
}
group = Some(tool.group());
bar = bar.item(self.tool(*tool));
}
if let Some(id) = self.sub_id("toolbar") {
bar = bar.id(id);
}
bar
}
pub fn paragraph_styles(&self) -> ParagraphStyles {
let theme = self.theme();
let content = self.part(RichTextEditorPart::Content).rest();
let px = |value: &Option<crate::node::spacing_value::SpacingValue>| {
value.as_ref().map_or(0.0, |value| value.resolve(&theme.spacing(), 0.0))
};
let length = |length: Option<Length>| match length.and_then(|length| length.resolve(theme)) {
Some(crate::node::dimension::Dimension::Px(px)) => px,
_ => 0.0,
};
let ink =
|part: &PartStyle, fallback: PaletteColor| part.foreground.clone().unwrap_or(TextColor::Palette(fallback));
let kind = |part: RichTextEditorPart| {
let over = self.part(part).rest();
let text = content.clone().merged(&over).text.resolve(theme);
let mut style = ParagraphStyle::new()
.font_size(text.font_size)
.weight(text.weight)
.line_height(text.line_height)
.font_family(text.font_family.clone())
.indent(px(&over.padding_start));
if let Some(color) = &over.foreground {
style = style.color(color.clone());
}
(style, over)
};
let dot = self.part(RichTextEditorPart::BulletMark).rest();
let number = self.part(RichTextEditorPart::OrderedMark).rest();
let bar = self.part(RichTextEditorPart::QuoteMark).rest();
let (code, code_part) = kind(RichTextEditorPart::CodeBlock);
let fill = match &code_part.background {
Some(crate::node::BackgroundColor::Palette(role)) => Some(TextColor::Palette(role.clone())),
Some(crate::node::BackgroundColor::Color(color)) => Some(TextColor::Color(*color)),
_ => None,
};
let code = match fill {
Some(fill) => {
let radius = code_part
.radius
.as_ref()
.map_or(0.0, |radius| radius.resolve(&theme.radii()).top_left);
code.background(fill, radius)
}
None => code,
}
.joined(true);
ParagraphStyles::new()
.gap(px(&content.gap))
.with(ParagraphKind::Heading(1), kind(RichTextEditorPart::Heading1).0)
.with(ParagraphKind::Heading(2), kind(RichTextEditorPart::Heading2).0)
.with(ParagraphKind::Heading(3), kind(RichTextEditorPart::Heading3).0)
.with(ParagraphKind::Heading(4), kind(RichTextEditorPart::Heading3).0)
.with(ParagraphKind::Heading(5), kind(RichTextEditorPart::Heading3).0)
.with(ParagraphKind::Heading(6), kind(RichTextEditorPart::Heading3).0)
.with(
ParagraphKind::Bullet,
kind(RichTextEditorPart::Bullet).0.marker(ParagraphMarker::Dot {
size: length(dot.width),
gap: px(&dot.gap),
color: ink(&dot, PaletteColor::MutedForeground),
}),
)
.with(
ParagraphKind::Ordered,
kind(RichTextEditorPart::Ordered).0.marker(ParagraphMarker::Number {
gap: px(&number.gap),
color: ink(&number, PaletteColor::MutedForeground),
}),
)
.with(
ParagraphKind::Quote,
kind(RichTextEditorPart::Quote).0.marker(ParagraphMarker::Bar {
width: length(bar.width),
color: ink(&bar, PaletteColor::Border),
}),
)
.with(ParagraphKind::Code, code)
}
pub fn content(&self) -> TextInput {
let part = self.part(RichTextEditorPart::Content).forced(self.forced);
let text = text_box::ink(self.theme(), &part.rest());
let placeholder = text_box::ink(
self.theme(),
&part.rest().merged(&self.part(RichTextEditorPart::Placeholder).rest()),
);
let kept = &self.kept;
let start = kept
.controller
.input
.selection()
.map_or(0, |selection| selection.range().start);
let picture = self.picture.clone();
let input = TextInput::styled(kept.text.clone(), text)
.placeholder_style(placeholder)
.spans(kept.spans.clone())
.paragraphs(kept.kinds.clone(), self.paragraph_styles())
.objects(kept.objects.clone(), move |object| match &picture {
Some(picture) => picture(object),
None => picture_of(object),
})
.resizable_objects(self.editable())
.rich_clipboard(true)
.controller(kept.controller.input.clone())
.multiline()
.rows(self.min_rows, self.max_rows)
.part(&part)
.describe("a11y_multiline", "true");
let mut input = text_box::text_box(input, &self.boxed, Vec::new());
if let Some(placeholder) = &self.placeholder {
input = input.placeholder(placeholder);
}
if let Some(id) = self.sub_id("content") {
input = input.id(id);
}
input = input.anchor_at(kept.link_anchor.clone(), start);
let changed = {
let (held, last, handlers) = (self.held.clone(), kept.last.clone(), self.on_change.clone());
let controller = kept.controller.clone();
move |change: &StyledChange| {
let document = change.to_rich_text();
*last.lock() = Some(document.clone());
if let Held::Bound(signal) = &held
&& signal.with_untracked(|held| *held != document)
{
signal.set(document.clone());
}
for handler in &handlers {
handler(&document);
}
controller.redraw();
}
};
let clicked = self.on_link_click.clone();
let (moved, pressed) = (kept.controller.clone(), kept.controller.clone());
let keys = self.keys();
input
.on_styled_change(changed)
.on_link_click(move |link: &LinkClick| {
for handler in &clicked {
handler(&link.target);
}
})
.on_key_down(move |event: KeyboardEvent| keys(&event))
.on_key_up(move |event: KeyboardEvent| {
if event.focus_within {
moved.redraw();
}
})
.on_mouse_up(move |_: MouseEvent| pressed.redraw())
.on_paste(self.paste_pictures())
}
fn paste_pictures(&self) -> impl Fn(PasteEvent) + Send + Sync + 'static {
let (keep, editor, editable) = (
self.on_paste_image.clone(),
self.kept.controller.clone(),
self.editable(),
);
move |event: PasteEvent| {
if !editable || event.default_prevented() || event.text().is_some_and(|text| !text.is_empty()) {
return;
}
let mut pictures: Vec<(String, String)> = Vec::new();
match (event.image(), &keep) {
(Some(image), Some(keep)) => pictures.extend(keep(image).map(|source| (source, String::new()))),
_ => {
for path in event.files() {
let extension = path
.extension()
.map(|extension| extension.to_string_lossy().to_lowercase());
if extension.is_some_and(|extension| {
crate::files::FileKind::of_extension(&extension) == crate::files::FileKind::Image
}) {
let name = path.file_stem().map(|name| name.to_string_lossy().into_owned());
pictures.push((path.to_string_lossy().into_owned(), name.unwrap_or_default()));
}
}
}
}
if pictures.is_empty() {
return;
}
event.prevent_default();
for (source, label) in pictures {
editor.insert_picture(&source, &label);
}
}
}
fn keys(&self) -> impl Fn(&KeyboardEvent) + Send + Sync + 'static {
let kept = self.kept.clone();
let (editable, shortcuts) = (self.editable(), self.markdown_shortcuts);
let open_link = self.open_link();
move |event: &KeyboardEvent| {
if !event.focus_within || event.default_prevented() || event.composing || !editable {
return;
}
let editor = &kept.controller;
let command = if cfg!(target_vendor = "apple") {
event.meta
} else {
event.ctrl
};
let handled = if command {
command_key(editor, event, &open_link)
} else if event.alt || event.meta || event.ctrl {
false
} else {
match event.key.as_str() {
"Enter" if !event.shift => enter_key(&kept, shortcuts),
"Backspace" => backspace_key(editor),
" " if shortcuts => block_shortcut(&kept),
"*" | "_" | "`" | "~" if shortcuts => mark_shortcut(&kept, &event.key),
_ => false,
}
};
if handled {
event.prevent_default();
editor.redraw();
}
}
}
pub fn link_panel(&self) -> Element {
let kept = self.kept.clone();
let close = {
let kept = kept.clone();
move || {
kept.link_open.set(false);
kept.refocus.store(true, Ordering::Relaxed);
kept.controller.redraw();
}
};
let apply: Run = {
let (kept, close) = (kept.clone(), close.clone());
Arc::new(move || {
let target = kept.link_target.get_untracked();
let target = target.trim();
kept
.controller
.set_link(if target.is_empty() { None } else { Some(target) });
close();
})
};
let remove: Run = {
let (kept, close) = (kept.clone(), close.clone());
Arc::new(move || {
kept.controller.set_link(None);
close();
})
};
let mut field = Input::new(kept.link_target.clone())
.size(InputSize::Sm)
.full_width(true)
.placeholder(&self.words.link_placeholder)
.label(&self.words.link_placeholder);
if let Some(id) = self.sub_id("link-field") {
field = field.id(id);
}
let view = field.view(self.theme());
let on_enter = apply.clone();
let control = view
.control()
.ref_element(kept.link_field.clone())
.on_key_down(move |event: KeyboardEvent| {
if event.focus_within && event.key == "Enter" && !event.composing && !(event.ctrl || event.alt || event.meta) {
event.prevent_default();
on_enter();
}
});
let mut row = Row::new()
.align_items(Alignment::Center)
.spacing(6.0)
.child(
Row::new()
.flex(1.0)
.min_width(0.0)
.align_items(Alignment::Stretch)
.child(view.render_with_end(control, None)),
)
.child({
let apply = apply.clone();
Button::new(&self.words.apply)
.size(ButtonSize::Sm)
.rules(&self.recipe.lend("apply"))
.on_click(move |_| apply())
.build(self.theme())
});
if self.format.marks.has_link {
row = row.child(
Button::new(&self.words.remove_link)
.variant(ButtonVariant::Ghost)
.rules(&self.recipe.lend("remove_link"))
.size(ButtonSize::Sm)
.on_click(move |_| remove())
.build(self.theme()),
);
}
let dismissed = close.clone();
Popover::new(kept.link_open.clone(), kept.link_anchor.clone())
.label(&self.words.link)
.placement(Placement::BottomStart)
.rules(&self.recipe.rules_for(RichTextEditorPart::LinkPanel, PopoverPart::Panel))
.on_dismiss(move || dismissed())
.child(row)
.build(self.theme())
}
pub fn footer(&self) -> Row {
let part = self.part(RichTextEditorPart::Footer);
let text = self.part(RichTextEditorPart::FooterText);
let line = |words: &str| {
Row::new().flex_shrink(1.0).min_width(0.0).child(
text
.text(words)
.nowrap()
.text_overflow(crate::node::node_kind::TextOverflow::Elipsis)
.part(&text),
)
};
let mut row = Row::new()
.part(&part)
.align_items(Alignment::Center)
.justify(Justify::SpaceBetween);
row = row.child(if self.markdown_shortcuts {
line(&self.words.shortcuts)
} else {
Row::new()
});
let mut count = Row::new()
.describe("a11y_live", "polite")
.child(text.text(&(self.words.count)(self.word_count)).nowrap().part(&text));
if let Some(id) = self.sub_id("count") {
count = count.id(id);
}
row.child(count)
}
pub fn root(&self) -> Column {
let mut root = Column::new()
.align_items(Alignment::Stretch)
.part(&self.part(RichTextEditorPart::Root))
.describe("a11y_role", "group");
if let Some(label) = self.boxed.label.as_ref().or(self.words.label.as_ref()) {
root = root.describe("a11y_name", label.clone());
}
#[cfg(feature = "form")]
if let Some(name) = &self.name {
let kept = &self.kept;
let document = RichText::new(
kept.text.get_untracked(),
kept.spans.get_untracked(),
kept.kinds.get_untracked(),
);
root = root
.name(name.clone())
.form_value(Some(Arc::from(document.to_markdown())));
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root
}
pub fn render(mut self) -> Element {
let mut root = self.root();
if let Some(toolbar) = self.toolbar.take() {
root = root.child(toolbar);
}
root = root.child(self.content());
if self.has_footer {
root = root.child(self.footer());
}
root.child(self.link_panel()).into()
}
}
fn command_key(editor: &RichTextController, event: &KeyboardEvent, open_link: &Run) -> bool {
let key = event.key.to_ascii_lowercase();
let digit = event
.code
.strip_prefix("Digit")
.and_then(|digit| digit.parse::<u8>().ok());
match (key.as_str(), event.shift, event.alt) {
("b", false, false) => {
editor.toggle_mark(TextMark::Bold);
}
("i", false, false) => {
editor.toggle_mark(TextMark::Italic);
}
("u", false, false) => {
editor.toggle_mark(TextMark::Underline);
}
("e", false, false) => {
editor.toggle_mark(TextMark::Code);
}
("x", true, false) => {
editor.toggle_mark(TextMark::Strikethrough);
}
("k", false, false) => open_link(),
(_, false, true) if digit.is_some_and(|digit| digit <= 3) => {
{
editor.set_paragraphs(style_kind(digit.unwrap_or(0)));
};
}
(_, true, false) if digit == Some(7) => {
editor.toggle_paragraphs(ParagraphKind::Ordered);
}
(_, true, false) if digit == Some(8) => {
editor.toggle_paragraphs(ParagraphKind::Bullet);
}
(_, true, false) if digit == Some(9) => {
editor.toggle_paragraphs(ParagraphKind::Quote);
}
_ => return false,
}
true
}
fn caret_paragraph(input: &TextInputController) -> Option<(ParagraphKind, std::ops::Range<usize>, usize)> {
let selection = input.selection().ok()?;
if !selection.range().is_empty() {
return None;
}
let (kind, range) = input.caret_paragraph().ok()??;
Some((kind, range, selection.caret))
}
fn enter_key(kept: &Kept, shortcuts: bool) -> bool {
let input = &kept.controller.input;
if let Ok(selection) = input.selection() {
let range = selection.range();
let pictured = kept
.objects
.with_untracked(|objects| objects.iter().any(|object| object.range() == range));
if pictured {
let _ = input.select(range.end, range.end);
}
}
let Some((kind, range, caret)) = caret_paragraph(input) else {
return false;
};
match kind {
ParagraphKind::Bullet | ParagraphKind::Ordered | ParagraphKind::Quote if range.is_empty() => {
input.set_paragraphs(ParagraphKind::Body).unwrap_or(false)
}
ParagraphKind::Heading(_) if caret == range.end && !range.is_empty() => {
input.replace_selection("\n").unwrap_or(false) && {
let _ = input.set_paragraphs(ParagraphKind::Body);
true
}
}
ParagraphKind::Body if shortcuts && kept.text.with_untracked(|text| text.get(range.clone()) == Some("```")) => {
input.select(range.start, range.end).is_ok() && input.replace_selection("").unwrap_or(false) && {
let _ = input.set_paragraphs(ParagraphKind::Code);
true
}
}
_ => false,
}
}
fn backspace_key(editor: &RichTextController) -> bool {
match caret_paragraph(&editor.input) {
Some((kind, range, caret)) if kind != ParagraphKind::Body && caret == range.start => {
editor.input.set_paragraphs(ParagraphKind::Body).unwrap_or(false)
}
_ => false,
}
}
fn block_of(prefix: &str) -> Option<ParagraphKind> {
let number = prefix.strip_suffix(['.', ')']);
match prefix {
"#" => Some(ParagraphKind::Heading(1)),
"##" => Some(ParagraphKind::Heading(2)),
"###" => Some(ParagraphKind::Heading(3)),
"-" | "*" | "+" => Some(ParagraphKind::Bullet),
">" => Some(ParagraphKind::Quote),
"```" => Some(ParagraphKind::Code),
_ if number
.is_some_and(|digits| !digits.is_empty() && digits.len() <= 3 && digits.bytes().all(|b| b.is_ascii_digit())) =>
{
Some(ParagraphKind::Ordered)
}
_ => None,
}
}
fn block_shortcut(kept: &Kept) -> bool {
let input = &kept.controller.input;
let Some((ParagraphKind::Body, range, caret)) = caret_paragraph(input) else {
return false;
};
let kind = kept
.text
.with_untracked(|text| text.get(range.start..caret).and_then(block_of));
let Some(kind) = kind else {
return false;
};
input.select(range.start, caret).is_ok() && input.replace_selection("").unwrap_or(false) && {
let _ = input.set_paragraphs(kind);
true
}
}
fn mark_delimiters(key: &str) -> &'static [(&'static str, TextMark)] {
match key {
"*" => &[("**", TextMark::Bold), ("*", TextMark::Italic)],
"_" => &[("__", TextMark::Bold), ("_", TextMark::Italic)],
"`" => &[("`", TextMark::Code)],
"~" => &[("~~", TextMark::Strikethrough)],
_ => &[],
}
}
fn delimited(typed: &str, delimiter: &str) -> Option<usize> {
let inner = typed.strip_suffix(delimiter)?;
let open = inner.rfind(delimiter)?;
let text = &inner[open + delimiter.len()..];
let mark = delimiter.chars().next()?;
let clean = !text.is_empty()
&& !text.contains(mark)
&& !text.starts_with(char::is_whitespace)
&& !text.ends_with(char::is_whitespace);
let alone = !inner[..open].ends_with(mark);
(clean && alone).then_some(open)
}
fn mark_shortcut(kept: &Kept, key: &str) -> bool {
let input = &kept.controller.input;
let Some((kind, range, caret)) = caret_paragraph(input) else {
return false;
};
if kind == ParagraphKind::Code {
return false;
}
let Some(typed) = kept
.text
.with_untracked(|text| text.get(range.start..caret).map(|before| format!("{before}{key}")))
else {
return false;
};
let Some((delimiter, mark, open)) = mark_delimiters(key)
.iter()
.find_map(|(delimiter, mark)| delimited(&typed, delimiter).map(|open| (*delimiter, *mark, open)))
else {
return false;
};
let start = range.start + open;
let inner = start + delimiter.len()..caret + key.len() - delimiter.len();
if input.select(inner.start, inner.end).is_err() {
return false;
}
let Ok(Some(between)) = input.selected_styled_text() else {
return false;
};
let mut spans = Vec::new();
let mut at = 0;
let mut after = crate::components::SpanStyle::default();
for (run, style) in between.runs() {
after = style.clone().with(mark, false);
spans.push(StyledSpan::new(at..at + run.len(), style.with(mark, true)));
at += run.len();
}
let marked = StyledText::new(between.text.clone(), spans);
if input.select(start, caret).is_err() || !input.replace_selection_styled(&marked).unwrap_or(false) {
return false;
}
after.link = None;
let _ = input.set_pending_style(Some(after));
true
}
impl crate::ui::Styled for RichTextEditor {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for RichTextEditor {
const NAME: &'static str = "rich_text_editor";
type Renderer = RichTextEditorRenderer;
fn parts() -> crate::ui::Declared {
RichTextEditorPart::declared()
}
fn states() -> crate::ui::Declared {
RichTextEditorState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{Background, Foreground, Muted, MutedForeground};
use RichTextEditorPart::*;
let edge = |color: PaletteColor| Border::inside(BorderSize::Sm, color);
let recipe = Recipe::new()
.base(Root, |s| {
s.width(560.0)
.max_width(Length::Fill)
.background(Background)
.border(edge(PaletteColor::Border))
.radius(RadiusSize::Lg)
})
.state(RichTextEditorState::FullWidth, Root, |s| s.width(Length::Fill))
.base(Toolbar, |s| {
s.height(40.0)
.padding_x(6.0)
.padding_y(0.0)
.gap(2.0)
.radius(0.0)
.background(PaletteColor::Bar)
.underline(edge(PaletteColor::Border))
})
.base(Tool, |s| s.square(28.0).padding_x(0.0).radius(RadiusSize::Sm))
.base(ToolOn, |s| s.background(Background).border(edge(PaletteColor::Border)))
.base(ToolIcon, |s| s.square(15.0))
.base(Style, |s| {
s.width(Length::Auto)
.height(28.0)
.padding_start(10.0)
.padding_end(6.0)
.gap(6.0)
.radius(RadiusSize::Sm)
.font_size(12.0)
})
.base(StyleChevron, |s| s.square(12.0))
.base(Content, |s| {
s.width(Length::Fill)
.padding_x(18.0)
.padding_y(16.0)
.gap(10.0)
.typography(TypographyStyle::Body)
.line_height(18.0 / 14.0)
.foreground(Foreground)
.cursor(CursorIcon::Text)
})
.base(Placeholder, |s| s.foreground(MutedForeground))
.base(Heading1, |s| {
s.font_size(28.0).weight(FontWeight::Bold).line_height(34.0 / 28.0)
})
.base(Heading2, |s| {
s.font_size(22.0).weight(FontWeight::SemiBold).line_height(26.0 / 22.0)
})
.base(Heading3, |s| {
s.font_size(18.0).weight(FontWeight::SemiBold).line_height(22.0 / 18.0)
})
.base(Bullet, |s| s.padding_start(21.0))
.base(BulletMark, |s| s.width(5.0).gap(10.0).foreground(MutedForeground))
.base(Ordered, |s| s.padding_start(24.0))
.base(OrderedMark, |s| s.gap(6.0).foreground(MutedForeground))
.base(Quote, |s| s.padding_start(14.0).foreground(MutedForeground))
.base(QuoteMark, |s| s.width(3.0).foreground(PaletteColor::Border))
.base(CodeBlock, |s| {
s.padding_start(12.0)
.font_family(FontFamily::Mono)
.font_size(13.0)
.line_height(21.0 / 13.0)
.background(Muted)
.radius(RadiusSize::Sm)
})
.base(Footer, |s| {
s.height(28.0)
.padding_x(12.0)
.gap(12.0)
.top_line(edge(PaletteColor::Border))
})
.base(FooterText, |s| {
s.font_family(FontFamily::Mono)
.font_size(11.0)
.foreground(MutedForeground)
})
.base(LinkPanel, |s| {
s.width(340.0).max_width(Length::Fill).padding_x(8.0).padding_y(8.0)
})
.state(TextBoxState::ReadOnly, Content, |s| s.background(Muted))
.state(TextBoxState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
.state(TextBoxState::Disabled, Content, |s| s.cursor(CursorIcon::Default));
[Judgment::Invalid, Judgment::Warning, Judgment::Valid]
.into_iter()
.fold(recipe, |recipe, judgment| {
recipe.state(TextBoxState::from(judgment), Root, |s| s.border(edge(judgment.color())))
})
}
}