1use rofd_dom::{Annotation, AnnotationId, AnnotationKind, AnnotationPayload, PageId, Rect};
2
3use crate::editor::Editor;
4use crate::payload_util::{move_payload, resize_payload};
5use crate::selection::AnnotationSelection;
6use crate::steps::annotation_steps::{
7 DeleteAnnotationStep, InsertAnnotationStep, ReplaceAnnotationStep,
8};
9use crate::steps::transaction::Transaction;
10
11impl Editor {
12 pub fn create_annotation(
14 &mut self,
15 kind: AnnotationKind,
16 page: PageId,
17 payload: AnnotationPayload,
18 ) -> AnnotationId {
19 let signed = self.document.signatures.declared;
23 let n = if signed {
24 self.document.highest_known_object_id() + 1
25 } else {
26 self.document.max_unit_id + 1
27 };
28 if !signed {
29 self.document.max_unit_id = n;
30 }
31 let id = AnnotationId::from_int(n);
32 let ann = Annotation {
33 id: id.clone(),
34 kind,
35 page,
36 creator: self.author.clone(),
37 created: self.current_ts,
38 modified: self.current_ts,
39 reply_to: None,
40 payload,
41 };
42 let txn = Transaction {
43 steps: vec![Box::new(InsertAnnotationStep { annotation: ann })],
44 selection_before: self.selection.clone(),
45 selection_after: AnnotationSelection::Single(id.clone()),
46 text_cursor_before: self.text_cursor.clone(),
47 text_cursor_after: None,
48 };
49 self.execute_transaction(txn);
50 id
51 }
52
53 pub fn delete_annotation(&mut self, id: &AnnotationId) {
55 let ann = match self.document.annotations.find(id).cloned() {
56 Some(a) => a,
57 None => return,
58 };
59 let sel_after = if self.selection.contains(id) {
60 AnnotationSelection::None
61 } else {
62 self.selection.clone()
63 };
64 let cur_after = if self
65 .text_cursor
66 .as_ref()
67 .is_some_and(|c| &c.annotation == id)
68 {
69 None
70 } else {
71 self.text_cursor.clone()
72 };
73 let txn = Transaction {
74 steps: vec![Box::new(DeleteAnnotationStep { annotation: ann })],
75 selection_before: self.selection.clone(),
76 selection_after: sel_after,
77 text_cursor_before: self.text_cursor.clone(),
78 text_cursor_after: cur_after,
79 };
80 self.execute_transaction(txn);
81 }
82
83 pub fn delete_selected(&mut self) {
85 let ids: Vec<AnnotationId> = match &self.selection {
86 AnnotationSelection::None => vec![],
87 AnnotationSelection::Single(id) => vec![id.clone()],
88 AnnotationSelection::Multi(ids) => ids.clone(),
89 };
90 for id in &ids {
91 self.delete_annotation(id);
92 }
93 }
94
95 pub fn move_annotation(&mut self, id: &AnnotationId, dx: f64, dy: f64) {
97 let before = match self.document.annotations.find(id).cloned() {
98 Some(a) => a,
99 None => return,
100 };
101 let mut after = before.clone();
102 move_payload(&mut after.payload, dx, dy);
103 after.modified = self.current_ts;
104 let txn = self.replace_txn(id.clone(), before, after);
105 self.execute_transaction(txn);
106 }
107
108 pub fn resize_annotation(&mut self, id: &AnnotationId, new_rect: Rect) {
110 let before = match self.document.annotations.find(id).cloned() {
111 Some(a) => a,
112 None => return,
113 };
114 let mut after = before.clone();
115 resize_payload(&mut after.payload, new_rect);
116 after.modified = self.current_ts;
117 let txn = self.replace_txn(id.clone(), before, after);
118 self.execute_transaction(txn);
119 }
120
121 pub fn move_annotation_vertex(
126 &mut self,
127 id: &AnnotationId,
128 index: usize,
129 new_point: (f64, f64),
130 ) {
131 let before = match self.document.annotations.find(id).cloned() {
132 Some(a) => a,
133 None => return,
134 };
135 let mut after = before.clone();
136 if !crate::payload_util::move_vertex_payload(&mut after.payload, index, new_point) {
137 return;
138 }
139 after.modified = self.current_ts;
140 let txn = self.replace_txn(id.clone(), before, after);
141 self.execute_transaction(txn);
142 }
143
144 pub(crate) fn replace_txn(
146 &self,
147 id: AnnotationId,
148 before: Annotation,
149 after: Annotation,
150 ) -> Transaction {
151 Transaction {
152 steps: vec![Box::new(ReplaceAnnotationStep { id, before, after })],
153 selection_before: self.selection.clone(),
154 selection_after: self.selection.clone(),
155 text_cursor_before: self.text_cursor.clone(),
156 text_cursor_after: self.text_cursor.clone(),
157 }
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164 use rofd_dom::{AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect};
165
166 fn editor_with_max_id(n: u64) -> Editor {
167 let mut e = Editor::new();
168 e.document.max_unit_id = n;
169 e.set_clock("tester".into(), 1_700_000_000_000);
170 e
171 }
172
173 #[test]
174 fn create_annotation_allocates_id_from_max_unit_id_plus_one() {
175 let mut e = editor_with_max_id(1500);
176 let id = e.create_annotation(
177 AnnotationKind::Note,
178 PageId::new("1"),
179 AnnotationPayload::Note {
180 rect: Rect {
181 x: 0.0,
182 y: 0.0,
183 w: 10.0,
184 h: 10.0,
185 },
186 color: Color::Rgb(0, 0, 0),
187 content: "hi".into(),
188 icon: NoteIcon::Note,
189 },
190 );
191 assert_eq!(id.0, "1501", "new id = max_unit_id + 1");
192 assert_eq!(e.document().max_unit_id, 1501, "max_unit_id 自增");
193 }
194
195 #[test]
196 fn create_annotation_ids_monotonic_unique() {
197 let mut e = editor_with_max_id(100);
198 let a = e.create_annotation(
199 AnnotationKind::Note,
200 PageId::new("1"),
201 AnnotationPayload::Note {
202 rect: Rect::default(),
203 color: Color::Rgb(0, 0, 0),
204 content: "a".into(),
205 icon: NoteIcon::Note,
206 },
207 );
208 let b = e.create_annotation(
209 AnnotationKind::Note,
210 PageId::new("1"),
211 AnnotationPayload::Note {
212 rect: Rect::default(),
213 color: Color::Rgb(0, 0, 0),
214 content: "b".into(),
215 icon: NoteIcon::Note,
216 },
217 );
218 assert_ne!(a, b, "ids 唯一");
219 assert_eq!(a.0, "101");
220 assert_eq!(b.0, "102");
221 }
222
223 #[test]
224 fn create_annotation_on_signed_document_allocates_above_known_ids_and_keeps_max_unit_id() {
225 let mut e = editor_with_max_id(65_545);
228 e.document.signatures.declared = true;
229 e.document.annotations.insert(rofd_dom::Annotation {
230 id: rofd_dom::AnnotationId::from_int(9),
231 kind: AnnotationKind::Note,
232 page: PageId::new("1"),
233 creator: "tester".into(),
234 created: 0,
235 modified: 0,
236 reply_to: None,
237 payload: AnnotationPayload::Note {
238 rect: Rect::default(),
239 color: Color::Rgb(0, 0, 0),
240 content: "earlier".into(),
241 icon: NoteIcon::Note,
242 },
243 });
244 let note = |content: &str| AnnotationPayload::Note {
245 rect: Rect::default(),
246 color: Color::Rgb(0, 0, 0),
247 content: content.to_owned(),
248 icon: NoteIcon::Note,
249 };
250 let first = e.create_annotation(AnnotationKind::Note, PageId::new("1"), note("first"));
251 assert_eq!(
252 first.0, "10",
253 "a signed document allocates above the highest known id"
254 );
255 assert_eq!(
256 e.document().max_unit_id,
257 65_545,
258 "the reserve ceiling is left alone"
259 );
260 let second = e.create_annotation(AnnotationKind::Note, PageId::new("1"), note("second"));
261 assert_eq!(second.0, "11", "ids stay unique and consecutive");
262 assert_eq!(e.document().max_unit_id, 65_545);
263 }
264}