danielsaidi / RichTextKit

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

Can't disable image pasting #158

Closed dave0111 closed 1 month ago

dave0111 commented 8 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 7 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 7 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.

danielsaidi commented 1 month ago

Ping @dave0111 and @carminchameleon

I don't know if you're still using RTK, but if you do, I just released a 1.1 that only sets the initial image configuration if it hasn't been manually set before.

I think this should fix this problem, since the configuration could otherwise be overwritten when the view is first set up.

I'll close this now, but please try it out and let me know if it works. Otherwise, I'll reopen this and keep investigating.