clipboardPaste method

  1. @experimental
Future<bool> clipboardPaste({
  1. void updateEditor()?,
})

Returns whether paste operation was handled here. updateEditor is called if paste operation was successful.

Implementation

@experimental
Future<bool> clipboardPaste({void Function()? updateEditor}) async {
  if (readOnly || !selection.isValid) return true;

  final clipboardConfig = config.clipboardConfig;

  if (await clipboardConfig?.onClipboardPaste?.call() == true) {
    updateEditor?.call();
    return true;
  }

  final pasteInternalImageSuccess = await _pasteInternalImage();
  if (pasteInternalImageSuccess) {
    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 clipboardService = ClipboardServiceProvider.instance;

  final onImagePaste = clipboardConfig?.onImagePaste;
  if (onImagePaste != null) {
    final imageBytes = await clipboardService.getImageFile();

    if (imageBytes != null) {
      final imageUrl = await onImagePaste(imageBytes);
      if (imageUrl != null) {
        replaceText(
          plainTextEditingValue.selection.end,
          0,
          BlockEmbed.image(imageUrl),
          null,
        );
        updateEditor?.call();
        return true;
      }
    }
  }

  final onGifPaste = clipboardConfig?.onGifPaste;
  if (onGifPaste != null) {
    final gifBytes = await clipboardService.getGifFile();
    if (gifBytes != null) {
      final gifUrl = await onGifPaste(gifBytes);
      if (gifUrl != null) {
        replaceText(
          plainTextEditingValue.selection.end,
          0,
          BlockEmbed.image(gifUrl),
          null,
        );
        updateEditor?.call();
        return true;
      }
    }
  }

  // Only process plain text if no image/gif was pasted.
  // Snapshot the input before using `await`.
  // See https://github.com/flutter/flutter/issues/11427
  final plainText = (await Clipboard.getData(Clipboard.kTextPlain))?.text;

  if (plainText != null) {
    final plainTextToPaste = await getTextToPaste(plainText);
    if (pastePlainTextOrDelta(plainTextToPaste,
        pastePlainText: _pastePlainText, pasteDelta: _pasteDelta)) {
      updateEditor?.call();
      return true;
    }
  }

  final onUnprocessedPaste = clipboardConfig?.onUnprocessedPaste;
  if (onUnprocessedPaste != null) {
    if (await onUnprocessedPaste()) {
      updateEditor?.call();
      return true;
    }
  }

  return false;
}