ChatInputTextView.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import Foundation
  2. import UIKit
  3. import MobileCoreServices
  4. public class ChatInputTextView: InputTextView {
  5. public weak var textViewPasteDelegate: ChatInputTextViewPasteDelegate?
  6. // MARK: - Image Paste Support
  7. open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  8. if action == NSSelectorFromString("paste:") && UIPasteboard.general.image != nil {
  9. return true
  10. }
  11. return super.canPerformAction(action, withSender: sender)
  12. }
  13. open override func paste(_ sender: Any?) {
  14. guard let image = UIPasteboard.general.image else {
  15. return super.paste(sender)
  16. }
  17. textViewPasteDelegate?.onImagePasted(image: image)
  18. }
  19. }
  20. extension ChatInputTextView: UIDropInteractionDelegate {
  21. public func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
  22. return session.items.count == 1 && session.hasItemsConforming(toTypeIdentifiers: [kUTTypeImage as String, kUTTypeText as String, kUTTypeMovie as String, kUTTypeVideo as String])
  23. }
  24. public func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
  25. return UIDropProposal(operation: .copy)
  26. }
  27. public func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
  28. if session.hasItemsConforming(toTypeIdentifiers: [kUTTypeImage as String]) {
  29. session.loadObjects(ofClass: UIImage.self) { [weak self] imageItems in
  30. if let images = imageItems as? [UIImage] {
  31. self?.textViewPasteDelegate?.onImageDragAndDropped(image: images[0])
  32. }
  33. }
  34. } else if session.hasItemsConforming(toTypeIdentifiers: [kUTTypeMovie as String, kUTTypeVideo as String]) {
  35. session.loadObjects(ofClass: NSData.self) { [weak self] videoItems in
  36. if let videos = videoItems as? [NSData] {
  37. let video = videos[0] as Data
  38. if let mimeType = Swime.mimeType(data: video) {
  39. DispatchQueue.global().async { [weak self] in
  40. if let fileName = FileHelper.saveData(data: video, name: "tmp_dragAndDrop", suffix: mimeType.ext, directory: .cachesDirectory) {
  41. DispatchQueue.main.async {
  42. self?.textViewPasteDelegate?.onVideoDragAndDropped(url: URL(fileURLWithPath: fileName))
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. } else if session.hasItemsConforming(toTypeIdentifiers: [kUTTypeText as String]) {
  50. session.loadObjects(ofClass: String.self) { [weak self] stringItems in
  51. if let isEmpty = self?.text.isEmpty, isEmpty {
  52. self?.text = stringItems[0]
  53. } else {
  54. var updatedText = self?.text
  55. updatedText?.append(" \(stringItems[0]) ")
  56. self?.text = updatedText
  57. }
  58. }
  59. }
  60. }
  61. }
  62. public protocol ChatInputTextViewPasteDelegate: class {
  63. func onImagePasted(image: UIImage)
  64. func onImageDragAndDropped(image: UIImage)
  65. func onVideoDragAndDropped(url: URL)
  66. }