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: optimize the open ai input status and clear the auto completion… (#1865)

Lucas.Xu346a09b2ae67
diff --git a/frontend/app_flowy/lib/plugins/document/document_page.dart b/frontend/app_flowy/lib/plugins/document/document_page.dart
index cd0e08f5a0..0f53268e35 100644
--- a/frontend/app_flowy/lib/plugins/document/document_page.dart
+++ b/frontend/app_flowy/lib/plugins/document/document_page.dart
@@ -33,7 +33,6 @@ class DocumentPage extends StatefulWidget {
class _DocumentPageState extends State<DocumentPage> {
late DocumentBloc documentBloc;
- final FocusNode _focusNode = FocusNode();
@override
void initState() {
@@ -44,6 +43,15 @@ class _DocumentPageState extends State<DocumentPage> {
super.initState();
}
+ @override
+ Future<void> dispose() async {
+ // https://github.com/flutter/flutter/issues/64935#issuecomment-686852369
+ super.dispose();
+
+ await _clearTemporaryNodes();
+ await documentBloc.close();
+ }
+
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
@@ -72,13 +80,6 @@ class _DocumentPageState extends State<DocumentPage> {
);
}
- @override
- Future<void> dispose() async {
- documentBloc.close();
- _focusNode.dispose();
- super.dispose();
- }
-
Widget _renderDocument(BuildContext context, DocumentState state) {
return Column(
children: [
@@ -172,4 +173,29 @@ class _DocumentPageState extends State<DocumentPage> {
),
);
}
+
+ Future<void> _clearTemporaryNodes() async {
+ final editorState = documentBloc.editorState;
+ final document = editorState.document;
+ if (document.root.children.isEmpty) {
+ return;
+ }
+ final temporaryNodeTypes = [
+ kAutoCompletionInputType,
+ ];
+ final iterator = NodeIterator(
+ document: document,
+ startNode: document.root.children.first,
+ );
+ final transaction = editorState.transaction;
+ while (iterator.moveNext()) {
+ final node = iterator.current;
+ if (temporaryNodeTypes.contains(node.type)) {
+ transaction.deleteNode(node);
+ }
+ }
+ if (transaction.operations.isNotEmpty) {
+ await editorState.apply(transaction, withUpdateCursor: false);
+ }
+ }
}
diff --git a/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/util/editor_extension.dart b/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/util/editor_extension.dart
index f85669b989..fd27c65cef 100644
--- a/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/util/editor_extension.dart
+++ b/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/util/editor_extension.dart
@@ -13,7 +13,10 @@ extension TextRobot on EditorState {
}) async {
final lines = text.split('\n');
for (final line in lines) {
- if (line.isEmpty) continue;
+ if (line.isEmpty) {
+ await insertNewLineAtCurrentSelection();
+ continue;
+ }
switch (inputType) {
case TextRobotInputType.character:
final iterator = line.runes.iterator;
@@ -34,11 +37,6 @@ extension TextRobot on EditorState {
}
break;
}
-
- // insert new line
- if (lines.length > 1) {
- await insertNewLineAtCurrentSelection();
- }
}
}
}
diff --git a/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/widgets/auto_completion_node_widget.dart b/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/widgets/auto_completion_node_widget.dart
index 97d488f662..a3eb0429a4 100644
--- a/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/widgets/auto_completion_node_widget.dart
+++ b/frontend/app_flowy/lib/plugins/document/presentation/plugins/openai/widgets/auto_completion_node_widget.dart
@@ -257,9 +257,14 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
}, (textCompletion) async {
loading.stop();
await _makeSurePreviousNodeIsEmptyTextNode();
- await widget.editorState.autoInsertText(
- textCompletion.choices.first.text,
- );
+ // Open AI result uses two '\n' as the begin syntax.
+ var texts = textCompletion.choices.first.text.split('\n');
+ if (texts.length > 2) {
+ texts.removeRange(0, 2);
+ await widget.editorState.autoInsertText(
+ texts.join('\n'),
+ );
+ }
focusNode.requestFocus();
});
}, (error) async {
diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart
index a20990f84b..89a1c393af 100644
--- a/frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart
@@ -315,7 +315,7 @@ Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
var jsonString = '';
switch (fileType) {
case ExportFileType.json:
- jsonString = jsonEncode(plainText);
+ jsonString = plainText;
break;
case ExportFileType.markdown:
jsonString = jsonEncode(markdownToDocument(plainText).toJson());
diff --git a/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock b/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock
index 971f196463..06431f182e 100644
--- a/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock
+++ b/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock
@@ -2,11 +2,13 @@ PODS:
- flowy_infra_ui (0.0.1):
- FlutterMacOS
- FlutterMacOS (1.0.0)
- - path_provider_macos (0.0.1):
+ - path_provider_foundation (0.0.1):
+ - Flutter
- FlutterMacOS
- rich_clipboard_macos (0.0.1):
- FlutterMacOS
- - shared_preferences_macos (0.0.1):
+ - shared_preferences_foundation (0.0.1):
+ - Flutter
- FlutterMacOS
- url_launcher_macos (0.0.1):
- FlutterMacOS
@@ -14,9 +16,9 @@ PODS:
DEPENDENCIES:
- flowy_infra_ui (from `Flutter/ephemeral/.symlinks/plugins/flowy_infra_ui/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
- - path_provider_macos (from `Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos`)
+ - path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/macos`)
- rich_clipboard_macos (from `Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos`)
- - shared_preferences_macos (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_macos/macos`)
+ - shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/macos`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
EXTERNAL SOURCES:
@@ -24,22 +26,22 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/flowy_infra_ui/macos
FlutterMacOS:
:path: Flutter/ephemeral
- path_provider_macos:
- :path: Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos
+ path_provider_foundation:
+ :path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/macos
rich_clipboard_macos:
:path: Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos
- shared_preferences_macos:
- :path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_macos/macos
+ shared_preferences_foundation:
+ :path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/macos
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
SPEC CHECKSUMS:
flowy_infra_ui: c34d49d615ed9fe552cd47f90d7850815a74e9e9
FlutterMacOS: ae6af50a8ea7d6103d888583d46bd8328a7e9811
- path_provider_macos: 3c0c3b4b0d4a76d2bf989a913c2de869c5641a19
+ path_provider_foundation: 37748e03f12783f9de2cb2c4eadfaa25fe6d4852
rich_clipboard_macos: 43364b66b9dc69d203eb8dd6d758e2d12e02723c
- shared_preferences_macos: a64dc611287ed6cbe28fd1297898db1336975727
- url_launcher_macos: 597e05b8e514239626bcf4a850fcf9ef5c856ec3
+ shared_preferences_foundation: 297b3ebca31b34ec92be11acd7fb0ba932c822ca
+ url_launcher_macos: c04e4fa86382d4f94f6b38f14625708be3ae52e2
PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c