Add cursor moving #19
3 changed files with 79 additions and 5 deletions
Expose merge_text_with_cursors on API
commit
e9e2328f03
|
|
@ -1,7 +1,11 @@
|
||||||
|
mod cursor;
|
||||||
mod diffs;
|
mod diffs;
|
||||||
mod operation_transformation;
|
mod operation_transformation;
|
||||||
mod tokenizer;
|
mod tokenizer;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub use operation_transformation::{EditedText, reconcile, reconcile_with_tokenizer};
|
pub use cursor::{CursorPosition, TextWithCursors};
|
||||||
pub use tokenizer::token::Token;
|
pub use operation_transformation::{
|
||||||
|
EditedText, reconcile, reconcile_with_cursors, reconcile_with_tokenizer,
|
||||||
|
};
|
||||||
|
pub use tokenizer::{Tokenizer, token::Token};
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,15 @@
|
||||||
//! # Modules
|
//! # Modules
|
||||||
//!
|
//!
|
||||||
//! - `errors`: Contains error types used in this crate.
|
//! - `errors`: Contains error types used in this crate.
|
||||||
|
|
||||||
use core::str;
|
use core::str;
|
||||||
|
|
||||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||||
|
use cursor::OwnedTextWithCursors;
|
||||||
use errors::SyncLibError;
|
use errors::SyncLibError;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
pub mod cursor;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
|
||||||
/// Encode binary data for easy transport over HTTP. Inverse of
|
/// Encode binary data for easy transport over HTTP. Inverse of
|
||||||
|
|
@ -93,7 +96,7 @@ pub fn merge(parent: &[u8], left: &[u8], right: &[u8]) -> Vec<u8> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// WASM wrapper around `reconcile::reconcile` for text merging.
|
/// WASM wrapper around `reconcile::reconcile` for merging text.
|
||||||
#[wasm_bindgen(js_name = mergeText)]
|
#[wasm_bindgen(js_name = mergeText)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn merge_text(parent: &str, left: &str, right: &str) -> String {
|
pub fn merge_text(parent: &str, left: &str, right: &str) -> String {
|
||||||
|
|
@ -102,6 +105,19 @@ pub fn merge_text(parent: &str, left: &str, right: &str) -> String {
|
||||||
reconcile::reconcile(parent, left, right)
|
reconcile::reconcile(parent, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// WASM wrapper around `reconcile::reconcile_with_cursors` for merging text.
|
||||||
|
#[wasm_bindgen(js_name = mergeText)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn merge_text_with_cursors(
|
||||||
|
parent: &str,
|
||||||
|
left: OwnedTextWithCursors,
|
||||||
|
right: OwnedTextWithCursors,
|
||||||
|
) -> OwnedTextWithCursors {
|
||||||
|
set_panic_hook();
|
||||||
|
|
||||||
|
reconcile::reconcile_with_cursors(parent, left.into(), right.into()).into()
|
||||||
|
}
|
||||||
|
|
||||||
/// Heuristically determine if the given data is a binary or a text file's
|
/// Heuristically determine if the given data is a binary or a text file's
|
||||||
/// content.
|
/// content.
|
||||||
#[wasm_bindgen(js_name = isBinary)]
|
#[wasm_bindgen(js_name = isBinary)]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
use insta::assert_debug_snapshot;
|
use insta::assert_debug_snapshot;
|
||||||
use sync_lib::*;
|
use sync_lib::{
|
||||||
|
cursor::{OwnedCursorPosition, OwnedTextWithCursors},
|
||||||
|
*,
|
||||||
|
};
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
#[wasm_bindgen_test(unsupported = test)]
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
|
|
@ -23,11 +26,62 @@ fn test_base64_to_bytes_error() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test(unsupported = test)]
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
fn merge_text() {
|
fn test_merge() {
|
||||||
let left = b"hello ";
|
let left = b"hello ";
|
||||||
let right = b"world";
|
let right = b"world";
|
||||||
let result = merge(b"", left, right);
|
let result = merge(b"", left, right);
|
||||||
assert_eq!(result, b"hello world");
|
assert_eq!(result, b"hello world");
|
||||||
|
|
||||||
|
let left = b"\0binary";
|
||||||
|
let right = b"other";
|
||||||
|
let result = merge(b"", left, right);
|
||||||
|
assert_eq!(result, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
|
fn test_merge_text() {
|
||||||
|
let left = "hello ";
|
||||||
|
let right = "world";
|
||||||
|
let result = merge_text("", left, right);
|
||||||
|
assert_eq!(result, "hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
|
fn test_merge_text_with_cursors() {
|
||||||
|
let result = merge_text_with_cursors(
|
||||||
|
"hi",
|
||||||
|
OwnedTextWithCursors::new("hi world", vec![]),
|
||||||
|
OwnedTextWithCursors::new(
|
||||||
|
"hi",
|
||||||
|
vec![
|
||||||
|
OwnedCursorPosition {
|
||||||
|
id: 0,
|
||||||
|
char_index: 1,
|
||||||
|
},
|
||||||
|
OwnedCursorPosition {
|
||||||
|
id: 1,
|
||||||
|
char_index: 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
OwnedTextWithCursors::new(
|
||||||
|
"hi world",
|
||||||
|
vec![
|
||||||
|
OwnedCursorPosition {
|
||||||
|
id: 0,
|
||||||
|
char_index: 1,
|
||||||
|
},
|
||||||
|
OwnedCursorPosition {
|
||||||
|
id: 1,
|
||||||
|
char_index: 8,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test(unsupported = test)]
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue