lexical: Bug: lexical-yjs create nodes using constructor without args

The createLexicalNodeFromCollabNode function creates a node using the constructor without arguments, and then synchronizes the properties using collabNode.

This can be fatal if the constructor requires arguments. It is dangerous to call a constructor in a way that was not designed by the user.

Lexical version: 0.12.0

Steps To Reproduce

  1. Use a node whose constructor throws an error if an argument does not exist with collab mode.

The current behavior

Error is thrown.

The expected behavior

No error is thrown.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

https://github.com/yf-yang/lexical/tree/refactor-yjs

To whom it may concern, I’ve already refactored most of the parts. Unit tests are passed. Played with the playground on my own and it works fine.

I haven’t worked on nested editors (I really hate nested editor design, maintaining multiple editor states simultaneously is really really really BAD). Needs suggestions on how to make them work when using JSON serialization methods.

Contributing to the traceability of this discussion.

In the past there have been several issues and PRs seeking to determine which properties should be excluded in yjs.

For reference, see:


Errata:

Something I’m thinking is that instead of defining importYjs or exportYjs methods, perhaps we could use importJSON and exportJSON as the intermediate bridge between the lexical node and the yjs node, so that the user does not have to code another serialization. This way the constructor would not be used, which is not reliable. In fact, in the discussion a few days ago that I proposed import/export yjs I had forgotten, but now I see that this had been my initial idea in the comment above:

It makes sense that the contract is given by serialization in JSON or HTML.

@acywatson , would love to get these PRs merged. Is there anything blocking this?

Hey - when you say “these PRs” I assume you mean the work around generic JSON serialization/deserialization.

The only thing holding this up is that it’s a big invasive change and landing it means that we (core) have to devote resources to monitoring and fixing any issues it might cause across all the Lexical implementations at Meta (not to mention all the external dependents). As you might guess, there are many such implementations and the consequences of a bug in some of them can be quite significant.

I’ve been lobbying to get this merged for a while now, and I think we will. In principle, we’ve agreed it’s an improvement. Logistically, it does present some challenges.

Hope that’s helpful.

2 solutions come to mind:

  1. A way to automate the serialization of any node, like this https://github.com/facebook/lexical/pull/3931
  2. Add importYjs/exportYjs methods.

I think solution 2 is unavoidable, since even in the PR I mentioned, import/exportJSON methods can still be overwritten due to occasions where properties are not serializable.

Also, I think importYjs/exportYjs is something that could be used for decorators. For example, if someone wanted to integrate Monaco or Codemirror and have a working undo manager. See: https://discord.com/channels/953974421008293909/1163567808823705732

Update: I correct myself again.

I think ideally a decorator would actually have an export/importYjs method. In case it is not user defined, it could fallback to export/importJSON.

The reason is that currently if one attempts real-time collaborative editing in a decorator, what happens is that the state of the decorator is completely overwritten. For example, you cannot see another user’s cursor in a table cell. The same would happen if a text editor such as Monaco or Prosemirror were implemented. It wouldn’t be collaborative editing, but rather LWW, which has noticeably inferior UX.

A way for the decorator to expose its underlying YDoc would allow it to synchronize its data structure more atomically.

Yeah. There is a similar PR with a “generic” clone that was ready to be merged but it seems the Lexical team hasn’t had the time.

As soon as that PR is merged I can update the import/export one.

@GermanJablo big fan of generic but overridable import/exports. The importYjs/exportYjs optional functions which fallback to the JSON make a lot of sense as well.

The WIP you mentioned is from Feb, is this stale?