Skip to content

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.

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.dart
index 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.dart
index 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.dart
index 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.lock
index f880223728..f6e21acf11 100644
--- a/frontend/appflowy_flutter/pubspec.lock
+++ b/frontend/appflowy_flutter/pubspec.lock
@@ -121,7 +121,7 @@ packages:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
- version: "3.1.0"
+ version: "3.1.1"
build_resolvers:
dependency: transitive
description:
@@ -156,7 +156,7 @@ packages:
name: built_value
url: "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_lifecycle
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.7"
+ version: "2.0.9"
flutter_svg:
dependency: transitive
description:
@@ -524,7 +524,7 @@ packages:
name: fluttertoast
url: "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: html
url: "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: logger
url: "https://pub.dartlang.org"
source: hosted
- version: "1.1.0"
+ version: "1.3.0"
logging:
dependency: transitive
description:
@@ -779,7 +779,7 @@ packages:
name: node_preamble
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.1"
+ version: "2.0.2"
package_config:
dependency: transitive
description:
@@ -856,35 +856,35 @@ packages:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.12"
+ version: "2.0.14"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.22"
+ version: "2.0.24"
path_provider_foundation:
dependency: transitive
description:
name: path_provider_foundation
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.1"
+ version: "2.2.0"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.8"
+ version: "2.1.10"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.5"
+ version: "2.0.6"
path_provider_windows:
dependency: transitive
description:
@@ -898,7 +898,7 @@ packages:
name: percent_indicator
url: "https://pub.dartlang.org"
source: hosted
- version: "4.2.2"
+ version: "4.2.3"
petitparser:
dependency: transitive
description:
@@ -919,7 +919,7 @@ packages:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.3"
+ version: "2.1.4"
pool:
dependency: transitive
description:
@@ -1024,7 +1024,7 @@ packages:
name: rich_clipboard_windows
url: "https://pub.dartlang.org"
source: hosted
- version: "1.0.1"
+ version: "1.0.2"
screen_retriever:
dependency: transitive
description:
@@ -1038,49 +1038,49 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.17"
+ version: "2.0.20"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.15"
+ version: "2.0.17"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.3"
+ version: "2.1.5"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.3"
+ version: "2.1.5"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.0"
+ version: "2.1.1"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.4"
+ version: "2.0.6"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.3"
+ version: "2.1.5"
shelf:
dependency: transitive
description:
@@ -1122,7 +1122,7 @@ packages:
name: sized_context
url: "https://pub.dartlang.org"
source: hosted
- version: "1.0.0+1"
+ version: "1.0.0+4"
sky_engine:
dependency: transitive
description: flutter
@@ -1155,7 +1155,7 @@ packages:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
- version: "0.10.11"
+ version: "0.10.12"
source_span:
dependency: transitive
description:
@@ -1295,56 +1295,56 @@ packages:
name: url_launcher
url: "https://pub.dartlang.org"
source: hosted
- version: "6.1.9"
+ version: "6.1.10"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
url: "https://pub.dartlang.org"
source: hosted
- version: "6.0.23"
+ version: "6.0.26"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
url: "https://pub.dartlang.org"
source: hosted
- version: "6.1.0"
+ version: "6.1.3"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
url: "https://pub.dartlang.org"
source: hosted
- version: "3.0.2"
+ version: "3.0.4"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
url: "https://pub.dartlang.org"
source: hosted
- version: "3.0.2"
+ version: "3.0.4"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
url: "https://pub.dartlang.org"
source: hosted
- version: "2.1.1"
+ version: "2.1.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
url: "https://pub.dartlang.org"
source: hosted
- version: "2.0.14"
+ version: "2.0.16"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
url: "https://pub.dartlang.org"
source: hosted
- version: "3.0.3"
+ version: "3.0.5"
uuid:
dependency: transitive
description:
@@ -1407,7 +1407,7 @@ packages:
name: window_manager
url: "https://pub.dartlang.org"
source: hosted
- version: "0.3.0"
+ version: "0.3.1"
xdg_directories:
dependency: transitive
description: