feat: handle paste event and insert images on the clipboard

Signed-off-by: NanoSector <rick@nanosector.nl>
This commit is contained in:
NanoSector 2022-10-30 17:50:15 +01:00
parent 28267fe6d8
commit dbd72b3523
No known key found for this signature in database
GPG Key ID: 7A23394270F33E20
2 changed files with 58 additions and 0 deletions

View File

@ -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
)
})
}
}
}
}

View File

@ -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;
}
}
}