From b1b977cf9e36ded62c34cf8eaac7de03f73643e8 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 10 Nov 2024 16:36:29 +0800 Subject: [PATCH] docs: js container path (#550) --- crates/loro-wasm/src/lib.rs | 15 +++++++++------ crates/loro-wasm/tests/basic.test.ts | 5 +++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/loro-wasm/src/lib.rs b/crates/loro-wasm/src/lib.rs index fc6899de..92cd53c6 100644 --- a/crates/loro-wasm/src/lib.rs +++ b/crates/loro-wasm/src/lib.rs @@ -192,6 +192,8 @@ extern "C" { pub type JsImportStatus; #[wasm_bindgen(typescript_type = "(change: ChangeMeta) => boolean")] pub type JsTravelChangeFunction; + #[wasm_bindgen(typescript_type = "(string|number)[]")] + pub type JsContainerPath; } mod observer { @@ -980,7 +982,7 @@ impl LoroDoc { /// Get the path from the root to the container #[wasm_bindgen(js_name = "getPathToContainer")] - pub fn get_path_to_container(&self, id: JsContainerID) -> JsResult> { + pub fn get_path_to_container(&self, id: JsContainerID) -> JsResult> { let id: ContainerID = id.to_owned().try_into()?; let ans = self .0 @@ -1776,12 +1778,13 @@ fn container_diff_to_js_value( obj.into() } -fn convert_container_path_to_js_value(path: &[(ContainerID, Index)]) -> Array { - let arr = Array::new_with_length(path.len() as u32); - for (i, p) in path.iter().enumerate() { - arr.set(i as u32, p.1.clone().into()); +fn convert_container_path_to_js_value(path: &[(ContainerID, Index)]) -> JsContainerPath { + let arr = Array::new(); + for p in path.iter() { + arr.push(&p.1.clone().into()); } - arr + let v: JsValue = arr.into(); + v.into() } /// The handler of a text container. It supports rich text CRDT. diff --git a/crates/loro-wasm/tests/basic.test.ts b/crates/loro-wasm/tests/basic.test.ts index cc320f2e..c12530f1 100644 --- a/crates/loro-wasm/tests/basic.test.ts +++ b/crates/loro-wasm/tests/basic.test.ts @@ -638,8 +638,9 @@ it("get path to container", () => { const doc = new LoroDoc(); const map = doc.getMap("map"); const list = map.setContainer("list", new LoroList()); - const path = doc.getPathToContainer(list.id); - expect(path).toStrictEqual(["map", "list"]) + const sub = list.insertContainer(0, new LoroMap()); + const path = doc.getPathToContainer(sub.id); + expect(path).toStrictEqual(["map", "list", 0]) }) it("json path", () => {