pub struct LoroText { /* private fields */ }
Expand description
LoroText container. It’s used to model plaintext/richtext.
Implementations§
source§impl LoroText
impl LoroText
sourcepub fn new() -> Self
pub fn new() -> Self
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.
sourcepub fn is_attached(&self) -> bool
pub fn is_attached(&self) -> bool
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.
sourcepub fn id(&self) -> ContainerID
pub fn id(&self) -> ContainerID
Get the ContainerID of the text container.
sourcepub fn insert(&self, pos: usize, s: &str) -> LoroResult<()>
pub fn insert(&self, pos: usize, s: &str) -> LoroResult<()>
Insert a string at the given unicode position.
sourcepub fn delete(&self, pos: usize, len: usize) -> LoroResult<()>
pub fn delete(&self, pos: usize, len: usize) -> LoroResult<()>
Delete a range of text at the given unicode position with unicode length.
sourcepub fn len_unicode(&self) -> usize
pub fn len_unicode(&self) -> usize
Get the length of the text container in Unicode.
sourcepub fn apply_delta(&self, delta: &[TextDelta]) -> LoroResult<()>
pub fn apply_delta(&self, delta: &[TextDelta]) -> LoroResult<()>
Apply a delta to the text container.
sourcepub fn mark(
&self,
range: Range<usize>,
key: &str,
value: impl Into<LoroValue>
) -> LoroResult<()>
pub fn mark( &self, range: Range<usize>, key: &str, value: impl Into<LoroValue> ) -> LoroResult<()>
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 textbefore
: when inserting text right before the given range, the mark will be expanded to include the inserted textnone
: the mark will not be expanded to include the inserted text at the boundariesboth
: 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.
sourcepub fn unmark(&self, range: Range<usize>, key: &str) -> LoroResult<()>
pub fn unmark(&self, range: Range<usize>, key: &str) -> LoroResult<()>
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 textbefore
: when inserting text right before the given range, the mark will be expanded to include the inserted textnone
: the mark will not be expanded to include the inserted text at the boundariesboth
: 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.
sourcepub fn to_delta(&self) -> LoroValue
pub fn to_delta(&self) -> LoroValue
Get the text in Delta format.
§Example
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().to_json_value(),
json!([
{ "insert": "Hello", "attributes": {"bold": true} },
{ "insert": " world!" },
])
);
text.unmark(3..5, "bold").unwrap();
assert_eq!(
text.to_delta().to_json_value(),
json!([
{ "insert": "Hel", "attributes": {"bold": true} },
{ "insert": "lo world!" },
])
);
sourcepub fn get_cursor(&self, pos: usize, side: Side) -> Option<Cursor>
pub fn get_cursor(&self, pos: usize, side: Side) -> Option<Cursor>
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
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);
Trait Implementations§
source§impl ContainerTrait for LoroText
impl ContainerTrait for LoroText
§type Handler = TextHandler
type Handler = TextHandler
source§fn to_container(&self) -> Container
fn to_container(&self) -> Container
source§fn to_handler(&self) -> Self::Handler
fn to_handler(&self) -> Self::Handler
source§fn from_handler(handler: Self::Handler) -> Self
fn from_handler(handler: Self::Handler) -> Self
source§fn is_attached(&self) -> bool
fn is_attached(&self) -> bool
source§fn get_attached(&self) -> Option<Self>
fn get_attached(&self) -> Option<Self>
source§fn try_from_container(container: Container) -> Option<Self>
fn try_from_container(container: Container) -> Option<Self>
Auto Trait Implementations§
impl Freeze for LoroText
impl RefUnwindSafe for LoroText
impl Send for LoroText
impl Sync for LoroText
impl Unpin for LoroText
impl UnwindSafe for LoroText
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more