Feature: history shortcuts (#400)

pull/401/head
Dmitry Sboychakov 4 years ago committed by GitHub
parent 2009a0d1ef
commit 0a67139f18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      lib/src/widgets/controller.dart
  2. 27
      lib/src/widgets/keyboard_listener.dart
  3. 12
      lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart

@ -85,7 +85,7 @@ class QuillController extends ChangeNotifier {
}
void _handleHistoryChange(int? len) {
if (len! > 0) {
if (len != 0) {
// if (this.selection.extentOffset >= document.length) {
// // cursor exceeds the length of document, position it in the end
// updateSelection(

@ -2,7 +2,19 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
enum InputShortcut { CUT, COPY, PASTE, SELECT_ALL }
//fixme workaround flutter MacOS issue https://github.com/flutter/flutter/issues/75595
extension _LogicalKeyboardKeyCaseExt on LogicalKeyboardKey {
static const _kUpperToLowerDist = 0x20;
static final _kLowerCaseA = LogicalKeyboardKey.keyA.keyId;
static final _kLowerCaseZ = LogicalKeyboardKey.keyZ.keyId;
LogicalKeyboardKey toUpperCase() {
if (keyId < _kLowerCaseA || keyId > _kLowerCaseZ) return this;
return LogicalKeyboardKey(keyId - _kUpperToLowerDist);
}
}
enum InputShortcut { CUT, COPY, PASTE, SELECT_ALL, UNDO, REDO }
typedef CursorMoveCallback = void Function(
LogicalKeyboardKey key, bool wordModifier, bool lineModifier, bool shift);
@ -28,6 +40,8 @@ class KeyboardEventHandler {
LogicalKeyboardKey.keyC,
LogicalKeyboardKey.keyV,
LogicalKeyboardKey.keyX,
LogicalKeyboardKey.keyZ.toUpperCase(),
LogicalKeyboardKey.keyZ,
LogicalKeyboardKey.delete,
LogicalKeyboardKey.backspace,
};
@ -88,14 +102,21 @@ class KeyboardEventHandler {
final isShortcutModifierPressed =
isMacOS ? event.isMetaPressed : event.isControlPressed;
if (_moveKeys.contains(key)) {
onCursorMove(
key,
isMacOS ? event.isAltPressed : event.isControlPressed,
isMacOS ? event.isMetaPressed : event.isAltPressed,
event.isShiftPressed);
} else if (isShortcutModifierPressed && _shortcutKeys.contains(key)) {
onShortcut(_keyToShortcut[key]);
} else if (isShortcutModifierPressed && (_shortcutKeys.contains(key))) {
if (key == LogicalKeyboardKey.keyZ ||
key == LogicalKeyboardKey.keyZ.toUpperCase()) {
onShortcut(
event.isShiftPressed ? InputShortcut.REDO : InputShortcut.UNDO);
} else {
onShortcut(_keyToShortcut[key]);
}
} else if (key == LogicalKeyboardKey.delete) {
onDelete(true);
} else if (key == LogicalKeyboardKey.backspace) {

@ -76,6 +76,18 @@ mixin RawEditorStateKeyboardMixin on EditorState {
}
return;
}
if (shortcut == InputShortcut.UNDO) {
if (widget.controller.hasUndo) {
widget.controller.undo();
}
return;
}
if (shortcut == InputShortcut.REDO) {
if (widget.controller.hasRedo) {
widget.controller.redo();
}
return;
}
if (shortcut == InputShortcut.CUT && !widget.readOnly) {
if (!selection.isCollapsed) {
final data = selection.textInside(plainText);

Loading…
Cancel
Save