1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
cell::{Cell, RefCell},
rc::{Rc, Weak},
};
use crate::{
format::{CharFormat, FormatChangeResult, FormattedElement, IsFormat},
text_document::{Element, ElementManager, ElementTrait, ModelError},
Block,
};
#[derive(Default, Clone, Debug)]
pub struct Text {
uuid: Cell<usize>,
element_manager: Weak<ElementManager>,
text: RefCell<String>,
char_format: RefCell<CharFormat>,
}
impl PartialEq for Text {
fn eq(&self, other: &Self) -> bool {
self.uuid == other.uuid && self.char_format == other.char_format
}
}
impl Text {
pub(crate) fn new(element_manager: Weak<ElementManager>) -> Self {
Text {
element_manager,
uuid: Default::default(),
char_format: RefCell::new(CharFormat {
..Default::default()
}),
text: RefCell::new(String::new()),
}
}
pub fn uuid(&self) -> usize {
self.uuid.get()
}
pub(crate) fn char_format(&self) -> CharFormat {
self.format()
}
pub fn plain_text(&self) -> String {
self.text.borrow().clone()
}
pub(crate) fn set_text<S: Into<String>>(&self, text: S) {
let plain_text: String = text.into();
self.text.replace(plain_text);
}
pub(crate) fn insert_plain_text<S: Into<String>>(&self, position_in_text: usize, text: S) {
let plain_text: String = text.into();
self.text
.borrow_mut()
.insert_str(position_in_text, plain_text.as_str())
}
pub(crate) fn split(&self, position_in_text: usize) -> Element {
let element_manager = self.element_manager.upgrade().unwrap();
let new_text_rc = element_manager
.insert_new_text(self.uuid(), crate::text_document::InsertMode::After)
.unwrap();
let new_element = element_manager.get(new_text_rc.uuid()).unwrap();
let original_text = self.plain_text();
let split = original_text.split_at(position_in_text);
self.set_text(&split.0.to_string());
new_text_rc.set_text(&split.1.to_string());
new_text_rc.set_format(&self.char_format()).unwrap();
new_element
}
pub(crate) fn remove_text(
&self,
left_position_in_text: usize,
right_position_in_text: usize,
) -> Result<(), ModelError> {
let mut text = self.plain_text();
if left_position_in_text > text.len() || right_position_in_text > text.len() {
return Err(ModelError::OutsideElementBounds);
}
text.replace_range(left_position_in_text..right_position_in_text, "");
self.set_text(&text);
Ok(())
}
pub fn text_length(&self) -> usize {
self.text.borrow().len()
}
fn parent_bloc_rc(&self) -> Rc<Block> {
let element_manager = self.element_manager.upgrade().unwrap();
match element_manager
.get_parent_element_using_uuid(self.uuid())
.unwrap()
{
Element::BlockElement(block) => block,
_ => unreachable!(),
}
}
pub fn position_in_block(&self) -> usize {
let parent_block = self.parent_bloc_rc();
parent_block.position_of_child(self.uuid())
}
pub fn start(&self) -> usize {
let parent_block = self.parent_bloc_rc();
parent_block.position() + self.position_in_block()
}
pub fn end(&self) -> usize {
self.start() + self.text_length()
}
}
impl ElementTrait for Text {
fn set_uuid(&self, uuid: usize) {
self.uuid.set(uuid);
}
fn verify_rule_with_parent(&self, parent_element: &Element) -> Result<(), ModelError> {
match parent_element {
Element::FrameElement(_) => Err(ModelError::WrongParent),
Element::BlockElement(_) => Ok(()),
Element::TextElement(_) => Err(ModelError::WrongParent),
Element::ImageElement(_) => Err(ModelError::WrongParent),
}
}
}
impl FormattedElement<CharFormat> for Text {
fn format(&self) -> CharFormat {
self.char_format.borrow().clone()
}
fn set_format(&self, format: &CharFormat) -> FormatChangeResult {
if &*self.char_format.borrow() == format {
Ok(None)
} else {
self.char_format.replace(format.clone());
Ok(Some(()))
}
}
fn merge_format(&self, format: &CharFormat) -> FormatChangeResult {
self.char_format.borrow_mut().merge_with(format)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remove_text() {
let text = Text::new(Weak::new());
text.set_text("plain_text");
text.remove_text(0, 10).unwrap();
assert_eq!(text.plain_text(), "");
text.set_text("plain_text");
text.remove_text(1, 9).unwrap();
assert_eq!(text.plain_text(), "pt");
}
}