Skip to content

Commit

Permalink
Adjust the ffi for apply_delta to map between ffi TextDelta and inter…
Browse files Browse the repository at this point in the history
…nal TextDelta
  • Loading branch information
mwildehahn authored and Leeeon233 committed Jan 6, 2025
1 parent ca51a78 commit 32d5c9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions crates/loro-ffi/src/container/text.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fmt::Display, sync::Arc};

use loro::{cursor::Side, LoroResult, PeerID, TextDelta, UpdateOptions, UpdateTimeoutError};
use loro::{cursor::Side, LoroResult, PeerID, UpdateOptions, UpdateTimeoutError};
use loro_internal::handler::TextDelta as InternalTextDelta;

use crate::{ContainerID, LoroValue, LoroValueLike};
use crate::{ContainerID, LoroValue, LoroValueLike, TextDelta};

use super::Cursor;

Expand Down Expand Up @@ -107,8 +108,9 @@ impl LoroText {

/// Apply a [delta](https://quilljs.com/docs/delta/) to the text container.
// TODO:
pub fn apply_delta(&self, delta: &[TextDelta]) -> LoroResult<()> {
self.text.apply_delta(delta)
pub fn apply_delta(&self, delta: Vec<TextDelta>) -> LoroResult<()> {
let internal_delta: Vec<InternalTextDelta> = delta.into_iter().map(|d| d.into()).collect();
self.text.apply_delta(&internal_delta)
}

/// Mark a range of text with a key-value pair.
Expand Down
26 changes: 26 additions & 0 deletions crates/loro-ffi/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ pub enum TextDelta {
},
}

impl From<TextDelta> for loro_internal::handler::TextDelta {
fn from(value: TextDelta) -> Self {
match value {
TextDelta::Retain { retain, attributes } => loro_internal::handler::TextDelta::Retain {
retain: retain as usize,
attributes: attributes.as_ref().map(|a| {
a.iter()
.map(|(k, v)| (k.to_string(), v.clone().into()))
.collect()
}),
},
TextDelta::Insert { insert, attributes } => loro_internal::handler::TextDelta::Insert {
insert,
attributes: attributes.as_ref().map(|a| {
a.iter()
.map(|(k, v)| (k.to_string(), v.clone().into()))
.collect()
}),
},
TextDelta::Delete { delete } => loro_internal::handler::TextDelta::Delete {
delete: delete as usize,
},
}
}
}

pub enum ListDiffItem {
/// Insert a new element into the list.
Insert {
Expand Down

0 comments on commit 32d5c9b

Please sign in to comment.