-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Custom undo/redo implementation (#97) * Fix after review (#97) * Fix after review (#97) * Fix after review (#97) * Fix after review (#97) * Auto-format (#97)
- Loading branch information
1 parent
117bce9
commit c64d071
Showing
18 changed files
with
723 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import '../code_controller.dart'; | ||
|
||
class RedoAction extends Action<RedoTextIntent> { | ||
final CodeController controller; | ||
|
||
RedoAction({ | ||
required this.controller, | ||
}); | ||
|
||
@override | ||
Object? invoke(RedoTextIntent intent) { | ||
controller.historyController.redo(); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import '../code_controller.dart'; | ||
|
||
class UndoAction extends Action<UndoTextIntent> { | ||
final CodeController controller; | ||
|
||
UndoAction({ | ||
required this.controller, | ||
}); | ||
|
||
@override | ||
Object? invoke(UndoTextIntent intent) { | ||
controller.historyController.undo(); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../code/code.dart'; | ||
import '../code_field/code_controller.dart'; | ||
import '../code_field/text_selection.dart'; | ||
import 'code_history_record.dart'; | ||
import 'limit_stack.dart'; | ||
|
||
/// A custom undo/redo implementation for [CodeController]. | ||
/// | ||
/// This is needed because the built-in implementation listens to the | ||
/// visible text changes in [TextEditingController] and sets that on undo/redo. | ||
/// This would delete hidden ranges and folded blocks. | ||
/// | ||
/// With this controller, new records are created: | ||
/// - If the line count has changed. | ||
/// - After the [idle] duration if the text has changed since the last record. | ||
/// - On any selection change other than that of inserting a single | ||
/// character, if the text has changed since the last record. | ||
class CodeHistoryController { | ||
final CodeController codeController; | ||
Code lastCode; | ||
TextSelection lastSelection; | ||
int _currentRecordIndex = 0; | ||
bool _isTextChanged = false; | ||
Timer? _debounceTimer; | ||
|
||
@visibleForTesting | ||
final stack = LimitStack<CodeHistoryRecord>(maxLength: limit); | ||
|
||
static const idle = Duration(seconds: 5); | ||
static const limit = 100; | ||
|
||
CodeHistoryController({ | ||
required this.codeController, | ||
}) : lastCode = codeController.code, | ||
lastSelection = codeController.value.selection { | ||
_push(); | ||
} | ||
|
||
void beforeChanged(Code code, TextSelection selection) { | ||
_dropRedoIfNeed(); | ||
bool shouldSave = false; | ||
|
||
if (_isTextChanged) { | ||
shouldSave = code.lines.lines.length != lastCode.lines.lines.length; | ||
} | ||
|
||
if (!shouldSave) { | ||
if (lastCode.text != code.text) { | ||
_isTextChanged = true; | ||
} | ||
|
||
if (_isTextChanged) { | ||
final isText1CharLonger = code.text.length == lastCode.text.length + 1; | ||
final isTypingContinuous = isText1CharLonger && | ||
selection.hasMovedOneCharacterRight(lastSelection); | ||
|
||
if (isTypingContinuous) { | ||
_setTimer(); | ||
} else { | ||
shouldSave = true; | ||
} | ||
} | ||
} | ||
|
||
if (shouldSave) { | ||
_push(); | ||
} | ||
|
||
lastCode = code; | ||
lastSelection = selection; | ||
} | ||
|
||
void _dropRedoIfNeed() { | ||
stack.removeAfter(_currentRecordIndex + 1); | ||
} | ||
|
||
void undo() { | ||
if (_isTextChanged) { | ||
_push(); | ||
} | ||
|
||
if (_currentRecordIndex == 0) { | ||
return; | ||
} | ||
|
||
_applyHistoryRecord(stack[--_currentRecordIndex]); | ||
} | ||
|
||
void redo() { | ||
if (_currentRecordIndex == stack.length - 1) { | ||
return; | ||
} | ||
|
||
_applyHistoryRecord(stack[++_currentRecordIndex]); | ||
} | ||
|
||
void _applyHistoryRecord(CodeHistoryRecord record) { | ||
lastCode = record.code; | ||
lastSelection = record.selection; | ||
|
||
codeController.applyHistoryRecord(record); | ||
} | ||
|
||
void _push() { | ||
_debounceTimer?.cancel(); | ||
_pushRecord(_createRecord()); | ||
_isTextChanged = false; | ||
} | ||
|
||
void _setTimer() { | ||
_debounceTimer?.cancel(); | ||
_debounceTimer = Timer(idle, _push); | ||
} | ||
|
||
CodeHistoryRecord _createRecord() { | ||
return CodeHistoryRecord( | ||
code: lastCode, | ||
selection: lastSelection, | ||
); | ||
} | ||
|
||
void _pushRecord(CodeHistoryRecord record) { | ||
stack.push(record); | ||
_currentRecordIndex = stack.length - 1; | ||
} | ||
|
||
void deleteHistory() { | ||
stack.clear(); | ||
_push(); | ||
_currentRecordIndex = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
import '../code/code.dart'; | ||
|
||
class CodeHistoryRecord with EquatableMixin { | ||
final Code code; | ||
final TextSelection selection; | ||
|
||
const CodeHistoryRecord({ | ||
required this.code, | ||
required this.selection, | ||
}); | ||
|
||
@override | ||
List<Object> get props => [ | ||
code, | ||
selection, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class LimitStack<T> extends Iterable<T> { | ||
final int maxLength; | ||
final _items = <T>[]; | ||
|
||
LimitStack({ | ||
required this.maxLength, | ||
}); | ||
|
||
@override | ||
int get length => _items.length; | ||
|
||
void push(T value) { | ||
_items.add(value); | ||
|
||
if (_items.length > maxLength) { | ||
_items.removeRange(0, _items.length - maxLength); | ||
} | ||
} | ||
|
||
void removeAfter(int n) { | ||
_items.removeRange(n, _items.length); | ||
} | ||
|
||
void clear() { | ||
_items.clear(); | ||
} | ||
|
||
T operator [](int n) { | ||
return _items[n]; | ||
} | ||
|
||
@override | ||
Iterator<T> get iterator => _items.iterator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.