Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
feat: trigger shortcut event by character (#2101)
Lucas.Xu1536cdd15a5c
diff --git a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/keyboard_service.dart b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/keyboard_service.dartindex b903fed4f8..2068a2a0f0 100644--- a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/keyboard_service.dart+++ b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/keyboard_service.dart@@ -129,7 +129,7 @@ class _AppFlowyKeyboardState extends State<AppFlowyKeyboard>// TODO: use cache to optimize the searching time.for (final shortcutEvent in widget.shortcutEvents) {- if (shortcutEvent.keybindings.containsKeyEvent(event)) {+ if (shortcutEvent.canRespondToRawKeyEvent(event)) {final result = shortcutEvent.handler(widget.editorState, event);if (result == KeyEventResult.handled) {return KeyEventResult.handled;@@ -157,3 +157,10 @@ class _AppFlowyKeyboardState extends State<AppFlowyKeyboard>return onKey(event);}}++extension on ShortcutEvent {+ bool canRespondToRawKeyEvent(RawKeyEvent event) {+ return ((character?.isNotEmpty ?? false) && character == event.character) ||+ keybindings.containsKeyEvent(event);+ }+}diff --git a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dartindex b4566acf92..825dc0b297 100644--- a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart+++ b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart@@ -247,7 +247,7 @@ List<ShortcutEvent> builtInShortcutEvents = [),ShortcutEvent(key: 'selection menu',- command: 'slash,shift+slash',+ character: '/',handler: slashShortcutHandler,),ShortcutEvent(@@ -304,7 +304,7 @@ List<ShortcutEvent> builtInShortcutEvents = [),ShortcutEvent(key: 'Underscore to italic',- command: 'shift+underscore',+ character: '_',handler: underscoreToItalicHandler,),ShortcutEvent(diff --git a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/shortcut_event.dart b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/shortcut_event.dartindex fb1a245b00..bba5aff1c3 100644--- a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/shortcut_event.dart+++ b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/shortcut_event/shortcut_event.dart@@ -8,12 +8,29 @@ import 'package:flutter/foundation.dart';class ShortcutEvent {ShortcutEvent({required this.key,- required this.command,+ this.character,+ this.command,required this.handler,String? windowsCommand,String? macOSCommand,String? linuxCommand,}) {+ // character and command cannot be null at the same time+ assert(+ !(character == null &&+ command == null &&+ windowsCommand == null &&+ macOSCommand == null &&+ linuxCommand == null),+ 'character and command cannot be null at the same time');+ assert(+ !(character != null &&+ (command != null &&+ windowsCommand != null &&+ macOSCommand != null &&+ linuxCommand != null)),+ 'character and command cannot be set at the same time');+updateCommand(command: command,windowsCommand: windowsCommand,@@ -43,7 +60,9 @@ class ShortcutEvent {////// Like, 'ctrl+c,cmd+c'///- String command;+ String? command;++ String? character;final ShortcutEventHandler handler;@@ -80,9 +99,9 @@ class ShortcutEvent {matched = true;}- if (matched) {+ if (matched && this.command != null) {_keybindings = this- .command+ .command!.split(',').map((e) => Keybinding.parse(e)).toList(growable: false);diff --git a/frontend/appflowy_flutter/pubspec.lock b/frontend/appflowy_flutter/pubspec.lockindex f880223728..f6e21acf11 100644--- a/frontend/appflowy_flutter/pubspec.lock+++ b/frontend/appflowy_flutter/pubspec.lock@@ -121,7 +121,7 @@ packages:name: build_daemonurl: "https://pub.dartlang.org"source: hosted- version: "3.1.0"+ version: "3.1.1"build_resolvers:dependency: transitivedescription:@@ -156,7 +156,7 @@ packages:name: built_valueurl: "https://pub.dartlang.org"source: hosted- version: "8.4.3"+ version: "8.4.4"calendar_view:dependency: "direct main"description:@@ -500,7 +500,7 @@ packages:name: flutter_plugin_android_lifecycleurl: "https://pub.dartlang.org"source: hosted- version: "2.0.7"+ version: "2.0.9"flutter_svg:dependency: transitivedescription:@@ -524,7 +524,7 @@ packages:name: fluttertoasturl: "https://pub.dartlang.org"source: hosted- version: "8.1.2"+ version: "8.2.1"freezed:dependency: "direct dev"description:@@ -599,7 +599,7 @@ packages:name: htmlurl: "https://pub.dartlang.org"source: hosted- version: "0.15.1"+ version: "0.15.2"http:dependency: "direct main"description:@@ -709,7 +709,7 @@ packages:name: loggerurl: "https://pub.dartlang.org"source: hosted- version: "1.1.0"+ version: "1.3.0"logging:dependency: transitivedescription:@@ -779,7 +779,7 @@ packages:name: node_preambleurl: "https://pub.dartlang.org"source: hosted- version: "2.0.1"+ version: "2.0.2"package_config:dependency: transitivedescription:@@ -856,35 +856,35 @@ packages:name: path_providerurl: "https://pub.dartlang.org"source: hosted- version: "2.0.12"+ version: "2.0.14"path_provider_android:dependency: transitivedescription:name: path_provider_androidurl: "https://pub.dartlang.org"source: hosted- version: "2.0.22"+ version: "2.0.24"path_provider_foundation:dependency: transitivedescription:name: path_provider_foundationurl: "https://pub.dartlang.org"source: hosted- version: "2.1.1"+ version: "2.2.0"path_provider_linux:dependency: transitivedescription:name: path_provider_linuxurl: "https://pub.dartlang.org"source: hosted- version: "2.1.8"+ version: "2.1.10"path_provider_platform_interface:dependency: transitivedescription:name: path_provider_platform_interfaceurl: "https://pub.dartlang.org"source: hosted- version: "2.0.5"+ version: "2.0.6"path_provider_windows:dependency: transitivedescription:@@ -898,7 +898,7 @@ packages:name: percent_indicatorurl: "https://pub.dartlang.org"source: hosted- version: "4.2.2"+ version: "4.2.3"petitparser:dependency: transitivedescription:@@ -919,7 +919,7 @@ packages:name: plugin_platform_interfaceurl: "https://pub.dartlang.org"source: hosted- version: "2.1.3"+ version: "2.1.4"pool:dependency: transitivedescription:@@ -1024,7 +1024,7 @@ packages:name: rich_clipboard_windowsurl: "https://pub.dartlang.org"source: hosted- version: "1.0.1"+ version: "1.0.2"screen_retriever:dependency: transitivedescription:@@ -1038,49 +1038,49 @@ packages:name: shared_preferencesurl: "https://pub.dartlang.org"source: hosted- version: "2.0.17"+ version: "2.0.20"shared_preferences_android:dependency: transitivedescription:name: shared_preferences_androidurl: "https://pub.dartlang.org"source: hosted- version: "2.0.15"+ version: "2.0.17"shared_preferences_foundation:dependency: transitivedescription:name: shared_preferences_foundationurl: "https://pub.dartlang.org"source: hosted- version: "2.1.3"+ version: "2.1.5"shared_preferences_linux:dependency: transitivedescription:name: shared_preferences_linuxurl: "https://pub.dartlang.org"source: hosted- version: "2.1.3"+ version: "2.1.5"shared_preferences_platform_interface:dependency: transitivedescription:name: shared_preferences_platform_interfaceurl: "https://pub.dartlang.org"source: hosted- version: "2.1.0"+ version: "2.1.1"shared_preferences_web:dependency: transitivedescription:name: shared_preferences_weburl: "https://pub.dartlang.org"source: hosted- version: "2.0.4"+ version: "2.0.6"shared_preferences_windows:dependency: transitivedescription:name: shared_preferences_windowsurl: "https://pub.dartlang.org"source: hosted- version: "2.1.3"+ version: "2.1.5"shelf:dependency: transitivedescription:@@ -1122,7 +1122,7 @@ packages:name: sized_contexturl: "https://pub.dartlang.org"source: hosted- version: "1.0.0+1"+ version: "1.0.0+4"sky_engine:dependency: transitivedescription: flutter@@ -1155,7 +1155,7 @@ packages:name: source_mapsurl: "https://pub.dartlang.org"source: hosted- version: "0.10.11"+ version: "0.10.12"source_span:dependency: transitivedescription:@@ -1295,56 +1295,56 @@ packages:name: url_launcherurl: "https://pub.dartlang.org"source: hosted- version: "6.1.9"+ version: "6.1.10"url_launcher_android:dependency: transitivedescription:name: url_launcher_androidurl: "https://pub.dartlang.org"source: hosted- version: "6.0.23"+ version: "6.0.26"url_launcher_ios:dependency: transitivedescription:name: url_launcher_iosurl: "https://pub.dartlang.org"source: hosted- version: "6.1.0"+ version: "6.1.3"url_launcher_linux:dependency: transitivedescription:name: url_launcher_linuxurl: "https://pub.dartlang.org"source: hosted- version: "3.0.2"+ version: "3.0.4"url_launcher_macos:dependency: transitivedescription:name: url_launcher_macosurl: "https://pub.dartlang.org"source: hosted- version: "3.0.2"+ version: "3.0.4"url_launcher_platform_interface:dependency: transitivedescription:name: url_launcher_platform_interfaceurl: "https://pub.dartlang.org"source: hosted- version: "2.1.1"+ version: "2.1.2"url_launcher_web:dependency: transitivedescription:name: url_launcher_weburl: "https://pub.dartlang.org"source: hosted- version: "2.0.14"+ version: "2.0.16"url_launcher_windows:dependency: transitivedescription:name: url_launcher_windowsurl: "https://pub.dartlang.org"source: hosted- version: "3.0.3"+ version: "3.0.5"uuid:dependency: transitivedescription:@@ -1407,7 +1407,7 @@ packages:name: window_managerurl: "https://pub.dartlang.org"source: hosted- version: "0.3.0"+ version: "0.3.1"xdg_directories:dependency: transitivedescription: