ember-mobiledoc-editor
ember-mobiledoc-editor copied to clipboard
stabilize cursor over re-renderings
It should be possible to use a heuristic to attempt to re-position the cursor at or near its position when the component's mobiledoc
changes and the editor rerenders.
See https://github.com/bustlelabs/ember-mobiledoc-editor/issues/112#issuecomment-255055175
The heuristic that we're currently using is to stash the section & cursor offset within that section and then to try and apply that back again post-render which we've found to be "good enough" in most simple situations.
If someone's added content in a previous section, then your cursor is kept in the same place in the section you're currently focused in. It doesn't handle the case of the current section having been edited before your cursor, as that ends up keeping your cursor at the same offset when you'd expect it to move if it were properly tracking the cursor (via OT/CRDTs etc…).
There's much more we could probably do in future with proper cursor tracking & collaborative editing, but something like this could cover the low hanging fruit.
I wonder if this is a job for ember-mobiledoc-editor or whether the code mobiledoc-kit should/could have stashCursor / applyCursor which the ember component hooks into.
@rlivsey Thanks for explaining your approach. I've been in favor for a little while of making the Position
and Range
objects in Mobiledoc "dumber" — right now they keep a reference to the instance of their Section
, which isn't stable over re-renders (the Post
references a different object for that section after the rerender). If a Position
were simply a tuple of [sectionIndex, characterOffset]
then it would be much easier to stash them and attempt to re-apply them later.
Do you think something like that would work for your current use case?
(And yes, in the future a CRDT structure would be even more bullet-proof because it could handle repositioning the cursor even when the content before it was edited)
A "dumber" Position
of [sectionIndex, characterOffset]
makes a lot of sense. As you say, that's what we end up stashing & reapplying so having that as-is without having to translate would be great.