Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
feat: implement enter key event handler and keep attributes after insert
Lucas.Xu14bd18e21c5d
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dartindex 0404be2a26..3a7ad36456 100644--- a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart+++ b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart@@ -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();diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dartindex 94b829519e..9233a1b08a 100644--- a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart+++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart@@ -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) {diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_in_edge_of_text_node_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_in_edge_of_text_node_handler.dartindex d1e89d393e..8b0c7c5423 100644--- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_in_edge_of_text_node_handler.dart+++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_in_edge_of_text_node_handler.dart@@ -1,12 +1,15 @@+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/position.dart';-import 'package:flowy_editor/document/selection.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';-import 'package:flowy_editor/extensions/path_extensions.dart';-import 'package:flowy_editor/extensions/node_extensions.dart';-import 'package:flutter/material.dart';-import 'package:flutter/services.dart';FlowyKeyEventHandler enterInEdgeOfTextNodeHandler = (editorState, event) {if (event.logicalKey != LogicalKeyboardKey.enter) {@@ -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;diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dartindex 6b1fbcd9ca..b062480cf2 100644--- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dart+++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dart@@ -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';