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: refactor the text editing code
Lucas.Xu5ecfc4ff2e82
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 e70dfc411a..94b829519e 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@@ -53,7 +53,7 @@ class TransactionBuilder {}textEdit(TextNode node, Delta Function() f) {- beforeSelection = state.cursorSelection;+ beforeSelection = state.service.selectionService.currentSelection;final path = node.path;final delta = f();@@ -91,6 +91,7 @@ class TransactionBuilder {formatText(TextNode node, int index, int length, Attributes attributes) {textEdit(node, () => Delta().retain(index).retain(length, attributes));+ afterSelection = beforeSelection;}deleteText(TextNode node, int index, int length) {diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/default_text_operations/format_rich_text_style.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/default_text_operations/format_rich_text_style.dartindex c88eab833f..5622785d45 100644--- a/frontend/app_flowy/packages/flowy_editor/lib/service/default_text_operations/format_rich_text_style.dart+++ b/frontend/app_flowy/packages/flowy_editor/lib/service/default_text_operations/format_rich_text_style.dart@@ -17,66 +17,29 @@ bool formatRichTextStyle(// 1. All nodes are text nodes.// 2. The first node is not TextNode.// 3. The last node is not TextNode.- if (textNodes.length == nodes.length) {- if (textNodes.length == 1) {+ for (var i = 0; i < textNodes.length; i++) {+ final textNode = textNodes[i];+ if (i == 0 && textNode == nodes.first) {builder.formatText(- textNodes.first,+ textNode,selection.start.offset,- selection.end.offset - selection.start.offset,+ textNode.toRawString().length - selection.start.offset,+ attributes,+ );+ } else if (i == textNodes.length - 1 && textNode == nodes.last) {+ builder.formatText(+ textNode,+ 0,+ selection.end.offset,attributes,);} else {- for (var i = 0; i < textNodes.length; i++) {- final node = textNodes[i];- if (i == 0) {- builder.formatText(- node,- selection.start.offset,- node.toRawString().length - selection.start.offset,- attributes,- );- } else if (i == textNodes.length - 1) {- builder.formatText(- node,- 0,- selection.end.offset,- attributes,- );- } else {- builder.formatText(- node,- 0,- node.toRawString().length,- attributes,- );- }- }- }- } else {- for (var i = 0; i < textNodes.length; i++) {- final node = textNodes[i];- if (i == 0 && node == nodes.first) {- builder.formatText(- node,- selection.start.offset,- node.toRawString().length - selection.start.offset,- attributes,- );- } else if (i == textNodes.length - 1 && node == nodes.last) {- builder.formatText(- node,- 0,- selection.end.offset,- attributes,- );- } else {- builder.formatText(- node,- 0,- node.toRawString().length,- attributes,- );- }+ builder.formatText(+ textNode,+ 0,+ textNode.toRawString().length,+ attributes,+ );}}