diff --git a/lib/common/widgets/text_field/controller.dart b/lib/common/widgets/text_field/controller.dart index 31fc2081..379c7ace 100644 --- a/lib/common/widgets/text_field/controller.dart +++ b/lib/common/widgets/text_field/controller.dart @@ -19,6 +19,7 @@ import 'dart:math'; import 'package:PiliPlus/common/widgets/image/network_img_layer.dart'; import 'package:PiliPlus/models/common/image_type.dart'; +import 'package:flutter/foundation.dart' show kDebugMode; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -1019,4 +1020,37 @@ class RichTextEditingController extends TextEditingController { } return newSelection; } + + String? getSelectionText(TextSelection selection) { + try { + String text = ''; + final start = selection.start; + final end = selection.end; + for (var e in items) { + final range = e.range; + if (start >= range.end) { + continue; + } + if (end <= range.start) { + break; + } + if (e.isRich) { + if (e.emote != null) { + text += e.rawText; + } else { + text += e.text; + } + } else { + text += e.text.substring( + max(start, range.start) - range.start, + min(end, range.end) - range.start, + ); + } + } + return text; + } catch (e) { + if (kDebugMode) debugPrint('err getSelectionText: $e'); + return null; + } + } } diff --git a/lib/common/widgets/text_field/editable_text.dart b/lib/common/widgets/text_field/editable_text.dart index a1951571..663f5dfd 100644 --- a/lib/common/widgets/text_field/editable_text.dart +++ b/lib/common/widgets/text_field/editable_text.dart @@ -2442,9 +2442,10 @@ class EditableTextState extends State if (selection.isCollapsed || widget.obscureText) { return; } - // TODO copy - String text = textEditingValue.text; - Clipboard.setData(ClipboardData(text: selection.textInside(text))); + final String text = + widget.controller.getSelectionText(selection) ?? + selection.textInside(textEditingValue.text); + Clipboard.setData(ClipboardData(text: text)); if (cause == SelectionChangedCause.toolbar) { bringIntoView(textEditingValue.selection.extent); hideToolbar(false); @@ -2480,11 +2481,13 @@ class EditableTextState extends State return; } final TextSelection selection = textEditingValue.selection; - final String text = textEditingValue.text; if (selection.isCollapsed) { return; } - Clipboard.setData(ClipboardData(text: selection.textInside(text))); + final String text = + widget.controller.getSelectionText(selection) ?? + selection.textInside(textEditingValue.text); + Clipboard.setData(ClipboardData(text: text)); _replaceText(ReplaceTextIntent(textEditingValue, '', selection, cause)); if (cause == SelectionChangedCause.toolbar) { // Schedule a call to bringIntoView() after renderEditable updates.