feat: add comment and fix issues

This commit is contained in:
Vincent Chan 2022-07-21 10:57:48 +08:00
parent f4bbe77612
commit 0626912a4c
2 changed files with 34 additions and 31 deletions

View File

@ -6,9 +6,12 @@ import 'package:flutter/material.dart';
import './document/selection.dart';
class ApplyOptions {
final bool noLog;
/// This flag indicates that
/// whether the transaction should be recorded into
/// the undo stack.
final bool recordUndo;
const ApplyOptions({
this.noLog = false,
this.recordUndo = true,
});
}
@ -45,19 +48,16 @@ class EditorState {
}
cursorSelection = transaction.afterSelection;
if (options.noLog) {
return;
if (options.recordUndo) {
final undoItem = undoManager.getUndoHistoryItem();
undoItem.addAll(transaction.operations);
if (undoItem.beforeSelection == null &&
transaction.beforeSelection != null) {
undoItem.beforeSelection = transaction.beforeSelection;
}
undoItem.afterSelection = transaction.afterSelection;
_debouncedSealHistoryItem();
}
final undoItem = undoManager.getUndoHistoryItem();
undoItem.addAll(transaction.operations);
if (undoItem.beforeSelection == null &&
transaction.beforeSelection != null) {
undoItem.beforeSelection = transaction.beforeSelection;
}
undoItem.afterSelection = transaction.afterSelection;
_debouncedSealHistoryItem();
}
_debouncedSealHistoryItem() {

View File

@ -6,6 +6,9 @@ import 'package:flowy_editor/operation/transaction_builder.dart';
import 'package:flowy_editor/operation/transaction.dart';
import 'package:flowy_editor/editor_state.dart';
/// This class contains operations to committed by users.
/// If a [HistoryItem] is not sealed, operations can be added sequentially.
/// Otherwise, the operations should be added to a new [HistoryItem].
class HistoryItem extends LinkedListEntry<HistoryItem> {
final List<Operation> operations = [];
Selection? beforeSelection;
@ -18,6 +21,8 @@ class HistoryItem extends LinkedListEntry<HistoryItem> {
_sealed = true;
}
bool get sealed => _sealed;
add(Operation op) {
operations.add(op);
}
@ -26,10 +31,6 @@ class HistoryItem extends LinkedListEntry<HistoryItem> {
operations.addAll(iterable);
}
bool get sealed {
return _sealed;
}
Transaction toTransaction(EditorState state) {
final builder = TransactionBuilder(state);
for (var i = operations.length - 1; i >= 0; i--) {
@ -67,24 +68,22 @@ class FixedSizeStack {
return last;
}
HistoryItem get last {
return _list.last;
}
HistoryItem get last => _list.last;
bool get isEmpty {
return _list.isEmpty;
}
bool get isEmpty => _list.isEmpty;
bool get isNonEmpty {
return _list.isNotEmpty;
}
bool get isNonEmpty => _list.isNotEmpty;
}
class UndoManager {
final undoStack = FixedSizeStack(20);
final redoStack = FixedSizeStack(20);
final FixedSizeStack undoStack;
final FixedSizeStack redoStack;
EditorState? state;
UndoManager([int stackSize = 20])
: undoStack = FixedSizeStack(stackSize),
redoStack = FixedSizeStack(stackSize);
HistoryItem getUndoHistoryItem() {
if (undoStack.isEmpty) {
final item = HistoryItem();
@ -101,11 +100,15 @@ class UndoManager {
}
undo() {
final s = state;
if (s == null) {
return;
}
final historyItem = undoStack.pop();
if (historyItem == null) {
return;
}
final transaction = historyItem.toTransaction(state!);
state!.apply(transaction, const ApplyOptions(noLog: true));
final transaction = historyItem.toTransaction(s);
s.apply(transaction, const ApplyOptions(recordUndo: false));
}
}