From 02db2adb7de3c94e7d2c977654ab18b8d2031e9e Mon Sep 17 00:00:00 2001 From: singerdmx Date: Wed, 23 Dec 2020 02:25:50 -0800 Subject: [PATCH] Fix copy/paste --- lib/widgets/raw_editor.dart | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/widgets/raw_editor.dart b/lib/widgets/raw_editor.dart index e0638d83..f752ecf2 100644 --- a/lib/widgets/raw_editor.dart +++ b/lib/widgets/raw_editor.dart @@ -457,12 +457,12 @@ class RawEditorState extends EditorState @override TextEditingValue get textEditingValue { - return widget.controller.plainTextEditingValue; + return getTextEditingValue(); } @override set textEditingValue(TextEditingValue value) { - widget.controller.updateSelection(value.selection, ChangeSource.LOCAL); + setTextEditingValue(value); } @override @@ -993,7 +993,40 @@ class RawEditorState extends EditorState @override setTextEditingValue(TextEditingValue value) { - widget.controller.updateSelection(value.selection, ChangeSource.LOCAL); + if (value.text == textEditingValue.text) { + widget.controller.updateSelection(value.selection, ChangeSource.LOCAL); + } else { + __setEditingValue(value); + } + } + + void __setEditingValue(TextEditingValue value) async { + if (await __isItCut(value)) { + widget.controller.replaceText( + textEditingValue.selection.start, + textEditingValue.text.length - value.text.length, + '', + value.selection, + ); + } else { + final TextEditingValue value = textEditingValue; + final ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); + if (data != null) { + final length = + textEditingValue.selection.end - textEditingValue.selection.start; + widget.controller.replaceText( + value.selection.start, + length, + data.text, + value.selection, + ); + } + } + } + + Future __isItCut(TextEditingValue value) async { + final ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); + return textEditingValue.text.length - value.text.length == data.text.length; } @override