metatext-app-ios-iphone-ipad/Views/UIKit/ImagePastableTextView.swift

29 lines
988 B
Swift
Raw Normal View History

2021-02-07 22:00:17 +01:00
// Copyright © 2021 Metabolist. All rights reserved.
import Combine
import UIKit
final class ImagePastableTextView: UITextView {
var canPasteImage = true
2021-02-09 05:33:49 +01:00
private(set) lazy var pastedItemProviders: AnyPublisher<NSItemProvider, Never> =
pastedItemProvidersSubject.eraseToAnyPublisher()
2021-02-07 22:00:17 +01:00
2021-02-09 05:33:49 +01:00
private let pastedItemProvidersSubject = PassthroughSubject<NSItemProvider, Never>()
2021-02-07 22:00:17 +01:00
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) {
return UIPasteboard.general.hasStrings || (UIPasteboard.general.hasImages && canPasteImage)
}
return super.canPerformAction(action, withSender: sender)
}
override func paste(_ sender: Any?) {
2021-02-09 05:33:49 +01:00
if UIPasteboard.general.hasImages, let itemProvider = UIPasteboard.general.itemProviders.first {
pastedItemProvidersSubject.send(itemProvider)
2021-02-07 22:00:17 +01:00
} else {
super.paste(sender)
}
}
}