feat: add push/push_container to LoroText/LoroList/LoroMovableList (#534)
Some checks are pending
Release WASM / Release (push) Waiting to run
Test All / build (push) Waiting to run

* feat: add push_container and push_str to list/text

* chore: add changeset file
This commit is contained in:
Zixuan Chen 2024-11-04 17:03:24 +08:00 committed by GitHub
parent f78e1da8a0
commit 7bf6db7398
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"loro-crdt": patch
---
Add `push` to LoroText and `pushContainer` to LoroList LoroMovableList

View file

@ -2270,6 +2270,10 @@ impl TextHandler {
MaybeDetached::Attached(a) => a.is_deleted(),
}
}
pub fn push_str(&self, s: &str) -> LoroResult<()> {
self.insert_utf8(self.len_utf8(), s)
}
}
fn event_len(s: &str) -> usize {
@ -2388,6 +2392,10 @@ impl ListHandler {
}
}
pub fn push_container<H: HandlerTrait>(&self, child: H) -> LoroResult<H> {
self.insert_container(self.len(), child)
}
pub fn insert_container_with_txn<H: HandlerTrait>(
&self,
txn: &mut Transaction,
@ -2925,6 +2933,10 @@ impl MovableListHandler {
}
}
pub fn push_container<H: HandlerTrait>(&self, child: H) -> LoroResult<H> {
self.insert_container(self.len(), child)
}
pub fn insert_container_with_txn<H: HandlerTrait>(
&self,
txn: &mut Transaction,

View file

@ -2163,6 +2163,12 @@ impl LoroText {
.get_cursor(pos, side_value)
.map(|pos| Cursor { pos })
}
/// Push a string to the end of the text.
pub fn push(&mut self, s: &str) -> JsResult<()> {
self.handler.push_str(s)?;
Ok(())
}
}
impl Default for LoroText {
@ -2676,6 +2682,11 @@ impl LoroList {
Ok(handler_to_js_value(c, self.doc.clone()).into())
}
#[wasm_bindgen(js_name = "pushContainer", skip_typescript)]
pub fn push_container(&mut self, child: JsContainer) -> JsResult<JsContainer> {
self.insert_container(self.length(), child)
}
/// Subscribe to the changes of the list.
///
/// Returns a subscription callback, which can be used to unsubscribe.
@ -2997,6 +3008,12 @@ impl LoroMovableList {
Ok(handler_to_js_value(c, self.doc.clone()).into())
}
/// Push a container to the end of the list.
#[wasm_bindgen(js_name = "pushContainer", skip_typescript)]
pub fn push_container(&mut self, child: JsContainer) -> JsResult<JsContainer> {
self.insert_container(self.length(), child)
}
/// Subscribe to the changes of the list.
///
/// Returns a subscription callback, which can be used to unsubscribe.
@ -5129,6 +5146,10 @@ interface LoroList<T = unknown> {
* ```
*/
insertContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
/**
* Push a container to the end of the list.
*/
pushContainer<C extends Container>(child: C): T extends C ? T : C;
/**
* Get the value at the index. If the value is a container, the corresponding handler will be returned.
*
@ -5201,6 +5222,10 @@ interface LoroMovableList<T = unknown> {
* ```
*/
insertContainer<C extends Container>(pos: number, child: C): T extends C ? T : C;
/**
* Push a container to the end of the list.
*/
pushContainer<C extends Container>(child: C): T extends C ? T : C;
/**
* Get the value at the index. If the value is a container, the corresponding handler will be returned.
*

View file

@ -654,3 +654,24 @@ it("json path", () => {
expect(result.length).toBe(1);
expect(result).toStrictEqual(["1984"])
})
it("can push string to text", () => {
const doc = new LoroDoc();
const text = doc.getText("text");
text.push("123");
expect(text.toString()).toBe("123");
})
it("can push container to list", () => {
const doc = new LoroDoc();
const list = doc.getList("list");
const map = list.pushContainer(new LoroMap());
expect(list.toJSON()).toStrictEqual([{}]);
})
it("can push container to movable list", () => {
const doc = new LoroDoc();
const list = doc.getMovableList("list");
const map = list.pushContainer(new LoroMap());
expect(list.toJSON()).toStrictEqual([{}]);
})

View file

@ -1566,6 +1566,11 @@ impl LoroText {
pub fn is_deleted(&self) -> bool {
self.handler.is_deleted()
}
/// Push a string to the end of the text container.
pub fn push_str(&self, s: &str) -> LoroResult<()> {
self.handler.push_str(s)
}
}
impl Default for LoroText {