Make JS API usable

This commit is contained in:
Andras Schmelczer 2025-04-02 21:07:33 +01:00
parent 565372b526
commit 40323c33ee
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 54 additions and 55 deletions

View file

@ -1,26 +1,29 @@
use reconcile::{CursorPosition, TextWithCursors};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
/// Wrapper type to expose `TextWithCursors` to JS. /// Wrapper type to expose `TextWithCursors` to JS.
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct OwnedTextWithCursors { pub struct TextWithCursors {
text: String, text: String,
cursors: Vec<OwnedCursorPosition>, cursors: Vec<CursorPosition>,
} }
impl OwnedTextWithCursors { #[wasm_bindgen]
pub fn new(text: impl Into<String>, cursors: Vec<OwnedCursorPosition>) -> Self { impl TextWithCursors {
Self { #[wasm_bindgen(constructor)]
text: text.into(), #[must_use]
cursors, 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<'_> { impl From<TextWithCursors> for reconcile::TextWithCursors<'_> {
fn from(owned: OwnedTextWithCursors) -> Self { fn from(owned: TextWithCursors) -> Self {
TextWithCursors::new_owned( reconcile::TextWithCursors::new_owned(
owned.text.to_string(), owned.text.to_string(),
owned owned
.cursors .cursors
@ -31,9 +34,9 @@ impl From<OwnedTextWithCursors> for TextWithCursors<'_> {
} }
} }
impl From<TextWithCursors<'_>> for OwnedTextWithCursors { impl From<reconcile::TextWithCursors<'_>> for TextWithCursors {
fn from(text_with_cursors: TextWithCursors<'_>) -> Self { fn from(text_with_cursors: reconcile::TextWithCursors<'_>) -> Self {
OwnedTextWithCursors { TextWithCursors {
text: text_with_cursors.text.into_owned(), text: text_with_cursors.text.into_owned(),
cursors: text_with_cursors cursors: text_with_cursors
.cursors .cursors
@ -47,23 +50,37 @@ impl From<TextWithCursors<'_>> for OwnedTextWithCursors {
/// Wrapper type to expose `CursorPosition` to JS. /// Wrapper type to expose `CursorPosition` to JS.
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct OwnedCursorPosition { pub struct CursorPosition {
pub id: usize, id: usize,
pub char_index: usize, char_index: usize,
} }
impl From<OwnedCursorPosition> for CursorPosition { #[wasm_bindgen]
fn from(owned: OwnedCursorPosition) -> Self { impl CursorPosition {
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, id: owned.id,
char_index: owned.char_index, char_index: owned.char_index,
} }
} }
} }
impl From<CursorPosition> for OwnedCursorPosition { impl From<reconcile::CursorPosition> for CursorPosition {
fn from(cursor: CursorPosition) -> Self { fn from(cursor: reconcile::CursorPosition) -> Self {
OwnedCursorPosition { CursorPosition {
id: cursor.id, id: cursor.id,
char_index: cursor.char_index, char_index: cursor.char_index,
} }

View file

@ -12,7 +12,7 @@
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 cursor::TextWithCursors;
use errors::SyncLibError; use errors::SyncLibError;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -110,9 +110,9 @@ pub fn merge_text(parent: &str, left: &str, right: &str) -> String {
#[must_use] #[must_use]
pub fn merge_text_with_cursors( pub fn merge_text_with_cursors(
parent: &str, parent: &str,
left: OwnedTextWithCursors, left: TextWithCursors,
right: OwnedTextWithCursors, right: TextWithCursors,
) -> OwnedTextWithCursors { ) -> TextWithCursors {
set_panic_hook(); set_panic_hook();
reconcile::reconcile_with_cursors(parent, left.into(), right.into()).into() reconcile::reconcile_with_cursors(parent, left.into(), right.into()).into()

View file

@ -1,6 +1,6 @@
use insta::assert_debug_snapshot; use insta::assert_debug_snapshot;
use sync_lib::{ use sync_lib::{
cursor::{OwnedCursorPosition, OwnedTextWithCursors}, cursor::{CursorPosition, TextWithCursors},
*, *,
}; };
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
@ -50,36 +50,18 @@ fn test_merge_text() {
fn test_merge_text_with_cursors() { fn test_merge_text_with_cursors() {
let result = merge_text_with_cursors( let result = merge_text_with_cursors(
"hi", "hi",
OwnedTextWithCursors::new("hi world", vec![]), TextWithCursors::new("hi world".to_owned(), vec![]),
OwnedTextWithCursors::new( TextWithCursors::new(
"hi", "hi".to_owned(),
vec![ vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)],
OwnedCursorPosition {
id: 0,
char_index: 1,
},
OwnedCursorPosition {
id: 1,
char_index: 2,
},
],
), ),
); );
assert_eq!( assert_eq!(
result, result,
OwnedTextWithCursors::new( TextWithCursors::new(
"hi world", "hi world".to_owned(),
vec![ vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)]
OwnedCursorPosition {
id: 0,
char_index: 1,
},
OwnedCursorPosition {
id: 1,
char_index: 2,
}
]
), ),
); );
} }