mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: text robot
This commit is contained in:
parent
91efcafd77
commit
34d4ea3e54
@ -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<HomePage> {
|
||||
_loadEditor(context, jsonString);
|
||||
}),
|
||||
|
||||
// Text Robot
|
||||
_buildSeparator(context, 'Text Robot'),
|
||||
_buildListTile(context, 'Type Text Automatically', () async {
|
||||
final jsonString = Future<String>.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<HomePage> {
|
||||
);
|
||||
}
|
||||
|
||||
void _loadEditor(BuildContext context, Future<String> jsonString) {
|
||||
Future<void> _loadEditor(
|
||||
BuildContext context,
|
||||
Future<String> jsonString,
|
||||
) async {
|
||||
final completer = Completer<void>();
|
||||
_jsonString = jsonString;
|
||||
setState(
|
||||
() {
|
||||
@ -213,6 +243,10 @@ class _HomePageState extends State<HomePage> {
|
||||
);
|
||||
},
|
||||
);
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
completer.complete();
|
||||
});
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
void _exportFile(
|
||||
|
@ -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<void> 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]);
|
||||
}
|
||||
}
|
||||
}
|
@ -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';
|
||||
|
@ -20,6 +20,19 @@ extension TextCommands on EditorState {
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> 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<void> formatText(
|
||||
EditorState editorState,
|
||||
Selection? selection,
|
||||
@ -95,4 +108,19 @@ extension TextCommands on EditorState {
|
||||
textNode: textNode,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user