rajdeep / proton

Purely native and extensible rich text editor for iOS and macOS Catalyst apps
Other
1.26k stars 81 forks source link

API for easily inserting a character at the given selection point, maintaining current style? #222

Closed MilesV64 closed 1 year ago

MilesV64 commented 1 year ago

Hello, thank you for this library! I'm wrapping my head around it for usage in a production app and so far it's looking great. One use case I have is when the user clicks a button, a small inline view gets inserted at the selection point. That's working great so far, but I want to actually surround that attachment with an empty space (" ") on both sides, and then move the cursor to after the trailing space.

Does this library have an easy API for inserting characters, rather than just attachments? If not, what's the best way to go about inserting a character while matching style (font size etc)?

I don't want to add space in the attachment view itself because if it's the first thing on a line, I want it to perfectly match the left margin.

I don't want this issue to get too unfocused but it would be great to have public access to the RichTextView even if its only casted as a generic UIScrollView -- this is important to me because I have some views which need to read from a scroll view's pan gesture recognizer for certain pull-to-dismiss gestures. Also so I can set delaysContentTouches to false as my attachment is a button and it feels nicer to have instant feedback :)

Thanks again!

Edit: Also, is there a way to get a delegate callback when anything in the text content changes, including inserting/deleting attachments? I only see editor(_ editor: EditorView, didChangeTextAt range: NSRange) for that use case which is only text changes.

rajdeep commented 1 year ago

@MilesV64, glad you're finding Proton helpful. For your case of inserting text at a given range, you should be able to use replaceCharacters. You can prepare your content i.e. attachment with required spaces on either side and use replaceCharacters to append that to the Editor at given range/selectedRange. Attachment also has a string property that you can use to get an NSAttributedString representation that you can use to append spaces easily. In regards to the attributes, you can look at editor.typingAttributes and add that to your prepared string. In summary:

In regards to RichTextView, it is not exposed as there are some constructs that are required for Proton to work which, if RichTextView is public, may get accidentally overriden by consumers causing unexpected results. Exposing some bits may still be possible but I think we need to discuss that as a separate issue.

For intercepting content changes, you may want to look at TextProcessors that does provide content related callbacks including that for insert/delete text, which may also be an attachment

MilesV64 commented 1 year ago

Thank you, I appreciate the response! This all sounds good.