diff --git a/lib/src/controller/quill_controller.dart b/lib/src/controller/quill_controller.dart index 4dd3e7bf..c328cf77 100644 --- a/lib/src/controller/quill_controller.dart +++ b/lib/src/controller/quill_controller.dart @@ -17,6 +17,7 @@ import '../document/nodes/embeddable.dart'; import '../document/nodes/leaf.dart'; import '../document/structs/doc_change.dart'; import '../document/style.dart'; +import '../editor/config/editor_configurations.dart'; import '../editor_toolbar_controller_shared/clipboard/clipboard_service_provider.dart'; import 'quill_controller_configurations.dart'; @@ -485,6 +486,9 @@ class QuillController extends ChangeNotifier { /// Used to give focus to the editor following a toolbar action FocusNode? editorFocusNode; + /// Used to access embedBuilders for clipboard output + QuillEditorConfigurations? editorConfigurations; + ImageUrl? _copiedImageUrl; ImageUrl? get copiedImageUrl => _copiedImageUrl; @@ -495,7 +499,12 @@ class QuillController extends ChangeNotifier { bool clipboardSelection(bool copy) { copiedImageUrl = null; - _pastePlainText = getPlainText(); + + /// Get the text for the selected region and expand the content of Embedded objects. + _pastePlainText = document.getPlainText( + selection.start, selection.end - selection.start, editorConfigurations); + + /// Get the internal representation so it can be pasted into a QuillEditor with style retained. _pasteStyleAndEmbed = getAllIndividualSelectionStylesAndEmbed(); if (!selection.isCollapsed) { diff --git a/lib/src/document/document.dart b/lib/src/document/document.dart index e1f75575..d001551e 100644 --- a/lib/src/document/document.dart +++ b/lib/src/document/document.dart @@ -6,6 +6,7 @@ import '../../quill_delta.dart'; import '../common/structs/offset_value.dart'; import '../common/structs/segment_leaf_node.dart'; import '../delta/delta_x.dart'; +import '../editor/config/editor_configurations.dart'; import '../editor/embed/embed_editor_builder.dart'; import '../rules/rule.dart'; import 'attribute.dart'; @@ -239,9 +240,9 @@ class Document { } /// Returns plain text within the specified text range. - String getPlainText(int index, int len) { + String getPlainText(int index, int len, [QuillEditorConfigurations? config]) { final res = queryChild(index); - return (res.node as Line).getPlainText(res.offset, len); + return (res.node as Line).getPlainText(res.offset, len, config); } /// Returns [Line] located at specified character [offset]. diff --git a/lib/src/document/nodes/line.dart b/lib/src/document/nodes/line.dart index d442cde6..b05dd990 100644 --- a/lib/src/document/nodes/line.dart +++ b/lib/src/document/nodes/line.dart @@ -4,6 +4,7 @@ import 'package:collection/collection.dart'; import '../../../../quill_delta.dart'; import '../../common/structs/offset_value.dart'; +import '../../editor/config/editor_configurations.dart'; import '../../editor/embed/embed_editor_builder.dart'; import '../attribute.dart'; import '../style.dart'; @@ -511,25 +512,35 @@ base class Line extends QuillContainer { } /// Returns plain text within the specified text range. - String getPlainText(int offset, int len) { + String getPlainText(int offset, int len, + [QuillEditorConfigurations? config]) { final plainText = StringBuffer(); - _getPlainText(offset, len, plainText); + _getPlainText(offset, len, plainText, config); return plainText.toString(); } - int _getNodeText(Leaf node, StringBuffer buffer, int offset, int remaining) { - final text = node.toPlainText(); + int _getNodeText(Leaf node, StringBuffer buffer, int offset, int remaining, + QuillEditorConfigurations? config) { + final text = + node.toPlainText(config?.embedBuilders, config?.unknownEmbedBuilder); if (text == Embed.kObjectReplacementCharacter) { buffer.write(Embed.kObjectReplacementCharacter); return remaining - node.length; } + /// Text for clipboard will expand the content of Embed nodes + if (node is Embed && config != null) { + buffer.write(text); + return remaining - 1; + } + final end = math.min(offset + remaining, text.length); buffer.write(text.substring(offset, end)); return remaining - (end - offset); } - int _getPlainText(int offset, int len, StringBuffer plainText) { + int _getPlainText(int offset, int len, StringBuffer plainText, + QuillEditorConfigurations? config) { var len0 = len; final data = queryChild(offset, false); var node = data.node as Leaf?; @@ -540,11 +551,12 @@ base class Line extends QuillContainer { plainText.write('\n'); len0 -= 1; } else { - len0 = _getNodeText(node, plainText, offset - node.offset, len0); + len0 = + _getNodeText(node, plainText, offset - node.offset, len0, config); while (!node!.isLast && len0 > 0) { node = node.next as Leaf; - len0 = _getNodeText(node, plainText, 0, len0); + len0 = _getNodeText(node, plainText, 0, len0, config); } if (len0 > 0) { @@ -555,7 +567,7 @@ base class Line extends QuillContainer { } if (len0 > 0 && nextLine != null) { - len0 = nextLine!._getPlainText(0, len0, plainText); + len0 = nextLine!._getPlainText(0, len0, plainText, config); } } diff --git a/lib/src/editor/editor.dart b/lib/src/editor/editor.dart index a46fba7e..d94f5c67 100644 --- a/lib/src/editor/editor.dart +++ b/lib/src/editor/editor.dart @@ -175,6 +175,8 @@ class QuillEditorState extends State void initState() { super.initState(); widget.configurations.controller.editorFocusNode ??= widget.focusNode; + widget.configurations.controller.editorConfigurations ??= + widget.configurations; if (configurations.autoFocus) { widget.configurations.controller.editorFocusNode?.requestFocus(); }