Skip to content

Commit 34d4ea3

Browse files
committed
feat: text robot
1 parent 91efcaf commit 34d4ea3

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import 'dart:async';
12
import 'dart:convert';
23
import 'dart:io';
34

45
import 'package:appflowy_editor/appflowy_editor.dart';
56
import 'package:example/pages/simple_editor.dart';
7+
import 'package:example/plugin/text_robot.dart';
68
import 'package:file_picker/file_picker.dart';
79
import 'package:flutter/foundation.dart';
810
import 'package:flutter/material.dart';
@@ -102,6 +104,30 @@ class _HomePageState extends State<HomePage> {
102104
_loadEditor(context, jsonString);
103105
}),
104106

107+
// Text Robot
108+
_buildSeparator(context, 'Text Robot'),
109+
_buildListTile(context, 'Type Text Automatically', () async {
110+
final jsonString = Future<String>.value(
111+
jsonEncode(EditorState.empty().document.toJson()).toString(),
112+
);
113+
await _loadEditor(context, jsonString);
114+
115+
Future.delayed(const Duration(seconds: 2), () {
116+
final textRobot = TextRobot(
117+
editorState: _editorState,
118+
);
119+
textRobot.insertText(
120+
r'''
121+
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
122+
"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?"
123+
124+
1914 translation by H. Rackham
125+
"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?"
126+
''',
127+
);
128+
});
129+
}),
130+
105131
// Encoder Demo
106132
_buildSeparator(context, 'Encoder Demo'),
107133
_buildListTile(context, 'Export To JSON', () {
@@ -200,7 +226,11 @@ class _HomePageState extends State<HomePage> {
200226
);
201227
}
202228

203-
void _loadEditor(BuildContext context, Future<String> jsonString) {
229+
Future<void> _loadEditor(
230+
BuildContext context,
231+
Future<String> jsonString,
232+
) async {
233+
final completer = Completer<void>();
204234
_jsonString = jsonString;
205235
setState(
206236
() {
@@ -213,6 +243,10 @@ class _HomePageState extends State<HomePage> {
213243
);
214244
},
215245
);
246+
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
247+
completer.complete();
248+
});
249+
return completer.future;
216250
}
217251

218252
void _exportFile(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:appflowy_editor/appflowy_editor.dart';
2+
3+
enum TextRobotInputType {
4+
character,
5+
word,
6+
}
7+
8+
class TextRobot {
9+
const TextRobot({
10+
required this.editorState,
11+
this.delay = const Duration(milliseconds: 30),
12+
});
13+
14+
final EditorState editorState;
15+
final Duration delay;
16+
17+
Future<void> insertText(
18+
String text, {
19+
TextRobotInputType inputType = TextRobotInputType.character,
20+
}) async {
21+
final lines = text.split('\n');
22+
var path = 0;
23+
for (final line in lines) {
24+
switch (inputType) {
25+
case TextRobotInputType.character:
26+
var index = 0;
27+
final iterator = line.runes.iterator;
28+
while (iterator.moveNext()) {
29+
// await editorState.insertText(
30+
// index,
31+
// iterator.currentAsString,
32+
// path: [path],
33+
// );
34+
await editorState.insertTextAtCurrentSelection(
35+
iterator.currentAsString,
36+
);
37+
index += iterator.currentSize;
38+
await Future.delayed(delay);
39+
}
40+
path += 1;
41+
break;
42+
default:
43+
}
44+
45+
// insert new line
46+
await editorState.insertNewLine(editorState, [path]);
47+
}
48+
}
49+
}

frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ export 'src/plugins/markdown/encoder/parser/image_node_parser.dart';
4242
export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart';
4343
export 'src/plugins/markdown/document_markdown.dart';
4444
export 'src/plugins/quill_delta/delta_document_encoder.dart';
45+
export 'src/commands/text/text_commands.dart';

frontend/app_flowy/packages/appflowy_editor/lib/src/commands/text/text_commands.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ extension TextCommands on EditorState {
2020
});
2121
}
2222

23+
Future<void> insertTextAtCurrentSelection(String text) async {
24+
return futureCommand(() async {
25+
final selection = getSelection(null);
26+
assert(selection.isCollapsed);
27+
final textNode = getTextNode(path: selection.start.path);
28+
await insertText(
29+
textNode.toPlainText().length,
30+
text,
31+
textNode: textNode,
32+
);
33+
});
34+
}
35+
2336
Future<void> formatText(
2437
EditorState editorState,
2538
Selection? selection,
@@ -95,4 +108,19 @@ extension TextCommands on EditorState {
95108
textNode: textNode,
96109
);
97110
}
111+
112+
Future<void> insertNewLine(
113+
EditorState editorState,
114+
Path path,
115+
) async {
116+
return futureCommand(() async {
117+
final transaction = editorState.transaction;
118+
transaction.insertNode(path, TextNode.empty());
119+
transaction.afterSelection = Selection.single(
120+
path: path,
121+
startOffset: 0,
122+
);
123+
apply(transaction);
124+
});
125+
}
98126
}

0 commit comments

Comments
 (0)