docs: js container path (#550)
Some checks are pending
Release WASM / Release (push) Waiting to run
Test All / build (push) Waiting to run

This commit is contained in:
Zixuan Chen 2024-11-10 16:36:29 +08:00 committed by GitHub
parent 9abeb81747
commit b1b977cf9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 8 deletions

View file

@ -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<Option<Array>> {
pub fn get_path_to_container(&self, id: JsContainerID) -> JsResult<Option<JsContainerPath>> {
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.

View file

@ -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", () => {