fix: minor issues

This commit is contained in:
Vincent Chan 2022-08-24 11:50:06 +08:00
parent 096544d6a3
commit a309a9c82c
4 changed files with 22 additions and 25 deletions

View File

@ -2,14 +2,14 @@ import 'package:appflowy_editor/appflowy_editor.dart';
abstract class Operation { abstract class Operation {
factory Operation.fromJson(Map<String, dynamic> map) { factory Operation.fromJson(Map<String, dynamic> map) {
String t = map["type"] as String; String t = map["op"] as String;
if (t == "insert-operation") { if (t == "insert") {
return InsertOperation.fromJson(map); return InsertOperation.fromJson(map);
} else if (t == "update-operation") { } else if (t == "update") {
return UpdateOperation.fromJson(map); return UpdateOperation.fromJson(map);
} else if (t == "delete-operation") { } else if (t == "delete") {
return DeleteOperation.fromJson(map); return DeleteOperation.fromJson(map);
} else if (t == "text-edit-operation") { } else if (t == "text-edit") {
return TextEditOperation.fromJson(map); return TextEditOperation.fromJson(map);
} }
@ -51,7 +51,7 @@ class InsertOperation extends Operation {
@override @override
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { return {
"type": "insert-operation", "op": "insert",
"path": path.toList(), "path": path.toList(),
"nodes": nodes.map((n) => n.toJson()), "nodes": nodes.map((n) => n.toJson()),
}; };
@ -95,7 +95,7 @@ class UpdateOperation extends Operation {
@override @override
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { return {
"type": "update-operation", "op": "update",
"path": path.toList(), "path": path.toList(),
"attributes": {...attributes}, "attributes": {...attributes},
"oldAttributes": {...oldAttributes}, "oldAttributes": {...oldAttributes},
@ -132,7 +132,7 @@ class DeleteOperation extends Operation {
@override @override
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { return {
"type": "delete-operation", "op": "delete",
"path": path.toList(), "path": path.toList(),
"nodes": nodes.map((n) => n.toJson()), "nodes": nodes.map((n) => n.toJson()),
}; };
@ -171,7 +171,7 @@ class TextEditOperation extends Operation {
@override @override
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { return {
"type": "text-edit-operation", "op": "text-edit",
"path": path.toList(), "path": path.toList(),
"delta": delta.toJson(), "delta": delta.toJson(),
"invert": inverted.toJson(), "invert": inverted.toJson(),

View File

@ -84,7 +84,7 @@ void main() {
expect(transaction.toJson(), { expect(transaction.toJson(), {
"operations": [ "operations": [
{ {
"type": "insert-operation", "op": "insert",
"path": [0], "path": [0],
"nodes": [item1.toJson()], "nodes": [item1.toJson()],
} }
@ -107,7 +107,7 @@ void main() {
expect(transaction.toJson(), { expect(transaction.toJson(), {
"operations": [ "operations": [
{ {
"type": "delete-operation", "op": "delete",
"path": [0], "path": [0],
"nodes": [item1.toJson()], "nodes": [item1.toJson()],
} }

View File

@ -2,26 +2,26 @@ use crate::core::document::position::Position;
use crate::core::{NodeAttributes, NodeSubTree, TextDelta}; use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
#[derive(Clone, serde::Serialize, serde::Deserialize)] #[derive(Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type")] #[serde(tag = "op")]
pub enum DocumentOperation { pub enum DocumentOperation {
#[serde(rename = "insert-operation")] #[serde(rename = "insert")]
Insert { Insert {
path: Position, path: Position,
nodes: Vec<Box<NodeSubTree>>, nodes: Vec<Box<NodeSubTree>>,
}, },
#[serde(rename = "update-operation")] #[serde(rename = "update")]
Update { Update {
path: Position, path: Position,
attributes: NodeAttributes, attributes: NodeAttributes,
#[serde(rename = "oldAttributes")] #[serde(rename = "oldAttributes")]
old_attributes: NodeAttributes, old_attributes: NodeAttributes,
}, },
#[serde(rename = "delete-operation")] #[serde(rename = "delete")]
Delete { Delete {
path: Position, path: Position,
nodes: Vec<Box<NodeSubTree>>, nodes: Vec<Box<NodeSubTree>>,
}, },
#[serde(rename = "text-edit-operation")] #[serde(rename = "text-edit")]
TextEdit { TextEdit {
path: Position, path: Position,
delta: TextDelta, delta: TextDelta,
@ -166,7 +166,7 @@ mod tests {
let result = serde_json::to_string(&insert).unwrap(); let result = serde_json::to_string(&insert).unwrap();
assert_eq!( assert_eq!(
result, result,
r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{}}]}"# r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","attributes":{}}]}"#
); );
} }
@ -184,7 +184,7 @@ mod tests {
let result = serde_json::to_string(&insert).unwrap(); let result = serde_json::to_string(&insert).unwrap();
assert_eq!( assert_eq!(
result, result,
r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{},"children":[{"type":"text","attributes":{}}]}]}"# r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","attributes":{},"children":[{"type":"text","attributes":{}}]}]}"#
); );
} }
@ -198,7 +198,7 @@ mod tests {
let result = serde_json::to_string(&insert).unwrap(); let result = serde_json::to_string(&insert).unwrap();
assert_eq!( assert_eq!(
result, result,
r#"{"type":"update-operation","path":[0,1],"attributes":{},"oldAttributes":{}}"# r#"{"op":"update","path":[0,1],"attributes":{},"oldAttributes":{}}"#
); );
} }
@ -210,9 +210,6 @@ mod tests {
inverted: Delta::new(), inverted: Delta::new(),
}; };
let result = serde_json::to_string(&insert).unwrap(); let result = serde_json::to_string(&insert).unwrap();
assert_eq!( assert_eq!(result, r#"{"op":"text-edit","path":[0,1],"delta":[],"inverted":[]}"#);
result,
r#"{"type":"text-edit-operation","path":[0,1],"delta":[],"inverted":[]}"#
);
} }
} }

View File

@ -12,7 +12,7 @@ impl Position {
impl Position { impl Position {
// delta is default to be 1 // delta is default to be 1
pub fn transform(pre_insert_path: &Position, b: &Position, delta: i64) -> Position { pub fn transform(pre_insert_path: &Position, b: &Position, offset: i64) -> Position {
if pre_insert_path.len() > b.len() { if pre_insert_path.len() > b.len() {
return b.clone(); return b.clone();
} }
@ -30,7 +30,7 @@ impl Position {
let prev_insert_last: usize = *pre_insert_path.0.last().unwrap(); let prev_insert_last: usize = *pre_insert_path.0.last().unwrap();
let b_at_index = b.0[pre_insert_path.0.len() - 1]; let b_at_index = b.0[pre_insert_path.0.len() - 1];
if prev_insert_last <= b_at_index { if prev_insert_last <= b_at_index {
prefix.push(((b_at_index as i64) + delta) as usize); prefix.push(((b_at_index as i64) + offset) as usize);
} else { } else {
prefix.push(b_at_index); prefix.push(b_at_index);
} }