automerge / automerge-swift-archived

Wrapper around Swift types that can be modified concurrently by different users, and merged again automatically (a CRDT).
MIT License
159 stars 14 forks source link

How to connect automerge's Text with UITextView? #42

Closed GregOriol closed 3 years ago

GregOriol commented 3 years ago

Hi,

I'm very much interested in using automerge to sync a long text content on iOS between devices, but I'm a little bit confused between automerge's change methods (insertion of character at position, ...) and how to connect them with a UITextView: would you have any tips? Mainly, my questions are:

Best regards,

lightsprint09 commented 3 years ago

Hey,

Interesting question.

I implement text views delegate method shouldChangeTextInRange (https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618630-textview)

When receiving a change I convert the given NSRange into a Swift.Range and use automerge replaceSubrange method.

lightsprint09 commented 3 years ago

I did not build a real-time editor, so I never build applying remote changes into a text view.

Is there a specific reason why you can not just replace the text views content with the new remote content?

GregOriol commented 3 years ago

Thanks for the shouldChange tip, I didn't think of this one!

Replacing content while the user is editing will break the cursor position or scroll position if the content has changed a lot, that doesn't seem very user friendly.

But, I just found another direction to look for: UITextView has a textStorage attribute, and it seems to provide access to its text content in a more detailed way.

lightsprint09 commented 3 years ago

Did a quick look into UITextView documentation.

textStorage looks promising (https://developer.apple.com/documentation/uikit/uitextview/1618611-textstorage)

lightsprint09 commented 3 years ago

Seems like we found the same solution.

If you feels confident with the solution close this issue.

GregOriol commented 3 years ago

I'll try it and reopen if needed.

Thanks!