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(crate) fn replace_txn(
113 &self,
114 id: AnnotationId,
115 before: Annotation,
116 after: Annotation,
117 ) -> Transaction {
118 Transaction {
119 steps: vec![Box::new(ReplaceAnnotationStep { id, before, after })],
120 selection_before: self.selection.clone(),
121 selection_after: self.selection.clone(),
122 text_cursor_before: self.text_cursor.clone(),
123 text_cursor_after: self.text_cursor.clone(),
124 }
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131 use rofd_dom::{AnnotationKind, AnnotationPayload, Color, NoteIcon, PageId, Rect};
132
133 fn editor_with_max_id(n: u64) -> Editor {
134 let mut e = Editor::new();
135 e.document.max_unit_id = n;
136 e.set_clock("tester".into(), 1_700_000_000_000);
137 e
138 }
139
140 #[test]
141 fn create_annotation_allocates_id_from_max_unit_id_plus_one() {
142 let mut e = editor_with_max_id(1500);
143 let id = e.create_annotation(
144 AnnotationKind::Note,
145 PageId::new("1"),
146 AnnotationPayload::Note {
147 rect: Rect {
148 x: 0.0,
149 y: 0.0,
150 w: 10.0,
151 h: 10.0,
152 },
153 color: Color::Rgb(0, 0, 0),
154 content: "hi".into(),
155 icon: NoteIcon::Note,
156 },
157 );
158 assert_eq!(id.0, "1501", "new id = max_unit_id + 1");
159 assert_eq!(e.document().max_unit_id, 1501, "max_unit_id 自增");
160 }
161
162 #[test]
163 fn create_annotation_ids_monotonic_unique() {
164 let mut e = editor_with_max_id(100);
165 let a = e.create_annotation(
166 AnnotationKind::Note,
167 PageId::new("1"),
168 AnnotationPayload::Note {
169 rect: Rect::default(),
170 color: Color::Rgb(0, 0, 0),
171 content: "a".into(),
172 icon: NoteIcon::Note,
173 },
174 );
175 let b = e.create_annotation(
176 AnnotationKind::Note,
177 PageId::new("1"),
178 AnnotationPayload::Note {
179 rect: Rect::default(),
180 color: Color::Rgb(0, 0, 0),
181 content: "b".into(),
182 icon: NoteIcon::Note,
183 },
184 );
185 assert_ne!(a, b, "ids 唯一");
186 assert_eq!(a.0, "101");
187 assert_eq!(b.0, "102");
188 }
189}