Paste Gif image similar to jpeg and png files
This commit change will allow users to paste GIF file in the Post by copying from other apps (previously similar to pasting Jpeg and PNG image functionality) Changelog-Added: Paste Gif image similar to jpeg and png files Signed-off-by: Swift Coder <scoder1747@gmail.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
87efc91527
commit
866afe970b
@@ -31,4 +31,7 @@ class Constants {
|
||||
static let DAMUS_WEBSITE_LOCAL_TEST_URL: URL = URL(string: "http://localhost:3000")!
|
||||
static let DAMUS_WEBSITE_STAGING_URL: URL = URL(string: "https://staging.damus.io")!
|
||||
static let DAMUS_WEBSITE_PRODUCTION_URL: URL = URL(string: "https://damus.io")!
|
||||
|
||||
// MARK: General constants
|
||||
static let GIF_IMAGE_TYPE: String = "com.compuserve.gif"
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ struct PostView: View {
|
||||
@State var error: String? = nil
|
||||
@State var uploadedMedias: [UploadedMedia] = []
|
||||
@State var image_upload_confirm: Bool = false
|
||||
@State var imagePastedFromPasteboard: UIImage? = nil
|
||||
@State var imagePastedFromPasteboard: PreUploadedMedia? = nil
|
||||
@State var imageUploadConfirmPasteboard: Bool = false
|
||||
@State var references: [RefId] = []
|
||||
@State var imageUploadConfirmDamusShare: Bool = false
|
||||
@@ -503,7 +503,7 @@ struct PostView: View {
|
||||
.alert(NSLocalizedString("Are you sure you want to upload this media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $imageUploadConfirmPasteboard) {
|
||||
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
|
||||
if let image = imagePastedFromPasteboard,
|
||||
let mediaToUpload = generateMediaUpload(PreUploadedMedia.uiimage(image)) {
|
||||
let mediaToUpload = generateMediaUpload(image) {
|
||||
Task {
|
||||
await self.handle_upload(media: mediaToUpload)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ struct TextViewWrapper: UIViewRepresentable {
|
||||
@EnvironmentObject var tagModel: TagModel
|
||||
@Binding var textHeight: CGFloat?
|
||||
let initialTextSuffix: String?
|
||||
@Binding var imagePastedFromPasteboard: UIImage?
|
||||
@Binding var imagePastedFromPasteboard: PreUploadedMedia?
|
||||
@Binding var imageUploadConfirmPasteboard: Bool
|
||||
|
||||
let cursorIndex: Int?
|
||||
@@ -244,11 +244,11 @@ struct TextViewWrapper: UIViewRepresentable {
|
||||
}
|
||||
|
||||
class CustomPostTextView: UITextView {
|
||||
@Binding var imagePastedFromPasteboard: UIImage?
|
||||
@Binding var imagePastedFromPasteboard: PreUploadedMedia?
|
||||
@Binding var imageUploadConfirm: Bool
|
||||
|
||||
// Custom initializer
|
||||
init(imagePastedFromPasteboard: Binding<UIImage?>, imageUploadConfirm: Binding<Bool>) {
|
||||
init(imagePastedFromPasteboard: Binding<PreUploadedMedia?>, imageUploadConfirm: Binding<Bool>) {
|
||||
self._imagePastedFromPasteboard = imagePastedFromPasteboard
|
||||
self._imageUploadConfirm = imageUploadConfirm
|
||||
super.init(frame: .zero, textContainer: nil)
|
||||
@@ -267,12 +267,31 @@ class CustomPostTextView: UITextView {
|
||||
|
||||
// Override paste to handle image pasting
|
||||
override func paste(_ sender: Any?) {
|
||||
if let image = UIPasteboard.general.image {
|
||||
imagePastedFromPasteboard = image
|
||||
let pasteboard = UIPasteboard.general
|
||||
|
||||
if let data = pasteboard.data(forPasteboardType: Constants.GIF_IMAGE_TYPE),
|
||||
let url = saveGIFToTemporaryDirectory(data) {
|
||||
imagePastedFromPasteboard = PreUploadedMedia.unprocessed_image(url)
|
||||
imageUploadConfirm = true
|
||||
} else if let image = pasteboard.image {
|
||||
// handle .png, .jpeg files here
|
||||
imagePastedFromPasteboard = PreUploadedMedia.uiimage(image)
|
||||
// Show alert view in PostView for Confirming upload
|
||||
imageUploadConfirm = true
|
||||
} else {
|
||||
super.paste(sender) // Fall back to default paste behavior if no image
|
||||
// fall back to default paste behavior if no image or gif file found
|
||||
super.paste(sender)
|
||||
}
|
||||
}
|
||||
|
||||
private func saveGIFToTemporaryDirectory(_ data: Data) -> URL? {
|
||||
let tempDirectory = FileManager.default.temporaryDirectory
|
||||
let gifURL = tempDirectory.appendingPathComponent("pasted_image.gif")
|
||||
do {
|
||||
try data.write(to: gifURL)
|
||||
return gifURL
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user