Upgrade to 2.5.2: Skip image when pasting

pull/555/head
X Code 3 years ago
parent 864c9bba44
commit 373193ac3c
  1. 3
      CHANGELOG.md
  2. 54
      lib/src/widgets/raw_editor.dart
  3. 33
      lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart
  4. 2
      pubspec.yaml

@ -1,3 +1,6 @@
## [2.5.2]
* Skip image when pasting.
## [2.5.1] ## [2.5.1]
* Bug fix for Desktop `Shift` + `Click` support. * Bug fix for Desktop `Shift` + `Click` support.

@ -661,60 +661,6 @@ class RawEditorState extends EditorState
getRenderEditor()!.debugAssertLayoutUpToDate(); getRenderEditor()!.debugAssertLayoutUpToDate();
} }
// set editing value from clipboard for mobile
Future<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 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<bool> _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 @override
bool showToolbar() { bool showToolbar() {
// Web is using native dom elements to enable clipboard functionality of the // Web is using native dom elements to enable clipboard functionality of the

@ -19,21 +19,28 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState
final oldText = widget.controller.document.toPlainText(); final oldText = widget.controller.document.toPlainText();
final newText = value.text; final newText = value.text;
final diff = getDiff(oldText, newText, cursorPosition); final diff = getDiff(oldText, newText, cursorPosition);
var data = diff.inserted; final insertedText = _adjustInsertedText(diff.inserted);
if (diff.inserted.codeUnits.contains(65532)) {
final sb = StringBuffer(); widget.controller.replaceText(
diff.start, diff.deleted.length, insertedText, value.selection);
for (var i = 0; i < data.length; i++) { }
if (data.codeUnitAt(i) == 65532) {
continue; String _adjustInsertedText(String text) {
} // For clip from editor, it may contain image, a.k.a 65532.
sb.write(data[i]); // For clip from browser, image is directly ignore.
} // Here we skip image when pasting.
data = sb.toString(); if (!text.codeUnits.contains(65532)) {
return text;
} }
widget.controller final sb = StringBuffer();
.replaceText(diff.start, diff.deleted.length, data, value.selection); for (var i = 0; i < text.length; i++) {
if (text.codeUnitAt(i) == 65532) {
continue;
}
sb.write(text[i]);
}
return sb.toString();
} }
@override @override

@ -1,6 +1,6 @@
name: flutter_quill name: flutter_quill
description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us) description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us)
version: 2.5.1 version: 2.5.2
#author: bulletjournal #author: bulletjournal
homepage: https://bulletjournal.us/home/index.html homepage: https://bulletjournal.us/home/index.html
repository: https://github.com/singerdmx/flutter-quill repository: https://github.com/singerdmx/flutter-quill

Loading…
Cancel
Save