mod error;
mod map;
mod ops;
use std::ops::Range;
pub use error::StepError;
pub use map::Mapped;
pub use map::StepMap;
use crate::Attrs;
use crate::BlockId;
use crate::BlockTree;
use crate::BlockTypeId;
use crate::Document;
use crate::InlineText;
use crate::Mark;
use crate::MarkSet;
use crate::MarkTypeId;
#[derive(Clone, Debug, PartialEq)]
pub enum Step {
InsertInline {
block: BlockId,
offset: u32,
content: InlineText,
},
RemoveInline {
block: BlockId,
range: Range<u32>,
},
AddMark {
block: BlockId,
range: Range<u32>,
mark: Mark,
},
RemoveMark {
block: BlockId,
range: Range<u32>,
kind: MarkTypeId,
},
RestoreMarks {
block: BlockId,
offset: u32,
runs: Vec<(u32, MarkSet)>,
},
SetBlock {
block: BlockId,
kind: BlockTypeId,
attrs: Attrs,
},
Split {
block: BlockId,
offset: u32,
new_block: BlockId,
kind: BlockTypeId,
attrs: Attrs,
},
Join {
block: BlockId,
next: BlockId,
},
InsertBlocks {
parent: Option<BlockId>,
index: usize,
blocks: Vec<BlockTree>,
},
RemoveBlocks {
parent: Option<BlockId>,
index: usize,
count: usize,
},
MoveBlocks {
parent: Option<BlockId>,
index: usize,
count: usize,
to_parent: Option<BlockId>,
to_index: usize,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Applied {
pub inverse: Step,
pub map: StepMap,
pub touched: Vec<BlockId>,
pub structural: bool,
}
impl Applied {
fn inline(inverse: Step, map: StepMap, block: BlockId) -> Self {
Self {
inverse,
map,
touched: vec![block],
structural: false,
}
}
fn structural(inverse: Step, map: StepMap, touched: Vec<BlockId>) -> Self {
Self {
inverse,
map,
touched,
structural: true,
}
}
}
impl Step {
pub fn apply(self, doc: &mut Document) -> Result<Applied, StepError> {
doc.record_before(&self);
let applied = match self {
Self::InsertInline {
block,
offset,
content,
} => ops::insert_inline(doc, block, offset, content),
Self::RemoveInline { block, range } => ops::remove_inline(doc, block, range),
Self::AddMark { block, range, mark } => {
let schema = doc.schema().clone();
ops::format(doc, block, range, |text, range| {
text.add_mark(range, &mark, &schema)
})
}
Self::RemoveMark { block, range, kind } => {
ops::format(doc, block, range, |text, range| {
text.remove_mark(range, kind)
})
}
Self::RestoreMarks {
block,
offset,
runs,
} => ops::restore_marks(doc, block, offset, &runs),
Self::SetBlock { block, kind, attrs } => ops::set_block(doc, block, kind, attrs),
Self::Split {
block,
offset,
new_block,
kind,
attrs,
} => {
ops::split(
doc,
block,
offset,
BlockTree::text(new_block, kind, InlineText::new()).with_attrs(attrs),
)
}
Self::Join { block, next } => ops::join(doc, block, next),
Self::InsertBlocks {
parent,
index,
blocks,
} => ops::insert_blocks(doc, parent, index, blocks),
Self::RemoveBlocks {
parent,
index,
count,
} => ops::remove_blocks(doc, parent, index, count),
Self::MoveBlocks {
parent,
index,
count,
to_parent,
to_index,
} => ops::move_blocks(doc, (parent, index, count), (to_parent, to_index)),
}?;
doc.bump_version();
doc.record_inverse(&applied.inverse);
Ok(applied)
}
}