From 860c4abbbabab1b8b47ce62dc5e3881f02e89844 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 | 12 ++++++++++ lib/src/controller/quill_controller.dart | 22 +++++++++++-------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 1878fb136..27e922e3a 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( + enableExternalRichPaste: 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..0bcb51627 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.enableExternalRichPaste, }); /// Callback to allow overriding the default clipboard paste handling. @@ -76,4 +77,15 @@ 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 from external sources (system clipboard) is enabled. + /// + /// When enabled, rich text content from other apps can be pasted into the editor, + /// using platform APIs to access the system clipboard. + /// + /// Defaults to `true`. + /// + /// See also: https://pub.dev/packages/flutter_quill#-rich-text-paste + @experimental + final bool? enableExternalRichPaste; } diff --git a/lib/src/controller/quill_controller.dart b/lib/src/controller/quill_controller.dart index e8578732d..5f29566e5 100644 --- a/lib/src/controller/quill_controller.dart +++ b/lib/src/controller/quill_controller.dart @@ -565,16 +565,20 @@ class QuillController extends ChangeNotifier { return true; } - final pasteHtmlSuccess = await pasteHTML(); - if (pasteHtmlSuccess) { - updateEditor?.call(); - return true; - } + const enableExternalRichPasteDefault = true; + if (clipboardConfig?.enableExternalRichPaste ?? + enableExternalRichPasteDefault) { + 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`.