visual-space/visual-editor

DocumentController - Using the update() method will inevitably add a new breakline at the end of the document

Closed this issue · 1 comments

(!) Calling update() will trigger two operations: clear() and compose(). clear() will use replace() to cleanup the entire document until we are left with [{"insert":"\n"}]. compose() will then use the new delta to append it to the document. documentController.compose() will trigger an insert on the rootNode (nodes list). Reminder: clear() has updated both the delta and rootNode to contain an empty line with a simple break line inside. This means we are adding empty rootNode "\n" + new data: "abc\n" and we will get "abc\n\n".

  • Attempt1 - Deleting in the controller delta the new line \n character such that we can do "" + "abc\n". This approach has some serious after effects because the delta and the rootNode go out of sync.
  • Attempt2 - Deleting the newline in the rootNode after the insert. However first time it was done the wrong way. I was removing the first child in the list thus leaving the document empty regardless of the delta provided by update(). This seems to work fine when you have just an empty field being updated with empty doc. However it no longer works when you attempt to update with a regular document that has chars. Another issue was that I did not update the internal delta of the controller to match the new state of the rootNode. Once again things were going crazy with further interactions due to the mismatch between internal delta and rootNode.
  • Final Attempt - I realised that I need to delete the last line of the rootNode. Also, we need to make sure this is done ONLY when compose() is called from clear(). That's why I created the overrideRootNode param. This entire setup might look like a hack, but there's simply no way to get rid of the double \n\n when updating the doc. The entire nodes manipulation code is built under the assumption that a document line will always end with \n. Therefore there's no simple way of getting rid of the initial \n of an empty doc. Thus we are left only with the option presented here: to remove the double \n if we now it was generated by update().
Screen.Recording.2023-08-15.at.18.07.44.mov

In case anyone is reading this ticket searching for updates. Yes, we are still alive. VE is still a project that we take care of. We've been slow to release any major updates because we are 110% focused on finishing our own startup projects and getting it live. Once it's live VE becomes major priority. Around end of September you will start seeing tickets moving.