1use rofd_dom::{AnnotationId, OfdDocument};
2
3use crate::cursor::TextCursor;
4use crate::selection::AnnotationSelection;
5use crate::steps::history::History;
6use crate::steps::transaction::Transaction;
7
8pub struct Editor {
11 pub(crate) document: OfdDocument,
12 pub(crate) selection: AnnotationSelection,
13 pub(crate) text_cursor: Option<TextCursor>,
14 pub(crate) history: History,
15 pub(crate) author: String,
16 pub(crate) current_ts: i64,
17}
18
19impl Editor {
20 pub fn new() -> Self {
21 Self {
22 document: OfdDocument::default(),
23 selection: AnnotationSelection::None,
24 text_cursor: None,
25 history: History::new(100),
26 author: String::new(),
27 current_ts: 0,
28 }
29 }
30
31 pub fn load_document(&mut self, doc: OfdDocument) {
32 self.document = doc;
33 self.selection = AnnotationSelection::None;
34 self.text_cursor = None;
35 self.history = History::new(100);
36 }
37
38 pub fn revise_document<T>(&mut self, revise: impl FnOnce(&mut OfdDocument) -> T) -> T {
45 revise(&mut self.document)
46 }
47
48 pub fn invalidate_pages(&mut self) {
52 self.document.body_revision += 1;
53 }
54
55 pub fn set_clock(&mut self, author: String, ts: i64) {
57 self.author = author;
58 self.current_ts = ts;
59 }
60
61 pub fn document(&self) -> &OfdDocument {
62 &self.document
63 }
64 pub fn selection(&self) -> &AnnotationSelection {
65 &self.selection
66 }
67 pub fn text_cursor(&self) -> Option<&TextCursor> {
68 self.text_cursor.as_ref()
69 }
70 pub fn can_undo(&self) -> bool {
71 self.history.can_undo()
72 }
73 pub fn can_redo(&self) -> bool {
74 self.history.can_redo()
75 }
76 pub fn history_len(&self) -> usize {
79 self.history.done_len()
80 }
81
82 pub fn select(&mut self, id: AnnotationId) {
84 self.selection = AnnotationSelection::Single(id);
85 }
86 pub fn set_selection(&mut self, sel: AnnotationSelection) {
88 self.selection = sel;
89 }
90 pub fn clear_selection(&mut self) {
92 self.selection = AnnotationSelection::None;
93 }
94 pub fn set_cursor(&mut self, annotation: AnnotationId, offset: usize) {
96 self.set_cursor_with_preferred_x(annotation, offset, None);
97 }
98
99 pub fn set_cursor_with_preferred_x(
102 &mut self,
103 annotation: AnnotationId,
104 offset: usize,
105 preferred_x: Option<f32>,
106 ) {
107 self.text_cursor = Some(TextCursor {
108 annotation,
109 offset,
110 preferred_x,
111 });
112 }
113 pub fn clear_cursor(&mut self) {
115 self.text_cursor = None;
116 }
117
118 pub(crate) fn execute_transaction(&mut self, txn: Transaction) {
123 for step in &txn.steps {
124 step.apply(&mut self.document.annotations);
125 }
126 self.selection = txn.selection_after.clone();
127 self.text_cursor = txn.text_cursor_after.clone();
128 self.history.push(txn);
129 }
130
131 pub fn undo(&mut self) -> bool {
132 let txn = self.history.undo();
133 if let Some(txn) = txn {
134 for step in txn.steps.iter().rev() {
135 step.revert(&mut self.document.annotations);
136 }
137 self.selection = txn.selection_before.clone();
138 self.text_cursor = txn.text_cursor_before.clone();
139 true
140 } else {
141 false
142 }
143 }
144
145 pub fn redo(&mut self) -> bool {
146 let txn = self.history.redo();
147 if let Some(txn) = txn {
148 for step in &txn.steps {
149 step.apply(&mut self.document.annotations);
150 }
151 self.selection = txn.selection_after.clone();
152 self.text_cursor = txn.text_cursor_after.clone();
153 true
154 } else {
155 false
156 }
157 }
158}
159
160impl Default for Editor {
161 fn default() -> Self {
162 Self::new()
163 }
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169 use crate::steps::annotation_steps::InsertAnnotationStep;
170 use rofd_dom::{
171 Annotation, AnnotationId, AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect,
172 };
173
174 fn note_ann(id: &str) -> Annotation {
175 Annotation {
176 id: AnnotationId::new(id),
177 kind: AnnotationKind::Note,
178 page: PageId::new("P0"),
179 creator: "t".into(),
180 created: 0,
181 modified: 0,
182 reply_to: None,
183 payload: AnnotationPayload::Note {
184 rect: Rect {
185 x: 0.0,
186 y: 0.0,
187 w: 1.0,
188 h: 1.0,
189 },
190 color: Color::Rgb(0, 0, 0),
191 content: "x".into(),
192 icon: NoteIcon::Note,
193 },
194 }
195 }
196
197 #[test]
198 fn execute_undo_redo_via_transaction() {
199 let mut e = Editor::new();
200 let ann = note_ann("21");
201 let txn = Transaction {
202 steps: vec![Box::new(InsertAnnotationStep {
203 annotation: ann.clone(),
204 })],
205 selection_before: AnnotationSelection::None,
206 selection_after: AnnotationSelection::Single(ann.id.clone()),
207 text_cursor_before: None,
208 text_cursor_after: None,
209 };
210 e.execute_transaction(txn);
211 assert!(e.document().annotations.find(&ann.id).is_some());
212 assert!(e.undo());
213 assert!(e.document().annotations.find(&ann.id).is_none());
214 assert!(e.redo());
215 assert!(e.document().annotations.find(&ann.id).is_some());
216 }
217
218 #[test]
219 fn history_capacity_evicts_oldest() {
220 let mut e = Editor::new();
221 for i in 0..105u32 {
222 let ann = note_ann(&format!("{}", i));
223 let txn = Transaction {
224 steps: vec![Box::new(InsertAnnotationStep { annotation: ann })],
225 selection_before: AnnotationSelection::None,
226 selection_after: AnnotationSelection::None,
227 text_cursor_before: None,
228 text_cursor_after: None,
229 };
230 e.execute_transaction(txn);
231 }
232 assert!(e.can_undo());
233 for _ in 0..100 {
234 assert!(e.undo());
235 }
236 assert!(!e.can_undo());
237 }
238
239 #[test]
240 fn set_cursor_with_preferred_x_round_trips() {
241 let mut e = Editor::new();
242 e.set_cursor_with_preferred_x(AnnotationId::from_int(1), 3, Some(4.5));
243 let c = e.text_cursor().unwrap();
244 assert_eq!((c.offset, c.preferred_x), (3, Some(4.5)));
245 e.set_cursor(AnnotationId::from_int(1), 3);
246 assert_eq!(e.text_cursor().unwrap().preferred_x, None);
247 }
248}
249
250#[cfg(test)]
251mod revising_tests {
252 use super::*;
253 use crate::steps::annotation_steps::InsertAnnotationStep;
254 use rofd_dom::{Annotation, AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect};
255
256 fn note_ann(id: &str) -> Annotation {
257 Annotation {
258 id: AnnotationId::new(id),
259 kind: AnnotationKind::Note,
260 page: PageId::new("P0"),
261 creator: "t".into(),
262 created: 0,
263 modified: 0,
264 reply_to: None,
265 payload: AnnotationPayload::Note {
266 rect: Rect {
267 x: 0.0,
268 y: 0.0,
269 w: 1.0,
270 h: 1.0,
271 },
272 color: Color::Rgb(0, 0, 0),
273 content: "x".into(),
274 icon: NoteIcon::Note,
275 },
276 }
277 }
278
279 #[test]
280 fn revising_keeps_the_selection_the_cursor_and_the_history() {
281 let mut e = Editor::new();
282 let ann = note_ann("21");
283 e.execute_transaction(Transaction {
284 steps: vec![Box::new(InsertAnnotationStep {
285 annotation: ann.clone(),
286 })],
287 selection_before: AnnotationSelection::None,
288 selection_after: AnnotationSelection::Single(ann.id.clone()),
289 text_cursor_before: None,
290 text_cursor_after: None,
291 });
292 e.set_cursor(ann.id.clone(), 2);
293
294 let highest = e.revise_document(|document| document.highest_known_object_id());
295
296 assert_eq!(highest, 21);
297 assert!(e.can_undo());
298 assert_eq!(
299 e.selection().clone(),
300 AnnotationSelection::Single(ann.id.clone())
301 );
302 assert_eq!(e.text_cursor().map(|cursor| cursor.offset), Some(2));
303 assert_eq!(
304 e.document().body_revision,
305 0,
306 "revising alone moves no page"
307 );
308 }
309
310 #[test]
311 fn invalidating_pages_moves_the_body_revision() {
312 let mut e = Editor::new();
313 e.invalidate_pages();
314 assert_eq!(e.document().body_revision, 1);
315 }
316}