feat: convert text to bulleted list style when inputing '* ' or '- '

This commit is contained in:
Lucas.Xu 2022-08-08 13:59:38 +08:00
parent 849e19e1ef
commit 7855e5403c
3 changed files with 66 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import 'package:flowy_editor/service/internal_key_event_handlers/delete_text_han
import 'package:flowy_editor/service/internal_key_event_handlers/enter_in_edge_of_text_node_handler.dart';
import 'package:flowy_editor/service/internal_key_event_handlers/slash_handler.dart';
import 'package:flowy_editor/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dart';
import 'package:flowy_editor/service/internal_key_event_handlers/whitespace_handler.dart';
import 'package:flowy_editor/service/keyboard_service.dart';
import 'package:flowy_editor/service/render_plugin_service.dart';
import 'package:flowy_editor/service/scroll_service.dart';
@ -40,6 +41,7 @@ List<FlowyKeyEventHandler> defaultKeyEventHandler = [
copyPasteKeysHandler,
enterInEdgeOfTextNodeHandler,
updateTextStyleByCommandXHandler,
whiteSpaceHandler,
];
class FlowyEditor extends StatefulWidget {

View File

@ -1,8 +1,9 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flowy_editor/service/keyboard_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flowy_editor/service/keyboard_service.dart';
// Handle delete text.
FlowyKeyEventHandler deleteTextHandler = (editorState, event) {
if (event.logicalKey != LogicalKeyboardKey.backspace) {
@ -28,9 +29,16 @@ FlowyKeyEventHandler deleteTextHandler = (editorState, event) {
if (index < 0) {
// 1. style
if (textNode.subtype != null) {
transactionBuilder.updateNode(textNode, {
'subtype': null,
});
transactionBuilder
..updateNode(textNode, {
'subtype': null,
})
..afterSelection = Selection.collapsed(
Position(
path: textNode.path,
offset: 0,
),
);
} else {
// 2. non-style
// find previous text node.

View File

@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flowy_editor/document/node.dart';
import 'package:flowy_editor/document/position.dart';
import 'package:flowy_editor/document/selection.dart';
import 'package:flowy_editor/operation/transaction_builder.dart';
import 'package:flowy_editor/render/rich_text/rich_text_style.dart';
import 'package:flowy_editor/service/keyboard_service.dart';
FlowyKeyEventHandler whiteSpaceHandler = (editorState, event) {
if (event.logicalKey != LogicalKeyboardKey.space) {
return KeyEventResult.ignored;
}
/// Process markdown input style.
///
/// like, #, *, -, 1., -[],
final selection = editorState.service.selectionService.currentSelection.value;
if (selection == null || !selection.isCollapsed) {
return KeyEventResult.ignored;
}
final textNodes = editorState.service.selectionService.currentSelectedNodes
.whereType<TextNode>();
if (textNodes.length != 1) {
return KeyEventResult.ignored;
}
final builder = TransactionBuilder(editorState);
final textNode = textNodes.first;
final text = textNode.toRawString();
if (text == '*' || text == '-') {
builder
..deleteText(textNode, 0, 1)
..updateNode(textNode, {
StyleKey.subtype: StyleKey.bulletedList,
})
..afterSelection = Selection.collapsed(
Position(
path: textNode.path,
offset: 0,
),
)
..commit();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
};