Generate an event from Doc state

I’m trying to make Lexical work when the document is already loaded (issue).

I know exactly what to do but not how to do it. The problem is here, the state gets read from the document as it comes in, but not from anything that was already loaded.

Is there a way to convert the current XmlText to an observe callback format?

In y-websocket there is a 'synced' event in WebsocketProvider that tells others it’s ready. You could try triggering something similar manually to see if it helps. It’s definitely a bug in lexical so probably someone there will handle it.

No, it’s definitely a case of not handling the already-present document state, emitting the sync event won’t help.

It would be great if there was a way to convert the current state to one or more events, then the fix would be just that.

I can only find encodeStateAsUpdate but it does not seem to be an event as gotten by the observeDeep callback.

I agree that it should ideally be fixed within lexical. As a workaround, you might consider attaching an empty Doc doc to the editor first, then do Y.applyUpdate(doc, Y.encodeStateAsUpdate(docExisting)). That should trigger the observe event on doc.

1 Like

Oh, good idea for the workaround, thanks!

But ideally I’d like to fix it in Lexical, I can make a PR but I’m just lost going from an update to the format that XmlText observe emits. The code that receives those updates is quite complex and I think it will be too error-prone to have separate code that loads state.

I’m sure there’s a better way, but you can use the same trick to capture the observe events and forward them to syncYjsChangesToLexical :

const tempDoc = new Y.Doc()
tempDoc.getMap().observeDeep(events => {
  syncYjsChangesToLexical(binding, provider, events, false)

Y.applyUpdate(tempDoc, Y.encodeStateAsUpdate(lexicalDoc))
1 Like

LOL that’s great, thanks! :slight_smile:

1 Like