From 0a67139f18821e15b97145ffd89ecb5a53aac860 Mon Sep 17 00:00:00 2001 From: Dmitry Sboychakov Date: Tue, 14 Sep 2021 09:56:18 +0530 Subject: [PATCH] Feature: history shortcuts (#400) --- lib/src/widgets/controller.dart | 2 +- lib/src/widgets/keyboard_listener.dart | 27 ++++++++++++++++--- .../raw_editor_state_keyboard_mixin.dart | 12 +++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index 7d2e0714..b3173ed2 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.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( diff --git a/lib/src/widgets/keyboard_listener.dart b/lib/src/widgets/keyboard_listener.dart index b1b868aa..4e755fdf 100644 --- a/lib/src/widgets/keyboard_listener.dart +++ b/lib/src/widgets/keyboard_listener.dart @@ -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) { diff --git a/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart b/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart index 9ba6502d..db860248 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart @@ -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);