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
chore: add ai writer keyboard shortcuts (#7516)
Richard Shiue555254e8febd
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dartindex 5221ab3766..74d1364436 100644--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dart+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dart@@ -12,6 +12,7 @@ import 'package:desktop_drop/desktop_drop.dart';import 'package:easy_localization/easy_localization.dart';import 'package:flowy_infra_ui/flowy_infra_ui.dart';import 'package:flutter/material.dart';+import 'package:flutter/services.dart';import 'package:flutter_bloc/flutter_bloc.dart';import 'package:flutter_chat_core/flutter_chat_core.dart';import 'package:flutter_chat_ui/flutter_chat_ui.dart'@@ -92,9 +93,19 @@ class AIChatPage extends StatelessWidget {}}},- child: _ChatContentPage(- view: view,- userProfile: userProfile,+ child: CallbackShortcuts(+ bindings: {+ SingleActivator(LogicalKeyboardKey.escape): () {+ context.read<ChatBloc>().add(ChatEvent.stopStream());+ },+ SingleActivator(control: true, LogicalKeyboardKey.keyC): () {+ context.read<ChatBloc>().add(ChatEvent.stopStream());+ },+ },+ child: _ChatContentPage(+ view: view,+ userProfile: userProfile,+ ),),);},diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dartindex ed77fadae4..aee2e2cb50 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart@@ -100,7 +100,6 @@ class AiWriterBlockComponent extends BlockComponentStatefulWidget {class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {final key = GlobalKey();final textController = TextEditingController();- final textFieldFocusNode = FocusNode();final overlayController = OverlayPortalController();final layerLink = LayerLink();@@ -118,7 +117,6 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {WidgetsBinding.instance.addPostFrameCallback((_) {overlayController.show();- textFieldFocusNode.requestFocus();if (!widget.node.isAiWriterInitialized) {aiWriterCubit.init();}@@ -128,7 +126,6 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {@overridevoid dispose() {textController.dispose();- textFieldFocusNode.dispose();aiWriterCubit.close();super.dispose();}@@ -154,12 +151,24 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {builder: (context, constraints) {return BlocListener<AiWriterCubit, AiWriterState>(listener: (context, state) {- if (state is SingleShotAiWriterState) {+ if (state is FailedContinueWritingAiWriterState) {showConfirmDialog(context: context,- title: state.title,- description: state.description,- onConfirm: state.onDismiss,+ title: LocaleKeys.ai_continueWritingEmptyDocumentTitle.tr(),+ description: LocaleKeys+ .ai_continueWritingEmptyDocumentDescription+ .tr(),+ onConfirm: state.onConfirm,+ );+ } else if (state is DiscardResponseAiWriterState) {+ showConfirmDialog(+ context: context,+ title: LocaleKeys.button_discard.tr(),+ description: LocaleKeys.document_plugins_discardResponse.tr(),+ confirmLabel: LocaleKeys.button_discard.tr(),+ style: ConfirmPopupStyle.cancelAndOk,+ onConfirm: state.onDiscard,+ onCancel: () {},);}},@@ -174,7 +183,7 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {behavior: state is GeneratingAiWriterState? HitTestBehavior.opaque: HitTestBehavior.translucent,- onPointerEvent: () => onTapOutside(),+ onPointerEvent: onTapOutside,);},),diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dartindex 56cdacdd6a..d31dc45dcd 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart@@ -10,6 +10,7 @@ import 'package:appflowy_result/appflowy_result.dart';import 'package:bloc/bloc.dart';import 'package:easy_localization/easy_localization.dart';import 'package:flutter/foundation.dart';+import 'package:flutter/services.dart';import '../../base/markdown_text_robot.dart';import 'ai_writer_block_operations.dart';@@ -32,6 +33,7 @@ class AiWriterCubit extends Cubit<AiWriterState> {isFirstRun: true,),) {+ HardwareKeyboard.instance.addHandler(_cancelShortcutHandler);editorState.service.keyboardService?.disableShortcuts();}@@ -49,6 +51,7 @@ class AiWriterCubit extends Cubit<AiWriterState> {@overrideFuture<void> close() async {selectedSourcesNotifier.dispose();+ HardwareKeyboard.instance.removeHandler(_cancelShortcutHandler);editorState.service.keyboardService?.enableShortcuts();await super.close();}@@ -147,6 +150,9 @@ class AiWriterCubit extends Cubit<AiWriterState> {if (state is! GeneratingAiWriterState) {return;}+ await _textRobot.stop(+ attributes: ApplySuggestionFormatType.replace.attributes,+ );final generatingState = state as GeneratingAiWriterState;await AIEventStopCompleteText(CompleteTextTaskPB(@@ -311,12 +317,9 @@ class AiWriterCubit extends Cubit<AiWriterState> {view.name == LocaleKeys.menuAppHeader_defaultNewPageName.tr()) {final readyState = state as ReadyAiWriterState;emit(- SingleShotAiWriterState(+ FailedContinueWritingAiWriterState(command,- title: LocaleKeys.ai_continueWritingEmptyDocumentTitle.tr(),- description:- LocaleKeys.ai_continueWritingEmptyDocumentDescription.tr(),- onDismiss: () {+ onConfirm: () {if (isImmediateRun) {removeAiWriterNode(editorState, node);}@@ -486,6 +489,46 @@ class AiWriterCubit extends Cubit<AiWriterState> {);}}++ bool _cancelShortcutHandler(KeyEvent event) {+ if (event is! KeyUpEvent) {+ return false;+ }++ switch (event.logicalKey) {+ case LogicalKeyboardKey.escape:+ if (state case GeneratingAiWriterState _) {+ stopStream();+ } else if (hasUnusedResponse()) {+ final saveState = state;+ emit(+ FailedContinueWritingAiWriterState(+ state.command,+ onConfirm: () {+ stopStream();+ exit();+ },+ ),+ );+ emit(saveState);+ } else {+ stopStream();+ exit();+ }+ return true;+ case LogicalKeyboardKey.keyC+ when HardwareKeyboard.instance.logicalKeysPressed+ .contains(LogicalKeyboardKey.controlLeft):+ if (state case GeneratingAiWriterState _) {+ stopStream();+ }+ return true;+ default:+ break;+ }++ return false;+ }}sealed class AiWriterState {@@ -527,15 +570,20 @@ class ErrorAiWriterState extends AiWriterState {final AIError error;}-class SingleShotAiWriterState extends AiWriterState {- const SingleShotAiWriterState(+class FailedContinueWritingAiWriterState extends AiWriterState {+ const FailedContinueWritingAiWriterState(+ super.command, {+ required this.onConfirm,+ });++ final void Function() onConfirm;+}++class DiscardResponseAiWriterState extends AiWriterState {+ const DiscardResponseAiWriterState(super.command, {- required this.title,- required this.description,- required this.onDismiss,+ required this.onDiscard,});- final String title;- final String description;- final void Function() onDismiss;+ final void Function() onDiscard;}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_gesture_detector.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_gesture_detector.dartindex 6b2ffd4692..84f785335b 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_gesture_detector.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_gesture_detector.dart@@ -21,18 +21,14 @@ class AiWriterGestureDetector extends StatelessWidget {TapGestureRecognizer:GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(() => TapGestureRecognizer(),- (instance) {- instance- ..onTap = onPointerEvent- ..onTapDown = (_) => onPointerEvent();- },+ (instance) => instance..onTapDown = (_) => onPointerEvent(),),ImmediateMultiDragGestureRecognizer:GestureRecognizerFactoryWithHandlers<- ImmediateMultiDragGestureRecognizer>(- () => ImmediateMultiDragGestureRecognizer(), (instance) {- instance.onStart = (offset) => null;- }),+ ImmediateMultiDragGestureRecognizer>(+ () => ImmediateMultiDragGestureRecognizer(),+ (instance) => instance.onStart = (offset) => null,+ ),},child: child,);