//! The inline mark set and its LoroText encoding: `strong`/`em`/`code` hold `{}`, //! `link` holds `{href, title}` (title null when absent). A null value means unset. use std::collections::HashMap; use loro::{LoroDoc, LoroValue}; pub const STRONG: &str = "strong"; pub const EM: &str = "em"; pub const CODE: &str = "code"; pub const LINK: &str = "link"; #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Link { pub href: String, pub title: Option, } #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Marks { pub strong: bool, pub em: bool, pub code: bool, pub link: Option, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum MarkType { Strong, Em, Code, Link, } impl MarkType { pub fn key(self) -> &'static str { match self { MarkType::Strong => STRONG, MarkType::Em => EM, MarkType::Code => CODE, MarkType::Link => LINK, } } /// Whether typing at the end of the mark extends it (PM `inclusive`). pub fn inclusive(self) -> bool { !matches!(self, MarkType::Link) } } /// The body's mark expand rules ([`note_model::schema::text_style_config`]). /// Runtime-only in Loro, so every doc that marks text must call this first. pub fn configure(doc: &LoroDoc) { doc.config_text_style(note_model::schema::text_style_config()); } fn empty_map() -> LoroValue { LoroValue::from(HashMap::::new()) } impl Marks { pub fn none() -> Self { Self::default() } pub fn is_empty(&self) -> bool { !self.strong && !self.em && !self.code && self.link.is_none() } pub fn has(&self, t: MarkType) -> bool { match t { MarkType::Strong => self.strong, MarkType::Em => self.em, MarkType::Code => self.code, MarkType::Link => self.link.is_some(), } } pub fn set(&mut self, t: MarkType, on: bool) { match t { MarkType::Strong => self.strong = on, MarkType::Em => self.em = on, MarkType::Code => self.code = on, MarkType::Link => { if !on { self.link = None } } } } /// Reads the attribute map of a delta run. pub fn from_attrs(attrs: Option<&HashMap>) -> Self { let mut m = Marks::default(); let Some(attrs) = attrs else { return m }; let on = |k: &str| attrs.get(k).is_some_and(|v| !v.is_null()); m.strong = on(STRONG); m.em = on(EM); m.code = on(CODE); if let Some(LoroValue::Map(map)) = attrs.get(LINK) && let Some(href) = map.get("href").and_then(|v| v.as_string()) { let title = map.get("title").and_then(|v| v.as_string()).map(|s| s.to_string()); m.link = Some(Link { href: href.to_string(), title }); } m } /// The value of mark key `key` (None when unset or not a mark key). pub fn value_of(&self, key: &str) -> Option { ALL.into_iter().find(|t| t.key() == key).and_then(|t| self.value(t)) } fn value(&self, t: MarkType) -> Option { match t { MarkType::Link => self.link.as_ref().map(|l| { let mut map: HashMap = HashMap::new(); map.insert("href".into(), LoroValue::from(l.href.as_str())); map.insert( "title".into(), l.title.as_deref().map_or(LoroValue::Null, LoroValue::from), ); LoroValue::from(map) }), _ => self.has(t).then(empty_map), } } } pub const ALL: [MarkType; 4] = [MarkType::Link, MarkType::Em, MarkType::Strong, MarkType::Code]; impl From<¬e_model::marks::Marks> for Marks { fn from(m: ¬e_model::marks::Marks) -> Self { Marks { strong: m.strong, em: m.em, code: m.code, link: m.link.as_ref().map(|(href, title)| Link { href: href.clone(), title: title.clone() }), } } } impl From<&Marks> for note_model::marks::Marks { fn from(m: &Marks) -> Self { note_model::marks::Marks { strong: m.strong, em: m.em, code: m.code, link: m.link.as_ref().map(|l| (l.href.clone(), l.title.clone())), } } }