mirror of
https://github.com/loro-dev/loro.git
synced 2024-11-24 12:20:06 +00:00
style: refine a few impl (#454)
This commit is contained in:
parent
7be915e87d
commit
ab6ee76b1e
4 changed files with 115 additions and 3 deletions
|
@ -182,6 +182,12 @@ impl std::fmt::Display for Index {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<usize> for Index {
|
||||
fn from(s: usize) -> Self {
|
||||
Index::Seq(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Index {
|
||||
type Error = &'static str;
|
||||
fn try_from(s: &str) -> Result<Self, &'static str> {
|
||||
|
|
|
@ -1908,6 +1908,25 @@ pub enum ValueOrContainer {
|
|||
Container(Container),
|
||||
}
|
||||
|
||||
impl ValueOrContainer {
|
||||
/// Get the deep value of the value or container.
|
||||
pub fn get_deep_value(&self) -> LoroValue {
|
||||
match self {
|
||||
ValueOrContainer::Value(v) => v.clone(),
|
||||
ValueOrContainer::Container(c) => match c {
|
||||
Container::List(c) => c.get_deep_value(),
|
||||
Container::Map(c) => c.get_deep_value(),
|
||||
Container::Text(c) => c.to_string().into(),
|
||||
Container::Tree(c) => c.get_value(),
|
||||
Container::MovableList(c) => c.get_deep_value(),
|
||||
#[cfg(feature = "counter")]
|
||||
Container::Counter(c) => c.get_value(),
|
||||
Container::Unknown(_) => LoroValue::Null,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// UndoManager can be used to undo and redo the changes made to the document with a certain peer.
|
||||
#[derive(Debug)]
|
||||
#[repr(transparent)]
|
||||
|
|
|
@ -4,8 +4,8 @@ use std::{
|
|||
};
|
||||
|
||||
use loro::{
|
||||
awareness::Awareness, FrontiersNotIncluded, LoroDoc, LoroError, LoroList, LoroMap, LoroText,
|
||||
ToJson,
|
||||
awareness::Awareness, loro_value, FrontiersNotIncluded, Index, LoroDoc, LoroError, LoroList,
|
||||
LoroMap, LoroText, LoroValue, ToJson,
|
||||
};
|
||||
use loro_internal::{handler::TextDelta, id::ID, vv, LoroResult};
|
||||
use serde_json::json;
|
||||
|
@ -890,3 +890,28 @@ fn test_tree_move() {
|
|||
tree.mov_after(node2, node1).unwrap();
|
||||
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn richtext_map_value() {
|
||||
let doc = LoroDoc::new();
|
||||
let text = doc.get_text("text");
|
||||
text.insert(0, "Hello").unwrap();
|
||||
text.mark(0..2, "comment", loro_value!({"b": {}})).unwrap();
|
||||
let delta = text.to_delta();
|
||||
assert_eq!(
|
||||
delta,
|
||||
loro_value!([
|
||||
{
|
||||
"insert": "He",
|
||||
"attributes": {
|
||||
"comment": {
|
||||
"b": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"insert": "llo",
|
||||
}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { describe, expect, expectTypeOf, it } from "vitest";
|
||||
import {
|
||||
Container,
|
||||
Diff,
|
||||
getType,
|
||||
isContainer,
|
||||
Loro,
|
||||
|
@ -8,6 +9,8 @@ import {
|
|||
LoroMap,
|
||||
LoroText,
|
||||
LoroTree,
|
||||
MapDiff,
|
||||
TextDiff,
|
||||
} from "../src";
|
||||
|
||||
it("basic example", () => {
|
||||
|
@ -92,7 +95,7 @@ it("basic sync example", () => {
|
|||
|
||||
it("basic events", () => {
|
||||
const doc = new Loro();
|
||||
doc.subscribe((event) => {});
|
||||
doc.subscribe((event) => { });
|
||||
const list = doc.getList("list");
|
||||
});
|
||||
|
||||
|
@ -475,3 +478,62 @@ it("fork", () => {
|
|||
doc.import(doc2.exportSnapshot());
|
||||
expect(doc.toJSON()).toStrictEqual({ map: { key: 2 } });
|
||||
});
|
||||
|
||||
it("has correct map value #453", async () => {
|
||||
{
|
||||
const doc = new Loro();
|
||||
const text = doc.getText("text");
|
||||
text.insert(0, "Hello");
|
||||
text.mark({ start: 0, end: 2 }, "bold", { b: {} });
|
||||
expect(text.toDelta()).toStrictEqual([
|
||||
{ insert: "He", attributes: { bold: { b: {} } } },
|
||||
{ insert: "llo" }
|
||||
]);
|
||||
let diff: Diff | undefined;
|
||||
let expectedDiff: TextDiff = {
|
||||
"type": "text",
|
||||
"diff": [
|
||||
{ insert: "He", attributes: { bold: { b: {} } } },
|
||||
{ insert: "llo" }
|
||||
]
|
||||
};
|
||||
doc.subscribe(e => {
|
||||
console.log("Text", e);
|
||||
diff = e.events[0].diff;
|
||||
})
|
||||
doc.commit();
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
expect(diff).toStrictEqual(expectedDiff);
|
||||
}
|
||||
{
|
||||
const map = new LoroMap();
|
||||
map.set('a', { b: {} });
|
||||
expect(map.toJSON()).toStrictEqual({ a: { b: {} } });
|
||||
}
|
||||
{
|
||||
const doc = new Loro();
|
||||
const map = doc.getMap("map");
|
||||
map.set('a', { b: {} });
|
||||
expect(map.toJSON()).toStrictEqual({ a: { b: {} } });
|
||||
}
|
||||
{
|
||||
const doc = new Loro();
|
||||
let diff: Diff | undefined;
|
||||
const expectedDiff: MapDiff = {
|
||||
"type": "map",
|
||||
"updated": {
|
||||
"a": {
|
||||
"b": {}
|
||||
}
|
||||
}
|
||||
};
|
||||
doc.subscribe(e => {
|
||||
diff = e.events[0].diff;
|
||||
})
|
||||
const map = doc.getMap("map");
|
||||
map.set('a', { b: {} });
|
||||
doc.commit();
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
expect(diff).toStrictEqual(expectedDiff);
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue