Make JS API usable
This commit is contained in:
parent
565372b526
commit
40323c33ee
3 changed files with 54 additions and 55 deletions
|
|
@ -1,26 +1,29 @@
|
|||
use reconcile::{CursorPosition, TextWithCursors};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// Wrapper type to expose `TextWithCursors` to JS.
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct OwnedTextWithCursors {
|
||||
pub struct TextWithCursors {
|
||||
text: String,
|
||||
cursors: Vec<OwnedCursorPosition>,
|
||||
cursors: Vec<CursorPosition>,
|
||||
}
|
||||
|
||||
impl OwnedTextWithCursors {
|
||||
pub fn new(text: impl Into<String>, cursors: Vec<OwnedCursorPosition>) -> Self {
|
||||
Self {
|
||||
text: text.into(),
|
||||
cursors,
|
||||
}
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
impl TextWithCursors {
|
||||
#[wasm_bindgen(constructor)]
|
||||
#[must_use]
|
||||
pub fn new(text: String, cursors: Vec<CursorPosition>) -> Self { Self { text, cursors } }
|
||||
|
||||
#[must_use]
|
||||
pub fn text(&self) -> String { self.text.clone() }
|
||||
|
||||
#[must_use]
|
||||
pub fn cursors(&self) -> Vec<CursorPosition> { self.cursors.clone() }
|
||||
}
|
||||
|
||||
impl From<OwnedTextWithCursors> for TextWithCursors<'_> {
|
||||
fn from(owned: OwnedTextWithCursors) -> Self {
|
||||
TextWithCursors::new_owned(
|
||||
impl From<TextWithCursors> for reconcile::TextWithCursors<'_> {
|
||||
fn from(owned: TextWithCursors) -> Self {
|
||||
reconcile::TextWithCursors::new_owned(
|
||||
owned.text.to_string(),
|
||||
owned
|
||||
.cursors
|
||||
|
|
@ -31,9 +34,9 @@ impl From<OwnedTextWithCursors> for TextWithCursors<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<TextWithCursors<'_>> for OwnedTextWithCursors {
|
||||
fn from(text_with_cursors: TextWithCursors<'_>) -> Self {
|
||||
OwnedTextWithCursors {
|
||||
impl From<reconcile::TextWithCursors<'_>> for TextWithCursors {
|
||||
fn from(text_with_cursors: reconcile::TextWithCursors<'_>) -> Self {
|
||||
TextWithCursors {
|
||||
text: text_with_cursors.text.into_owned(),
|
||||
cursors: text_with_cursors
|
||||
.cursors
|
||||
|
|
@ -47,23 +50,37 @@ impl From<TextWithCursors<'_>> for OwnedTextWithCursors {
|
|||
/// Wrapper type to expose `CursorPosition` to JS.
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct OwnedCursorPosition {
|
||||
pub id: usize,
|
||||
pub char_index: usize,
|
||||
pub struct CursorPosition {
|
||||
id: usize,
|
||||
char_index: usize,
|
||||
}
|
||||
|
||||
impl From<OwnedCursorPosition> for CursorPosition {
|
||||
fn from(owned: OwnedCursorPosition) -> Self {
|
||||
CursorPosition {
|
||||
#[wasm_bindgen]
|
||||
impl CursorPosition {
|
||||
#[wasm_bindgen(constructor)]
|
||||
#[must_use]
|
||||
pub fn new(id: usize, char_index: usize) -> Self { Self { id, char_index } }
|
||||
|
||||
#[must_use]
|
||||
pub fn id(&self) -> usize { self.id }
|
||||
|
||||
#[wasm_bindgen(js_name = characterPosition)]
|
||||
#[must_use]
|
||||
pub fn char_index(&self) -> usize { self.char_index }
|
||||
}
|
||||
|
||||
impl From<CursorPosition> for reconcile::CursorPosition {
|
||||
fn from(owned: CursorPosition) -> Self {
|
||||
reconcile::CursorPosition {
|
||||
id: owned.id,
|
||||
char_index: owned.char_index,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CursorPosition> for OwnedCursorPosition {
|
||||
fn from(cursor: CursorPosition) -> Self {
|
||||
OwnedCursorPosition {
|
||||
impl From<reconcile::CursorPosition> for CursorPosition {
|
||||
fn from(cursor: reconcile::CursorPosition) -> Self {
|
||||
CursorPosition {
|
||||
id: cursor.id,
|
||||
char_index: cursor.char_index,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
use core::str;
|
||||
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use cursor::OwnedTextWithCursors;
|
||||
use cursor::TextWithCursors;
|
||||
use errors::SyncLibError;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
|
|
@ -110,9 +110,9 @@ pub fn merge_text(parent: &str, left: &str, right: &str) -> String {
|
|||
#[must_use]
|
||||
pub fn merge_text_with_cursors(
|
||||
parent: &str,
|
||||
left: OwnedTextWithCursors,
|
||||
right: OwnedTextWithCursors,
|
||||
) -> OwnedTextWithCursors {
|
||||
left: TextWithCursors,
|
||||
right: TextWithCursors,
|
||||
) -> TextWithCursors {
|
||||
set_panic_hook();
|
||||
|
||||
reconcile::reconcile_with_cursors(parent, left.into(), right.into()).into()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use insta::assert_debug_snapshot;
|
||||
use sync_lib::{
|
||||
cursor::{OwnedCursorPosition, OwnedTextWithCursors},
|
||||
cursor::{CursorPosition, TextWithCursors},
|
||||
*,
|
||||
};
|
||||
use wasm_bindgen_test::*;
|
||||
|
|
@ -50,36 +50,18 @@ fn test_merge_text() {
|
|||
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,
|
||||
},
|
||||
],
|
||||
TextWithCursors::new("hi world".to_owned(), vec![]),
|
||||
TextWithCursors::new(
|
||||
"hi".to_owned(),
|
||||
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)],
|
||||
),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
OwnedTextWithCursors::new(
|
||||
"hi world",
|
||||
vec![
|
||||
OwnedCursorPosition {
|
||||
id: 0,
|
||||
char_index: 1,
|
||||
},
|
||||
OwnedCursorPosition {
|
||||
id: 1,
|
||||
char_index: 2,
|
||||
}
|
||||
]
|
||||
TextWithCursors::new(
|
||||
"hi world".to_owned(),
|
||||
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)]
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue