From 34d4ea3e54c62674ea781d8bf44cd4393c2418ab Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sat, 7 Jan 2023 09:38:38 +0800 Subject: [PATCH 1/6] feat: text robot --- .../example/lib/home_page.dart | 36 +++++++++++++- .../example/lib/plugin/text_robot.dart | 49 +++++++++++++++++++ .../appflowy_editor/lib/appflowy_editor.dart | 1 + .../lib/src/commands/text/text_commands.dart | 28 +++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart 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 c4b706c6f6..d68ae7e361 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 @@ -1,8 +1,10 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:example/pages/simple_editor.dart'; +import 'package:example/plugin/text_robot.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -102,6 +104,30 @@ class _HomePageState extends State { _loadEditor(context, jsonString); }), + // Text Robot + _buildSeparator(context, 'Text Robot'), + _buildListTile(context, 'Type Text Automatically', () async { + final jsonString = Future.value( + jsonEncode(EditorState.empty().document.toJson()).toString(), + ); + await _loadEditor(context, jsonString); + + Future.delayed(const Duration(seconds: 2), () { + final textRobot = TextRobot( + editorState: _editorState, + ); + textRobot.insertText( + r''' +Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC +"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" + +1914 translation by H. Rackham +"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" +''', + ); + }); + }), + // Encoder Demo _buildSeparator(context, 'Encoder Demo'), _buildListTile(context, 'Export To JSON', () { @@ -200,7 +226,11 @@ class _HomePageState extends State { ); } - void _loadEditor(BuildContext context, Future jsonString) { + Future _loadEditor( + BuildContext context, + Future jsonString, + ) async { + final completer = Completer(); _jsonString = jsonString; setState( () { @@ -213,6 +243,10 @@ class _HomePageState extends State { ); }, ); + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + completer.complete(); + }); + return completer.future; } void _exportFile( diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart new file mode 100644 index 0000000000..593bcb7042 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart @@ -0,0 +1,49 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; + +enum TextRobotInputType { + character, + word, +} + +class TextRobot { + const TextRobot({ + required this.editorState, + this.delay = const Duration(milliseconds: 30), + }); + + final EditorState editorState; + final Duration delay; + + Future insertText( + String text, { + TextRobotInputType inputType = TextRobotInputType.character, + }) async { + final lines = text.split('\n'); + var path = 0; + for (final line in lines) { + switch (inputType) { + case TextRobotInputType.character: + var index = 0; + final iterator = line.runes.iterator; + while (iterator.moveNext()) { + // await editorState.insertText( + // index, + // iterator.currentAsString, + // path: [path], + // ); + await editorState.insertTextAtCurrentSelection( + iterator.currentAsString, + ); + index += iterator.currentSize; + await Future.delayed(delay); + } + path += 1; + break; + default: + } + + // insert new line + await editorState.insertNewLine(editorState, [path]); + } + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart index cf5c4e2d75..710200e589 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart @@ -42,3 +42,4 @@ export 'src/plugins/markdown/encoder/parser/image_node_parser.dart'; export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart'; export 'src/plugins/markdown/document_markdown.dart'; export 'src/plugins/quill_delta/delta_document_encoder.dart'; +export 'src/commands/text/text_commands.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart index aa19c50d77..0e2ec3abbc 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart @@ -20,6 +20,19 @@ extension TextCommands on EditorState { }); } + Future insertTextAtCurrentSelection(String text) async { + return futureCommand(() async { + final selection = getSelection(null); + assert(selection.isCollapsed); + final textNode = getTextNode(path: selection.start.path); + await insertText( + textNode.toPlainText().length, + text, + textNode: textNode, + ); + }); + } + Future formatText( EditorState editorState, Selection? selection, @@ -95,4 +108,19 @@ extension TextCommands on EditorState { textNode: textNode, ); } + + Future insertNewLine( + EditorState editorState, + Path path, + ) async { + return futureCommand(() async { + final transaction = editorState.transaction; + transaction.insertNode(path, TextNode.empty()); + transaction.afterSelection = Selection.single( + path: path, + startOffset: 0, + ); + apply(transaction); + }); + } } From 494e31993b6165d2c7a4225faeef4f4d4fb2aeb3 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sat, 7 Jan 2023 11:24:14 +0800 Subject: [PATCH 2/6] feat: add openAI plugin --- .../packages/appflowy_editor/CHANGELOG.md | 9 +++ .../example/lib/pages/simple_editor.dart | 3 + .../lib/plugin/AI/getgpt3completions.dart | 68 +++++++++++++++++++ .../example/lib/plugin/text_robot.dart | 66 +++++++++++++++--- .../appflowy_editor/example/pubspec.yaml | 1 + .../lib/src/commands/text/text_commands.dart | 11 +-- .../src/render/rich_text/checkbox_text.dart | 1 - .../selection_menu/selection_menu_widget.dart | 2 +- .../lib/src/render/toolbar/toolbar_item.dart | 1 - .../space_on_web_handler.dart | 1 - .../packages/appflowy_editor/pubspec.yaml | 2 +- 11 files changed, 145 insertions(+), 20 deletions(-) create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/CHANGELOG.md b/frontend/app_flowy/packages/appflowy_editor/CHANGELOG.md index d07a7b12fc..0e12ebf994 100644 --- a/frontend/app_flowy/packages/appflowy_editor/CHANGELOG.md +++ b/frontend/app_flowy/packages/appflowy_editor/CHANGELOG.md @@ -1,3 +1,12 @@ +## 0.0.9 +* Support customize the text color and text background color. +* Fix some bugs. + +## 0.0.8 +* Fix the toolbar display issue. +* Fix the copy/paste issue on Windows. +* Minor Updates. + ## 0.0.7 * Refactor theme customizer, and support dark mode. * Support export and import markdown. diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart index 23fcca5af6..84397811da 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; +import 'package:example/plugin/text_robot.dart'; import 'package:flutter/material.dart'; class SimpleEditor extends StatelessWidget { @@ -64,6 +65,8 @@ class SimpleEditor extends StatelessWidget { codeBlockMenuItem, // Emoji emojiMenuItem, + // Text Robot + textRobotMenuItem, ], ); } else { diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart new file mode 100644 index 0000000000..e86bb71024 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart @@ -0,0 +1,68 @@ +import 'package:http/http.dart' as http; +import 'dart:async'; +import 'dart:convert'; + +Future getGPT3Completion( + String apiKey, + String prompt, + String suffix, + int maxTokens, + double temperature, + Function(String) onData, // callback function to handle streaming data +) async { + final data = { + 'prompt': prompt, + 'suffix': suffix, + 'max_tokens': maxTokens, + 'temperature': temperature, + 'stream': true, // set stream parameter to true + }; + + final headers = { + 'Authorization': apiKey, + 'Content-Type': 'application/json', + }; + final request = http.Request( + 'POST', + Uri.parse('https://api.openai.com/v1/engines/text-davinci-003/completions'), + ); + request.body = json.encode(data); + request.headers.addAll(headers); + + final httpResponse = await request.send(); + + if (httpResponse.statusCode == 200) { + await for (final chunk in httpResponse.stream) { + var result = utf8.decode(chunk).split('text": "'); + var text = ''; + if (result.length > 1) { + result = result[1].split('",'); + if (result.isNotEmpty) { + text = result.first; + } + } + + final processedText = text + .replaceAll('\\n', '\n') + .replaceAll('\\r', '\r') + .replaceAll('\\t', '\t') + .replaceAll('\\b', '\b') + .replaceAll('\\f', '\f') + .replaceAll('\\v', '\v') + .replaceAll('\\\'', '\'') + .replaceAll('"', '"') + .replaceAll('\\0', '0') + .replaceAll('\\1', '1') + .replaceAll('\\2', '2') + .replaceAll('\\3', '3') + .replaceAll('\\4', '4') + .replaceAll('\\5', '5') + .replaceAll('\\6', '6') + .replaceAll('\\7', '7') + .replaceAll('\\8', '8') + .replaceAll('\\9', '9'); + + onData(processedText); + } + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart index 593bcb7042..538514fd43 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart @@ -1,4 +1,57 @@ import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +SelectionMenuItem textRobotMenuItem = SelectionMenuItem( + name: () => 'Open AI', + icon: (editorState, onSelected) => Icon( + Icons.rocket, + size: 18.0, + color: onSelected + ? editorState.editorStyle.selectionMenuItemSelectedIconColor + : editorState.editorStyle.selectionMenuItemIconColor, + ), + keywords: ['open ai', 'gpt3', 'ai'], + handler: ((editorState, menuService, context) async { + showDialog( + context: context, + builder: (context) { + final controller = TextEditingController(text: ''); + return AlertDialog( + content: RawKeyboardListener( + focusNode: FocusNode(), + child: TextField( + autofocus: true, + controller: controller, + maxLines: null, + decoration: const InputDecoration( + border: OutlineInputBorder(), + hintText: 'Please input something...', + ), + ), + onKey: (key) { + if (key is! RawKeyDownEvent) return; + if (key.logicalKey == LogicalKeyboardKey.enter) { + Navigator.of(context).pop(); + // fetch the result and insert it + // Please fill in your own API key + getGPT3Completion('', controller.text, '', 200, .3, + (result) async { + await editorState.insertTextAtCurrentSelection( + result, + ); + }); + } else if (key.logicalKey == LogicalKeyboardKey.escape) { + Navigator.of(context).pop(); + } + }, + ), + ); + }, + ); + }), +); enum TextRobotInputType { character, @@ -19,31 +72,24 @@ class TextRobot { TextRobotInputType inputType = TextRobotInputType.character, }) async { final lines = text.split('\n'); - var path = 0; for (final line in lines) { switch (inputType) { case TextRobotInputType.character: - var index = 0; final iterator = line.runes.iterator; while (iterator.moveNext()) { - // await editorState.insertText( - // index, - // iterator.currentAsString, - // path: [path], - // ); await editorState.insertTextAtCurrentSelection( iterator.currentAsString, ); - index += iterator.currentSize; await Future.delayed(delay); } - path += 1; break; default: } // insert new line - await editorState.insertNewLine(editorState, [path]); + if (lines.length > 1) { + await editorState.insertNewLine(editorState); + } } } } diff --git a/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml b/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml index 885573b00a..ab0eb3aa7b 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml +++ b/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: flutter_math_fork: ^0.6.3+1 appflowy_editor_plugins: path: ../../../packages/appflowy_editor_plugins + http: ^0.13.5 dev_dependencies: flutter_test: diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart index 0e2ec3abbc..6ee5778889 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart @@ -110,14 +110,15 @@ extension TextCommands on EditorState { } Future insertNewLine( - EditorState editorState, - Path path, - ) async { + EditorState editorState, { + Path? path, + }) async { return futureCommand(() async { + final p = path ?? getSelection(null).start.path.next; final transaction = editorState.transaction; - transaction.insertNode(path, TextNode.empty()); + transaction.insertNode(p, TextNode.empty()); transaction.afterSelection = Selection.single( - path: path, + path: p, startOffset: 0, ); apply(transaction); diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/checkbox_text.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/checkbox_text.dart index 461fef0f30..d2b9cbbda2 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/checkbox_text.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/checkbox_text.dart @@ -1,5 +1,4 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:appflowy_editor/src/commands/text/text_commands.dart'; import 'package:appflowy_editor/src/render/rich_text/built_in_text_widget.dart'; import 'package:appflowy_editor/src/extensions/text_style_extension.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_widget.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_widget.dart index 7c7f86e6ab..d5181174cf 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_widget.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_widget.dart @@ -96,7 +96,7 @@ class _SelectionMenuWidgetState extends State { final items = widget.items .where( (item) => item.keywords.any((keyword) { - final value = keyword.contains(newKeyword); + final value = keyword.contains(newKeyword.toLowerCase()); if (value) { maxKeywordLength = max(maxKeywordLength, keyword.length); } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart index 6cdf729b11..4c30f1f9d3 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart @@ -1,5 +1,4 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:appflowy_editor/src/commands/text/text_commands.dart'; import 'package:appflowy_editor/src/extensions/url_launcher_extension.dart'; import 'package:appflowy_editor/src/flutter/overlay.dart'; import 'package:appflowy_editor/src/infra/clipboard.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/space_on_web_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/space_on_web_handler.dart index a74e3e3c96..a69b2f4447 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/space_on_web_handler.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/space_on_web_handler.dart @@ -1,5 +1,4 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:appflowy_editor/src/commands/text/text_commands.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/pubspec.yaml b/frontend/app_flowy/packages/appflowy_editor/pubspec.yaml index 1602da9e06..67b6955541 100644 --- a/frontend/app_flowy/packages/appflowy_editor/pubspec.yaml +++ b/frontend/app_flowy/packages/appflowy_editor/pubspec.yaml @@ -1,6 +1,6 @@ name: appflowy_editor description: A highly customizable rich-text editor for Flutter -version: 0.0.7 +version: 0.0.9 homepage: https://github.com/AppFlowy-IO/AppFlowy platforms: From fc1efeb70bde084b4c699043065b7e7042c90763 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sun, 8 Jan 2023 13:17:18 +0800 Subject: [PATCH 3/6] feat: support prompt and suffix --- .../example/lib/home_page.dart | 2 +- .../example/lib/pages/simple_editor.dart | 11 +- .../auto_completion.dart} | 56 ++------- .../lib/plugin/AI/continue_to_write.dart | 50 ++++++++ .../lib/plugin/AI/getgpt3completions.dart | 16 ++- .../example/lib/plugin/AI/text_robot.dart | 48 ++++++++ .../lib/src/commands/text/text_commands.dart | 111 +++++++++--------- .../lib/src/core/transform/transaction.dart | 19 +++ .../appflowy_editor/lib/src/editor_state.dart | 28 +++-- 9 files changed, 221 insertions(+), 120 deletions(-) rename frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/{text_robot.dart => AI/auto_completion.dart} (54%) create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart 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 d68ae7e361..a20990f84b 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 @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:example/pages/simple_editor.dart'; -import 'package:example/plugin/text_robot.dart'; +import 'package:example/plugin/AI/text_robot.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart index 84397811da..0a1d551390 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart @@ -2,7 +2,9 @@ import 'dart:convert'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; -import 'package:example/plugin/text_robot.dart'; +import 'package:example/plugin/AI/continue_to_write.dart'; +import 'package:example/plugin/AI/auto_completion.dart'; +import 'package:example/plugin/AI/getgpt3completions.dart'; import 'package:flutter/material.dart'; class SimpleEditor extends StatelessWidget { @@ -65,8 +67,11 @@ class SimpleEditor extends StatelessWidget { codeBlockMenuItem, // Emoji emojiMenuItem, - // Text Robot - textRobotMenuItem, + // Open AI + if (apiKey.isNotEmpty) ...[ + autoCompletionMenuItem, + continueToWriteMenuItem, + ] ], ); } else { diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart similarity index 54% rename from frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart rename to frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart index 538514fd43..3e80e3d3dd 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/text_robot.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart @@ -1,10 +1,11 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/text_robot.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -SelectionMenuItem textRobotMenuItem = SelectionMenuItem( - name: () => 'Open AI', +SelectionMenuItem autoCompletionMenuItem = SelectionMenuItem( + name: () => 'Auto generate content', icon: (editorState, onSelected) => Icon( Icons.rocket, size: 18.0, @@ -12,7 +13,7 @@ SelectionMenuItem textRobotMenuItem = SelectionMenuItem( ? editorState.editorStyle.selectionMenuItemSelectedIconColor : editorState.editorStyle.selectionMenuItemIconColor, ), - keywords: ['open ai', 'gpt3', 'ai'], + keywords: ['auto generate content', 'open ai', 'gpt3', 'ai'], handler: ((editorState, menuService, context) async { showDialog( context: context, @@ -35,11 +36,11 @@ SelectionMenuItem textRobotMenuItem = SelectionMenuItem( if (key.logicalKey == LogicalKeyboardKey.enter) { Navigator.of(context).pop(); // fetch the result and insert it - // Please fill in your own API key - getGPT3Completion('', controller.text, '', 200, .3, - (result) async { - await editorState.insertTextAtCurrentSelection( + final textRobot = TextRobot(editorState: editorState); + getGPT3Completion(apiKey, controller.text, '', (result) async { + await textRobot.insertText( result, + inputType: TextRobotInputType.character, ); }); } else if (key.logicalKey == LogicalKeyboardKey.escape) { @@ -52,44 +53,3 @@ SelectionMenuItem textRobotMenuItem = SelectionMenuItem( ); }), ); - -enum TextRobotInputType { - character, - word, -} - -class TextRobot { - const TextRobot({ - required this.editorState, - this.delay = const Duration(milliseconds: 30), - }); - - final EditorState editorState; - final Duration delay; - - Future insertText( - String text, { - TextRobotInputType inputType = TextRobotInputType.character, - }) async { - final lines = text.split('\n'); - for (final line in lines) { - switch (inputType) { - case TextRobotInputType.character: - final iterator = line.runes.iterator; - while (iterator.moveNext()) { - await editorState.insertTextAtCurrentSelection( - iterator.currentAsString, - ); - await Future.delayed(delay); - } - break; - default: - } - - // insert new line - if (lines.length > 1) { - await editorState.insertNewLine(editorState); - } - } - } -} diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart new file mode 100644 index 0000000000..5af22bd4e8 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart @@ -0,0 +1,50 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/text_robot.dart'; +import 'package:flutter/material.dart'; + +SelectionMenuItem continueToWriteMenuItem = SelectionMenuItem( + name: () => 'Continue To Write', + icon: (editorState, onSelected) => Icon( + Icons.print, + size: 18.0, + color: onSelected + ? editorState.editorStyle.selectionMenuItemSelectedIconColor + : editorState.editorStyle.selectionMenuItemIconColor, + ), + keywords: ['continue to write'], + handler: ((editorState, menuService, context) async { + // get the current text + final selection = + editorState.service.selectionService.currentSelection.value; + final textNodes = editorState.service.selectionService.currentSelectedNodes; + if (selection == null || !selection.isCollapsed || textNodes.length != 1) { + return; + } + final textNode = textNodes.first as TextNode; + final prompt = textNode.delta.slice(0, selection.startIndex).toPlainText(); + final suffix = textNode.delta + .slice( + selection.endIndex, + textNode.toPlainText().length, + ) + .toPlainText(); + debugPrint('AI: prompt = $prompt, suffix = $suffix'); + final textRobot = TextRobot(editorState: editorState); + getGPT3Completion( + apiKey, + prompt, + suffix, + (result) async { + if (result == '\\n') { + await editorState.insertNewLineAtCurrentSelection(); + } else { + await textRobot.insertText( + result, + inputType: TextRobotInputType.word, + ); + } + }, + ); + }), +); diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart index e86bb71024..c7b8c094fc 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart @@ -2,14 +2,19 @@ import 'package:http/http.dart' as http; import 'dart:async'; import 'dart:convert'; +// Please fill in your own API key +const apiKey = ''; + Future getGPT3Completion( String apiKey, String prompt, String suffix, - int maxTokens, - double temperature, - Function(String) onData, // callback function to handle streaming data -) async { + Future Function(String) + onData, // callback function to handle streaming data + { + int maxTokens = 200, + double temperature = .3, +}) async { final data = { 'prompt': prompt, 'suffix': suffix, @@ -43,7 +48,6 @@ Future getGPT3Completion( } final processedText = text - .replaceAll('\\n', '\n') .replaceAll('\\r', '\r') .replaceAll('\\t', '\t') .replaceAll('\\b', '\b') @@ -62,7 +66,7 @@ Future getGPT3Completion( .replaceAll('\\8', '8') .replaceAll('\\9', '9'); - onData(processedText); + await onData(processedText); } } } diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart new file mode 100644 index 0000000000..348caf6eed --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart @@ -0,0 +1,48 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; + +enum TextRobotInputType { + character, + word, +} + +class TextRobot { + const TextRobot({ + required this.editorState, + this.delay = const Duration(milliseconds: 30), + }); + + final EditorState editorState; + final Duration delay; + + Future insertText( + String text, { + TextRobotInputType inputType = TextRobotInputType.character, + }) async { + final lines = text.split('\\n'); + for (final line in lines) { + if (line.isEmpty) continue; + switch (inputType) { + case TextRobotInputType.character: + final iterator = line.runes.iterator; + while (iterator.moveNext()) { + await editorState.insertTextAtCurrentSelection( + iterator.currentAsString, + ); + await Future.delayed(delay, () {}); + } + break; + case TextRobotInputType.word: + await editorState.insertTextAtCurrentSelection( + line, + ); + await Future.delayed(delay, () {}); + break; + } + + // insert new line + if (lines.length > 1) { + await editorState.insertNewLineAtCurrentSelection(); + } + } + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart index 6ee5778889..f8e0db5916 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart @@ -12,25 +12,21 @@ extension TextCommands on EditorState { Path? path, TextNode? textNode, }) async { - return futureCommand(() { - final n = getTextNode(path: path, textNode: textNode); - apply( - transaction..insertText(n, index, text), - ); - }); + final n = getTextNode(path: path, textNode: textNode); + return apply( + transaction..insertText(n, index, text), + ); } Future insertTextAtCurrentSelection(String text) async { - return futureCommand(() async { - final selection = getSelection(null); - assert(selection.isCollapsed); - final textNode = getTextNode(path: selection.start.path); - await insertText( - textNode.toPlainText().length, - text, - textNode: textNode, - ); - }); + final selection = getSelection(null); + assert(selection.isCollapsed); + final textNode = getTextNode(path: selection.start.path); + return insertText( + selection.startIndex, + text, + textNode: textNode, + ); } Future formatText( @@ -40,13 +36,11 @@ extension TextCommands on EditorState { Path? path, TextNode? textNode, }) async { - return futureCommand(() { - final n = getTextNode(path: path, textNode: textNode); - final s = getSelection(selection); - apply( - transaction..formatText(n, s.startIndex, s.length, attributes), - ); - }); + final n = getTextNode(path: path, textNode: textNode); + final s = getSelection(selection); + return apply( + transaction..formatText(n, s.startIndex, s.length, attributes), + ); } Future formatTextWithBuiltInAttribute( @@ -57,26 +51,24 @@ extension TextCommands on EditorState { Path? path, TextNode? textNode, }) async { - return futureCommand(() { - final n = getTextNode(path: path, textNode: textNode); - if (BuiltInAttributeKey.globalStyleKeys.contains(key)) { - final attr = n.attributes - ..removeWhere( - (key, _) => BuiltInAttributeKey.globalStyleKeys.contains(key)) - ..addAll(attributes) - ..addAll({ - BuiltInAttributeKey.subtype: key, - }); - apply( - transaction..updateNode(n, attr), - ); - } else if (BuiltInAttributeKey.partialStyleKeys.contains(key)) { - final s = getSelection(selection); - apply( - transaction..formatText(n, s.startIndex, s.length, attributes), - ); - } - }); + final n = getTextNode(path: path, textNode: textNode); + if (BuiltInAttributeKey.globalStyleKeys.contains(key)) { + final attr = n.attributes + ..removeWhere( + (key, _) => BuiltInAttributeKey.globalStyleKeys.contains(key)) + ..addAll(attributes) + ..addAll({ + BuiltInAttributeKey.subtype: key, + }); + return apply( + transaction..updateNode(n, attr), + ); + } else if (BuiltInAttributeKey.partialStyleKeys.contains(key)) { + final s = getSelection(selection); + return apply( + transaction..formatText(n, s.startIndex, s.length, attributes), + ); + } } Future formatTextToCheckbox( @@ -109,19 +101,28 @@ extension TextCommands on EditorState { ); } - Future insertNewLine( - EditorState editorState, { + Future insertNewLine({ Path? path, }) async { - return futureCommand(() async { - final p = path ?? getSelection(null).start.path.next; - final transaction = editorState.transaction; - transaction.insertNode(p, TextNode.empty()); - transaction.afterSelection = Selection.single( - path: p, - startOffset: 0, - ); - apply(transaction); - }); + final p = path ?? getSelection(null).start.path.next; + final transaction = this.transaction; + transaction.insertNode(p, TextNode.empty()); + transaction.afterSelection = Selection.single( + path: p, + startOffset: 0, + ); + return apply(transaction); + } + + Future insertNewLineAtCurrentSelection() async { + final selection = getSelection(null); + assert(selection.isCollapsed); + final textNode = getTextNode(path: selection.start.path); + final transaction = this.transaction; + transaction.splitText( + textNode, + selection.startIndex, + ); + return apply(transaction); } } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart index 4df4adb228..544fe8d0ca 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart @@ -169,6 +169,25 @@ extension TextTransaction on Transaction { )); } + void splitText(TextNode textNode, int offset) { + final delta = textNode.delta; + final first = delta.slice(0, offset); + final second = delta.slice(offset, delta.length); + final path = textNode.path.next; + updateText(textNode, first); + insertNode( + path, + TextNode( + attributes: textNode.attributes, + delta: second, + ), + ); + afterSelection = Selection.collapsed(Position( + path: path, + offset: 0, + )); + } + /// Inserts the text content at a specified index. /// /// Optionally, you may specify formatting attributes that are applied to the inserted string. diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart index be49ea19d0..7b756e30c2 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart @@ -96,13 +96,21 @@ class EditorState { return null; } - updateCursorSelection(Selection? cursorSelection, - [CursorUpdateReason reason = CursorUpdateReason.others]) { + Future updateCursorSelection( + Selection? cursorSelection, [ + CursorUpdateReason reason = CursorUpdateReason.others, + ]) { + final completer = Completer(); + // broadcast to other users here if (reason != CursorUpdateReason.uiEvent) { service.selectionService.updateSelection(cursorSelection); } _cursorSelection = cursorSelection; + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + completer.complete(); + }); + return completer.future; } Timer? _debouncedSealHistoryItemTimer; @@ -121,14 +129,17 @@ class EditorState { /// /// The options can be used to determine whether the editor /// should record the transaction in undo/redo stack. - void apply( + Future apply( Transaction transaction, { ApplyOptions options = const ApplyOptions(recordUndo: true), ruleCount = 0, withUpdateCursor = true, - }) { + }) async { + final completer = Completer(); + if (!editable) { - return; + completer.complete(); + return completer.future; } // TODO: validate the transation. for (final op in transaction.operations) { @@ -137,10 +148,11 @@ class EditorState { _observer.add(transaction); - WidgetsBinding.instance.addPostFrameCallback((_) { + WidgetsBinding.instance.addPostFrameCallback((_) async { _applyRules(ruleCount); if (withUpdateCursor) { - updateCursorSelection(transaction.afterSelection); + await updateCursorSelection(transaction.afterSelection); + completer.complete(); } }); @@ -160,6 +172,8 @@ class EditorState { redoItem.afterSelection = transaction.afterSelection; undoManager.redoStack.push(redoItem); } + + return completer.future; } void _debouncedSealHistoryItem() { From 310236dca089d0b086f79076dd00e6db36f32ced Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sun, 8 Jan 2023 15:48:28 +0800 Subject: [PATCH 4/6] feat: add sample for open AI editing --- .../example/lib/pages/simple_editor.dart | 4 + .../lib/plugin/AI/continue_to_write.dart | 1 - .../lib/plugin/AI/getgpt3completions.dart | 41 +++- .../example/lib/plugin/AI/smart_edit.dart | 198 ++++++++++++++++++ .../appflowy_editor/lib/appflowy_editor.dart | 1 + .../appflowy_editor/lib/src/editor_state.dart | 4 + .../lib/src/service/editor_service.dart | 6 + .../lib/src/service/toolbar_service.dart | 13 +- 8 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart index 0a1d551390..3f94a7bda3 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart @@ -5,6 +5,7 @@ import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; import 'package:example/plugin/AI/continue_to_write.dart'; import 'package:example/plugin/AI/auto_completion.dart'; import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/smart_edit.dart'; import 'package:flutter/material.dart'; class SimpleEditor extends StatelessWidget { @@ -73,6 +74,9 @@ class SimpleEditor extends StatelessWidget { continueToWriteMenuItem, ] ], + toolbarItems: [ + smartEditItem, + ], ); } else { return const Center( diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart index 5af22bd4e8..692e459b81 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart @@ -29,7 +29,6 @@ SelectionMenuItem continueToWriteMenuItem = SelectionMenuItem( textNode.toPlainText().length, ) .toPlainText(); - debugPrint('AI: prompt = $prompt, suffix = $suffix'); final textRobot = TextRobot(editorState: editorState); getGPT3Completion( apiKey, diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart index c7b8c094fc..553c4fc31c 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart @@ -14,13 +14,14 @@ Future getGPT3Completion( { int maxTokens = 200, double temperature = .3, + bool stream = true, }) async { final data = { 'prompt': prompt, 'suffix': suffix, 'max_tokens': maxTokens, 'temperature': temperature, - 'stream': true, // set stream parameter to true + 'stream': stream, // set stream parameter to true }; final headers = { @@ -70,3 +71,41 @@ Future getGPT3Completion( } } } + +Future getGPT3Edit( + String apiKey, + String input, + String instruction, { + required Future Function(List result) onResult, + required Future Function() onError, + int n = 1, + double temperature = .3, +}) async { + final data = { + 'model': 'text-davinci-edit-001', + 'input': input, + 'instruction': instruction, + 'temperature': temperature, + 'n': n, + }; + + final headers = { + 'Authorization': apiKey, + 'Content-Type': 'application/json', + }; + + var response = await http.post( + Uri.parse('https://api.openai.com/v1/edits'), + headers: headers, + body: json.encode(data), + ); + if (response.statusCode == 200) { + final result = json.decode(response.body); + final choices = result['choices']; + if (choices != null && choices is List) { + onResult(choices.map((e) => e['text'] as String).toList()); + } + } else { + onError(); + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart new file mode 100644 index 0000000000..dc299928c4 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart @@ -0,0 +1,198 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +ToolbarItem smartEditItem = ToolbarItem( + id: 'appflowy.toolbar.smart_edit', + type: 5, + iconBuilder: (isHighlight) { + return Icon( + Icons.edit, + color: isHighlight ? Colors.lightBlue : Colors.white, + size: 14, + ); + }, + validator: (editorState) { + final nodes = editorState.service.selectionService.currentSelectedNodes; + return nodes.whereType().length == nodes.length && + 1 == nodes.length; + }, + highlightCallback: (_) => false, + tooltipsMessage: 'Smart Edit', + handler: (editorState, context) { + showDialog( + context: context, + builder: (context) { + return AlertDialog( + content: SmartEditWidget( + editorState: editorState, + ), + ); + }, + ); + }, +); + +class SmartEditWidget extends StatefulWidget { + const SmartEditWidget({ + super.key, + required this.editorState, + }); + + final EditorState editorState; + + @override + State createState() => _SmartEditWidgetState(); +} + +class _SmartEditWidgetState extends State { + final inputEventController = TextEditingController(text: ''); + final resultController = TextEditingController(text: ''); + + var result = ''; + + Iterable get currentSelectedTextNodes => + widget.editorState.service.selectionService.currentSelectedNodes + .whereType(); + Selection? get currentSelection => + widget.editorState.service.selectionService.currentSelection.value; + + @override + Widget build(BuildContext context) { + return Container( + constraints: const BoxConstraints(maxWidth: 400), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + RawKeyboardListener( + focusNode: FocusNode(), + child: TextField( + autofocus: true, + controller: inputEventController, + maxLines: null, + decoration: const InputDecoration( + border: OutlineInputBorder(), + hintText: 'Describe how you\'d like AppFlowy to edit this text', + ), + ), + onKey: (key) { + if (key is! RawKeyDownEvent) return; + if (key.logicalKey == LogicalKeyboardKey.enter) { + _requestGPT3EditResult(); + } else if (key.logicalKey == LogicalKeyboardKey.escape) { + Navigator.of(context).pop(); + } + }, + ), + if (result.isNotEmpty) ...[ + const SizedBox(height: 20), + const Text( + 'Result: ', + style: TextStyle(color: Colors.grey), + ), + const SizedBox(height: 10), + SizedBox( + height: 300, + child: TextField( + controller: resultController..text = result, + maxLines: null, + decoration: const InputDecoration( + border: OutlineInputBorder(), + hintText: + 'Describe how you\'d like AppFlowy to edit this text', + ), + ), + ), + const SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Cancel'), + ), + TextButton( + onPressed: () { + Navigator.of(context).pop(); + + // replace the text + final selection = currentSelection; + if (selection != null) { + assert(selection.isSingle); + final transaction = widget.editorState.transaction; + transaction.replaceText( + currentSelectedTextNodes.first, + selection.startIndex, + selection.length, + resultController.text, + ); + widget.editorState.apply(transaction); + } + }, + child: const Text('Replace'), + ), + ], + ), + ] + ], + ), + ); + } + + void _requestGPT3EditResult() { + final selection = + widget.editorState.service.selectionService.currentSelection.value; + if (selection == null || !selection.isSingle) { + return; + } + final text = + widget.editorState.service.selectionService.currentSelectedNodes + .whereType() + .first + .delta + .slice( + selection.startIndex, + selection.endIndex, + ) + .toPlainText(); + if (text.isEmpty) { + Navigator.of(context).pop(); + return; + } + + showDialog( + context: context, + builder: (context) { + return AlertDialog( + content: Column( + mainAxisSize: MainAxisSize.min, + children: const [ + CircularProgressIndicator(), + SizedBox(height: 10), + Text('Loading'), + ], + ), + ); + }, + ); + + getGPT3Edit( + apiKey, + text, + inputEventController.text, + onResult: (result) async { + Navigator.of(context).pop(true); + setState(() { + this.result = result.join('\n').trim(); + }); + }, + onError: () async { + Navigator.of(context).pop(true); + }, + ); + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart index 710200e589..eab2d43c60 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart @@ -43,3 +43,4 @@ export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart'; export 'src/plugins/markdown/document_markdown.dart'; export 'src/plugins/quill_delta/delta_document_encoder.dart'; export 'src/commands/text/text_commands.dart'; +export 'src/render/toolbar/toolbar_item.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart index 7b756e30c2..00613fa16b 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/editor_state.dart @@ -3,6 +3,7 @@ import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/infra/log.dart'; import 'package:appflowy_editor/src/render/selection_menu/selection_menu_widget.dart'; import 'package:appflowy_editor/src/render/style/editor_style.dart'; +import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:appflowy_editor/src/service/service.dart'; import 'package:flutter/material.dart'; @@ -60,6 +61,9 @@ class EditorState { /// Stores the selection menu items. List selectionMenuItems = []; + /// Stores the toolbar items. + List toolbarItems = []; + /// Operation stream. Stream get transactionStream => _observer.stream; final StreamController _observer = StreamController.broadcast(); diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart index 9d58eb066c..7f9bbe43b9 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart @@ -1,6 +1,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/flutter/overlay.dart'; import 'package:appflowy_editor/src/render/image/image_node_builder.dart'; +import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:appflowy_editor/src/service/shortcut_event/built_in_shortcut_events.dart'; import 'package:flutter/material.dart' hide Overlay, OverlayEntry; @@ -30,6 +31,7 @@ class AppFlowyEditor extends StatefulWidget { this.customBuilders = const {}, this.shortcutEvents = const [], this.selectionMenuItems = const [], + this.toolbarItems = const [], this.editable = true, this.autoFocus = false, ThemeData? themeData, @@ -51,6 +53,8 @@ class AppFlowyEditor extends StatefulWidget { final List selectionMenuItems; + final List toolbarItems; + late final ThemeData themeData; final bool editable; @@ -74,6 +78,7 @@ class _AppFlowyEditorState extends State { super.initState(); editorState.selectionMenuItems = widget.selectionMenuItems; + editorState.toolbarItems = widget.toolbarItems; editorState.themeData = widget.themeData; editorState.service.renderPluginService = _createRenderPlugin(); editorState.editable = widget.editable; @@ -94,6 +99,7 @@ class _AppFlowyEditorState extends State { if (editorState.service != oldWidget.editorState.service) { editorState.selectionMenuItems = widget.selectionMenuItems; + editorState.toolbarItems = widget.toolbarItems; editorState.service.renderPluginService = _createRenderPlugin(); } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart index 9b9d002cd1..d17f639340 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart @@ -35,11 +35,20 @@ class _FlowyToolbarState extends State implements AppFlowyToolbarService { OverlayEntry? _toolbarOverlay; final _toolbarWidgetKey = GlobalKey(debugLabel: '_toolbar_widget'); + late final List toolbarItems; + + @override + void initState() { + super.initState(); + + toolbarItems = [...defaultToolbarItems, ...widget.editorState.toolbarItems] + ..sort((a, b) => a.type.compareTo(b.type)); + } @override void showInOffset(Offset offset, Alignment alignment, LayerLink layerLink) { hide(); - final items = _filterItems(defaultToolbarItems); + final items = _filterItems(toolbarItems); if (items.isEmpty) { return; } @@ -65,7 +74,7 @@ class _FlowyToolbarState extends State @override bool triggerHandler(String id) { - final items = defaultToolbarItems.where((item) => item.id == id); + final items = toolbarItems.where((item) => item.id == id); if (items.length != 1) { assert(items.length == 1, 'The toolbar item\'s id must be unique'); return false; From fa0a334d6c8c055efbb6e377e31be4af3d9fa487 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Mon, 9 Jan 2023 12:31:26 +0800 Subject: [PATCH 5/6] feat: refactor the gpt3 api and support multi line completion --- .../example/lib/pages/simple_editor.dart | 2 +- .../lib/plugin/AI/auto_completion.dart | 20 +-- .../lib/plugin/AI/continue_to_write.dart | 107 ++++++++++++---- .../lib/plugin/AI/getgpt3completions.dart | 111 ---------------- .../example/lib/plugin/AI/gpt3.dart | 119 ++++++++++++++++++ .../example/lib/plugin/AI/smart_edit.dart | 6 +- .../example/lib/plugin/AI/text_robot.dart | 13 +- .../lib/src/core/transform/transaction.dart | 3 +- 8 files changed, 230 insertions(+), 151 deletions(-) delete mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart create mode 100644 frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/gpt3.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart index 3f94a7bda3..9d7aebf7ff 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart @@ -4,7 +4,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; import 'package:example/plugin/AI/continue_to_write.dart'; import 'package:example/plugin/AI/auto_completion.dart'; -import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/gpt3.dart'; import 'package:example/plugin/AI/smart_edit.dart'; import 'package:flutter/material.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart index 3e80e3d3dd..c2e9447b6b 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart @@ -1,5 +1,5 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/gpt3.dart'; import 'package:example/plugin/AI/text_robot.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -37,12 +37,18 @@ SelectionMenuItem autoCompletionMenuItem = SelectionMenuItem( Navigator.of(context).pop(); // fetch the result and insert it final textRobot = TextRobot(editorState: editorState); - getGPT3Completion(apiKey, controller.text, '', (result) async { - await textRobot.insertText( - result, - inputType: TextRobotInputType.character, - ); - }); + const gpt3 = GPT3APIClient(apiKey: apiKey); + gpt3.getGPT3Completion( + controller.text, + '', + onResult: (result) async { + await textRobot.insertText( + result, + inputType: TextRobotInputType.character, + ); + }, + onError: () async {}, + ); } else if (key.logicalKey == LogicalKeyboardKey.escape) { Navigator.of(context).pop(); } diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart index 692e459b81..e3e407d481 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart @@ -1,5 +1,5 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/gpt3.dart'; import 'package:example/plugin/AI/text_robot.dart'; import 'package:flutter/material.dart'; @@ -14,35 +14,96 @@ SelectionMenuItem continueToWriteMenuItem = SelectionMenuItem( ), keywords: ['continue to write'], handler: ((editorState, menuService, context) async { - // get the current text + // Two cases + // 1. if there is content in the text node where the cursor is located, + // then we use the current text content as data. + // 2. if there is no content in the text node where the cursor is located, + // then we use the previous / next text node's content as data. + final selection = editorState.service.selectionService.currentSelection.value; - final textNodes = editorState.service.selectionService.currentSelectedNodes; - if (selection == null || !selection.isCollapsed || textNodes.length != 1) { + if (selection == null || !selection.isCollapsed) { return; } - final textNode = textNodes.first as TextNode; - final prompt = textNode.delta.slice(0, selection.startIndex).toPlainText(); - final suffix = textNode.delta - .slice( - selection.endIndex, - textNode.toPlainText().length, - ) - .toPlainText(); + + final textNodes = editorState.service.selectionService.currentSelectedNodes + .whereType(); + if (textNodes.isEmpty) { + return; + } + final textRobot = TextRobot(editorState: editorState); - getGPT3Completion( - apiKey, + const gpt3 = GPT3APIClient(apiKey: apiKey); + final textNode = textNodes.first; + + var prompt = ''; + var suffix = ''; + + void continueToWriteInSingleLine() { + prompt = textNode.delta.slice(0, selection.startIndex).toPlainText(); + suffix = textNode.delta + .slice( + selection.endIndex, + textNode.toPlainText().length, + ) + .toPlainText(); + } + + void continueToWriteInMulitLines() { + final parent = textNode.parent; + if (parent != null) { + for (final node in parent.children) { + if (node is! TextNode || node.toPlainText().isEmpty) continue; + if (node.path < textNode.path) { + prompt += '${node.toPlainText()}\n'; + } else if (node.path > textNode.path) { + suffix += '${node.toPlainText()}\n'; + } + } + } + } + + if (textNodes.first.toPlainText().isNotEmpty) { + continueToWriteInSingleLine(); + } else { + continueToWriteInMulitLines(); + } + + if (prompt.isEmpty && suffix.isEmpty) { + return; + } + + late final BuildContext diglogContext; + + showDialog( + context: context, + builder: (context) { + diglogContext = context; + return AlertDialog( + content: Column( + mainAxisSize: MainAxisSize.min, + children: const [ + CircularProgressIndicator(), + SizedBox(height: 10), + Text('Loading'), + ], + ), + ); + }, + ); + + gpt3.getGPT3Completion( prompt, suffix, - (result) async { - if (result == '\\n') { - await editorState.insertNewLineAtCurrentSelection(); - } else { - await textRobot.insertText( - result, - inputType: TextRobotInputType.word, - ); - } + onResult: (result) async { + Navigator.of(diglogContext).pop(true); + await textRobot.insertText( + result, + inputType: TextRobotInputType.word, + ); + }, + onError: () async { + Navigator.of(diglogContext).pop(true); }, ); }), diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart deleted file mode 100644 index 553c4fc31c..0000000000 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/getgpt3completions.dart +++ /dev/null @@ -1,111 +0,0 @@ -import 'package:http/http.dart' as http; -import 'dart:async'; -import 'dart:convert'; - -// Please fill in your own API key -const apiKey = ''; - -Future getGPT3Completion( - String apiKey, - String prompt, - String suffix, - Future Function(String) - onData, // callback function to handle streaming data - { - int maxTokens = 200, - double temperature = .3, - bool stream = true, -}) async { - final data = { - 'prompt': prompt, - 'suffix': suffix, - 'max_tokens': maxTokens, - 'temperature': temperature, - 'stream': stream, // set stream parameter to true - }; - - final headers = { - 'Authorization': apiKey, - 'Content-Type': 'application/json', - }; - final request = http.Request( - 'POST', - Uri.parse('https://api.openai.com/v1/engines/text-davinci-003/completions'), - ); - request.body = json.encode(data); - request.headers.addAll(headers); - - final httpResponse = await request.send(); - - if (httpResponse.statusCode == 200) { - await for (final chunk in httpResponse.stream) { - var result = utf8.decode(chunk).split('text": "'); - var text = ''; - if (result.length > 1) { - result = result[1].split('",'); - if (result.isNotEmpty) { - text = result.first; - } - } - - final processedText = text - .replaceAll('\\r', '\r') - .replaceAll('\\t', '\t') - .replaceAll('\\b', '\b') - .replaceAll('\\f', '\f') - .replaceAll('\\v', '\v') - .replaceAll('\\\'', '\'') - .replaceAll('"', '"') - .replaceAll('\\0', '0') - .replaceAll('\\1', '1') - .replaceAll('\\2', '2') - .replaceAll('\\3', '3') - .replaceAll('\\4', '4') - .replaceAll('\\5', '5') - .replaceAll('\\6', '6') - .replaceAll('\\7', '7') - .replaceAll('\\8', '8') - .replaceAll('\\9', '9'); - - await onData(processedText); - } - } -} - -Future getGPT3Edit( - String apiKey, - String input, - String instruction, { - required Future Function(List result) onResult, - required Future Function() onError, - int n = 1, - double temperature = .3, -}) async { - final data = { - 'model': 'text-davinci-edit-001', - 'input': input, - 'instruction': instruction, - 'temperature': temperature, - 'n': n, - }; - - final headers = { - 'Authorization': apiKey, - 'Content-Type': 'application/json', - }; - - var response = await http.post( - Uri.parse('https://api.openai.com/v1/edits'), - headers: headers, - body: json.encode(data), - ); - if (response.statusCode == 200) { - final result = json.decode(response.body); - final choices = result['choices']; - if (choices != null && choices is List) { - onResult(choices.map((e) => e['text'] as String).toList()); - } - } else { - onError(); - } -} diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/gpt3.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/gpt3.dart new file mode 100644 index 0000000000..a63f47ba3d --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/gpt3.dart @@ -0,0 +1,119 @@ +import 'package:http/http.dart' as http; +import 'dart:async'; +import 'dart:convert'; + +// Please fill in your own API key +const apiKey = ''; + +enum GPT3API { + completion, + edit, +} + +extension on GPT3API { + Uri get uri { + switch (this) { + case GPT3API.completion: + return Uri.parse('https://api.openai.com/v1/completions'); + case GPT3API.edit: + return Uri.parse('https://api.openai.com/v1/edits'); + } + } +} + +class GPT3APIClient { + const GPT3APIClient({ + required this.apiKey, + }); + + final String apiKey; + + /// Get completions from GPT-3 + /// + /// [prompt] is the prompt text + /// [suffix] is the suffix text + /// [onResult] is the callback function to handle the result + /// [maxTokens] is the maximum number of tokens to generate + /// [temperature] is the temperature of the model + /// + /// See https://beta.openai.com/docs/api-reference/completions/create + Future getGPT3Completion( + String prompt, + String suffix, { + required Future Function(String result) onResult, + required Future Function() onError, + int maxTokens = 200, + double temperature = .3, + }) async { + final data = { + 'model': 'text-davinci-003', + 'prompt': prompt, + 'suffix': suffix, + 'max_tokens': maxTokens, + 'temperature': temperature, + 'stream': false, + }; + + final headers = { + 'Authorization': apiKey, + 'Content-Type': 'application/json', + }; + + final response = await http.post( + GPT3API.completion.uri, + headers: headers, + body: json.encode(data), + ); + + if (response.statusCode == 200) { + final result = json.decode(response.body); + final choices = result['choices']; + if (choices != null && choices is List) { + for (final choice in choices) { + final text = choice['text']; + await onResult(text); + } + } + } else { + await onError(); + } + } + + Future getGPT3Edit( + String apiKey, + String input, + String instruction, { + required Future Function(List result) onResult, + required Future Function() onError, + int n = 1, + double temperature = .3, + }) async { + final data = { + 'model': 'text-davinci-edit-001', + 'input': input, + 'instruction': instruction, + 'temperature': temperature, + 'n': n, + }; + + final headers = { + 'Authorization': apiKey, + 'Content-Type': 'application/json', + }; + + final response = await http.post( + Uri.parse('https://api.openai.com/v1/edits'), + headers: headers, + body: json.encode(data), + ); + if (response.statusCode == 200) { + final result = json.decode(response.body); + final choices = result['choices']; + if (choices != null && choices is List) { + await onResult(choices.map((e) => e['text'] as String).toList()); + } + } else { + await onError(); + } + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart index dc299928c4..49084cdd9d 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart @@ -1,5 +1,5 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:example/plugin/AI/getgpt3completions.dart'; +import 'package:example/plugin/AI/gpt3.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -52,6 +52,8 @@ class _SmartEditWidgetState extends State { var result = ''; + final gpt3 = const GPT3APIClient(apiKey: apiKey); + Iterable get currentSelectedTextNodes => widget.editorState.service.selectionService.currentSelectedNodes .whereType(); @@ -180,7 +182,7 @@ class _SmartEditWidgetState extends State { }, ); - getGPT3Edit( + gpt3.getGPT3Edit( apiKey, text, inputEventController.text, diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart index 348caf6eed..a3ac4adb58 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart @@ -18,7 +18,7 @@ class TextRobot { String text, { TextRobotInputType inputType = TextRobotInputType.character, }) async { - final lines = text.split('\\n'); + final lines = text.split('\n'); for (final line in lines) { if (line.isEmpty) continue; switch (inputType) { @@ -32,10 +32,13 @@ class TextRobot { } break; case TextRobotInputType.word: - await editorState.insertTextAtCurrentSelection( - line, - ); - await Future.delayed(delay, () {}); + final words = line.split(' ').map((e) => '$e '); + for (final word in words) { + await editorState.insertTextAtCurrentSelection( + word, + ); + await Future.delayed(delay, () {}); + } break; } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart index 544fe8d0ca..81d821d19e 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/transform/transaction.dart @@ -171,10 +171,9 @@ extension TextTransaction on Transaction { void splitText(TextNode textNode, int offset) { final delta = textNode.delta; - final first = delta.slice(0, offset); final second = delta.slice(offset, delta.length); final path = textNode.path.next; - updateText(textNode, first); + deleteText(textNode, offset, delta.length); insertNode( path, TextNode( From ab108e1dc199d79b5da4f788946c3af9c5476c24 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 10 Jan 2023 09:28:11 +0800 Subject: [PATCH 6/6] chore: format dart code --- .../packages/appflowy_editor/lib/src/service/editor_service.dart | 1 - .../appflowy_editor/lib/src/service/toolbar_service.dart | 1 - .../appflowy_editor/test/service/toolbar_service_test.dart | 1 - 3 files changed, 3 deletions(-) diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart index 7f9bbe43b9..1c15df05f8 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart @@ -1,7 +1,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/flutter/overlay.dart'; import 'package:appflowy_editor/src/render/image/image_node_builder.dart'; -import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:appflowy_editor/src/service/shortcut_event/built_in_shortcut_events.dart'; import 'package:flutter/material.dart' hide Overlay, OverlayEntry; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart index d17f639340..06343b3740 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart @@ -1,5 +1,4 @@ import 'package:appflowy_editor/src/flutter/overlay.dart'; -import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:flutter/material.dart' hide Overlay, OverlayEntry; import 'package:appflowy_editor/appflowy_editor.dart'; diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart index 2388507003..6c2ab09157 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart +++ b/frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart @@ -1,5 +1,4 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:appflowy_editor/src/render/toolbar/toolbar_item_widget.dart'; import 'package:appflowy_editor/src/render/toolbar/toolbar_widget.dart'; import 'package:flutter_test/flutter_test.dart';