From ab6ee76b1e9db0313cf217a699f1f32b7599f66e Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Mon, 9 Sep 2024 15:58:00 +0800 Subject: [PATCH] style: refine a few impl (#454) --- crates/loro-internal/src/event.rs | 6 +++ crates/loro/src/lib.rs | 19 +++++++++ crates/loro/tests/loro_rust_test.rs | 29 ++++++++++++- loro-js/tests/basic.test.ts | 64 ++++++++++++++++++++++++++++- 4 files changed, 115 insertions(+), 3 deletions(-) diff --git a/crates/loro-internal/src/event.rs b/crates/loro-internal/src/event.rs index 556408e3..097ab1c7 100644 --- a/crates/loro-internal/src/event.rs +++ b/crates/loro-internal/src/event.rs @@ -182,6 +182,12 @@ impl std::fmt::Display for Index { } } +impl From 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 { diff --git a/crates/loro/src/lib.rs b/crates/loro/src/lib.rs index a49040c4..b1dc2bc8 100644 --- a/crates/loro/src/lib.rs +++ b/crates/loro/src/lib.rs @@ -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)] diff --git a/crates/loro/tests/loro_rust_test.rs b/crates/loro/tests/loro_rust_test.rs index d9b36a46..0ce5d22b 100644 --- a/crates/loro/tests/loro_rust_test.rs +++ b/crates/loro/tests/loro_rust_test.rs @@ -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", + } + ]) + ); +} diff --git a/loro-js/tests/basic.test.ts b/loro-js/tests/basic.test.ts index 03dedc01..197578d2 100644 --- a/loro-js/tests/basic.test.ts +++ b/loro-js/tests/basic.test.ts @@ -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); + } +})