danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
814 stars 97 forks source link

Can't disable image pasting #158

Open dave0111 opened 3 months ago

dave0111 commented 3 months ago

I'm trying to disable the possibility to past an image in RichTextEditor but can't get it to work.

So far, I've been trying to use imageConfiguration, that, from what I could understand, should work like this

RichTextEditor(text: self.$text,
                          context: self.context) {

     $0.imageConfiguration = .init(pasteConfiguration: .disabled,
                                                        dropConfiguration: .disabled,
                                                        maxImageSize: (width: .points(0), height: .points(0)))

     print("Paste config: \($0.imagePasteConfiguration)") // This prints "disabled" as expected
}

However, it still allows me to paste images.

This doesn't work either:

RichTextEditor(text: self.$text,
                          context: self.context) {

     $0.imageConfiguration = .disabled

     print("Paste config: \($0.imagePasteConfiguration)") // This prints "disabled" as expected
}

Am I doing something wrong? Because I couldn't find any other way of preventing the user from inserting an image

carminchameleon commented 2 months ago

I'm curious about this too!

I set the format to .plainText to prevent image pasting, and it worked. However, we also support iPads. so users can drag and drop images into the text editor on these devices.

 RichTextEditor(text: $vm.attributedText, context: context, format: .plainText) {
                    $0.setRichTextColor(.foreground, to: UIColor.textOnSurfaceBackground)
                    $0.imageConfiguration = RichTextImageConfiguration(pasteConfiguration: .disabled, dropConfiguration: .disabled,maxImageSize: (width: RichTextImageAttachmentSize.points(0), height: RichTextImageAttachmentSize.points(0)))                    
                }

If there is another way to prevent drag drop, please let me know!

danielsaidi commented 2 months ago

Hi @dave0111 and @carminchameleon

Setting the paste configuration should work, so this may have gotten lost when rewriting the action handling earlier in 2024.

I'm busy work other projects atm, but will happily merge any fix for this.