mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 12:57:20 +00:00
feat: add push/push_container to LoroText/LoroList/LoroMovableList (#534)
* feat: add push_container and push_str to list/text * chore: add changeset file
This commit is contained in:
parent
f78e1da8a0
commit
7bf6db7398
5 changed files with 68 additions and 0 deletions
5
.changeset/hip-pears-accept.md
Normal file
5
.changeset/hip-pears-accept.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"loro-crdt": patch
|
||||
---
|
||||
|
||||
Add `push` to LoroText and `pushContainer` to LoroList LoroMovableList
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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([{}]);
|
||||
})
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue