rofd_editor/steps/
annotation_steps.rs1use rofd_dom::{Annotation, AnnotationId, AnnotationModel};
2
3use crate::steps::step_trait::Step;
4
5#[derive(Debug)]
6pub struct InsertAnnotationStep {
7 pub annotation: Annotation,
8}
9impl Step for InsertAnnotationStep {
10 fn apply(&self, anns: &mut AnnotationModel) {
11 anns.insert(self.annotation.clone());
12 }
13 fn revert(&self, anns: &mut AnnotationModel) {
14 anns.remove(&self.annotation.id);
15 }
16}
17
18#[derive(Debug)]
19pub struct DeleteAnnotationStep {
20 pub annotation: Annotation,
21}
22impl Step for DeleteAnnotationStep {
23 fn apply(&self, anns: &mut AnnotationModel) {
24 anns.remove(&self.annotation.id);
25 }
26 fn revert(&self, anns: &mut AnnotationModel) {
27 anns.insert(self.annotation.clone());
28 }
29}
30
31#[derive(Debug)]
32pub struct ReplaceAnnotationStep {
33 pub id: AnnotationId,
34 pub before: Annotation,
35 pub after: Annotation,
36}
37impl Step for ReplaceAnnotationStep {
38 fn apply(&self, anns: &mut AnnotationModel) {
39 if let Some(a) = anns.find_mut(&self.id) {
40 *a = self.after.clone();
41 }
42 }
43 fn revert(&self, anns: &mut AnnotationModel) {
44 if let Some(a) = anns.find_mut(&self.id) {
45 *a = self.before.clone();
46 }
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53 use rofd_dom::{AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect};
54
55 fn note_ann(id: &str, content: &str) -> Annotation {
56 Annotation {
57 id: AnnotationId::new(id),
58 kind: AnnotationKind::Note,
59 page: PageId::new("P0"),
60 creator: "t".into(),
61 created: 0,
62 modified: 0,
63 reply_to: None,
64 payload: AnnotationPayload::Note {
65 rect: Rect {
66 x: 0.0,
67 y: 0.0,
68 w: 10.0,
69 h: 10.0,
70 },
71 color: Color::Rgb(0, 0, 0),
72 content: content.into(),
73 icon: NoteIcon::Note,
74 },
75 }
76 }
77
78 #[test]
79 fn insert_then_revert_yields_empty() {
80 let mut m = AnnotationModel::default();
81 let ann = note_ann("11", "a");
82 let step = InsertAnnotationStep {
83 annotation: ann.clone(),
84 };
85 step.apply(&mut m);
86 assert!(m.find(&ann.id).is_some());
87 step.revert(&mut m);
88 assert!(m.find(&ann.id).is_none());
89 }
90
91 #[test]
92 fn delete_then_revert_restores() {
93 let mut m = AnnotationModel::default();
94 let ann = note_ann("12", "a");
95 m.insert(ann.clone());
96 let step = DeleteAnnotationStep {
97 annotation: ann.clone(),
98 };
99 step.apply(&mut m);
100 assert!(m.find(&ann.id).is_none());
101 step.revert(&mut m);
102 assert!(m.find(&ann.id).is_some());
103 }
104
105 #[test]
106 fn replace_then_revert_restores_before() {
107 let mut m = AnnotationModel::default();
108 let before = note_ann("13", "before");
109 let mut after = before.clone();
110 if let AnnotationPayload::Note { content, .. } = &mut after.payload {
111 *content = "after".into();
112 }
113 m.insert(before.clone());
114 let step = ReplaceAnnotationStep {
115 id: before.id.clone(),
116 before: before.clone(),
117 after: after.clone(),
118 };
119 step.apply(&mut m);
120 let got = m.find(&before.id).unwrap();
121 assert!(
122 matches!(&got.payload, AnnotationPayload::Note { content, .. } if content == "after")
123 );
124 step.revert(&mut m);
125 let got = m.find(&before.id).unwrap();
126 assert!(
127 matches!(&got.payload, AnnotationPayload::Note { content, .. } if content == "before")
128 );
129 }
130}