forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

marks.rs151 lines, 4.4 KiBHistoryRawDownload
1//! The inline mark set and its LoroText encoding: `strong`/`em`/`code` hold `{}`,
2//! `link` holds `{href, title}` (title null when absent). A null value means unset.
3
4use std::collections::HashMap;
5
6use loro::{LoroDoc, LoroValue};
7
8pub const STRONG: &str = "strong";
9pub const EM: &str = "em";
10pub const CODE: &str = "code";
11pub const LINK: &str = "link";
12
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14pub struct Link {
15 pub href: String,
16 pub title: Option<String>,
17}
18
19#[derive(Clone, Debug, Default, PartialEq, Eq)]
20pub struct Marks {
21 pub strong: bool,
22 pub em: bool,
23 pub code: bool,
24 pub link: Option<Link>,
25}
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum MarkType {
29 Strong,
30 Em,
31 Code,
32 Link,
33}
34
35impl MarkType {
36 pub fn key(self) -> &'static str {
37 match self {
38 MarkType::Strong => STRONG,
39 MarkType::Em => EM,
40 MarkType::Code => CODE,
41 MarkType::Link => LINK,
42 }
43 }
44
45 /// Whether typing at the end of the mark extends it (PM `inclusive`).
46 pub fn inclusive(self) -> bool {
47 !matches!(self, MarkType::Link)
48 }
49}
50
51/// The body's mark expand rules ([`note_model::schema::text_style_config`]).
52/// Runtime-only in Loro, so every doc that marks text must call this first.
53pub fn configure(doc: &LoroDoc) {
54 doc.config_text_style(note_model::schema::text_style_config());
55}
56
57fn empty_map() -> LoroValue {
58 LoroValue::from(HashMap::<String, LoroValue>::new())
59}
60
61impl Marks {
62 pub fn none() -> Self {
63 Self::default()
64 }
65
66 pub fn is_empty(&self) -> bool {
67 !self.strong && !self.em && !self.code && self.link.is_none()
68 }
69
70 pub fn has(&self, t: MarkType) -> bool {
71 match t {
72 MarkType::Strong => self.strong,
73 MarkType::Em => self.em,
74 MarkType::Code => self.code,
75 MarkType::Link => self.link.is_some(),
76 }
77 }
78
79 pub fn set(&mut self, t: MarkType, on: bool) {
80 match t {
81 MarkType::Strong => self.strong = on,
82 MarkType::Em => self.em = on,
83 MarkType::Code => self.code = on,
84 MarkType::Link => {
85 if !on {
86 self.link = None
87 }
88 }
89 }
90 }
91
92 /// Reads the attribute map of a delta run.
93 pub fn from_attrs<S: std::hash::BuildHasher>(attrs: Option<&HashMap<String, LoroValue, S>>) -> Self {
94 let mut m = Marks::default();
95 let Some(attrs) = attrs else { return m };
96 let on = |k: &str| attrs.get(k).is_some_and(|v| !v.is_null());
97 m.strong = on(STRONG);
98 m.em = on(EM);
99 m.code = on(CODE);
100 if let Some(LoroValue::Map(map)) = attrs.get(LINK)
101 && let Some(href) = map.get("href").and_then(|v| v.as_string()) {
102 let title = map.get("title").and_then(|v| v.as_string()).map(|s| s.to_string());
103 m.link = Some(Link { href: href.to_string(), title });
104 }
105 m
106 }
107
108 /// The value of mark key `key` (None when unset or not a mark key).
109 pub fn value_of(&self, key: &str) -> Option<LoroValue> {
110 ALL.into_iter().find(|t| t.key() == key).and_then(|t| self.value(t))
111 }
112
113 fn value(&self, t: MarkType) -> Option<LoroValue> {
114 match t {
115 MarkType::Link => self.link.as_ref().map(|l| {
116 let mut map: HashMap<String, LoroValue> = HashMap::new();
117 map.insert("href".into(), LoroValue::from(l.href.as_str()));
118 map.insert(
119 "title".into(),
120 l.title.as_deref().map_or(LoroValue::Null, LoroValue::from),
121 );
122 LoroValue::from(map)
123 }),
124 _ => self.has(t).then(empty_map),
125 }
126 }
127}
128
129pub const ALL: [MarkType; 4] = [MarkType::Link, MarkType::Em, MarkType::Strong, MarkType::Code];
130
131impl From<&note_model::marks::Marks> for Marks {
132 fn from(m: &note_model::marks::Marks) -> Self {
133 Marks {
134 strong: m.strong,
135 em: m.em,
136 code: m.code,
137 link: m.link.as_ref().map(|(href, title)| Link { href: href.clone(), title: title.clone() }),
138 }
139 }
140}
141
142impl From<&Marks> for note_model::marks::Marks {
143 fn from(m: &Marks) -> Self {
144 note_model::marks::Marks {
145 strong: m.strong,
146 em: m.em,
147 code: m.code,
148 link: m.link.as_ref().map(|l| (l.href.clone(), l.title.clone())),
149 }
150 }
151}