From 931f7e65137ac977a5a884c8d587f93c4c14a695 Mon Sep 17 00:00:00 2001 From: Andy Trand Date: Tue, 7 Dec 2021 12:53:13 +0200 Subject: [PATCH] remove newline on concat (#502) --- lib/src/models/quill_delta.dart | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/src/models/quill_delta.dart b/lib/src/models/quill_delta.dart index e4a11055..3cab1910 100644 --- a/lib/src/models/quill_delta.dart +++ b/lib/src/models/quill_delta.dart @@ -608,9 +608,28 @@ class Delta { } } + /// Removes trailing '\n' + void _trimNewLine() { + if (isNotEmpty) { + final lastOp = _operations.last; + final lastOpData = lastOp.data; + + if (lastOpData is String && lastOpData.endsWith('\n')) { + _operations.removeLast(); + if (lastOpData.length > 1) { + insert(lastOpData.substring(0, lastOpData.length - 1), + lastOp.attributes); + } + } + } + } + /// Concatenates [other] with this delta and returns the result. - Delta concat(Delta other) { + Delta concat(Delta other, {bool trimNewLine = false}) { final result = Delta.from(this); + if (trimNewLine) { + result._trimNewLine(); + } if (other.isNotEmpty) { // In case first operation of other can be merged with last operation in // our list.