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/home_page.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart index c4b706c6f6..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 @@ -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/AI/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/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart index 23fcca5af6..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 @@ -2,6 +2,10 @@ import 'dart:convert'; 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/gpt3.dart'; +import 'package:example/plugin/AI/smart_edit.dart'; import 'package:flutter/material.dart'; class SimpleEditor extends StatelessWidget { @@ -64,6 +68,14 @@ class SimpleEditor extends StatelessWidget { codeBlockMenuItem, // Emoji emojiMenuItem, + // Open AI + if (apiKey.isNotEmpty) ...[ + autoCompletionMenuItem, + continueToWriteMenuItem, + ] + ], + toolbarItems: [ + smartEditItem, ], ); } else { 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 new file mode 100644 index 0000000000..c2e9447b6b --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/auto_completion.dart @@ -0,0 +1,61 @@ +import 'package:appflowy_editor/appflowy_editor.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'; + +SelectionMenuItem autoCompletionMenuItem = SelectionMenuItem( + name: () => 'Auto generate content', + icon: (editorState, onSelected) => Icon( + Icons.rocket, + size: 18.0, + color: onSelected + ? editorState.editorStyle.selectionMenuItemSelectedIconColor + : editorState.editorStyle.selectionMenuItemIconColor, + ), + keywords: ['auto generate content', '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 + final textRobot = TextRobot(editorState: editorState); + 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 new file mode 100644 index 0000000000..e3e407d481 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/continue_to_write.dart @@ -0,0 +1,110 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:example/plugin/AI/gpt3.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 { + // 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; + if (selection == null || !selection.isCollapsed) { + return; + } + + final textNodes = editorState.service.selectionService.currentSelectedNodes + .whereType(); + if (textNodes.isEmpty) { + return; + } + + final textRobot = TextRobot(editorState: editorState); + 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, + 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/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 new file mode 100644 index 0000000000..49084cdd9d --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/smart_edit.dart @@ -0,0 +1,200 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:example/plugin/AI/gpt3.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 = ''; + + final gpt3 = const GPT3APIClient(apiKey: apiKey); + + 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'), + ], + ), + ); + }, + ); + + gpt3.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/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..a3ac4adb58 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/AI/text_robot.dart @@ -0,0 +1,51 @@ +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: + final words = line.split(' ').map((e) => '$e '); + for (final word in words) { + await editorState.insertTextAtCurrentSelection( + word, + ); + await Future.delayed(delay, () {}); + } + break; + } + + // insert new line + if (lines.length > 1) { + await editorState.insertNewLineAtCurrentSelection(); + } + } + } +} 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/appflowy_editor.dart b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart index cf5c4e2d75..eab2d43c60 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,5 @@ 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'; +export 'src/render/toolbar/toolbar_item.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..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,12 +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 { + final selection = getSelection(null); + assert(selection.isCollapsed); + final textNode = getTextNode(path: selection.start.path); + return insertText( + selection.startIndex, + text, + textNode: textNode, + ); } Future formatText( @@ -27,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( @@ -44,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( @@ -95,4 +100,29 @@ extension TextCommands on EditorState { textNode: textNode, ); } + + Future insertNewLine({ + Path? path, + }) async { + 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..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 @@ -169,6 +169,24 @@ extension TextTransaction on Transaction { )); } + void splitText(TextNode textNode, int offset) { + final delta = textNode.delta; + final second = delta.slice(offset, delta.length); + final path = textNode.path.next; + deleteText(textNode, offset, delta.length); + 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..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(); @@ -96,13 +100,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 +133,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 +152,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 +176,8 @@ class EditorState { redoItem.afterSelection = transaction.afterSelection; undoManager.redoStack.push(redoItem); } + + return completer.future; } void _debouncedSealHistoryItem() { 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/editor_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart index 9d58eb066c..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 @@ -30,6 +30,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 +52,8 @@ class AppFlowyEditor extends StatefulWidget { final List selectionMenuItems; + final List toolbarItems; + late final ThemeData themeData; final bool editable; @@ -74,6 +77,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 +98,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/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/lib/src/service/toolbar_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart index 9b9d002cd1..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'; @@ -35,11 +34,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 +73,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; 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: 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';