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 n = self.document.max_unit_id + 1;
20 self.document.max_unit_id = n;
21 let id = AnnotationId::from_int(n);
22 let ann = Annotation {
23 id: id.clone(),
24 kind,
25 page,
26 creator: self.author.clone(),
27 created: self.current_ts,
28 modified: self.current_ts,
29 reply_to: None,
30 payload,
31 };
32 let txn = Transaction {
33 steps: vec![Box::new(InsertAnnotationStep { annotation: ann })],
34 selection_before: self.selection.clone(),
35 selection_after: AnnotationSelection::Single(id.clone()),
36 text_cursor_before: self.text_cursor.clone(),
37 text_cursor_after: None,
38 };
39 self.execute_transaction(txn);
40 id
41 }
42
43 pub fn delete_annotation(&mut self, id: &AnnotationId) {
45 let ann = match self.document.annotations.find(id).cloned() {
46 Some(a) => a,
47 None => return,
48 };
49 let sel_after = if self.selection.contains(id) {
50 AnnotationSelection::None
51 } else {
52 self.selection.clone()
53 };
54 let cur_after = if self
55 .text_cursor
56 .as_ref()
57 .is_some_and(|c| &c.annotation == id)
58 {
59 None
60 } else {
61 self.text_cursor.clone()
62 };
63 let txn = Transaction {
64 steps: vec![Box::new(DeleteAnnotationStep { annotation: ann })],
65 selection_before: self.selection.clone(),
66 selection_after: sel_after,
67 text_cursor_before: self.text_cursor.clone(),
68 text_cursor_after: cur_after,
69 };
70 self.execute_transaction(txn);
71 }
72
73 pub fn delete_selected(&mut self) {
75 let ids: Vec<AnnotationId> = match &self.selection {
76 AnnotationSelection::None => vec![],
77 AnnotationSelection::Single(id) => vec![id.clone()],
78 AnnotationSelection::Multi(ids) => ids.clone(),
79 };
80 for id in &ids {
81 self.delete_annotation(id);
82 }
83 }
84
85 pub fn move_annotation(&mut self, id: &AnnotationId, dx: f64, dy: f64) {
87 let before = match self.document.annotations.find(id).cloned() {
88 Some(a) => a,
89 None => return,
90 };
91 let mut after = before.clone();
92 move_payload(&mut after.payload, dx, dy);
93 after.modified = self.current_ts;
94 let txn = self.replace_txn(id.clone(), before, after);
95 self.execute_transaction(txn);
96 }
97
98 pub fn resize_annotation(&mut self, id: &AnnotationId, new_rect: Rect) {
100 let before = match self.document.annotations.find(id).cloned() {
101 Some(a) => a,
102 None => return,
103 };
104 let mut after = before.clone();
105 resize_payload(&mut after.payload, new_rect);
106 after.modified = self.current_ts;
107 let txn = self.replace_txn(id.clone(), before, after);
108 self.execute_transaction(txn);
109 }
110
111 pub fn move_annotation_vertex(
116 &mut self,
117 id: &AnnotationId,
118 index: usize,
119 new_point: (f64, f64),
120 ) {
121 let before = match self.document.annotations.find(id).cloned() {
122 Some(a) => a,
123 None => return,
124 };
125 let mut after = before.clone();
126 if !crate::payload_util::move_vertex_payload(&mut after.payload, index, new_point) {
127 return;
128 }
129 after.modified = self.current_ts;
130 let txn = self.replace_txn(id.clone(), before, after);
131 self.execute_transaction(txn);
132 }
133
134 pub(crate) fn replace_txn(
136 &self,
137 id: AnnotationId,
138 before: Annotation,
139 after: Annotation,
140 ) -> Transaction {
141 Transaction {
142 steps: vec![Box::new(ReplaceAnnotationStep { id, before, after })],
143 selection_before: self.selection.clone(),
144 selection_after: self.selection.clone(),
145 text_cursor_before: self.text_cursor.clone(),
146 text_cursor_after: self.text_cursor.clone(),
147 }
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154 use rofd_dom::{AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect};
155
156 fn editor_with_max_id(n: u64) -> Editor {
157 let mut e = Editor::new();
158 e.document.max_unit_id = n;
159 e.set_clock("tester".into(), 1_700_000_000_000);
160 e
161 }
162
163 #[test]
164 fn create_annotation_allocates_id_from_max_unit_id_plus_one() {
165 let mut e = editor_with_max_id(1500);
166 let id = e.create_annotation(
167 AnnotationKind::Note,
168 PageId::new("1"),
169 AnnotationPayload::Note {
170 rect: Rect {
171 x: 0.0,
172 y: 0.0,
173 w: 10.0,
174 h: 10.0,
175 },
176 color: Color::Rgb(0, 0, 0),
177 content: "hi".into(),
178 icon: NoteIcon::Note,
179 },
180 );
181 assert_eq!(id.0, "1501", "new id = max_unit_id + 1");
182 assert_eq!(e.document().max_unit_id, 1501, "max_unit_id 自增");
183 }
184
185 #[test]
186 fn create_annotation_ids_monotonic_unique() {
187 let mut e = editor_with_max_id(100);
188 let a = e.create_annotation(
189 AnnotationKind::Note,
190 PageId::new("1"),
191 AnnotationPayload::Note {
192 rect: Rect::default(),
193 color: Color::Rgb(0, 0, 0),
194 content: "a".into(),
195 icon: NoteIcon::Note,
196 },
197 );
198 let b = 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: "b".into(),
205 icon: NoteIcon::Note,
206 },
207 );
208 assert_ne!(a, b, "ids 唯一");
209 assert_eq!(a.0, "101");
210 assert_eq!(b.0, "102");
211 }
212}