76 lines
1.5 KiB
Rust
Raw Normal View History

2021-08-05 20:05:40 +08:00
use crate::core::{Delta, Interval, OpBuilder, Operation};
const MAX_UNDOS: usize = 20;
2021-08-05 22:52:19 +08:00
#[derive(Debug, Clone)]
pub struct RevId(pub u64);
2021-08-05 20:05:40 +08:00
#[derive(Debug, Clone)]
pub struct Revision {
2021-08-05 22:52:19 +08:00
rev_id: RevId,
pub delta: Delta,
}
impl Revision {
pub fn new(rev_id: RevId, delta: Delta) -> Revision { Self { rev_id, delta } }
2021-08-05 20:05:40 +08:00
}
#[derive(Debug, Clone)]
pub struct UndoResult {
success: bool,
len: u64,
}
2021-08-05 22:52:19 +08:00
impl UndoResult {
pub fn fail() -> Self {
UndoResult {
success: false,
len: 0,
}
}
pub fn success(len: u64) -> Self { UndoResult { success: true, len } }
}
2021-08-05 20:05:40 +08:00
#[derive(Debug, Clone)]
pub struct History {
cur_undo: usize,
2021-08-05 22:52:19 +08:00
undos: Vec<Delta>,
redos: Vec<Delta>,
2021-08-05 20:05:40 +08:00
}
impl History {
pub fn new() -> Self {
History {
cur_undo: 1,
undos: Vec::new(),
redos: Vec::new(),
}
}
pub fn can_undo(&self) -> bool { !self.undos.is_empty() }
pub fn can_redo(&self) -> bool { !self.redos.is_empty() }
2021-08-05 22:52:19 +08:00
pub fn add_undo(&mut self, delta: Delta) { self.undos.push(delta); }
2021-08-05 20:05:40 +08:00
2021-08-05 22:52:19 +08:00
pub fn add_redo(&mut self, delta: Delta) { self.redos.push(delta); }
pub fn undo(&mut self) -> Option<Delta> {
2021-08-05 20:05:40 +08:00
if !self.can_undo() {
return None;
}
2021-08-05 22:52:19 +08:00
let delta = self.undos.pop().unwrap();
Some(delta)
}
2021-08-05 20:05:40 +08:00
2021-08-05 22:52:19 +08:00
pub fn redo(&mut self) -> Option<Delta> {
if !self.can_redo() {
return None;
}
2021-08-05 20:05:40 +08:00
2021-08-05 22:52:19 +08:00
let delta = self.redos.pop().unwrap();
Some(delta)
2021-08-05 20:05:40 +08:00
}
}