mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-04-25 07:07:32 -04:00
Merge pull request #1031 from AppFlowy-IO/refactor/node_body_serde
Refactor/node body serde
This commit is contained in:
commit
1ab0a8351e
18 changed files with 930 additions and 208 deletions
1
shared-lib/Cargo.lock
generated
1
shared-lib/Cargo.lock
generated
|
@ -821,6 +821,7 @@ dependencies = [
|
||||||
"md5",
|
"md5",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serde_repr",
|
||||||
"strum",
|
"strum",
|
||||||
"strum_macros",
|
"strum_macros",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|
|
@ -10,14 +10,15 @@ bytecount = "0.6.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
#protobuf = {version = "2.18.0"}
|
#protobuf = {version = "2.18.0"}
|
||||||
#flowy-derive = { path = "../flowy-derive" }
|
#flowy-derive = { path = "../flowy-derive" }
|
||||||
tokio = {version = "1", features = ["sync"]}
|
tokio = { version = "1", features = ["sync"] }
|
||||||
dashmap = "5"
|
dashmap = "5"
|
||||||
md5 = "0.7.0"
|
md5 = "0.7.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
|
||||||
serde_json = {version = "1.0"}
|
serde_json = { version = "1.0" }
|
||||||
derive_more = {version = "0.99", features = ["display"]}
|
serde_repr = { version = "0.1" }
|
||||||
|
derive_more = { version = "0.99", features = ["display"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
tracing = { version = "0.1", features = ["log"] }
|
tracing = { version = "0.1", features = ["log"] }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
@ -29,5 +30,3 @@ indextree = "4.4.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
flowy_unit_test = []
|
flowy_unit_test = []
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::core::OperationTransform;
|
use crate::core::OperationTransform;
|
||||||
use crate::errors::OTError;
|
use crate::errors::OTError;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_repr::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
|
pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
|
||||||
|
|
||||||
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
||||||
|
@ -39,8 +39,8 @@ impl NodeAttributes {
|
||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&mut self, key: &AttributeKey) {
|
pub fn delete<K: ToString>(&mut self, key: K) {
|
||||||
self.insert(key.clone(), AttributeValue(None));
|
self.insert(key.to_string(), AttributeValue::empty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,53 +94,104 @@ impl OperationTransform for NodeAttributes {
|
||||||
|
|
||||||
pub type AttributeKey = String;
|
pub type AttributeKey = String;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Eq, PartialEq, Hash, Debug, Clone, Serialize_repr, Deserialize_repr)]
|
||||||
pub struct AttributeValue(pub Option<String>);
|
#[repr(u8)]
|
||||||
|
pub enum ValueType {
|
||||||
impl std::convert::From<&usize> for AttributeValue {
|
IntType = 0,
|
||||||
fn from(val: &usize) -> Self {
|
FloatType = 1,
|
||||||
AttributeValue::from(*val)
|
StrType = 2,
|
||||||
}
|
BoolType = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<usize> for AttributeValue {
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
fn from(val: usize) -> Self {
|
pub struct AttributeValue {
|
||||||
if val > 0_usize {
|
pub ty: ValueType,
|
||||||
AttributeValue(Some(format!("{}", val)))
|
pub value: Option<String>,
|
||||||
} else {
|
|
||||||
AttributeValue(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<&str> for AttributeValue {
|
impl AttributeValue {
|
||||||
fn from(val: &str) -> Self {
|
pub fn empty() -> Self {
|
||||||
val.to_owned().into()
|
Self {
|
||||||
|
ty: ValueType::StrType,
|
||||||
|
value: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn from_int(val: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
ty: ValueType::IntType,
|
||||||
|
value: Some(val.to_string()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl std::convert::From<String> for AttributeValue {
|
pub fn from_float(val: f64) -> Self {
|
||||||
fn from(val: String) -> Self {
|
Self {
|
||||||
if val.is_empty() {
|
ty: ValueType::FloatType,
|
||||||
AttributeValue(None)
|
value: Some(val.to_string()),
|
||||||
} else {
|
|
||||||
AttributeValue(Some(val))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl std::convert::From<&bool> for AttributeValue {
|
pub fn from_bool(val: bool) -> Self {
|
||||||
fn from(val: &bool) -> Self {
|
Self {
|
||||||
AttributeValue::from(*val)
|
ty: ValueType::BoolType,
|
||||||
|
value: Some(val.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn from_str(s: &str) -> Self {
|
||||||
|
let value = if s.is_empty() { None } else { Some(s.to_string()) };
|
||||||
|
Self {
|
||||||
|
ty: ValueType::StrType,
|
||||||
|
value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn int_value(&self) -> Option<i64> {
|
||||||
|
let value = self.value.as_ref()?;
|
||||||
|
Some(value.parse::<i64>().unwrap_or(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bool_value(&self) -> Option<bool> {
|
||||||
|
let value = self.value.as_ref()?;
|
||||||
|
Some(value.parse::<bool>().unwrap_or(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn str_value(&self) -> Option<String> {
|
||||||
|
self.value.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn float_value(&self) -> Option<f64> {
|
||||||
|
let value = self.value.as_ref()?;
|
||||||
|
Some(value.parse::<f64>().unwrap_or(0.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<bool> for AttributeValue {
|
impl std::convert::From<bool> for AttributeValue {
|
||||||
fn from(val: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
let val = match val {
|
AttributeValue::from_bool(value)
|
||||||
true => Some("true".to_owned()),
|
}
|
||||||
false => None,
|
}
|
||||||
};
|
|
||||||
AttributeValue(val)
|
pub struct NodeAttributeBuilder {
|
||||||
|
attributes: NodeAttributes,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeAttributeBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
attributes: NodeAttributes::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert<K: ToString, V: Into<AttributeValue>>(mut self, key: K, value: V) -> Self {
|
||||||
|
self.attributes.insert(key, value);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete<K: ToString>(mut self, key: K) -> Self {
|
||||||
|
self.attributes.delete(key);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> NodeAttributes {
|
||||||
|
self.attributes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
159
shared-lib/lib-ot/src/core/document/attributes_serde.rs
Normal file
159
shared-lib/lib-ot/src/core/document/attributes_serde.rs
Normal file
|
@ -0,0 +1,159 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use serde::{
|
||||||
|
de::{self, MapAccess, Visitor},
|
||||||
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::AttributeValue;
|
||||||
|
|
||||||
|
impl Serialize for AttributeValue {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
match self.ty {
|
||||||
|
super::ValueType::IntType => {
|
||||||
|
//
|
||||||
|
if let Some(value) = self.int_value() {
|
||||||
|
serializer.serialize_i64(value)
|
||||||
|
} else {
|
||||||
|
serializer.serialize_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super::ValueType::FloatType => {
|
||||||
|
if let Some(value) = self.float_value() {
|
||||||
|
serializer.serialize_f64(value)
|
||||||
|
} else {
|
||||||
|
serializer.serialize_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super::ValueType::StrType => {
|
||||||
|
if let Some(value) = self.str_value() {
|
||||||
|
serializer.serialize_str(&value)
|
||||||
|
} else {
|
||||||
|
serializer.serialize_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super::ValueType::BoolType => {
|
||||||
|
if let Some(value) = self.bool_value() {
|
||||||
|
serializer.serialize_bool(value)
|
||||||
|
} else {
|
||||||
|
serializer.serialize_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for AttributeValue {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<AttributeValue, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct AttributeValueVisitor;
|
||||||
|
impl<'de> Visitor<'de> for AttributeValueVisitor {
|
||||||
|
type Value = AttributeValue;
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("bool, usize or string")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_bool(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_int(value as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::from_str(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_none<E>(self) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(AttributeValue::empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_unit<E>(self) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
// the value that contains null will be processed here.
|
||||||
|
Ok(AttributeValue::empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
// https://github.com/serde-rs/json/issues/505
|
||||||
|
let mut map = map;
|
||||||
|
let value = map.next_value::<AttributeValue>()?;
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_any(AttributeValueVisitor)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
#![allow(clippy::module_inception)]
|
#![allow(clippy::module_inception)]
|
||||||
mod attributes;
|
mod attributes;
|
||||||
|
mod attributes_serde;
|
||||||
mod node;
|
mod node;
|
||||||
|
mod node_serde;
|
||||||
mod node_tree;
|
mod node_tree;
|
||||||
mod operation;
|
mod operation;
|
||||||
mod operation_serde;
|
mod operation_serde;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
use super::node_serde::*;
|
||||||
use crate::core::NodeBody::Delta;
|
use crate::core::NodeBody::Delta;
|
||||||
use crate::core::{AttributeKey, AttributeValue, NodeAttributes, OperationTransform, TextDelta};
|
use crate::core::{AttributeKey, AttributeValue, NodeAttributes, OperationTransform};
|
||||||
use crate::errors::OTError;
|
use crate::errors::OTError;
|
||||||
|
use crate::rich_text::RichTextDelta;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
||||||
|
@ -9,12 +11,17 @@ pub struct NodeData {
|
||||||
pub node_type: String,
|
pub node_type: String,
|
||||||
|
|
||||||
#[serde(skip_serializing_if = "NodeAttributes::is_empty")]
|
#[serde(skip_serializing_if = "NodeAttributes::is_empty")]
|
||||||
|
#[serde(default)]
|
||||||
pub attributes: NodeAttributes,
|
pub attributes: NodeAttributes,
|
||||||
|
|
||||||
|
#[serde(serialize_with = "serialize_body")]
|
||||||
|
#[serde(deserialize_with = "deserialize_body")]
|
||||||
#[serde(skip_serializing_if = "NodeBody::is_empty")]
|
#[serde(skip_serializing_if = "NodeBody::is_empty")]
|
||||||
|
#[serde(default)]
|
||||||
pub body: NodeBody,
|
pub body: NodeBody,
|
||||||
|
|
||||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
|
#[serde(default)]
|
||||||
pub children: Vec<NodeData>,
|
pub children: Vec<NodeData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,14 +32,24 @@ impl NodeData {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn split(self) -> (Node, Vec<NodeData>) {
|
||||||
|
let node = Node {
|
||||||
|
node_type: self.node_type,
|
||||||
|
body: self.body,
|
||||||
|
attributes: self.attributes,
|
||||||
|
};
|
||||||
|
|
||||||
|
(node, self.children)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for [`NodeData`]
|
/// Builder for [`NodeData`]
|
||||||
pub struct NodeBuilder {
|
pub struct NodeDataBuilder {
|
||||||
node: NodeData,
|
node: NodeData,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NodeBuilder {
|
impl NodeDataBuilder {
|
||||||
pub fn new<T: ToString>(node_type: T) -> Self {
|
pub fn new<T: ToString>(node_type: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
node: NodeData::new(node_type.to_string()),
|
node: NodeData::new(node_type.to_string()),
|
||||||
|
@ -73,10 +90,10 @@ impl NodeBuilder {
|
||||||
/// The NodeBody implements the [`OperationTransform`] trait which means it can perform
|
/// The NodeBody implements the [`OperationTransform`] trait which means it can perform
|
||||||
/// compose, transform and invert.
|
/// compose, transform and invert.
|
||||||
///
|
///
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum NodeBody {
|
pub enum NodeBody {
|
||||||
Empty,
|
Empty,
|
||||||
Delta(TextDelta),
|
Delta(RichTextDelta),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::default::Default for NodeBody {
|
impl std::default::Default for NodeBody {
|
||||||
|
@ -142,8 +159,12 @@ impl OperationTransform for NodeBody {
|
||||||
///
|
///
|
||||||
/// Each NodeBody except the Empty should have its corresponding changeset variant.
|
/// Each NodeBody except the Empty should have its corresponding changeset variant.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum NodeBodyChangeset {
|
pub enum NodeBodyChangeset {
|
||||||
Delta { delta: TextDelta, inverted: TextDelta },
|
Delta {
|
||||||
|
delta: RichTextDelta,
|
||||||
|
inverted: RichTextDelta,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NodeBodyChangeset {
|
impl NodeBodyChangeset {
|
||||||
|
@ -175,7 +196,7 @@ impl Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_body_changeset(&mut self, changeset: &NodeBodyChangeset) {
|
pub fn apply_body_changeset(&mut self, changeset: NodeBodyChangeset) {
|
||||||
match changeset {
|
match changeset {
|
||||||
NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta.clone())) {
|
NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta.clone())) {
|
||||||
Ok(new_body) => self.body = new_body,
|
Ok(new_body) => self.body = new_body,
|
||||||
|
|
75
shared-lib/lib-ot/src/core/document/node_serde.rs
Normal file
75
shared-lib/lib-ot/src/core/document/node_serde.rs
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
use super::NodeBody;
|
||||||
|
use crate::rich_text::RichTextDelta;
|
||||||
|
use serde::de::{self, MapAccess, Visitor};
|
||||||
|
use serde::ser::SerializeMap;
|
||||||
|
use serde::{Deserializer, Serializer};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub fn serialize_body<S>(body: &NodeBody, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut map = serializer.serialize_map(Some(3))?;
|
||||||
|
match body {
|
||||||
|
NodeBody::Empty => {}
|
||||||
|
NodeBody::Delta(delta) => {
|
||||||
|
map.serialize_key("delta")?;
|
||||||
|
map.serialize_value(delta)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize_body<'de, D>(deserializer: D) -> Result<NodeBody, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct NodeBodyVisitor();
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for NodeBodyVisitor {
|
||||||
|
type Value = NodeBody;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("Expect NodeBody")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: de::SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut delta = RichTextDelta::default();
|
||||||
|
while let Some(op) = seq.next_element()? {
|
||||||
|
delta.add(op);
|
||||||
|
}
|
||||||
|
Ok(NodeBody::Delta(delta))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut delta: Option<RichTextDelta> = None;
|
||||||
|
while let Some(key) = map.next_key()? {
|
||||||
|
match key {
|
||||||
|
"delta" => {
|
||||||
|
if delta.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("delta"));
|
||||||
|
}
|
||||||
|
delta = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
panic!("Unexpected key: {}", other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if delta.is_some() {
|
||||||
|
return Ok(NodeBody::Delta(delta.unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(de::Error::missing_field("delta"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deserializer.deserialize_any(NodeBodyVisitor())
|
||||||
|
}
|
|
@ -3,6 +3,8 @@ use crate::core::{Node, NodeAttributes, NodeBodyChangeset, NodeData, NodeOperati
|
||||||
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
||||||
use indextree::{Arena, Children, FollowingSiblings, NodeId};
|
use indextree::{Arena, Children, FollowingSiblings, NodeId};
|
||||||
|
|
||||||
|
use super::NodeOperationList;
|
||||||
|
|
||||||
///
|
///
|
||||||
pub struct NodeTree {
|
pub struct NodeTree {
|
||||||
arena: Arena<Node>,
|
arena: Arena<Node>,
|
||||||
|
@ -11,21 +13,42 @@ pub struct NodeTree {
|
||||||
|
|
||||||
impl Default for NodeTree {
|
impl Default for NodeTree {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new("root")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NodeTree {
|
impl NodeTree {
|
||||||
pub fn new() -> NodeTree {
|
pub fn new(root_name: &str) -> NodeTree {
|
||||||
let mut arena = Arena::new();
|
let mut arena = Arena::new();
|
||||||
let root = arena.new_node(Node::new("root"));
|
let root = arena.new_node(Node::new(root_name));
|
||||||
NodeTree { arena, root }
|
NodeTree { arena, root }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_bytes(root_name: &str, bytes: Vec<u8>) -> Result<Self, OTError> {
|
||||||
|
let operations = NodeOperationList::from_bytes(bytes)?.into_inner();
|
||||||
|
Self::from_operations(root_name, operations)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_operations(root_name: &str, operations: Vec<NodeOperation>) -> Result<Self, OTError> {
|
||||||
|
let mut node_tree = NodeTree::new(root_name);
|
||||||
|
|
||||||
|
for operation in operations {
|
||||||
|
let _ = node_tree.apply_op(operation)?;
|
||||||
|
}
|
||||||
|
Ok(node_tree)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_node(&self, node_id: NodeId) -> Option<&Node> {
|
pub fn get_node(&self, node_id: NodeId) -> Option<&Node> {
|
||||||
Some(self.arena.get(node_id)?.get())
|
Some(self.arena.get(node_id)?.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_node_at_path(&self, path: &Path) -> Option<&Node> {
|
||||||
|
{
|
||||||
|
let node_id = self.node_id_at_path(path)?;
|
||||||
|
self.get_node(node_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -35,13 +58,13 @@ impl NodeTree {
|
||||||
/// let root_path: Path = vec![0].into();
|
/// let root_path: Path = vec![0].into();
|
||||||
/// let op = NodeOperation::Insert {path: root_path.clone(),nodes };
|
/// let op = NodeOperation::Insert {path: root_path.clone(),nodes };
|
||||||
///
|
///
|
||||||
/// let mut node_tree = NodeTree::new();
|
/// let mut node_tree = NodeTree::new("root");
|
||||||
/// node_tree.apply_op(&op).unwrap();
|
/// node_tree.apply_op(op).unwrap();
|
||||||
/// let node_id = node_tree.node_at_path(&root_path).unwrap();
|
/// let node_id = node_tree.node_id_at_path(&root_path).unwrap();
|
||||||
/// let node_path = node_tree.path_of_node(node_id);
|
/// let node_path = node_tree.path_from_node_id(node_id);
|
||||||
/// debug_assert_eq!(node_path, root_path);
|
/// debug_assert_eq!(node_path, root_path);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn node_at_path<T: Into<Path>>(&self, path: T) -> Option<NodeId> {
|
pub fn node_id_at_path<T: Into<Path>>(&self, path: T) -> Option<NodeId> {
|
||||||
let path = path.into();
|
let path = path.into();
|
||||||
if path.is_empty() {
|
if path.is_empty() {
|
||||||
return Some(self.root);
|
return Some(self.root);
|
||||||
|
@ -54,7 +77,7 @@ impl NodeTree {
|
||||||
Some(iterate_node)
|
Some(iterate_node)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path_of_node(&self, node_id: NodeId) -> Path {
|
pub fn path_from_node_id(&self, node_id: NodeId) -> Path {
|
||||||
let mut path = vec![];
|
let mut path = vec![];
|
||||||
let mut current_node = node_id;
|
let mut current_node = node_id;
|
||||||
// Use .skip(1) on the ancestors iterator to skip the root node.
|
// Use .skip(1) on the ancestors iterator to skip the root node.
|
||||||
|
@ -93,15 +116,14 @@ impl NodeTree {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
|
/// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
|
||||||
/// let node = NodeData::new("text".to_string());
|
/// let node_1 = NodeData::new("text".to_string());
|
||||||
/// let inserted_path: Path = vec![0].into();
|
/// let inserted_path: Path = vec![0].into();
|
||||||
///
|
///
|
||||||
/// let mut node_tree = NodeTree::new();
|
/// let mut node_tree = NodeTree::new("root");
|
||||||
/// node_tree.apply_op(&NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node.clone()] }).unwrap();
|
/// node_tree.apply_op(NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node_1.clone()] }).unwrap();
|
||||||
///
|
///
|
||||||
/// let inserted_note = node_tree.node_at_path(&inserted_path).unwrap();
|
/// let node_2 = node_tree.get_node_at_path(&inserted_path).unwrap();
|
||||||
/// let inserted_data = node_tree.get_node(inserted_note).unwrap();
|
/// assert_eq!(node_2.node_type, node_1.node_type);
|
||||||
/// assert_eq!(inserted_data.node_type, node.node_type);
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn child_from_node_at_index(&self, node_id: NodeId, index: usize) -> Option<NodeId> {
|
pub fn child_from_node_at_index(&self, node_id: NodeId, index: usize) -> Option<NodeId> {
|
||||||
let children = node_id.children(&self.arena);
|
let children = node_id.children(&self.arena);
|
||||||
|
@ -137,25 +159,26 @@ impl NodeTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply(&mut self, transaction: Transaction) -> Result<(), OTError> {
|
pub fn apply(&mut self, transaction: Transaction) -> Result<(), OTError> {
|
||||||
for op in &transaction.operations {
|
let operations = transaction.into_operations();
|
||||||
self.apply_op(op)?;
|
for operation in operations {
|
||||||
|
self.apply_op(operation)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_op(&mut self, op: &NodeOperation) -> Result<(), OTError> {
|
pub fn apply_op(&mut self, op: NodeOperation) -> Result<(), OTError> {
|
||||||
match op {
|
match op {
|
||||||
NodeOperation::Insert { path, nodes } => self.insert_nodes(path, nodes),
|
NodeOperation::Insert { path, nodes } => self.insert_nodes(&path, nodes),
|
||||||
NodeOperation::UpdateAttributes { path, attributes, .. } => self.update_attributes(path, attributes),
|
NodeOperation::UpdateAttributes { path, attributes, .. } => self.update_attributes(&path, attributes),
|
||||||
NodeOperation::UpdateBody { path, changeset } => self.update_body(path, changeset),
|
NodeOperation::UpdateBody { path, changeset } => self.update_body(&path, changeset),
|
||||||
NodeOperation::Delete { path, nodes } => self.delete_node(path, nodes),
|
NodeOperation::Delete { path, nodes } => self.delete_node(&path, nodes),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Inserts nodes at given path
|
/// Inserts nodes at given path
|
||||||
///
|
///
|
||||||
/// returns error if the path is empty
|
/// returns error if the path is empty
|
||||||
///
|
///
|
||||||
fn insert_nodes(&mut self, path: &Path, nodes: &[NodeData]) -> Result<(), OTError> {
|
fn insert_nodes(&mut self, path: &Path, nodes: Vec<NodeData>) -> Result<(), OTError> {
|
||||||
debug_assert!(!path.is_empty());
|
debug_assert!(!path.is_empty());
|
||||||
if path.is_empty() {
|
if path.is_empty() {
|
||||||
return Err(OTErrorCode::PathIsEmpty.into());
|
return Err(OTErrorCode::PathIsEmpty.into());
|
||||||
|
@ -164,7 +187,7 @@ impl NodeTree {
|
||||||
let (parent_path, last_path) = path.split_at(path.0.len() - 1);
|
let (parent_path, last_path) = path.split_at(path.0.len() - 1);
|
||||||
let last_index = *last_path.first().unwrap();
|
let last_index = *last_path.first().unwrap();
|
||||||
let parent_node = self
|
let parent_node = self
|
||||||
.node_at_path(parent_path)
|
.node_id_at_path(parent_path)
|
||||||
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
||||||
|
|
||||||
self.insert_nodes_at_index(parent_node, last_index, nodes)
|
self.insert_nodes_at_index(parent_node, last_index, nodes)
|
||||||
|
@ -172,8 +195,9 @@ impl NodeTree {
|
||||||
|
|
||||||
/// Inserts nodes before the node with node_id
|
/// Inserts nodes before the node with node_id
|
||||||
///
|
///
|
||||||
fn insert_nodes_before(&mut self, node_id: &NodeId, nodes: &[NodeData]) {
|
fn insert_nodes_before(&mut self, node_id: &NodeId, nodes: Vec<NodeData>) {
|
||||||
for node in nodes {
|
for node in nodes {
|
||||||
|
let (node, children) = node.split();
|
||||||
let new_node_id = self.arena.new_node(node.into());
|
let new_node_id = self.arena.new_node(node.into());
|
||||||
if node_id.is_removed(&self.arena) {
|
if node_id.is_removed(&self.arena) {
|
||||||
tracing::warn!("Node:{:?} is remove before insert", node_id);
|
tracing::warn!("Node:{:?} is remove before insert", node_id);
|
||||||
|
@ -181,23 +205,18 @@ impl NodeTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
node_id.insert_before(new_node_id, &mut self.arena);
|
node_id.insert_before(new_node_id, &mut self.arena);
|
||||||
self.append_nodes(&new_node_id, &node.children);
|
self.append_nodes(&new_node_id, children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_nodes_at_index(
|
fn insert_nodes_at_index(&mut self, parent: NodeId, index: usize, nodes: Vec<NodeData>) -> Result<(), OTError> {
|
||||||
&mut self,
|
|
||||||
parent: NodeId,
|
|
||||||
index: usize,
|
|
||||||
insert_children: &[NodeData],
|
|
||||||
) -> Result<(), OTError> {
|
|
||||||
if index == 0 && parent.children(&self.arena).next().is_none() {
|
if index == 0 && parent.children(&self.arena).next().is_none() {
|
||||||
self.append_nodes(&parent, insert_children);
|
self.append_nodes(&parent, nodes);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == parent.children(&self.arena).count() {
|
if index == parent.children(&self.arena).count() {
|
||||||
self.append_nodes(&parent, insert_children);
|
self.append_nodes(&parent, nodes);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,30 +224,31 @@ impl NodeTree {
|
||||||
.child_from_node_at_index(parent, index)
|
.child_from_node_at_index(parent, index)
|
||||||
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
||||||
|
|
||||||
self.insert_nodes_before(&node_to_insert, insert_children);
|
self.insert_nodes_before(&node_to_insert, nodes);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append_nodes(&mut self, parent: &NodeId, nodes: &[NodeData]) {
|
fn append_nodes(&mut self, parent: &NodeId, nodes: Vec<NodeData>) {
|
||||||
for node in nodes {
|
for node in nodes {
|
||||||
|
let (node, children) = node.split();
|
||||||
let new_node_id = self.arena.new_node(node.into());
|
let new_node_id = self.arena.new_node(node.into());
|
||||||
parent.append(new_node_id, &mut self.arena);
|
parent.append(new_node_id, &mut self.arena);
|
||||||
|
|
||||||
self.append_nodes(&new_node_id, &node.children);
|
self.append_nodes(&new_node_id, children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_attributes(&mut self, path: &Path, attributes: &NodeAttributes) -> Result<(), OTError> {
|
fn update_attributes(&mut self, path: &Path, attributes: NodeAttributes) -> Result<(), OTError> {
|
||||||
self.mut_node_at_path(path, |node| {
|
self.mut_node_at_path(path, |node| {
|
||||||
let new_attributes = NodeAttributes::compose(&node.attributes, attributes)?;
|
let new_attributes = NodeAttributes::compose(&node.attributes, &attributes)?;
|
||||||
node.attributes = new_attributes;
|
node.attributes = new_attributes;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_node(&mut self, path: &Path, nodes: &[NodeData]) -> Result<(), OTError> {
|
fn delete_node(&mut self, path: &Path, nodes: Vec<NodeData>) -> Result<(), OTError> {
|
||||||
let mut update_node = self
|
let mut update_node = self
|
||||||
.node_at_path(path)
|
.node_id_at_path(path)
|
||||||
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
||||||
|
|
||||||
for _ in 0..nodes.len() {
|
for _ in 0..nodes.len() {
|
||||||
|
@ -243,7 +263,7 @@ impl NodeTree {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_body(&mut self, path: &Path, changeset: &NodeBodyChangeset) -> Result<(), OTError> {
|
fn update_body(&mut self, path: &Path, changeset: NodeBodyChangeset) -> Result<(), OTError> {
|
||||||
self.mut_node_at_path(path, |node| {
|
self.mut_node_at_path(path, |node| {
|
||||||
node.apply_body_changeset(changeset);
|
node.apply_body_changeset(changeset);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -252,10 +272,10 @@ impl NodeTree {
|
||||||
|
|
||||||
fn mut_node_at_path<F>(&mut self, path: &Path, f: F) -> Result<(), OTError>
|
fn mut_node_at_path<F>(&mut self, path: &Path, f: F) -> Result<(), OTError>
|
||||||
where
|
where
|
||||||
F: Fn(&mut Node) -> Result<(), OTError>,
|
F: FnOnce(&mut Node) -> Result<(), OTError>,
|
||||||
{
|
{
|
||||||
let node_id = self
|
let node_id = self
|
||||||
.node_at_path(path)
|
.node_id_at_path(path)
|
||||||
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
.ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
|
||||||
match self.arena.get_mut(node_id) {
|
match self.arena.get_mut(node_id) {
|
||||||
None => tracing::warn!("The path: {:?} does not contain any nodes", path),
|
None => tracing::warn!("The path: {:?} does not contain any nodes", path),
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::core::document::operation_serde::*;
|
|
||||||
use crate::core::document::path::Path;
|
use crate::core::document::path::Path;
|
||||||
use crate::core::{NodeAttributes, NodeBodyChangeset, NodeData};
|
use crate::core::{NodeAttributes, NodeBodyChangeset, NodeData};
|
||||||
|
use crate::errors::OTError;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
#[serde(tag = "op")]
|
#[serde(tag = "op")]
|
||||||
pub enum NodeOperation {
|
pub enum NodeOperation {
|
||||||
#[serde(rename = "insert")]
|
#[serde(rename = "insert")]
|
||||||
|
@ -16,9 +17,9 @@ pub enum NodeOperation {
|
||||||
old_attributes: NodeAttributes,
|
old_attributes: NodeAttributes,
|
||||||
},
|
},
|
||||||
|
|
||||||
#[serde(rename = "edit-body")]
|
#[serde(rename = "update-body")]
|
||||||
#[serde(serialize_with = "serialize_edit_body")]
|
// #[serde(serialize_with = "serialize_edit_body")]
|
||||||
// #[serde(deserialize_with = "operation_serde::deserialize_edit_body")]
|
// #[serde(deserialize_with = "deserialize_edit_body")]
|
||||||
UpdateBody { path: Path, changeset: NodeBodyChangeset },
|
UpdateBody { path: Path, changeset: NodeBodyChangeset },
|
||||||
|
|
||||||
#[serde(rename = "delete")]
|
#[serde(rename = "delete")]
|
||||||
|
@ -99,59 +100,43 @@ impl NodeOperation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
mod tests {
|
pub struct NodeOperationList {
|
||||||
use crate::core::{NodeAttributes, NodeBodyChangeset, NodeBuilder, NodeData, NodeOperation, Path, TextDelta};
|
operations: Vec<NodeOperation>,
|
||||||
#[test]
|
}
|
||||||
fn test_serialize_insert_operation() {
|
|
||||||
let insert = NodeOperation::Insert {
|
|
||||||
path: Path(vec![0, 1]),
|
|
||||||
nodes: vec![NodeData::new("text".to_owned())],
|
|
||||||
};
|
|
||||||
let result = serde_json::to_string(&insert).unwrap();
|
|
||||||
assert_eq!(result, r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}"#);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
impl NodeOperationList {
|
||||||
fn test_serialize_insert_sub_trees() {
|
pub fn into_inner(self) -> Vec<NodeOperation> {
|
||||||
let insert = NodeOperation::Insert {
|
self.operations
|
||||||
path: Path(vec![0, 1]),
|
}
|
||||||
nodes: vec![NodeBuilder::new("text")
|
}
|
||||||
.add_node(NodeData::new("text".to_owned()))
|
|
||||||
.build()],
|
impl std::ops::Deref for NodeOperationList {
|
||||||
};
|
type Target = Vec<NodeOperation>;
|
||||||
let result = serde_json::to_string(&insert).unwrap();
|
|
||||||
assert_eq!(
|
fn deref(&self) -> &Self::Target {
|
||||||
result,
|
&self.operations
|
||||||
r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","children":[{"type":"text"}]}]}"#
|
}
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
impl std::ops::DerefMut for NodeOperationList {
|
||||||
#[test]
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
fn test_serialize_update_operation() {
|
&mut self.operations
|
||||||
let insert = NodeOperation::UpdateAttributes {
|
}
|
||||||
path: Path(vec![0, 1]),
|
}
|
||||||
attributes: NodeAttributes::new(),
|
|
||||||
old_attributes: NodeAttributes::new(),
|
impl NodeOperationList {
|
||||||
};
|
pub fn new(operations: Vec<NodeOperation>) -> Self {
|
||||||
let result = serde_json::to_string(&insert).unwrap();
|
Self { operations }
|
||||||
assert_eq!(
|
}
|
||||||
result,
|
|
||||||
r#"{"op":"update","path":[0,1],"attributes":{},"oldAttributes":{}}"#
|
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, OTError> {
|
||||||
);
|
let operation_list = serde_json::from_slice(&bytes).map_err(|err| OTError::serde().context(err))?;
|
||||||
}
|
Ok(operation_list)
|
||||||
|
}
|
||||||
#[test]
|
|
||||||
fn test_serialize_text_edit_operation() {
|
pub fn to_bytes(&self) -> Result<Vec<u8>, OTError> {
|
||||||
let changeset = NodeBodyChangeset::Delta {
|
let bytes = serde_json::to_vec(self).map_err(|err| OTError::serde().context(err))?;
|
||||||
delta: TextDelta::new(),
|
Ok(bytes)
|
||||||
inverted: TextDelta::new(),
|
|
||||||
};
|
|
||||||
let insert = NodeOperation::UpdateBody {
|
|
||||||
path: Path(vec![0, 1]),
|
|
||||||
changeset,
|
|
||||||
};
|
|
||||||
let result = serde_json::to_string(&insert).unwrap();
|
|
||||||
assert_eq!(result, r#"{"op":"edit-body","path":[0,1],"delta":[],"inverted":[]}"#);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
use crate::core::{NodeBodyChangeset, Path};
|
use crate::core::{NodeBodyChangeset, Path};
|
||||||
|
use crate::rich_text::RichTextDelta;
|
||||||
|
use serde::de::{self, MapAccess, Visitor};
|
||||||
use serde::ser::SerializeMap;
|
use serde::ser::SerializeMap;
|
||||||
use serde::Serializer;
|
use serde::{Deserializer, Serializer};
|
||||||
|
use std::convert::TryInto;
|
||||||
|
use std::fmt;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn serialize_edit_body<S>(path: &Path, changeset: &NodeBodyChangeset, serializer: S) -> Result<S::Ok, S::Error>
|
pub fn serialize_edit_body<S>(path: &Path, changeset: &NodeBodyChangeset, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
|
@ -21,9 +27,101 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn deserialize_edit_body<'de, D>(deserializer: D) -> Result<NodeBodyChangeset, D::Error>
|
#[allow(dead_code)]
|
||||||
// where
|
pub fn deserialize_edit_body<'de, D>(deserializer: D) -> Result<(Path, NodeBodyChangeset), D::Error>
|
||||||
// D: Deserializer<'de>,
|
where
|
||||||
// {
|
D: Deserializer<'de>,
|
||||||
// todo!()
|
{
|
||||||
// }
|
struct NodeBodyChangesetVisitor();
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for NodeBodyChangesetVisitor {
|
||||||
|
type Value = (Path, NodeBodyChangeset);
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("Expect Path and NodeBodyChangeset")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut path: Option<Path> = None;
|
||||||
|
let mut delta_changeset = DeltaBodyChangeset::<V::Error>::new();
|
||||||
|
while let Some(key) = map.next_key()? {
|
||||||
|
match key {
|
||||||
|
"delta" => {
|
||||||
|
if delta_changeset.delta.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("delta"));
|
||||||
|
}
|
||||||
|
delta_changeset.delta = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
"inverted" => {
|
||||||
|
if delta_changeset.inverted.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("inverted"));
|
||||||
|
}
|
||||||
|
delta_changeset.inverted = Some(map.next_value()?);
|
||||||
|
}
|
||||||
|
"path" => {
|
||||||
|
if path.is_some() {
|
||||||
|
return Err(de::Error::duplicate_field("path"));
|
||||||
|
}
|
||||||
|
|
||||||
|
path = Some(map.next_value::<Path>()?)
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
panic!("Unexpected key: {}", other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if path.is_none() {
|
||||||
|
return Err(de::Error::missing_field("path"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let changeset = delta_changeset.try_into()?;
|
||||||
|
|
||||||
|
Ok((path.unwrap(), changeset))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deserializer.deserialize_any(NodeBodyChangesetVisitor())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct DeltaBodyChangeset<E> {
|
||||||
|
delta: Option<RichTextDelta>,
|
||||||
|
inverted: Option<RichTextDelta>,
|
||||||
|
error: PhantomData<E>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E> DeltaBodyChangeset<E> {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
delta: None,
|
||||||
|
inverted: None,
|
||||||
|
error: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E> std::convert::TryInto<NodeBodyChangeset> for DeltaBodyChangeset<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Error = E;
|
||||||
|
|
||||||
|
fn try_into(self) -> Result<NodeBodyChangeset, Self::Error> {
|
||||||
|
if self.delta.is_none() {
|
||||||
|
return Err(de::Error::missing_field("delta"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.inverted.is_none() {
|
||||||
|
return Err(de::Error::missing_field("inverted"));
|
||||||
|
}
|
||||||
|
let changeset = NodeBodyChangeset::Delta {
|
||||||
|
delta: self.delta.unwrap(),
|
||||||
|
inverted: self.inverted.unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(changeset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,26 +2,46 @@ use crate::core::document::path::Path;
|
||||||
use crate::core::{NodeAttributes, NodeData, NodeOperation, NodeTree};
|
use crate::core::{NodeAttributes, NodeData, NodeOperation, NodeTree};
|
||||||
use indextree::NodeId;
|
use indextree::NodeId;
|
||||||
|
|
||||||
|
use super::{NodeBodyChangeset, NodeOperationList};
|
||||||
|
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
pub operations: Vec<NodeOperation>,
|
operations: NodeOperationList,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transaction {
|
impl Transaction {
|
||||||
fn new(operations: Vec<NodeOperation>) -> Transaction {
|
pub fn new(operations: NodeOperationList) -> Transaction {
|
||||||
Transaction { operations }
|
Transaction { operations }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_operations(self) -> Vec<NodeOperation> {
|
||||||
|
self.operations.into_inner()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Deref for Transaction {
|
||||||
|
type Target = NodeOperationList;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.operations
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::DerefMut for Transaction {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.operations
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TransactionBuilder<'a> {
|
pub struct TransactionBuilder<'a> {
|
||||||
node_tree: &'a NodeTree,
|
node_tree: &'a NodeTree,
|
||||||
operations: Vec<NodeOperation>,
|
operations: NodeOperationList,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TransactionBuilder<'a> {
|
impl<'a> TransactionBuilder<'a> {
|
||||||
pub fn new(node_tree: &'a NodeTree) -> TransactionBuilder {
|
pub fn new(node_tree: &'a NodeTree) -> TransactionBuilder {
|
||||||
TransactionBuilder {
|
TransactionBuilder {
|
||||||
node_tree,
|
node_tree,
|
||||||
operations: Vec::new(),
|
operations: NodeOperationList::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,13 +59,13 @@ impl<'a> TransactionBuilder<'a> {
|
||||||
/// // 0 -- text_1
|
/// // 0 -- text_1
|
||||||
/// // 1 -- text_2
|
/// // 1 -- text_2
|
||||||
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
|
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
|
||||||
/// let mut node_tree = NodeTree::new();
|
/// let mut node_tree = NodeTree::new("root");
|
||||||
/// let transaction = TransactionBuilder::new(&node_tree)
|
/// let transaction = TransactionBuilder::new(&node_tree)
|
||||||
/// .insert_nodes_at_path(0,vec![ NodeData::new("text_1"), NodeData::new("text_2")])
|
/// .insert_nodes_at_path(0,vec![ NodeData::new("text_1"), NodeData::new("text_2")])
|
||||||
/// .finalize();
|
/// .finalize();
|
||||||
/// node_tree.apply(transaction).unwrap();
|
/// node_tree.apply(transaction).unwrap();
|
||||||
///
|
///
|
||||||
/// node_tree.node_at_path(vec![0, 0]);
|
/// node_tree.node_id_at_path(vec![0, 0]);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
pub fn insert_nodes_at_path<T: Into<Path>>(self, path: T, nodes: Vec<NodeData>) -> Self {
|
pub fn insert_nodes_at_path<T: Into<Path>>(self, path: T, nodes: Vec<NodeData>) -> Self {
|
||||||
|
@ -69,7 +89,7 @@ impl<'a> TransactionBuilder<'a> {
|
||||||
/// // -- 0
|
/// // -- 0
|
||||||
/// // |-- text
|
/// // |-- text
|
||||||
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
|
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
|
||||||
/// let mut node_tree = NodeTree::new();
|
/// let mut node_tree = NodeTree::new("root");
|
||||||
/// let transaction = TransactionBuilder::new(&node_tree)
|
/// let transaction = TransactionBuilder::new(&node_tree)
|
||||||
/// .insert_node_at_path(0, NodeData::new("text"))
|
/// .insert_node_at_path(0, NodeData::new("text"))
|
||||||
/// .finalize();
|
/// .finalize();
|
||||||
|
@ -80,23 +100,39 @@ impl<'a> TransactionBuilder<'a> {
|
||||||
self.insert_nodes_at_path(path, vec![node])
|
self.insert_nodes_at_path(path, vec![node])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_attributes_at_path(self, path: &Path, attributes: NodeAttributes) -> Self {
|
pub fn update_attributes_at_path(mut self, path: &Path, attributes: NodeAttributes) -> Self {
|
||||||
|
match self.node_tree.get_node_at_path(path) {
|
||||||
|
Some(node) => {
|
||||||
let mut old_attributes = NodeAttributes::new();
|
let mut old_attributes = NodeAttributes::new();
|
||||||
let node = self.node_tree.node_at_path(path).unwrap();
|
|
||||||
let node_data = self.node_tree.get_node(node).unwrap();
|
|
||||||
|
|
||||||
for key in attributes.keys() {
|
for key in attributes.keys() {
|
||||||
let old_attrs = &node_data.attributes;
|
let old_attrs = &node.attributes;
|
||||||
if let Some(value) = old_attrs.get(key.as_str()) {
|
if let Some(value) = old_attrs.get(key.as_str()) {
|
||||||
old_attributes.insert(key.clone(), value.clone());
|
old_attributes.insert(key.clone(), value.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.push(NodeOperation::UpdateAttributes {
|
self.operations.push(NodeOperation::UpdateAttributes {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
attributes,
|
attributes,
|
||||||
old_attributes,
|
old_attributes,
|
||||||
})
|
});
|
||||||
|
}
|
||||||
|
None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_body_at_path(mut self, path: &Path, changeset: NodeBodyChangeset) -> Self {
|
||||||
|
match self.node_tree.node_id_at_path(path) {
|
||||||
|
Some(_) => {
|
||||||
|
self.operations.push(NodeOperation::UpdateBody {
|
||||||
|
path: path.clone(),
|
||||||
|
changeset,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
|
||||||
|
}
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_node_at_path(self, path: &Path) -> Self {
|
pub fn delete_node_at_path(self, path: &Path) -> Self {
|
||||||
|
@ -104,7 +140,7 @@ impl<'a> TransactionBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_nodes_at_path(mut self, path: &Path, length: usize) -> Self {
|
pub fn delete_nodes_at_path(mut self, path: &Path, length: usize) -> Self {
|
||||||
let mut node = self.node_tree.node_at_path(path).unwrap();
|
let mut node = self.node_tree.node_id_at_path(path).unwrap();
|
||||||
let mut deleted_nodes = vec![];
|
let mut deleted_nodes = vec![];
|
||||||
for _ in 0..length {
|
for _ in 0..length {
|
||||||
deleted_nodes.push(self.get_deleted_nodes(node));
|
deleted_nodes.push(self.get_deleted_nodes(node));
|
||||||
|
|
|
@ -37,6 +37,7 @@ impl OTError {
|
||||||
static_ot_error!(duplicate_revision, OTErrorCode::DuplicatedRevision);
|
static_ot_error!(duplicate_revision, OTErrorCode::DuplicatedRevision);
|
||||||
static_ot_error!(revision_id_conflict, OTErrorCode::RevisionIDConflict);
|
static_ot_error!(revision_id_conflict, OTErrorCode::RevisionIDConflict);
|
||||||
static_ot_error!(internal, OTErrorCode::Internal);
|
static_ot_error!(internal, OTErrorCode::Internal);
|
||||||
|
static_ot_error!(serde, OTErrorCode::SerdeError);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for OTError {
|
impl fmt::Display for OTError {
|
||||||
|
|
|
@ -82,17 +82,6 @@ impl TextAttributes {
|
||||||
self.inner.retain(|k, _| k != &key);
|
self.inner.retain(|k, _| k != &key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn block_attributes_except_header(attributes: &Attributes) -> Attributes
|
|
||||||
// { let mut new_attributes = Attributes::new();
|
|
||||||
// attributes.iter().for_each(|(k, v)| {
|
|
||||||
// if k != &AttributeKey::Header {
|
|
||||||
// new_attributes.insert(k.clone(), v.clone());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// new_attributes
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Update inner by constructing new attributes from the other if it's
|
// Update inner by constructing new attributes from the other if it's
|
||||||
// not None and replace the key/value with self key/value.
|
// not None and replace the key/value with self key/value.
|
||||||
pub fn merge(&mut self, other: Option<TextAttributes>) {
|
pub fn merge(&mut self, other: Option<TextAttributes>) {
|
||||||
|
@ -257,7 +246,6 @@ impl std::convert::From<TextAttribute> for TextAttributes {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Display, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, Display, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||||
// serde.rs/variant-attrs.html
|
|
||||||
// #[serde(rename_all = "snake_case")]
|
// #[serde(rename_all = "snake_case")]
|
||||||
pub enum TextAttributeKey {
|
pub enum TextAttributeKey {
|
||||||
#[serde(rename = "bold")]
|
#[serde(rename = "bold")]
|
||||||
|
@ -298,7 +286,6 @@ pub enum TextAttributeKey {
|
||||||
Header,
|
Header,
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub trait AttributeValueData<'a>: Serialize + Deserialize<'a> {}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct TextAttributeValue(pub Option<String>);
|
pub struct TextAttributeValue(pub Option<String>);
|
||||||
|
|
||||||
|
|
162
shared-lib/lib-ot/tests/node/editor_test.rs
Normal file
162
shared-lib/lib-ot/tests/node/editor_test.rs
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
use super::script::{NodeScript::*, *};
|
||||||
|
use lib_ot::{
|
||||||
|
core::{NodeData, Path},
|
||||||
|
rich_text::{AttributeBuilder, RichTextDeltaBuilder, TextAttribute, TextAttributes},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn appflowy_editor_deserialize_node_test() {
|
||||||
|
let mut test = NodeTest::new();
|
||||||
|
let node: NodeData = serde_json::from_str(EXAMPLE_JSON).unwrap();
|
||||||
|
let path: Path = 0.into();
|
||||||
|
|
||||||
|
let expected_delta = RichTextDeltaBuilder::new()
|
||||||
|
.insert("👋 ")
|
||||||
|
.insert_with_attributes(
|
||||||
|
"Welcome to ",
|
||||||
|
AttributeBuilder::new().add_attr(TextAttribute::Bold(true)).build(),
|
||||||
|
)
|
||||||
|
.insert_with_attributes(
|
||||||
|
"AppFlowy Editor",
|
||||||
|
AttributeBuilder::new().add_attr(TextAttribute::Italic(true)).build(),
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
test.run_scripts(vec![
|
||||||
|
InsertNode {
|
||||||
|
path: path.clone(),
|
||||||
|
node: node.clone(),
|
||||||
|
},
|
||||||
|
AssertNumberOfNodesAtPath { path: None, len: 1 },
|
||||||
|
AssertNumberOfNodesAtPath {
|
||||||
|
path: Some(0.into()),
|
||||||
|
len: 14,
|
||||||
|
},
|
||||||
|
AssertNumberOfNodesAtPath {
|
||||||
|
path: Some(0.into()),
|
||||||
|
len: 14,
|
||||||
|
},
|
||||||
|
AssertNodeDelta {
|
||||||
|
path: vec![0, 1].into(),
|
||||||
|
expected: expected_delta,
|
||||||
|
},
|
||||||
|
AssertNode {
|
||||||
|
path: vec![0, 0].into(),
|
||||||
|
expected: Some(node.children[0].clone()),
|
||||||
|
},
|
||||||
|
AssertNode {
|
||||||
|
path: vec![0, 3].into(),
|
||||||
|
expected: Some(node.children[3].clone()),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
const EXAMPLE_JSON: &str = r#"
|
||||||
|
{
|
||||||
|
"type": "editor",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"type": "image",
|
||||||
|
"attributes": {
|
||||||
|
"image_src": "https://s1.ax1x.com/2022/08/26/v2sSbR.jpg",
|
||||||
|
"align": "center"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": {
|
||||||
|
"subtype": "heading",
|
||||||
|
"heading": "h1"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
"delta": [
|
||||||
|
{
|
||||||
|
"insert": "👋 "
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"insert": "Welcome to ",
|
||||||
|
"attributes": {
|
||||||
|
"bold": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"insert": "AppFlowy Editor",
|
||||||
|
"attributes": {
|
||||||
|
"italic": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ "type": "text", "delta": [] },
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"body": {
|
||||||
|
"delta": [
|
||||||
|
{ "insert": "AppFlowy Editor is a " },
|
||||||
|
{ "insert": "highly customizable", "attributes": { "bold": true } },
|
||||||
|
{ "insert": " " },
|
||||||
|
{ "insert": "rich-text editor", "attributes": { "italic": true } },
|
||||||
|
{ "insert": " for " },
|
||||||
|
{ "insert": "Flutter", "attributes": { "underline": true } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "checkbox": true, "subtype": "checkbox" },
|
||||||
|
"body": {
|
||||||
|
"delta": [{ "insert": "Customizable" }]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "checkbox": true, "subtype": "checkbox" },
|
||||||
|
"delta": [{ "insert": "Test-covered" }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "checkbox": false, "subtype": "checkbox" },
|
||||||
|
"delta": [{ "insert": "more to come!" }]
|
||||||
|
},
|
||||||
|
{ "type": "text", "delta": [] },
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "subtype": "quote" },
|
||||||
|
"delta": [{ "insert": "Here is an exmaple you can give it a try" }]
|
||||||
|
},
|
||||||
|
{ "type": "text", "delta": [] },
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"delta": [
|
||||||
|
{ "insert": "You can also use " },
|
||||||
|
{
|
||||||
|
"insert": "AppFlowy Editor",
|
||||||
|
"attributes": {
|
||||||
|
"italic": true,
|
||||||
|
"bold": true,
|
||||||
|
"backgroundColor": "0x6000BCF0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ "insert": " as a component to build your own app." }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{ "type": "text", "delta": [] },
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "subtype": "bulleted-list" },
|
||||||
|
"delta": [{ "insert": "Use / to insert blocks" }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"attributes": { "subtype": "bulleted-list" },
|
||||||
|
"delta": [
|
||||||
|
{
|
||||||
|
"insert": "Select text to trigger to the toolbar to format your notes."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
"#;
|
|
@ -1,2 +1,4 @@
|
||||||
|
mod editor_test;
|
||||||
|
mod operation_test;
|
||||||
mod script;
|
mod script;
|
||||||
mod test;
|
mod tree_test;
|
||||||
|
|
70
shared-lib/lib-ot/tests/node/operation_test.rs
Normal file
70
shared-lib/lib-ot/tests/node/operation_test.rs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
use lib_ot::{
|
||||||
|
core::{NodeAttributeBuilder, NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
|
||||||
|
rich_text::RichTextDeltaBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn operation_insert_node_serde_test() {
|
||||||
|
let insert = NodeOperation::Insert {
|
||||||
|
path: Path(vec![0, 1]),
|
||||||
|
nodes: vec![NodeData::new("text".to_owned())],
|
||||||
|
};
|
||||||
|
let result = serde_json::to_string(&insert).unwrap();
|
||||||
|
assert_eq!(result, r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}"#);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn operation_insert_node_with_children_serde_test() {
|
||||||
|
let node = NodeDataBuilder::new("text")
|
||||||
|
.add_node(NodeData::new("sub_text".to_owned()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let insert = NodeOperation::Insert {
|
||||||
|
path: Path(vec![0, 1]),
|
||||||
|
nodes: vec![node],
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_string(&insert).unwrap(),
|
||||||
|
r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","children":[{"type":"sub_text"}]}]}"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn operation_update_node_attributes_serde_test() {
|
||||||
|
let operation = NodeOperation::UpdateAttributes {
|
||||||
|
path: Path(vec![0, 1]),
|
||||||
|
attributes: NodeAttributeBuilder::new().insert("bold", true).build(),
|
||||||
|
old_attributes: NodeAttributeBuilder::new().insert("bold", false).build(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = serde_json::to_string(&operation).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
r#"{"op":"update","path":[0,1],"attributes":{"bold":true},"oldAttributes":{"bold":false}}"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn operation_update_node_body_serialize_test() {
|
||||||
|
let delta = RichTextDeltaBuilder::new().insert("AppFlowy...").build();
|
||||||
|
let inverted = delta.invert_str("");
|
||||||
|
let changeset = NodeBodyChangeset::Delta { delta, inverted };
|
||||||
|
let insert = NodeOperation::UpdateBody {
|
||||||
|
path: Path(vec![0, 1]),
|
||||||
|
changeset,
|
||||||
|
};
|
||||||
|
let result = serde_json::to_string(&insert).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
r#"{"op":"update-body","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#
|
||||||
|
);
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn operation_update_node_body_deserialize_test() {
|
||||||
|
let json_1 = r#"{"op":"update-body","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#;
|
||||||
|
let operation: NodeOperation = serde_json::from_str(json_1).unwrap();
|
||||||
|
let json_2 = serde_json::to_string(&operation).unwrap();
|
||||||
|
assert_eq!(json_1, json_2);
|
||||||
|
}
|
|
@ -1,11 +1,16 @@
|
||||||
use lib_ot::core::{NodeAttributes, NodeData, NodeTree, Path, TransactionBuilder};
|
use lib_ot::{
|
||||||
|
core::{NodeAttributes, NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
|
||||||
|
rich_text::RichTextDelta,
|
||||||
|
};
|
||||||
|
|
||||||
pub enum NodeScript {
|
pub enum NodeScript {
|
||||||
InsertNode { path: Path, node: NodeData },
|
InsertNode { path: Path, node: NodeData },
|
||||||
InsertAttributes { path: Path, attributes: NodeAttributes },
|
UpdateAttributes { path: Path, attributes: NodeAttributes },
|
||||||
|
UpdateBody { path: Path, changeset: NodeBodyChangeset },
|
||||||
DeleteNode { path: Path },
|
DeleteNode { path: Path },
|
||||||
AssertNumberOfChildrenAtPath { path: Option<Path>, len: usize },
|
AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
|
||||||
AssertNode { path: Path, expected: Option<NodeData> },
|
AssertNode { path: Path, expected: Option<NodeData> },
|
||||||
|
AssertNodeDelta { path: Path, expected: RichTextDelta },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NodeTest {
|
pub struct NodeTest {
|
||||||
|
@ -15,7 +20,7 @@ pub struct NodeTest {
|
||||||
impl NodeTest {
|
impl NodeTest {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
node_tree: NodeTree::new(),
|
node_tree: NodeTree::new("root"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,12 +39,19 @@ impl NodeTest {
|
||||||
|
|
||||||
self.node_tree.apply(transaction).unwrap();
|
self.node_tree.apply(transaction).unwrap();
|
||||||
}
|
}
|
||||||
NodeScript::InsertAttributes { path, attributes } => {
|
NodeScript::UpdateAttributes { path, attributes } => {
|
||||||
let transaction = TransactionBuilder::new(&self.node_tree)
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
||||||
.update_attributes_at_path(&path, attributes)
|
.update_attributes_at_path(&path, attributes)
|
||||||
.finalize();
|
.finalize();
|
||||||
self.node_tree.apply(transaction).unwrap();
|
self.node_tree.apply(transaction).unwrap();
|
||||||
}
|
}
|
||||||
|
NodeScript::UpdateBody { path, changeset } => {
|
||||||
|
//
|
||||||
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
||||||
|
.update_body_at_path(&path, changeset)
|
||||||
|
.finalize();
|
||||||
|
self.node_tree.apply(transaction).unwrap();
|
||||||
|
}
|
||||||
NodeScript::DeleteNode { path } => {
|
NodeScript::DeleteNode { path } => {
|
||||||
let transaction = TransactionBuilder::new(&self.node_tree)
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
||||||
.delete_node_at_path(&path)
|
.delete_node_at_path(&path)
|
||||||
|
@ -47,7 +59,7 @@ impl NodeTest {
|
||||||
self.node_tree.apply(transaction).unwrap();
|
self.node_tree.apply(transaction).unwrap();
|
||||||
}
|
}
|
||||||
NodeScript::AssertNode { path, expected } => {
|
NodeScript::AssertNode { path, expected } => {
|
||||||
let node_id = self.node_tree.node_at_path(path);
|
let node_id = self.node_tree.node_id_at_path(path);
|
||||||
|
|
||||||
match node_id {
|
match node_id {
|
||||||
None => assert!(node_id.is_none()),
|
None => assert!(node_id.is_none()),
|
||||||
|
@ -57,7 +69,7 @@ impl NodeTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NodeScript::AssertNumberOfChildrenAtPath {
|
NodeScript::AssertNumberOfNodesAtPath {
|
||||||
path,
|
path,
|
||||||
len: expected_len,
|
len: expected_len,
|
||||||
} => match path {
|
} => match path {
|
||||||
|
@ -66,11 +78,19 @@ impl NodeTest {
|
||||||
assert_eq!(len, expected_len)
|
assert_eq!(len, expected_len)
|
||||||
}
|
}
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
let node_id = self.node_tree.node_at_path(path).unwrap();
|
let node_id = self.node_tree.node_id_at_path(path).unwrap();
|
||||||
let len = self.node_tree.number_of_children(Some(node_id));
|
let len = self.node_tree.number_of_children(Some(node_id));
|
||||||
assert_eq!(len, expected_len)
|
assert_eq!(len, expected_len)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
NodeScript::AssertNodeDelta { path, expected } => {
|
||||||
|
let node = self.node_tree.get_node_at_path(&path).unwrap();
|
||||||
|
if let NodeBody::Delta(delta) = node.body.clone() {
|
||||||
|
debug_assert_eq!(delta, expected);
|
||||||
|
} else {
|
||||||
|
panic!("Node body type not match, expect Delta");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
use crate::node::script::NodeScript::*;
|
use crate::node::script::NodeScript::*;
|
||||||
use crate::node::script::NodeTest;
|
use crate::node::script::NodeTest;
|
||||||
use lib_ot::core::{NodeBuilder, NodeData, Path};
|
use lib_ot::core::NodeBody;
|
||||||
|
use lib_ot::core::NodeBodyChangeset;
|
||||||
|
use lib_ot::core::OperationTransform;
|
||||||
|
use lib_ot::core::{NodeData, NodeDataBuilder, Path};
|
||||||
|
use lib_ot::rich_text::RichTextDeltaBuilder;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn node_insert_test() {
|
fn node_insert_test() {
|
||||||
|
@ -23,7 +27,7 @@ fn node_insert_test() {
|
||||||
#[test]
|
#[test]
|
||||||
fn node_insert_node_with_children_test() {
|
fn node_insert_node_with_children_test() {
|
||||||
let mut test = NodeTest::new();
|
let mut test = NodeTest::new();
|
||||||
let inserted_node = NodeBuilder::new("text").add_node(NodeData::new("image")).build();
|
let inserted_node = NodeDataBuilder::new("text").add_node(NodeData::new("image")).build();
|
||||||
let path: Path = 0.into();
|
let path: Path = 0.into();
|
||||||
let scripts = vec![
|
let scripts = vec![
|
||||||
InsertNode {
|
InsertNode {
|
||||||
|
@ -129,7 +133,7 @@ fn node_insert_node_in_ordered_nodes_test() {
|
||||||
path: path_4,
|
path: path_4,
|
||||||
expected: Some(node_3),
|
expected: Some(node_3),
|
||||||
},
|
},
|
||||||
AssertNumberOfChildrenAtPath { path: None, len: 4 },
|
AssertNumberOfNodesAtPath { path: None, len: 4 },
|
||||||
];
|
];
|
||||||
test.run_scripts(scripts);
|
test.run_scripts(scripts);
|
||||||
}
|
}
|
||||||
|
@ -147,7 +151,7 @@ fn node_insert_with_attributes_test() {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
node: inserted_node.clone(),
|
node: inserted_node.clone(),
|
||||||
},
|
},
|
||||||
InsertAttributes {
|
UpdateAttributes {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
attributes: inserted_node.attributes.clone(),
|
attributes: inserted_node.attributes.clone(),
|
||||||
},
|
},
|
||||||
|
@ -175,3 +179,32 @@ fn node_delete_test() {
|
||||||
];
|
];
|
||||||
test.run_scripts(scripts);
|
test.run_scripts(scripts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn node_update_body_test() {
|
||||||
|
let mut test = NodeTest::new();
|
||||||
|
let path: Path = 0.into();
|
||||||
|
|
||||||
|
let s = "Hello".to_owned();
|
||||||
|
let init_delta = RichTextDeltaBuilder::new().insert(&s).build();
|
||||||
|
let delta = RichTextDeltaBuilder::new().retain(s.len()).insert(" AppFlowy").build();
|
||||||
|
let inverted = delta.invert(&init_delta);
|
||||||
|
let expected = init_delta.compose(&delta).unwrap();
|
||||||
|
|
||||||
|
let node = NodeDataBuilder::new("text")
|
||||||
|
.insert_body(NodeBody::Delta(init_delta))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let scripts = vec![
|
||||||
|
InsertNode {
|
||||||
|
path: path.clone(),
|
||||||
|
node: node.clone(),
|
||||||
|
},
|
||||||
|
UpdateBody {
|
||||||
|
path: path.clone(),
|
||||||
|
changeset: NodeBodyChangeset::Delta { delta, inverted },
|
||||||
|
},
|
||||||
|
AssertNodeDelta { path, expected },
|
||||||
|
];
|
||||||
|
test.run_scripts(scripts);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue