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.

chore: adjust replace, insert below, discard selection behavior (#7719)

Richard Shiuebcb1e8e4f569
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.dart
index 7a885a3fe8..7b9da81d44 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
@@ -57,8 +57,13 @@ class AiWriterCubit extends Cubit<AiWriterState> {
bool withDiscard = true,
bool withUnformat = true,
}) async {
+ if (aiWriterNode == null) {
+ return;
+ }
if (withDiscard) {
- await _textRobot.discard();
+ await _textRobot.discard(
+ afterSelection: aiWriterNode!.aiWriterSelection,
+ );
}
_textRobot.clear();
_textRobot.reset();
@@ -232,6 +237,12 @@ class AiWriterCubit extends Cubit<AiWriterState> {
'trigger accept action, markdown text: $trimmedMarkdownText',
);
+ await formatSelection(
+ editorState,
+ selection,
+ ApplySuggestionFormatType.clear,
+ );
+
await _textRobot.deleteAINodes();
await _textRobot.replace(
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart
index 6b5f27e028..ef8ee81219 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart
@@ -40,6 +40,7 @@ class _AiWriterScrollWrapperState extends State<AiWriterScrollWrapper> {
onRemoveNode: () {
aiWriterRegistered = false;
widget.editorState.service.keyboardService?.enableShortcuts();
+ widget.editorState.service.keyboardService?.enable();
},
onAppendToDocument: onAppendToDocument,
);
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
index 031e157b33..429cd51221 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
@@ -118,7 +118,7 @@ class MarkdownTextRobot {
}
await _lock.synchronized(() async {
- await _refresh(inMemoryUpdate: false);
+ await _refresh(inMemoryUpdate: false, updateSelection: true);
});
if (_enableDebug) {
@@ -156,7 +156,9 @@ class MarkdownTextRobot {
}
/// Discard the inserted content
- Future<void> discard() async {
+ Future<void> discard({
+ Selection? afterSelection,
+ }) async {
final start = _insertPosition;
if (start == null) {
return;
@@ -165,6 +167,8 @@ class MarkdownTextRobot {
return;
}
+ afterSelection ??= Selection.collapsed(start);
+
// fallback to the calculated position if the selection is null.
final end = Position(
path: start.path.nextNPath(_insertedNodes.length - 1),
@@ -174,7 +178,7 @@ class MarkdownTextRobot {
);
final transaction = editorState.transaction
..deleteNodes(deletedNodes)
- ..afterSelection = Selection.collapsed(start);
+ ..afterSelection = afterSelection;
await editorState.apply(
transaction,