Embed copy to clipboard

pull/2074/head
AtlasAutocode 9 months ago
parent fd5b86f124
commit 2fe8ffc444
  1. 11
      lib/src/controller/quill_controller.dart
  2. 5
      lib/src/document/document.dart
  3. 28
      lib/src/document/nodes/line.dart
  4. 2
      lib/src/editor/editor.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) {

@ -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].

@ -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<Leaf?> {
}
/// 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<Leaf?> {
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<Leaf?> {
}
if (len0 > 0 && nextLine != null) {
len0 = nextLine!._getPlainText(0, len0, plainText);
len0 = nextLine!._getPlainText(0, len0, plainText, config);
}
}

@ -175,6 +175,8 @@ class QuillEditorState extends State<QuillEditor>
void initState() {
super.initState();
widget.configurations.controller.editorFocusNode ??= widget.focusNode;
widget.configurations.controller.editorConfigurations ??=
widget.configurations;
if (configurations.autoFocus) {
widget.configurations.controller.editorFocusNode?.requestFocus();
}

Loading…
Cancel
Save