use webassembly with the server acting only as an intermediate
This commit is contained in:
parent
9aa1130c35
commit
7d0ef62c42
29 changed files with 850 additions and 325 deletions
13
dist_text_js/Cargo.toml
Normal file
13
dist_text_js/Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "dist_text_js"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
dist_text = { path = "../dist_text" }
|
||||
serde = { version = "1.0.153", features = ["derive"] }
|
||||
serde-wasm-bindgen = "0.5.0"
|
||||
wasm-bindgen = "0.2.84"
|
38
dist_text_js/src/lib.rs
Normal file
38
dist_text_js/src/lib.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use wasm_bindgen::prelude::*;
|
||||
use dist_text::crdts::list::Op;
|
||||
use dist_text::text::Text;
|
||||
|
||||
#[wasm_bindgen(getter_with_clone)]
|
||||
pub struct State {
|
||||
pub text: String,
|
||||
|
||||
#[wasm_bindgen(skip)]
|
||||
pub inner: Text,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl State {
|
||||
// wasm does not support trait impls yet, hence the default implementation here
|
||||
pub fn default() -> Self {
|
||||
Self { text: String::default(), inner: Text::default() }
|
||||
}
|
||||
|
||||
pub fn apply(&mut self, ops: JsValue) -> String {
|
||||
let ops: Vec<Op<u16, String>> = serde_wasm_bindgen::from_value(ops).unwrap_or(Vec::new());
|
||||
self.inner.apply_ops(ops);
|
||||
|
||||
self.inner.to_string()
|
||||
}
|
||||
|
||||
pub fn execute(&mut self, action: &str, start: usize, end: usize, data: &str, src: &str) -> JsValue {
|
||||
let ops = match action {
|
||||
"insertText" | "insertFromPaste" => self.inner.insert(start, end, data, src),
|
||||
"insertLineBreak" => self.inner.insert_linebreak(start, end, src),
|
||||
"deleteContentBackward" => self.inner.delete_backward(start, end, src),
|
||||
"deleteContentForward" => self.inner.delete_forward(start, end, src),
|
||||
&_ => Vec::default()
|
||||
};
|
||||
|
||||
serde_wasm_bindgen::to_value(&ops).unwrap_or_default()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue