From 373193ac3ccd8acebdde1bb137d3282f68abca9b Mon Sep 17 00:00:00 2001 From: X Code Date: Wed, 22 Dec 2021 21:19:44 -0800 Subject: [PATCH] Upgrade to 2.5.2: Skip image when pasting --- CHANGELOG.md | 3 ++ lib/src/widgets/raw_editor.dart | 54 ------------------- ...editor_state_selection_delegate_mixin.dart | 33 +++++++----- pubspec.yaml | 2 +- 4 files changed, 24 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72e0794f..dfc6b171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [2.5.2] +* Skip image when pasting. + ## [2.5.1] * Bug fix for Desktop `Shift` + `Click` support. diff --git a/lib/src/widgets/raw_editor.dart b/lib/src/widgets/raw_editor.dart index ce9540ab..e8cf766d 100644 --- a/lib/src/widgets/raw_editor.dart +++ b/lib/src/widgets/raw_editor.dart @@ -661,60 +661,6 @@ class RawEditorState extends EditorState getRenderEditor()!.debugAssertLayoutUpToDate(); } - // set editing value from clipboard for mobile - Future _setEditingValue(TextEditingValue value) async { - if (await _isItCut(value)) { - widget.controller.replaceText( - textEditingValue.selection.start, - textEditingValue.text.length - value.text.length, - '', - value.selection, - ); - } else { - final value = textEditingValue; - final data = await Clipboard.getData(Clipboard.kTextPlain); - if (data != null) { - final length = - textEditingValue.selection.end - textEditingValue.selection.start; - var str = data.text!; - final codes = data.text!.codeUnits; - // For clip from editor, it may contain image, a.k.a 65532. - // For clip from browser, image is directly ignore. - // Here we skip image when pasting. - if (codes.contains(65532)) { - final sb = StringBuffer(); - for (var i = 0; i < str.length; i++) { - if (str.codeUnitAt(i) == 65532) { - continue; - } - sb.write(str[i]); - } - str = sb.toString(); - } - widget.controller.replaceText( - value.selection.start, - length, - str, - value.selection, - ); - // move cursor to the end of pasted text selection - widget.controller.updateSelection( - TextSelection.collapsed( - offset: value.selection.start + data.text!.length), - ChangeSource.LOCAL); - } - } - } - - Future _isItCut(TextEditingValue value) async { - final data = await Clipboard.getData(Clipboard.kTextPlain); - if (data == null) { - return false; - } - return textEditingValue.text.length - value.text.length == - data.text!.length; - } - @override bool showToolbar() { // Web is using native dom elements to enable clipboard functionality of the diff --git a/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart b/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart index 2b51ef05..51d27769 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart @@ -19,21 +19,28 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState final oldText = widget.controller.document.toPlainText(); final newText = value.text; final diff = getDiff(oldText, newText, cursorPosition); - var data = diff.inserted; - if (diff.inserted.codeUnits.contains(65532)) { - final sb = StringBuffer(); - - for (var i = 0; i < data.length; i++) { - if (data.codeUnitAt(i) == 65532) { - continue; - } - sb.write(data[i]); - } - data = sb.toString(); + final insertedText = _adjustInsertedText(diff.inserted); + + widget.controller.replaceText( + diff.start, diff.deleted.length, insertedText, value.selection); + } + + String _adjustInsertedText(String text) { + // For clip from editor, it may contain image, a.k.a 65532. + // For clip from browser, image is directly ignore. + // Here we skip image when pasting. + if (!text.codeUnits.contains(65532)) { + return text; } - widget.controller - .replaceText(diff.start, diff.deleted.length, data, value.selection); + final sb = StringBuffer(); + for (var i = 0; i < text.length; i++) { + if (text.codeUnitAt(i) == 65532) { + continue; + } + sb.write(text[i]); + } + return sb.toString(); } @override diff --git a/pubspec.yaml b/pubspec.yaml index b3200073..08930761 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_quill description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us) -version: 2.5.1 +version: 2.5.2 #author: bulletjournal homepage: https://bulletjournal.us/home/index.html repository: https://github.com/singerdmx/flutter-quill