From 8aca48274f62f86438ac715e7e505c7912b54a2c Mon Sep 17 00:00:00 2001 From: He Hong <49644098+TheHeBoy@users.noreply.github.com> Date: Sat, 22 Jul 2023 10:32:37 +0800 Subject: [PATCH] Fix paste text mess up style and Add support copy/cut block text. (#1321) * 1. Fix paste text mess up style. 2. Add support copy/cut block text. * Remove '../../../flutter_quill.dart' --- CHANGELOG.md | 4 +++ lib/src/models/documents/nodes/line.dart | 25 ++++++++++++------- lib/src/models/documents/style.dart | 3 +++ ...editor_state_selection_delegate_mixin.dart | 16 +++++++----- pubspec.yaml | 2 +- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b5f640b..f1daded5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# [7.2.17] +- Fix paste text mess up style. +- Add support copy/cut block text. + # [7.2.16] - Allow for custom context menu. diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index bb06bbf9..f20c7c59 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -405,25 +405,32 @@ class Line extends Container { var node = data.node as Leaf?; if (node != null) { var pos = 0; - if (node is Text || node.value is Embeddable) { - pos = node.length - data.offset; - result.add(OffsetValue( - beg, node is Text ? node.style : node.value as Embeddable)); + pos = node.length - data.offset; + if (node is Text && node.style.isNotEmpty) { + result.add(OffsetValue(beg, node.style, node.length)); + } else if (node.value is Embeddable) { + result.add(OffsetValue(beg, node.value as Embeddable, node.length)); } while (!node!.isLast && pos < local) { node = node.next as Leaf; - if (node is Text || node.value is Embeddable) { - result.add(OffsetValue( - pos + beg, node is Text ? node.style : node.value as Embeddable)); - pos += node.length; + if (node is Text && node.style.isNotEmpty) { + result.add(OffsetValue(pos + beg, node.style, node.length)); + } else if (node.value is Embeddable) { + result.add( + OffsetValue(pos + beg, node.value as Embeddable, node.length)); } + pos += node.length; } } + if (style.isNotEmpty) { + result.add(OffsetValue(beg, style)); + } + final remaining = len - local; if (remaining > 0 && nextLine != null) { final rest = nextLine! - .collectAllIndividualStylesAndEmbed(0, remaining, beg: local); + .collectAllIndividualStylesAndEmbed(0, remaining, beg: local + beg); result.addAll(rest); } diff --git a/lib/src/models/documents/style.dart b/lib/src/models/documents/style.dart index 9ade3186..58469a2a 100644 --- a/lib/src/models/documents/style.dart +++ b/lib/src/models/documents/style.dart @@ -42,6 +42,9 @@ class Style { bool get isInline => isNotEmpty && values.every((item) => item.isInline); + bool get isBlock => + isNotEmpty && values.every((item) => item.scope == AttributeScope.BLOCK); + bool get isIgnored => isNotEmpty && values.every((item) => item.scope == AttributeScope.IGNORE); diff --git a/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart b/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart index ac7c274d..52619f0b 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart @@ -6,6 +6,7 @@ import 'package:flutter/widgets.dart'; import '../../models/documents/document.dart'; import '../../models/documents/nodes/embeddable.dart'; import '../../models/documents/nodes/leaf.dart'; +import '../../models/documents/style.dart'; import '../../utils/delta.dart'; import '../editor.dart'; @@ -51,12 +52,15 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState if (styleAndEmbed is Embeddable) { widget.controller.replaceText(pos + offset, 0, styleAndEmbed, null); } else { - widget.controller.formatTextStyle( - pos + offset, - i == pasteStyleAndEmbed.length - 1 - ? pastePlainText.length - offset - : pasteStyleAndEmbed[i + 1].offset, - styleAndEmbed); + final style = styleAndEmbed as Style; + if (style.isInline) { + widget.controller.formatTextStyle( + pos + offset, pasteStyleAndEmbed[i].length!, style); + } else if (style.isBlock) { + style.values.forEach((attribute) { + widget.controller.document.format(pos + offset, 0, attribute); + }); + } } } } diff --git a/pubspec.yaml b/pubspec.yaml index fdbbe83e..6a8b8a8c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_quill description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us) -version: 7.2.16 +version: 7.2.17 #author: bulletjournal homepage: https://bulletjournal.us/home/index.html repository: https://github.com/singerdmx/flutter-quill