update undo package

This commit is contained in:
2026-01-15 17:28:55 -08:00
parent 92a8c580b6
commit 20d794bbca
165 changed files with 2119 additions and 1230 deletions
+23
View File
@@ -0,0 +1,23 @@
class Change<T> {
Change(
this._oldValue,
this._execute(),
this._undo(T oldValue), {
this.description = '',
});
final String description;
final void Function() _execute;
final T _oldValue;
final void Function(T oldValue) _undo;
void execute() {
_execute();
}
void undo() {
_undo(_oldValue);
}
}
+43
View File
@@ -0,0 +1,43 @@
import 'package:undo/undo.dart';
class SimpleStack<T> extends ChangeStack {
/// Simple stack for keeping track of changes and easy callback for new state changes
SimpleStack(
this._state, {
int? limit,
this.onUpdate,
}) : super(limit: limit) {
if (onUpdate != null) {
onUpdate!(_state);
}
}
late T _state;
/// Current state
T get state => _state;
set state(T val) => modify(val);
void Function(T val)? onUpdate;
void modify(T val) {
try {
add(Change<T>(
_state,
() => _newValue(val),
_newValue,
));
} catch (e) {
rethrow;
}
}
void _newValue(T val) {
_state = val;
if (onUpdate != null) {
onUpdate!(val);
}
}
}
+92
View File
@@ -0,0 +1,92 @@
import 'dart:collection';
import 'change.dart';
class ChangeStack {
/// Changes to keep track of
ChangeStack({this.limit});
/// Limit changes to store in the history
int? limit;
final Queue<List<Change>> _history = ListQueue();
final Queue<List<Change>> _redos = ListQueue();
/// List of changes in the history
List<Change> get history => _history.last;
/// List of changes in the redo stack
List<Change> get redos => _redos.first;
/// Can redo the previous change
bool get canRedo => _redos.isNotEmpty;
/// Can undo the previous change
bool get canUndo => _history.isNotEmpty;
/// Add New Change and Clear Redo Stack
void add<T>(Change<T> change) {
try {
change.execute();
_history.addLast([change]);
_moveForward();
} catch (e) {
rethrow;
}
}
void _moveForward() {
_redos.clear();
if (limit != null && _history.length > limit! + 1) {
_history.removeFirst();
}
}
/// Add New Group of Changes and Clear Redo Stack
void addGroup<T>(List<Change<T>> changes) {
try {
_applyChanges(changes);
_history.addLast(changes);
_moveForward();
} catch (e) {
rethrow;
}
}
void _applyChanges(List<Change> changes) {
for (final change in changes) {
change.execute();
}
}
/// Clear Undo History
@deprecated
void clear() => clearHistory();
/// Clear Undo History
void clearHistory() {
_history.clear();
_redos.clear();
}
/// Redo Previous Undo
void redo() {
if (canRedo) {
final changes = _redos.removeFirst();
_applyChanges(changes);
_history.addLast(changes);
}
}
/// Undo Last Change
void undo() {
if (canUndo) {
final changes = _history.removeLast();
for (final change in changes) {
change.undo();
}
_redos.addFirst(changes);
}
}
}