mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: implement enter key event handler and keep attributes after insert
This commit is contained in:
parent
1ba15b321b
commit
14bd18e21c
@ -205,6 +205,19 @@ class TextNode extends Node {
|
||||
return map;
|
||||
}
|
||||
|
||||
TextNode copyWith({
|
||||
String? type,
|
||||
LinkedList<Node>? children,
|
||||
Attributes? attributes,
|
||||
Delta? delta,
|
||||
}) =>
|
||||
TextNode(
|
||||
type: type ?? this.type,
|
||||
children: children ?? this.children,
|
||||
attributes: attributes ?? this.attributes,
|
||||
delta: delta ?? this.delta,
|
||||
);
|
||||
|
||||
// TODO: It's unneccesry to compute everytime.
|
||||
String toRawString() =>
|
||||
_delta.operations.whereType<TextInsert>().map((op) => op.content).join();
|
||||
|
@ -1,19 +1,18 @@
|
||||
import 'dart:collection';
|
||||
import 'dart:math';
|
||||
import 'package:flowy_editor/editor_state.dart';
|
||||
|
||||
import 'package:flowy_editor/document/attributes.dart';
|
||||
import 'package:flowy_editor/document/node.dart';
|
||||
import 'package:flowy_editor/document/path.dart';
|
||||
import 'package:flowy_editor/document/position.dart';
|
||||
import 'package:flowy_editor/document/text_delta.dart';
|
||||
import 'package:flowy_editor/document/attributes.dart';
|
||||
import 'package:flowy_editor/document/selection.dart';
|
||||
|
||||
import './operation.dart';
|
||||
import './transaction.dart';
|
||||
import 'package:flowy_editor/document/text_delta.dart';
|
||||
import 'package:flowy_editor/editor_state.dart';
|
||||
import 'package:flowy_editor/operation/operation.dart';
|
||||
import 'package:flowy_editor/operation/transaction.dart';
|
||||
|
||||
/// A [TransactionBuilder] is used to build the transaction from the state.
|
||||
/// It will save make a snapshot of the cursor selection state automatically.
|
||||
/// The cursor can be resoted if the transaction is undo.
|
||||
/// The cursor can be resorted if the transaction is undo.
|
||||
|
||||
class TransactionBuilder {
|
||||
final List<Operation> operations = [];
|
||||
@ -30,8 +29,12 @@ class TransactionBuilder {
|
||||
}
|
||||
|
||||
insertNode(Path path, Node node) {
|
||||
beforeSelection = state.cursorSelection;
|
||||
beforeSelection = state.service.selectionService.currentSelection;
|
||||
add(InsertOperation(path: path, value: node));
|
||||
// FIXME: Not exactly correct, needs to be customized.
|
||||
afterSelection = Selection.collapsed(
|
||||
Position(path: path, offset: 0),
|
||||
);
|
||||
}
|
||||
|
||||
updateNode(Node node, Attributes attributes) {
|
||||
|
@ -1,13 +1,16 @@
|
||||
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/service/keyboard_service.dart';
|
||||
import 'package:flowy_editor/extensions/path_extensions.dart';
|
||||
import 'package:flowy_editor/extensions/node_extensions.dart';
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:flowy_editor/document/node.dart';
|
||||
import 'package:flowy_editor/document/text_delta.dart';
|
||||
import 'package:flowy_editor/extensions/node_extensions.dart';
|
||||
import 'package:flowy_editor/extensions/path_extensions.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 enterInEdgeOfTextNodeHandler = (editorState, event) {
|
||||
if (event.logicalKey != LogicalKeyboardKey.enter) {
|
||||
return KeyEventResult.ignored;
|
||||
@ -23,12 +26,19 @@ FlowyKeyEventHandler enterInEdgeOfTextNodeHandler = (editorState, event) {
|
||||
}
|
||||
|
||||
final textNode = nodes.first as TextNode;
|
||||
|
||||
if (textNode.selectable!.end() == selection.end) {
|
||||
final needCopyAttributes = StyleKey.globalStyleKeys
|
||||
.where((key) => key != StyleKey.heading)
|
||||
.contains(textNode.subtype);
|
||||
TransactionBuilder(editorState)
|
||||
..insertNode(
|
||||
textNode.path.next,
|
||||
TextNode.empty(),
|
||||
textNode.copyWith(
|
||||
children: LinkedList(),
|
||||
delta: Delta([TextInsert(' ')]),
|
||||
attributes:
|
||||
needCopyAttributes ? {StyleKey.subtype: textNode.subtype} : null,
|
||||
),
|
||||
)
|
||||
..commit();
|
||||
return KeyEventResult.handled;
|
||||
@ -36,7 +46,11 @@ FlowyKeyEventHandler enterInEdgeOfTextNodeHandler = (editorState, event) {
|
||||
TransactionBuilder(editorState)
|
||||
..insertNode(
|
||||
textNode.path,
|
||||
TextNode.empty(),
|
||||
textNode.copyWith(
|
||||
children: LinkedList(),
|
||||
delta: Delta([TextInsert(' ')]),
|
||||
attributes: {},
|
||||
),
|
||||
)
|
||||
..commit();
|
||||
return KeyEventResult.handled;
|
||||
|
@ -1,8 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flowy_editor/document/node.dart';
|
||||
import 'package:flowy_editor/extensions/text_node_extensions.dart';
|
||||
import 'package:flowy_editor/render/rich_text/rich_text_style.dart';
|
||||
import 'package:flowy_editor/service/default_text_operations/format_rich_text_style.dart';
|
||||
import 'package:flowy_editor/service/keyboard_service.dart';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user