From dbd72b3523adbc5e5ed5fec09a3897a3318bb4d7 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Sun, 30 Oct 2022 17:50:15 +0100 Subject: [PATCH 1/3] 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; + } + } +} From 2c2ca419dd1d6bde108bd212a302a49f06ecd017 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Sun, 30 Oct 2022 18:00:45 +0100 Subject: [PATCH 2/3] chore: add project entries Signed-off-by: NanoSector --- Mastodon.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 4106af7de..9f0a8f44c 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -115,6 +115,7 @@ 62FD27D32893707B00B205C5 /* BookmarkViewController+DataSourceProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FD27D22893707B00B205C5 /* BookmarkViewController+DataSourceProvider.swift */; }; 62FD27D52893708A00B205C5 /* BookmarkViewModel+Diffable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FD27D42893708A00B205C5 /* BookmarkViewModel+Diffable.swift */; }; 87FFDA5D898A5C42ADCB35E7 /* Pods_Mastodon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4ABE34829701A4496C5BB64 /* Pods_Mastodon.framework */; }; + CD91FB31290EDA6F00BB9463 /* MetaTextView+PasteExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD91FB30290EDA6F00BB9463 /* MetaTextView+PasteExtensions.swift */; }; DB0009A626AEE5DC009B9D2D /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = DB0009A926AEE5DC009B9D2D /* Intents.intentdefinition */; settings = {ATTRIBUTES = (codegen, ); }; }; DB0009A726AEE5DC009B9D2D /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = DB0009A926AEE5DC009B9D2D /* Intents.intentdefinition */; }; DB0140CF25C42AEE00F9F3CF /* OSLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB0140CE25C42AEE00F9F3CF /* OSLog.swift */; }; @@ -858,6 +859,7 @@ BB482D32A7B9825BF5327C4F /* Pods-Mastodon-MastodonUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon-MastodonUITests.release.xcconfig"; path = "Target Support Files/Pods-Mastodon-MastodonUITests/Pods-Mastodon-MastodonUITests.release.xcconfig"; sourceTree = ""; }; BD7598A87F4497045EDEF252 /* Pods-Mastodon.asdk - release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon.asdk - release.xcconfig"; path = "Target Support Files/Pods-Mastodon/Pods-Mastodon.asdk - release.xcconfig"; sourceTree = ""; }; C3789232A52F43529CA67E95 /* Pods-MastodonIntent.asdk - debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MastodonIntent.asdk - debug.xcconfig"; path = "Target Support Files/Pods-MastodonIntent/Pods-MastodonIntent.asdk - debug.xcconfig"; sourceTree = ""; }; + CD91FB30290EDA6F00BB9463 /* MetaTextView+PasteExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MetaTextView+PasteExtensions.swift"; sourceTree = ""; }; CD92E0F10BDE4FE7C4B999F2 /* Pods_MastodonTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MastodonTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D7D7CF93E262178800077512 /* Pods-Mastodon-AppShared.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon-AppShared.debug.xcconfig"; path = "Target Support Files/Pods-Mastodon-AppShared/Pods-Mastodon-AppShared.debug.xcconfig"; sourceTree = ""; }; DB0009A826AEE5DC009B9D2D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Base.lproj/Intents.intentdefinition; sourceTree = ""; }; @@ -2493,6 +2495,7 @@ DB44767A260B3B8C00B66B82 /* CustomEmojiPickerInputView.swift */, DB221B15260C395900AEFE46 /* CustomEmojiPickerInputViewModel.swift */, DBC7A671260C897100E57475 /* StatusContentWarningEditorView.swift */, + CD91FB30290EDA6F00BB9463 /* MetaTextView+PasteExtensions.swift */, ); path = View; sourceTree = ""; @@ -4203,6 +4206,7 @@ DB49A61F25FF32AA00B98345 /* EmojiService+CustomEmojiViewModel.swift in Sources */, 6213AF5A28939C8400BCADB6 /* BookmarkViewModel.swift in Sources */, 5B24BBDB262DB14800A9381B /* ReportStatusViewModel+Diffable.swift in Sources */, + CD91FB31290EDA6F00BB9463 /* MetaTextView+PasteExtensions.swift in Sources */, DB4F0968269ED8AD00D62E92 /* SearchHistoryTableHeaderView.swift in Sources */, 0FB3D2FE25E4CB6400AAD544 /* OnboardingHeadlineTableViewCell.swift in Sources */, 5DA732CC2629CEF500A92342 /* UIView+Remove.swift in Sources */, From 9d7614a4037f98f26cb1bc0c5bf461c501b9cb2f Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 1 Nov 2022 19:55:51 +0100 Subject: [PATCH 3/3] feat: partially restore image paste handler functionality after SwiftUI rewrite Signed-off-by: NanoSector --- .../Scene/Compose/ComposeViewController.swift | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index bf9145d6c..42945514d 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -1212,3 +1212,33 @@ extension ComposeViewController: UIAdaptivePresentationControllerDelegate { // } // //} + +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 { + logger.warning("Got image paste event, however attachments are not yet re-implemented."); +// viewModel.attachmentServices = viewModel.attachmentServices + images.map({ image in +// MastodonAttachmentService( +// context: context, +// image: image, +// initialAuthenticationBox: viewModel.authenticationBox +// ) +// }) + } + } + } +}