mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #1676 from LucasXu0/text_robot
feat: integrate OpenAI service
This commit is contained in:
commit
92baa573e1
@ -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.
|
||||
|
@ -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<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(
|
||||
|
@ -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 {
|
||||
|
@ -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();
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
@ -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<TextNode>();
|
||||
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);
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
@ -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<void> getGPT3Completion(
|
||||
String prompt,
|
||||
String suffix, {
|
||||
required Future<void> Function(String result) onResult,
|
||||
required Future<void> 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<void> getGPT3Edit(
|
||||
String apiKey,
|
||||
String input,
|
||||
String instruction, {
|
||||
required Future<void> Function(List<String> result) onResult,
|
||||
required Future<void> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<TextNode>().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<SmartEditWidget> createState() => _SmartEditWidgetState();
|
||||
}
|
||||
|
||||
class _SmartEditWidgetState extends State<SmartEditWidget> {
|
||||
final inputEventController = TextEditingController(text: '');
|
||||
final resultController = TextEditingController(text: '');
|
||||
|
||||
var result = '';
|
||||
|
||||
final gpt3 = const GPT3APIClient(apiKey: apiKey);
|
||||
|
||||
Iterable<TextNode> get currentSelectedTextNodes =>
|
||||
widget.editorState.service.selectionService.currentSelectedNodes
|
||||
.whereType<TextNode>();
|
||||
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<TextNode>()
|
||||
.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);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
@ -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<void> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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:
|
||||
|
@ -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';
|
||||
|
@ -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<void> 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<void> 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<void> 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<void> formatTextToCheckbox(
|
||||
@ -95,4 +100,29 @@ extension TextCommands on EditorState {
|
||||
textNode: textNode,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> 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<void> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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<SelectionMenuItem> selectionMenuItems = [];
|
||||
|
||||
/// Stores the toolbar items.
|
||||
List<ToolbarItem> toolbarItems = [];
|
||||
|
||||
/// Operation stream.
|
||||
Stream<Transaction> get transactionStream => _observer.stream;
|
||||
final StreamController<Transaction> _observer = StreamController.broadcast();
|
||||
@ -96,13 +100,21 @@ class EditorState {
|
||||
return null;
|
||||
}
|
||||
|
||||
updateCursorSelection(Selection? cursorSelection,
|
||||
[CursorUpdateReason reason = CursorUpdateReason.others]) {
|
||||
Future<void> updateCursorSelection(
|
||||
Selection? cursorSelection, [
|
||||
CursorUpdateReason reason = CursorUpdateReason.others,
|
||||
]) {
|
||||
final completer = Completer<void>();
|
||||
|
||||
// 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<void> apply(
|
||||
Transaction transaction, {
|
||||
ApplyOptions options = const ApplyOptions(recordUndo: true),
|
||||
ruleCount = 0,
|
||||
withUpdateCursor = true,
|
||||
}) {
|
||||
}) async {
|
||||
final completer = Completer<void>();
|
||||
|
||||
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() {
|
||||
|
@ -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';
|
||||
|
@ -96,7 +96,7 @@ class _SelectionMenuWidgetState extends State<SelectionMenuWidget> {
|
||||
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);
|
||||
}
|
||||
|
@ -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';
|
||||
|
@ -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<SelectionMenuItem> selectionMenuItems;
|
||||
|
||||
final List<ToolbarItem> toolbarItems;
|
||||
|
||||
late final ThemeData themeData;
|
||||
|
||||
final bool editable;
|
||||
@ -74,6 +77,7 @@ class _AppFlowyEditorState extends State<AppFlowyEditor> {
|
||||
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<AppFlowyEditor> {
|
||||
|
||||
if (editorState.service != oldWidget.editorState.service) {
|
||||
editorState.selectionMenuItems = widget.selectionMenuItems;
|
||||
editorState.toolbarItems = widget.toolbarItems;
|
||||
editorState.service.renderPluginService = _createRenderPlugin();
|
||||
}
|
||||
|
||||
|
@ -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';
|
||||
|
||||
|
@ -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<FlowyToolbar>
|
||||
implements AppFlowyToolbarService {
|
||||
OverlayEntry? _toolbarOverlay;
|
||||
final _toolbarWidgetKey = GlobalKey(debugLabel: '_toolbar_widget');
|
||||
late final List<ToolbarItem> 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<FlowyToolbar>
|
||||
|
||||
@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;
|
||||
|
@ -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:
|
||||
|
@ -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';
|
||||
|
Loading…
Reference in New Issue
Block a user