tilo_ui_graph/rich.rs
1//! Rich text as a component shows it: paragraphs with their marks, and how each reference in them
2//! reads for the person reading.
3//!
4//! The paragraphs are tilo-doc's, as the app keeps them (kind, text, marks counted in characters,
5//! attributes). A reference is a run marked `mention`, valued with what it refers to
6//! (`tilo_reference::embed`); beside the paragraphs, [`RichText::embeds`] says how each reference
7//! reads for this reader, resolved where the text was read, under their permissions.
8//!
9//! A paragraph projecting a reference (a transclusion or an embed) carries that reference's first
10//! content as the entry's [`preview`](Embed::preview): one level deep, every reference inside it a
11//! link, so a note embedding a note that embeds a note never opens out twice.
12//!
13//! A window's patches are encoded without field names (postcard), so every field is always written.
14
15use serde::{Deserialize, Serialize};
16use std::collections::BTreeMap;
17
18/// Rich text, with how its references read for the person reading it.
19#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
20#[serde(deny_unknown_fields)]
21pub struct RichText {
22 pub blocks: Vec<Block>,
23 /// How each reference in the text reads, by the `mention` mark's value. A reference with
24 /// no entry reads as its run.
25 #[serde(default)]
26 pub embeds: Vec<Embed>,
27 /// The document's named styles, each one's look by the name a paragraph's `style` attribute
28 /// names (`font`, `size`, `bold`, `italic`, `color`, `align`, `space_before`, `space_after`,
29 /// `indent`, `line_spacing`): what a paragraph set in one looks like where it does not say
30 /// otherwise. The view reads them; it never writes them back.
31 #[serde(default)]
32 pub styles: BTreeMap<String, BTreeMap<String, Attr>>,
33}
34
35/// One paragraph: its kind (`paragraph`, `heading`, `list_item`, `quote`, `divider`), its
36/// text, the marks over runs of it and its attributes (`level`, `ordered`).
37#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
38#[serde(deny_unknown_fields)]
39pub struct Block {
40 pub kind: String,
41 pub text: String,
42 #[serde(default)]
43 pub marks: Vec<Mark>,
44 #[serde(default)]
45 pub attrs: BTreeMap<String, Attr>,
46}
47
48/// A mark over the characters `start..end` of a paragraph's text.
49#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
50#[serde(deny_unknown_fields)]
51pub struct Mark {
52 pub start: usize,
53 pub end: usize,
54 pub name: String,
55 pub value: Attr,
56}
57
58/// The value of a mark or an attribute.
59#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
60#[serde(rename_all = "snake_case")]
61pub enum Attr {
62 Bool(bool),
63 Int(i64),
64 Float(f64),
65 Text(String),
66}
67
68/// How one reference reads for the person reading.
69#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
70#[serde(deny_unknown_fields)]
71pub struct Embed {
72 /// The `mention` mark's value naming what it refers to.
73 pub reference: String,
74 pub view: InlineView,
75 /// What the reference shows of itself where the text projects it (an embed): the start of
76 /// its own content, one level deep — a reference inside it is a [`Run`] naming what it
77 /// names, drawn as a link and never expanded again. Absent for a reference the text only
78 /// links.
79 #[serde(default)]
80 pub preview: Option<Vec<Run>>,
81}
82
83/// A stretch of a projected reference's content: its text, and what it refers to when it
84/// refers to something (drawn as a link, never opened out).
85#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(deny_unknown_fields)]
87pub struct Run {
88 pub text: String,
89 #[serde(default)]
90 pub reference: Option<String>,
91}
92
93/// A reference as it reads inline: the reader's inline view of its type.
94#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
95#[serde(rename_all = "snake_case")]
96pub enum InlineView {
97 /// A person: their avatar and what the room calls them.
98 Person {
99 identity: tilo_member_types::MemberId,
100 name: String,
101 },
102 /// A resource the reader can read: its title as its app publishes it now, the icon of its
103 /// type, and what its type shows beside the title (a task's state and number).
104 Resource {
105 title: String,
106 #[serde(default)]
107 icon: Option<String>,
108 #[serde(default)]
109 detail: Option<String>,
110 },
111 /// A resource kept where the text was read cannot say how it reads (another app's): it
112 /// reads as its run, and opens by its reference.
113 Elsewhere,
114 /// A resource the reader cannot read: it is shown as existing, without its title or
115 /// anything else of it, and offers to ask for access.
116 Unreadable,
117}
118
119impl RichText {
120 /// How the reference `reference` reads, when the text says.
121 #[must_use]
122 pub fn embed(&self, reference: &str) -> Option<&InlineView> {
123 self.embeds
124 .iter()
125 .find(|embed| embed.reference == reference)
126 .map(|embed| &embed.view)
127 }
128
129 /// Every string it holds, for a receiver that bounds what it is sent.
130 pub fn strings(&self) -> impl Iterator<Item = &str> {
131 self.blocks
132 .iter()
133 .flat_map(|block| {
134 std::iter::once(block.kind.as_str())
135 .chain(std::iter::once(block.text.as_str()))
136 .chain(block.marks.iter().flat_map(|mark| {
137 std::iter::once(mark.name.as_str()).chain(mark.value.text())
138 }))
139 .chain(
140 block.attrs.iter().flat_map(|(key, value)| {
141 std::iter::once(key.as_str()).chain(value.text())
142 }),
143 )
144 })
145 .chain(self.embeds.iter().flat_map(|embed| {
146 std::iter::once(embed.reference.as_str())
147 .chain(embed.view.strings())
148 .chain(embed.preview.iter().flatten().flat_map(|run| {
149 std::iter::once(run.text.as_str()).chain(run.reference.as_deref())
150 }))
151 }))
152 }
153}
154
155impl Attr {
156 fn text(&self) -> Option<&str> {
157 match self {
158 Self::Text(text) => Some(text),
159 Self::Bool(_) | Self::Int(_) | Self::Float(_) => None,
160 }
161 }
162}
163
164impl InlineView {
165 fn strings(&self) -> Vec<&str> {
166 match self {
167 Self::Person { identity, name } => vec![identity.id(), name],
168 Self::Resource {
169 title,
170 icon,
171 detail,
172 } => std::iter::once(title.as_str())
173 .chain(icon.as_deref())
174 .chain(detail.as_deref())
175 .collect(),
176 Self::Elsewhere | Self::Unreadable => Vec::new(),
177 }
178 }
179}