From f36deecf9be482954efc7e16a20a22a9b58656f7 Mon Sep 17 00:00:00 2001 From: Ellet Date: Fri, 8 Nov 2024 20:19:20 +0300 Subject: [PATCH] feat: add the option to disable rich text paste feature, partial fix to https://github.com/singerdmx/flutter-quill/issues/2350 --- example/lib/main.dart | 18 +++++++++------- .../clipboard/quill_clipboard_config.dart | 9 ++++++++ lib/src/controller/quill_controller.dart | 21 +++++++++++-------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 1878fb136..170e2081e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -40,15 +40,11 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - late final QuillController _controller; - final FocusNode _editorFocusNode = FocusNode(); - final ScrollController _editorScrollController = ScrollController(); - - @override - void initState() { - super.initState(); - _controller = QuillController.basic(config: QuillControllerConfig( + final QuillController _controller = () { + return QuillController.basic( + config: QuillControllerConfig( clipboardConfig: QuillClipboardConfig( + enableRichPaste: true, onImagePaste: (imageBytes) async { if (kIsWeb) { // Dart IO is unsupported on the web. @@ -69,7 +65,13 @@ class _HomePageState extends State { }, ), )); + }(); + final FocusNode _editorFocusNode = FocusNode(); + final ScrollController _editorScrollController = ScrollController(); + @override + void initState() { + super.initState(); // Load document _controller.document = Document.fromJson(kQuillDefaultSample); } diff --git a/lib/src/controller/clipboard/quill_clipboard_config.dart b/lib/src/controller/clipboard/quill_clipboard_config.dart index 206fcfdc3..d1cd739de 100644 --- a/lib/src/controller/clipboard/quill_clipboard_config.dart +++ b/lib/src/controller/clipboard/quill_clipboard_config.dart @@ -13,6 +13,7 @@ class QuillClipboardConfig { @experimental this.onGifPaste, @experimental this.onDeltaPaste, @experimental this.onPlainTextPaste, + @experimental this.enableRichPaste, }); /// Callback to allow overriding the default clipboard paste handling. @@ -76,4 +77,12 @@ class QuillClipboardConfig { /// Return modified text to override the pasted content, or `null` to use the default. @experimental final Future Function(String plainText)? onPlainTextPaste; + + /// Determines if rich text pasting is enabled. + /// + /// Defaults to `true`. + /// + /// See also: https://pub.dev/packages/flutter_quill#-rich-text-paste + @experimental + final bool? enableRichPaste; } diff --git a/lib/src/controller/quill_controller.dart b/lib/src/controller/quill_controller.dart index e8578732d..24335b657 100644 --- a/lib/src/controller/quill_controller.dart +++ b/lib/src/controller/quill_controller.dart @@ -565,16 +565,19 @@ class QuillController extends ChangeNotifier { return true; } - final pasteHtmlSuccess = await pasteHTML(); - if (pasteHtmlSuccess) { - updateEditor?.call(); - return true; - } + const enableRichPasteDefault = true; + if (clipboardConfig?.enableRichPaste ?? enableRichPasteDefault) { + final pasteHtmlSuccess = await pasteHTML(); + if (pasteHtmlSuccess) { + updateEditor?.call(); + return true; + } - final pasteMarkdownSuccess = await pasteMarkdown(); - if (pasteMarkdownSuccess) { - updateEditor?.call(); - return true; + final pasteMarkdownSuccess = await pasteMarkdown(); + if (pasteMarkdownSuccess) { + updateEditor?.call(); + return true; + } } // Snapshot the input before using `await`.