Skip to content

Commit

Permalink
feat: add the option to disable rich text paste feature, partial fix to
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoEllet committed Nov 8, 2024
1 parent 79ccfe8 commit 860c4ab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
18 changes: 10 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
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.
Expand All @@ -69,7 +65,13 @@ class _HomePageState extends State<HomePage> {
},
),
));
}();
final FocusNode _editorFocusNode = FocusNode();
final ScrollController _editorScrollController = ScrollController();

@override
void initState() {
super.initState();
// Load document
_controller.document = Document.fromJson(kQuillDefaultSample);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/controller/clipboard/quill_clipboard_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,4 +77,15 @@ class QuillClipboardConfig {
/// Return modified text to override the pasted content, or `null` to use the default.
@experimental
final Future<String?> 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;
}
22 changes: 13 additions & 9 deletions lib/src/controller/quill_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 860c4ab

Please sign in to comment.