history: Ensure unique History in redos and undos when push. (#761)
This commit is contained in:
parent
ebe93210af
commit
99e7f8ee70
1 changed files with 14 additions and 1 deletions
|
|
@ -13,6 +13,11 @@ pub trait HistoryItem: Clone + PartialEq {
|
||||||
/// This is now used in Input for undo/redo operations. You can also use this in
|
/// This is now used in Input for undo/redo operations. You can also use this in
|
||||||
/// your own models to keep track of changes, for example to track the tab
|
/// your own models to keep track of changes, for example to track the tab
|
||||||
/// history for prev/next features.
|
/// history for prev/next features.
|
||||||
|
///
|
||||||
|
/// ## Use cases
|
||||||
|
///
|
||||||
|
/// - Undo/redo operations in Input
|
||||||
|
/// - Tracking tab history for prev/next features
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct History<I: HistoryItem> {
|
pub struct History<I: HistoryItem> {
|
||||||
undos: Vec<I>,
|
undos: Vec<I>,
|
||||||
|
|
@ -86,6 +91,7 @@ where
|
||||||
|
|
||||||
if self.unique {
|
if self.unique {
|
||||||
self.undos.retain(|c| *c != item);
|
self.undos.retain(|c| *c != item);
|
||||||
|
self.redos.retain(|c| *c != item);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut item = item;
|
let mut item = item;
|
||||||
|
|
@ -251,6 +257,13 @@ mod tests {
|
||||||
assert_eq!(changes.len(), 1);
|
assert_eq!(changes.len(), 1);
|
||||||
assert_eq!(changes[0].tab_index, 1);
|
assert_eq!(changes[0].tab_index, 1);
|
||||||
|
|
||||||
|
assert_eq!(history.redos().len(), 1);
|
||||||
|
// Push duplicate, should be ignored
|
||||||
|
history.push(2.into());
|
||||||
|
|
||||||
|
assert_eq!(history.undos().len(), 2);
|
||||||
|
assert_eq!(history.redos().len(), 1);
|
||||||
|
|
||||||
// Redo the last undone change
|
// Redo the last undone change
|
||||||
let changes = history.redo().unwrap();
|
let changes = history.redo().unwrap();
|
||||||
assert_eq!(changes.len(), 1);
|
assert_eq!(changes.len(), 1);
|
||||||
|
|
@ -260,7 +273,7 @@ mod tests {
|
||||||
history.push(3.into());
|
history.push(3.into());
|
||||||
|
|
||||||
// Check the version and undo stack
|
// Check the version and undo stack
|
||||||
assert_eq!(history.version(), 6);
|
assert_eq!(history.version(), 7);
|
||||||
assert_eq!(history.undos().len(), 4);
|
assert_eq!(history.undos().len(), 4);
|
||||||
|
|
||||||
// Undo all changes
|
// Undo all changes
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue