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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use std::{fmt::Display, sync::Arc};
use loro::TextDelta as InternalTextDelta;
use loro::{
cursor::{PosType, Side},
ContainerTrait, LoroResult, PeerID, UpdateOptions, UpdateTimeoutError,
};
use crate::{
ContainerID, DiffEvent, LoroDoc, LoroValue, LoroValueLike, Subscriber, Subscription, TextDelta,
};
use super::Cursor;
#[derive(Debug, Clone)]
pub struct LoroText {
pub(crate) inner: loro::LoroText,
}
impl LoroText {
/// Create a new container that is detached from the document.
///
/// The edits on a detached container will not be persisted.
/// To attach the container to the document, please insert it into an attached container.
pub fn new() -> Self {
Self {
inner: loro::LoroText::new(),
}
}
/// Whether the container is attached to a document
///
/// The edits on a detached container will not be persisted.
/// To attach the container to the document, please insert it into an attached container.
pub fn is_attached(&self) -> bool {
self.inner.is_attached()
}
/// If a detached container is attached, this method will return its corresponding attached handler.
pub fn get_attached(&self) -> Option<Arc<LoroText>> {
self.inner
.get_attached()
.map(|x| Arc::new(LoroText { inner: x }))
}
/// Get the [ContainerID] of the text container.
pub fn id(&self) -> ContainerID {
self.inner.id().into()
}
/// Iterate each span(internal storage unit) of the text.
///
/// The callback function will be called for each character in the text.
/// If the callback returns `false`, the iteration will stop.
// TODO:
pub fn iter(&self, callback: impl FnMut(&str) -> bool) {
self.inner.iter(callback);
}
/// Insert a string at the given unicode position.
pub fn insert(&self, pos: u32, s: &str) -> LoroResult<()> {
self.inner.insert(pos as usize, s)
}
/// Insert a string at the given utf-8 position.
pub fn insert_utf8(&self, pos: u32, s: &str) -> LoroResult<()> {
self.inner.insert_utf8(pos as usize, s)
}
/// Insert a string at the given utf-16 position.
pub fn insert_utf16(&self, pos: u32, s: &str) -> LoroResult<()> {
self.inner.insert_utf16(pos as usize, s)
}
/// Delete a range of text at the given unicode position with unicode length.
pub fn delete(&self, pos: u32, len: u32) -> LoroResult<()> {
self.inner.delete(pos as usize, len as usize)
}
/// Delete a range of text at the given utf-8 position with utf-8 length.
pub fn delete_utf8(&self, pos: u32, len: u32) -> LoroResult<()> {
self.inner.delete_utf8(pos as usize, len as usize)
}
/// Delete a range of text at the given utf-16 position with utf-16 length.
pub fn delete_utf16(&self, pos: u32, len: u32) -> LoroResult<()> {
self.inner.delete_utf16(pos as usize, len as usize)
}
/// Get a string slice at the given Unicode range
pub fn slice(&self, start_index: u32, end_index: u32) -> LoroResult<String> {
self.inner.slice(start_index as usize, end_index as usize)
}
/// Get a string slice at the given UTF-16 range
pub fn slice_utf16(&self, start_index: u32, end_index: u32) -> LoroResult<String> {
self.inner
.slice_utf16(start_index as usize, end_index as usize)
}
pub fn slice_delta(
&self,
start_index: u32,
end_index: u32,
pos_type: PosType,
) -> LoroResult<Vec<TextDelta>> {
self.inner
.slice_delta(start_index as usize, end_index as usize, pos_type)
.map(|d| d.into_iter().map(|x| x.into()).collect())
}
/// Get the characters at given unicode position.
pub fn char_at(&self, pos: u32) -> LoroResult<String> {
self.inner.char_at(pos as usize).map(|c| c.to_string())
}
/// Delete specified character and insert string at the same position at given unicode position.
pub fn splice(&self, pos: u32, len: u32, s: &str) -> LoroResult<String> {
self.inner.splice(pos as usize, len as usize, s)
}
/// Delete specified range and insert a string at the same UTF-16 position.
pub fn splice_utf16(&self, pos: u32, len: u32, s: &str) -> LoroResult<()> {
self.inner.splice_utf16(pos as usize, len as usize, s)
}
/// Whether the text container is empty.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Get the length of the text container in UTF-8.
pub fn len_utf8(&self) -> u32 {
self.inner.len_utf8() as u32
}
/// Get the length of the text container in Unicode.
pub fn len_unicode(&self) -> u32 {
self.inner.len_unicode() as u32
}
/// Get the length of the text container in UTF-16.
pub fn len_utf16(&self) -> u32 {
self.inner.len_utf16() as u32
}
/// Update the current text based on the provided text.
pub fn update(&self, text: &str, options: UpdateOptions) -> Result<(), UpdateTimeoutError> {
self.inner.update(text, options)
}
/// Apply a [delta](https://quilljs.com/docs/delta/) to the text container.
pub fn apply_delta(&self, delta: Vec<TextDelta>) -> LoroResult<()> {
let internal_delta: Vec<InternalTextDelta> = delta.into_iter().map(|d| d.into()).collect();
self.inner.apply_delta(&internal_delta)
}
/// Mark a range of text with a key-value pair.
///
/// You can use it to create a highlight, make a range of text bold, or add a link to a range of text.
///
/// You can specify the `expand` option to set the behavior when inserting text at the boundary of the range.
///
/// - `after`(default): when inserting text right after the given range, the mark will be expanded to include the inserted text
/// - `before`: when inserting text right before the given range, the mark will be expanded to include the inserted text
/// - `none`: the mark will not be expanded to include the inserted text at the boundaries
/// - `both`: when inserting text either right before or right after the given range, the mark will be expanded to include the inserted text
///
/// *You should make sure that a key is always associated with the same expand type.*
///
/// Note: this is not suitable for unmergeable annotations like comments.
pub fn mark(
&self,
from: u32,
to: u32,
key: &str,
value: Arc<dyn LoroValueLike>,
) -> LoroResult<()> {
self.inner
.mark(from as usize..to as usize, key, value.as_loro_value())
}
pub fn mark_utf8(
&self,
from: u32,
to: u32,
key: &str,
value: Arc<dyn LoroValueLike>,
) -> LoroResult<()> {
self.inner
.mark_utf8(from as usize..to as usize, key, value.as_loro_value())
}
pub fn mark_utf16(
&self,
from: u32,
to: u32,
key: &str,
value: Arc<dyn LoroValueLike>,
) -> LoroResult<()> {
self.inner
.mark_utf16(from as usize..to as usize, key, value.as_loro_value())
}
/// Unmark a range of text with a key and a value.
///
/// You can use it to remove highlights, bolds or links
///
/// You can specify the `expand` option to set the behavior when inserting text at the boundary of the range.
///
/// **Note: You should specify the same expand type as when you mark the text.**
///
/// - `after`(default): when inserting text right after the given range, the mark will be expanded to include the inserted text
/// - `before`: when inserting text right before the given range, the mark will be expanded to include the inserted text
/// - `none`: the mark will not be expanded to include the inserted text at the boundaries
/// - `both`: when inserting text either right before or right after the given range, the mark will be expanded to include the inserted text
///
/// *You should make sure that a key is always associated with the same expand type.*
///
/// Note: you cannot delete unmergeable annotations like comments by this method.
pub fn unmark(&self, from: u32, to: u32, key: &str) -> LoroResult<()> {
self.inner.unmark(from as usize..to as usize, key)
}
pub fn unmark_utf16(&self, from: u32, to: u32, key: &str) -> LoroResult<()> {
self.inner.unmark_utf16(from as usize..to as usize, key)
}
/// Get the text in [Delta](https://quilljs.com/docs/delta/) format.
///
/// # Example
/// ```
/// use loro::{LoroDoc, ToJson, ExpandType, TextDelta};
/// use serde_json::json;
/// use std::collections::HashMap;
///
/// let doc = LoroDoc::new();
/// let text = doc.get_text("text");
/// text.insert(0, "Hello world!").unwrap();
/// text.mark(0..5, "bold", true).unwrap();
/// assert_eq!(
/// text.to_delta(),
/// vec![
/// TextDelta::Insert {
/// insert: "Hello".to_string(),
/// attributes: Some(HashMap::from_iter([("bold".to_string(), true.into())])),
/// },
/// TextDelta::Insert {
/// insert: " world!".to_string(),
/// attributes: None,
/// },
/// ]
/// );
/// text.unmark(3..5, "bold").unwrap();
/// assert_eq!(
/// text.to_delta(),
/// vec![
/// TextDelta::Insert {
/// insert: "Hel".to_string(),
/// attributes: Some(HashMap::from_iter([("bold".to_string(), true.into())])),
/// },
/// TextDelta::Insert {
/// insert: "lo world!".to_string(),
/// attributes: None,
/// },
/// ]
/// );
/// ```
pub fn to_delta(&self) -> Vec<TextDelta> {
self.inner
.to_delta()
.into_iter()
.map(|d| d.into())
.collect()
}
/// Get the text in [Delta](https://quilljs.com/docs/delta/) format.
///
/// # Example
/// ```
/// # use loro::{LoroDoc, ToJson, ExpandType};
/// # use serde_json::json;
///
/// let doc = LoroDoc::new();
/// let text = doc.get_text("text");
/// text.insert(0, "Hello world!").unwrap();
/// text.mark(0..5, "bold", true).unwrap();
/// assert_eq!(
/// text.get_richtext_value().to_json_value(),
/// json!([
/// { "insert": "Hello", "attributes": {"bold": true} },
/// { "insert": " world!" },
/// ])
/// );
/// text.unmark(3..5, "bold").unwrap();
/// assert_eq!(
/// text.get_richtext_value().to_json_value(),
/// json!([
/// { "insert": "Hel", "attributes": {"bold": true} },
/// { "insert": "lo world!" },
/// ])
/// );
/// ```
pub fn get_richtext_value(&self) -> LoroValue {
self.inner.get_richtext_value().into()
}
/// Get the cursor at the given position.
///
/// Using "index" to denote cursor positions can be unstable, as positions may
/// shift with document edits. To reliably represent a position or range within
/// a document, it is more effective to leverage the unique ID of each item/character
/// in a List CRDT or Text CRDT.
///
/// Loro optimizes State metadata by not storing the IDs of deleted elements. This
/// approach complicates tracking cursors since they rely on these IDs. The solution
/// recalculates position by replaying relevant history to update stable positions
/// accurately. To minimize the performance impact of history replay, the system
/// updates cursor info to reference only the IDs of currently present elements,
/// thereby reducing the need for replay.
///
/// # Example
///
/// ```
/// # use loro::{LoroDoc, ToJson};
/// let doc = LoroDoc::new();
/// let text = &doc.get_text("text");
/// text.insert(0, "01234").unwrap();
/// let pos = text.get_cursor(5, Default::default()).unwrap();
/// assert_eq!(doc.get_cursor_pos(&pos).unwrap().current.pos, 5);
/// text.insert(0, "01234").unwrap();
/// assert_eq!(doc.get_cursor_pos(&pos).unwrap().current.pos, 10);
/// text.delete(0, 10).unwrap();
/// assert_eq!(doc.get_cursor_pos(&pos).unwrap().current.pos, 0);
/// text.insert(0, "01234").unwrap();
/// assert_eq!(doc.get_cursor_pos(&pos).unwrap().current.pos, 5);
/// ```
pub fn get_cursor(&self, pos: u32, side: Side) -> Option<Arc<Cursor>> {
self.inner
.get_cursor(pos as usize, side)
.map(|v| Arc::new(v.into()))
}
pub fn update_by_line(
&self,
text: &str,
options: UpdateOptions,
) -> Result<(), UpdateTimeoutError> {
self.inner.update_by_line(text, options)
}
pub fn is_deleted(&self) -> bool {
self.inner.is_deleted()
}
pub fn push_str(&self, s: &str) -> LoroResult<()> {
self.inner.push_str(s)
}
pub fn get_editor_at_unicode_pos(&self, pos: u32) -> Option<PeerID> {
self.inner.get_editor_at_unicode_pos(pos as usize)
}
pub fn doc(&self) -> Option<Arc<LoroDoc>> {
self.inner.doc().map(|x| Arc::new(LoroDoc { doc: x }))
}
pub fn convert_pos(&self, index: u32, from: PosType, to: PosType) -> Option<u32> {
self.inner
.convert_pos(index as usize, from, to)
.map(|v| v as u32)
}
pub fn subscribe(&self, subscriber: Arc<dyn Subscriber>) -> Option<Arc<Subscription>> {
self.inner
.subscribe(Arc::new(move |e| {
subscriber.on_diff(DiffEvent::from(e));
}))
.map(|x| Arc::new(x.into()))
}
}
impl Display for LoroText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner.to_string())
}
}
impl Default for LoroText {
fn default() -> Self {
Self::new()
}
}