From 34d4ea3e54c62674ea781d8bf44cd4393c2418ab Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sat, 7 Jan 2023 09:38:38 +0800 Subject: [PATCH] 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); + }); + } }