mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: add serialize/deserialize
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct NodeAttributes(pub HashMap<String, Option<String>>);
|
pub struct NodeAttributes(pub HashMap<String, Option<String>>);
|
||||||
|
|
||||||
impl NodeAttributes {
|
impl NodeAttributes {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::core::document::position::Position;
|
use crate::core::document::position::Position;
|
||||||
use crate::core::{NodeAttributes, NodeData, TextDelta};
|
use crate::core::{NodeAttributes, NodeData, TextDelta};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum DocumentOperation {
|
pub enum DocumentOperation {
|
||||||
Insert {
|
Insert {
|
||||||
path: Position,
|
path: Position,
|
||||||
@ -98,3 +98,56 @@ impl DocumentOperation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::core::Position;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_path_1() {
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1]), &Position(vec![0, 1]), 1) }.0,
|
||||||
|
vec![0, 2]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_path_2() {
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1]), &Position(vec![0, 2]), 1) }.0,
|
||||||
|
vec![0, 3]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_path_3() {
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1]), &Position(vec![0, 2, 7, 8, 9]), 1) }.0,
|
||||||
|
vec![0, 3, 7, 8, 9]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_path_not_changed() {
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1, 2]), &Position(vec![0, 0, 7, 8, 9]), 1) }.0,
|
||||||
|
vec![0, 0, 7, 8, 9]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1, 2]), &Position(vec![0, 1]), 1) }.0,
|
||||||
|
vec![0, 1]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![1, 1]), &Position(vec![1, 0]), 1) }.0,
|
||||||
|
vec![1, 0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_delta() {
|
||||||
|
assert_eq!(
|
||||||
|
{ Position::transform(&Position(vec![0, 1]), &Position(vec![0, 1]), 5) }.0,
|
||||||
|
vec![0, 6]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::core::{NodeAttributes, TextDelta};
|
use crate::core::{NodeAttributes, TextDelta};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct NodeData {
|
pub struct NodeData {
|
||||||
pub node_type: String,
|
pub node_type: String,
|
||||||
pub attributes: NodeAttributes,
|
pub attributes: NodeAttributes,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#[derive(Clone)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Position(pub Vec<usize>);
|
pub struct Position(pub Vec<usize>);
|
||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
@ -20,7 +20,7 @@ impl Position {
|
|||||||
return b.clone();
|
return b.clone();
|
||||||
}
|
}
|
||||||
// check the prefix
|
// check the prefix
|
||||||
for i in 0..(pre_insert_path.len()) {
|
for i in 0..(pre_insert_path.len() - 1) {
|
||||||
if pre_insert_path.0[i] != b.0[i] {
|
if pre_insert_path.0[i] != b.0[i] {
|
||||||
return b.clone();
|
return b.clone();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user