use std::{ops::Range, sync::Arc};
use crate::{
app::component::{DevtoolsFormatter, DevtoolsInspectable},
core::Signal,
};
mod objects;
mod ops;
mod paragraphs;
mod rich_text;
pub use objects::{InlineObject, OBJECT_CHARACTER};
pub(crate) use objects::{normalize_objects, place_objects, slice_objects, splice_objects, text_with_labels};
pub(crate) use ops::{insertion_style, normalize, restyle, slice, splice, style_at, summarize};
pub use paragraphs::{ParagraphKind, ParagraphMarker, ParagraphStyle, ParagraphStyles};
pub(crate) use paragraphs::{normalize_kinds, paragraph_of, paragraph_ranges, place_kinds, splice_kinds};
pub use rich_text::RichText;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct SpanStyle {
pub bold: bool,
pub italic: bool,
pub code: bool,
pub strikethrough: bool,
pub underline: bool,
pub link: Option<Arc<str>>,
}
impl SpanStyle {
pub fn new() -> Self {
Self::default()
}
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
pub fn italic(mut self) -> Self {
self.italic = true;
self
}
pub fn code(mut self) -> Self {
self.code = true;
self
}
pub fn strikethrough(mut self) -> Self {
self.strikethrough = true;
self
}
pub fn underline(mut self) -> Self {
self.underline = true;
self
}
pub fn link(mut self, target: impl Into<Arc<str>>) -> Self {
self.link = Some(target.into());
self
}
pub fn has(&self, mark: TextMark) -> bool {
match mark {
TextMark::Bold => self.bold,
TextMark::Italic => self.italic,
TextMark::Code => self.code,
TextMark::Strikethrough => self.strikethrough,
TextMark::Underline => self.underline,
}
}
pub fn with(mut self, mark: TextMark, on: bool) -> Self {
match mark {
TextMark::Bold => self.bold = on,
TextMark::Italic => self.italic = on,
TextMark::Code => self.code = on,
TextMark::Strikethrough => self.strikethrough = on,
TextMark::Underline => self.underline = on,
}
self
}
pub fn is_plain(&self) -> bool {
*self == Self::default()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TextMark {
Bold,
Italic,
Code,
Strikethrough,
Underline,
}
impl TextMark {
pub const ALL: [Self; 5] = [
Self::Bold,
Self::Italic,
Self::Code,
Self::Strikethrough,
Self::Underline,
];
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct StyledSpan {
pub range: Range<usize>,
pub style: SpanStyle,
}
impl StyledSpan {
pub fn new(range: Range<usize>, style: SpanStyle) -> Self {
Self { range, style }
}
}
impl DevtoolsInspectable for StyledSpan {
fn inspect(&self, formatter: &mut DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct StyledText {
pub text: String,
pub spans: Vec<StyledSpan>,
}
impl StyledText {
pub fn new(text: impl Into<String>, spans: Vec<StyledSpan>) -> Self {
let text = text.into();
let spans = normalize(&text, spans);
Self { text, spans }
}
pub fn plain(text: impl Into<String>) -> Self {
Self {
text: text.into(),
spans: Vec::new(),
}
}
pub fn style_at(&self, offset: usize) -> SpanStyle {
style_at(&self.spans, offset)
}
pub fn runs(&self) -> Vec<(&str, SpanStyle)> {
ops::runs(&self.spans, self.text.len())
.into_iter()
.filter(|(range, _)| !range.is_empty())
.map(|(range, style)| (&self.text[range], style))
.collect()
}
}
impl DevtoolsInspectable for StyledText {
fn inspect(&self, formatter: &mut DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
#[derive(Clone)]
pub struct StyledChange {
pub(crate) value: Signal<String>,
pub(crate) spans: Arc<Vec<StyledSpan>>,
pub(crate) paragraphs: Option<Arc<Vec<ParagraphKind>>>,
pub(crate) objects: Option<Arc<Vec<InlineObject>>>,
pub(crate) edit: StyledEdit,
}
impl StyledChange {
pub fn with_text<R>(&self, f: impl FnOnce(&str) -> R) -> R {
self.value.with_untracked(|text| f(text))
}
pub fn inserted_text(&self) -> Option<String> {
let StyledEdit::Text { replaced, inserted } = &self.edit else {
return None;
};
let range = replaced.start..replaced.start + inserted;
self.with_text(|text| text.get(range).map(str::to_owned))
}
pub fn spans(&self) -> &[StyledSpan] {
&self.spans
}
pub fn edit(&self) -> &StyledEdit {
&self.edit
}
pub fn paragraphs(&self) -> &[ParagraphKind] {
self.paragraphs.as_deref().map_or(&[], Vec::as_slice)
}
pub fn objects(&self) -> &[InlineObject] {
self.objects.as_deref().map_or(&[], Vec::as_slice)
}
pub fn to_rich_text(&self) -> RichText {
RichText::new(
self.value.get_untracked(),
self.spans.to_vec(),
self.paragraphs().to_vec(),
)
.with_objects(self.objects().to_vec())
}
pub fn to_styled_text(&self) -> StyledText {
StyledText {
text: self.value.get_untracked(),
spans: self.spans.to_vec(),
}
}
}
impl std::fmt::Debug for StyledChange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StyledChange")
.field("edit", &self.edit)
.field("spans", &self.spans)
.finish_non_exhaustive()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StyledEdit {
Text { replaced: Range<usize>, inserted: usize },
Style { range: Range<usize> },
Paragraphs { range: Range<usize> },
Replaced,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MarkPresence {
#[default]
None,
Some,
All,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SelectionStyle {
pub bold: MarkPresence,
pub italic: MarkPresence,
pub code: MarkPresence,
pub strikethrough: MarkPresence,
pub underline: MarkPresence,
pub link: Option<Arc<str>>,
pub has_link: bool,
}
impl DevtoolsInspectable for SelectionStyle {
fn inspect(&self, formatter: &mut DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
impl SelectionStyle {
pub fn mark(&self, mark: TextMark) -> MarkPresence {
match mark {
TextMark::Bold => self.bold,
TextMark::Italic => self.italic,
TextMark::Code => self.code,
TextMark::Strikethrough => self.strikethrough,
TextMark::Underline => self.underline,
}
}
pub(crate) fn of_style(style: &SpanStyle) -> Self {
let presence = |on: bool| if on { MarkPresence::All } else { MarkPresence::None };
Self {
bold: presence(style.bold),
italic: presence(style.italic),
code: presence(style.code),
strikethrough: presence(style.strikethrough),
underline: presence(style.underline),
link: style.link.clone(),
has_link: style.link.is_some(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkClick {
pub target: Arc<str>,
pub range: Range<usize>,
}