mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: apply operation
This commit is contained in:
parent
aa90613bf6
commit
bb7c7e4f41
@ -1,5 +1,5 @@
|
|||||||
use crate::core::document::position::Position;
|
use crate::core::document::position::Position;
|
||||||
use crate::core::{NodeData, Transaction};
|
use crate::core::{DeleteOperation, DocumentOperation, InsertOperation, NodeData, TextEditOperation, Transaction, UpdateOperation};
|
||||||
use indextree::{Arena, NodeId};
|
use indextree::{Arena, NodeId};
|
||||||
|
|
||||||
pub struct DocumentTree {
|
pub struct DocumentTree {
|
||||||
@ -86,7 +86,54 @@ impl DocumentTree {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply(&self, _transaction: Transaction) {
|
pub fn apply(&mut self, transaction: Transaction) {
|
||||||
|
for op in &transaction.operations {
|
||||||
|
self.apply_op(op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_op(&mut self, op: &DocumentOperation) {
|
||||||
|
match op {
|
||||||
|
DocumentOperation::Insert(op) => self.apply_insert(op),
|
||||||
|
DocumentOperation::Update(op) => self.apply_update(op),
|
||||||
|
DocumentOperation::Delete(op) => self.apply_delete(op),
|
||||||
|
DocumentOperation::TextEdit(op) => self.apply_text_edit(op),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_insert(&mut self, op: &InsertOperation) {
|
||||||
|
let parent_path = &op.path.0[0..(op.path.0.len() - 1)];
|
||||||
|
let last_index = op.path.0[op.path.0.len() - 1];
|
||||||
|
let parent_node = self.node_at_path(&Position(parent_path.to_vec()));
|
||||||
|
if let Some(parent_node) = parent_node {
|
||||||
|
self.insert_child_at_index(parent_node, last_index, &op.nodes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_child_at_index(&mut self, parent: NodeId, index: usize, insert_children: &[NodeId]) {
|
||||||
|
if index == 0 && insert_children.len() == 0 {
|
||||||
|
for id in insert_children {
|
||||||
|
parent.append(*id, &mut self.arena);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let node_to_insert = self.child_at_index_of_path(parent, index).unwrap();
|
||||||
|
|
||||||
|
for id in insert_children {
|
||||||
|
node_to_insert.insert_before(*id, &mut self.arena);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_update(&self, _op: &UpdateOperation) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_delete(&self, _op: &DeleteOperation) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_text_edit(&self, _op: &TextEditOperation) {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,23 +35,23 @@ impl DocumentOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct InsertOperation {
|
pub struct InsertOperation {
|
||||||
path: Position,
|
pub path: Position,
|
||||||
nodes: Vec<NodeId>,
|
pub nodes: Vec<NodeId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UpdateOperation {
|
pub struct UpdateOperation {
|
||||||
path: Position,
|
pub path: Position,
|
||||||
attributes: NodeAttributes,
|
pub attributes: NodeAttributes,
|
||||||
old_attributes: NodeAttributes,
|
pub old_attributes: NodeAttributes,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DeleteOperation {
|
pub struct DeleteOperation {
|
||||||
path: Position,
|
pub path: Position,
|
||||||
nodes: Vec<NodeId>,
|
pub nodes: Vec<NodeId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TextEditOperation {
|
pub struct TextEditOperation {
|
||||||
path: Position,
|
pub path: Position,
|
||||||
delta: TextDelta,
|
pub delta: TextDelta,
|
||||||
inverted: TextDelta,
|
pub inverted: TextDelta,
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ fn main() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_documents() {
|
fn test_documents() {
|
||||||
let document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let tb = TransactionBuilder::new();
|
let tb = TransactionBuilder::new();
|
||||||
document.apply(tb.finalize());
|
document.apply(tb.finalize());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user