mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-26 03:56:45 +08:00
copy/cut rich text
Closes #1047 Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2442,9 +2442,10 @@ class EditableTextState extends State<EditableText>
|
||||
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<EditableText>
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user