Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-equivalent to handling nested XmlText #439

Open
zswaff opened this issue May 14, 2024 · 2 comments
Open

JS-equivalent to handling nested XmlText #439

zswaff opened this issue May 14, 2024 · 2 comments

Comments

@zswaff
Copy link

zswaff commented May 14, 2024

Problem

Hey all! I'm wondering if we're missing something about a good way to read nested XmlText. In essence we have a YDoc that looks something like

<XmlFragment a="a">
  <XmlText b="b">
    Shallow
    <XmlText c="c">
      Deep
    </XmlText>
  </XmlText>
</XmlFragment>

Although in the general case the data can be nested arbitrarily deeply with multiple children at each level. A third party lib we are using is making these docs so we can't change or simplify the format.

It is possible to create a doc like this using the JS library. Our only goal is to be able to read/write from this sort of format using yrs. Right now it is not possible to read/write this format as far as we can tell because XmlText does not have children.

Proposed solution

We are working on a PR that implements first_child on XmlText (literally on XmlTextRef) so that we can iterate through these children. For this implementation it seems like we need to iterate over every XmlText branch node that may contain Map and one or more SplittableString entities following it for every child node.

Two questions here:

  1. Is there a preexisting solution that we're missing?
  2. Does this seem like the right solution? Will it be mergeable when we finish?

Additional Context

At Dart we use lexical for text editing; lexical comes with existing YJS bindings. Our backend is in Python and we need to manipulate the YDocs on the backend so our goal is to be able to parse and create docs following the lexical format. The lexical code is not so well-documented yet but the operating code on their end is here.

For more info check out this post in the ypy lib that has more context including a full example with the YDoc update bytes, expected data, and code example for JS that demonstrates how to parse docs in this format using the JS lib.

@Horusiath
Copy link
Collaborator

Horusiath commented May 15, 2024

Maybe the question to begin with should be why are you using nested text elements?

If you want to apply formatting, there're a format/insert_with_attributes methods. If you need to insert non-text contents like images or other Y-types, you can use insert_embed - see the docs for the details. If you need to read the formatted content you can use diff method which returns chunks of formatted text/embeds.

@zswaff
Copy link
Author

zswaff commented May 15, 2024

Hey, thanks so much for the response.

The short answer is that this isn't our data pattern--this is the format that lexical uses to encode its documents (code here). I would much rather use a simpler/more standard pattern if possible but for now I was trying to work with what lexical produces.

If you think that this is not a valid use case for YJS it would be helpful to understand that better and then I suppose I could go to the lexical team to see if we can change things there, although that seems tough.

Between the fact that the JS library permits this pattern and that the lexical team is using it, I figured it was valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants