From bcc014e84d09633ee14d5090f06e609fa95af481 Mon Sep 17 00:00:00 2001 From: squidrye Date: Fri, 24 Mar 2023 08:39:35 +0530 Subject: [PATCH] refactor: using predefined operation insert node to create new nodes. --- .../lib/src/core/transform/transaction.dart | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart index 5aba71b243..10b0c81507 100644 --- a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart +++ b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart @@ -33,19 +33,19 @@ class Transaction { /// Inserts the [Node] at the given [Path]. void insertNode( - Path path, - Node node, { - bool deepCopy = true, - }) { + Path path, + Node node, { + bool deepCopy = true, + }) { insertNodes(path, [node], deepCopy: deepCopy); } /// Inserts a sequence of [Node]s at the given [Path]. void insertNodes( - Path path, - Iterable nodes, { - bool deepCopy = true, - }) { + Path path, + Iterable nodes, { + bool deepCopy = true, + }) { if (deepCopy) { add(InsertOperation(path, nodes.map((e) => e.copyWith()))); } else { @@ -122,9 +122,7 @@ class Transaction { void add(Operation op, {bool transform = true}) { final Operation? last = operations.isEmpty ? null : operations.last; if (last != null) { - if (op is UpdateTextOperation && - last is UpdateTextOperation && - op.path.equals(last.path)) { + if (op is UpdateTextOperation && last is UpdateTextOperation && op.path.equals(last.path)) { final newOp = UpdateTextOperation( op.path, last.delta.compose(op.delta), @@ -148,11 +146,11 @@ class Transaction { extension TextTransaction on Transaction { void mergeText( - TextNode first, - TextNode second, { - int? firstOffset, - int secondOffset = 0, - }) { + TextNode first, + TextNode second, { + int? firstOffset, + int secondOffset = 0, + }) { final firstLength = first.delta.length; final secondLength = second.delta.length; firstOffset ??= firstLength; @@ -192,15 +190,14 @@ extension TextTransaction on Transaction { /// Optionally, you may specify formatting attributes that are applied to the inserted string. /// By default, the formatting attributes before the insert position will be reused. void insertText( - TextNode textNode, - int index, - String text, { - Attributes? attributes, - }) { + TextNode textNode, + int index, + String text, { + Attributes? attributes, + }) { var newAttributes = attributes; if (index != 0 && attributes == null) { - newAttributes = - textNode.delta.slice(max(index - 1, 0), index).first.attributes; + newAttributes = textNode.delta.slice(max(index - 1, 0), index).first.attributes; if (newAttributes != null) { newAttributes = {...newAttributes}; // make a copy } @@ -218,11 +215,11 @@ extension TextTransaction on Transaction { /// Assigns a formatting attributes to a range of text. void formatText( - TextNode textNode, - int index, - int length, - Attributes attributes, - ) { + TextNode textNode, + int index, + int length, + Attributes attributes, + ) { afterSelection = beforeSelection; updateText( textNode, @@ -234,10 +231,10 @@ extension TextTransaction on Transaction { /// Deletes the text of specified length starting at index. void deleteText( - TextNode textNode, - int index, - int length, - ) { + TextNode textNode, + int index, + int length, + ) { updateText( textNode, Delta() @@ -254,16 +251,15 @@ extension TextTransaction on Transaction { /// Optionally, you may specify formatting attributes that are applied to the inserted string. /// By default, the formatting attributes before the insert position will be reused. void replaceText( - TextNode textNode, - int index, - int length, - String text, { - Attributes? attributes, - }) { + TextNode textNode, + int index, + int length, + String text, { + Attributes? attributes, + }) { var newAttributes = attributes; if (index != 0 && attributes == null) { - newAttributes = - textNode.delta.slice(max(index - 1, 0), index).first.attributes; + newAttributes = textNode.delta.slice(max(index - 1, 0), index).first.attributes; if (newAttributes == null) { final slicedDelta = textNode.delta.slice(index, index + length); if (slicedDelta.isNotEmpty) { @@ -287,10 +283,10 @@ extension TextTransaction on Transaction { } void replaceTexts( - List textNodes, - Selection selection, - List texts, - ) { + List textNodes, + Selection selection, + List texts, + ) { if (textNodes.isEmpty || texts.isEmpty) { return; } @@ -388,6 +384,7 @@ extension TextTransaction on Transaction { textNodes.first.toPlainText().length, text, ); + path = path.next; } else if (i == length - 1 && textNodes.length >= 2) { replaceText( textNodes.last, @@ -395,6 +392,7 @@ extension TextTransaction on Transaction { selection.endIndex, text, ); + path = path.next; } else { if (i < textNodes.length - 1) { replaceText( @@ -403,24 +401,26 @@ extension TextTransaction on Transaction { textNodes[i].toPlainText().length, text, ); + path = path.next; } else { if (i == texts.length - 1) { //ADD OFFSET CHARACTER TO END OF LAST TEXT-NODE TO AVOID DATA LOSS - document.insert(path, [ + insertNode( + path, TextNode( delta: Delta()..insert("${text}${offSetChar}"), ), - ]); + ); } else { - document.insert(path, [ + insertNode( + path, TextNode( delta: Delta()..insert(text), ), - ]); + ); } } } - path = path.next; } afterSelection = null; return;