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::*;
/// 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,
}

View file

@ -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()