From dbd72b3523adbc5e5ed5fec09a3897a3318bb4d7 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Sun, 30 Oct 2022 17:50:15 +0100 Subject: [PATCH] feat: handle paste event and insert images on the clipboard Signed-off-by: NanoSector --- .../Scene/Compose/ComposeViewController.swift | 29 +++++++++++++++++++ .../View/MetaTextView+PasteExtensions.swift | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 Mastodon/Scene/Compose/View/MetaTextView+PasteExtensions.swift diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index f5dfc8ba3..6ca09eba0 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -1449,3 +1449,32 @@ extension ComposeViewController { } } + +extension ComposeViewController { + public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { + + // Enable pasting images + if (action == #selector(UIResponderStandardEditActions.paste(_:))) { + return UIPasteboard.general.hasStrings || UIPasteboard.general.hasImages; + } + + return super.canPerformAction(action, withSender: sender); + } + + override func paste(_ sender: Any?) { + logger.debug("Paste event received") + + // Look for images on the clipboard + if (UIPasteboard.general.hasImages) { + if let images = UIPasteboard.general.images { + viewModel.attachmentServices = viewModel.attachmentServices + images.map({ image in + MastodonAttachmentService( + context: context, + image: image, + initialAuthenticationBox: viewModel.authenticationBox + ) + }) + } + } + } +} diff --git a/Mastodon/Scene/Compose/View/MetaTextView+PasteExtensions.swift b/Mastodon/Scene/Compose/View/MetaTextView+PasteExtensions.swift new file mode 100644 index 000000000..8fe1949af --- /dev/null +++ b/Mastodon/Scene/Compose/View/MetaTextView+PasteExtensions.swift @@ -0,0 +1,29 @@ +// +// MetaTextView+PasteExtensions.swift +// Mastodon +// +// Created by Rick Kerkhof on 30/10/2022. +// + +import Foundation +import MetaTextKit +import UIKit + +extension MetaTextView { + public override func paste(_ sender: Any?) { + super.paste(sender) + + var nextResponder = self.next; + + // Force the event to bubble through ALL responders + // This is a workaround as somewhere down the chain the paste event gets eaten + while (nextResponder != nil) { + if let nextResponder = nextResponder { + if (nextResponder.responds(to: #selector(UIResponderStandardEditActions.paste(_:)))) { + nextResponder.perform(#selector(UIResponderStandardEditActions.paste(_:)), with: sender) + } + } + nextResponder = nextResponder?.next; + } + } +}