-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Proposal: DecoratorElementNode #5930
Comments
I don't have any specific feedback on the implementation proposal here but this would be a very welcome feature addition if it could be made to work well! I wonder if this style of decoration would be better off as a protocol that any node could participate in (possibly excluding TextNode), rather than adding adding a separate superclass for this behavior |
This comment was marked as outdated.
This comment was marked as outdated.
The "DecoratorElementNode" proposed in the issue sounded like some other class hierarchy where it extends ElementNode but also has Decorator functionality. I think a challenge with the above comment's approach is the backwards incompatibility where code that has to traverse the document now has to look for children through decorators, children expect to have their parent be an ElementNode (and the related functionality to be able to remove themselves). Maybe for practical reasons these decorator's root nodes would have to be some other specific ElementNode class that behave like a shadow root? |
This comment was marked as outdated.
This comment was marked as outdated.
I think that existing production code should be fine in any scenario where nodes using this new protocol or node type are just not even registered with the editor. My primary concern is that there is a fair amount of utility code that expects to be able to work with a Lexical document and currently covers all cases, but when a new way of traversing the tree is added they will need to be changed accordingly ( |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Proposal updated. |
LGTM! I bet this can be implemented. The only question is who has capacity + knowledge for this :) |
To be honest, this problem will definitely become the main reason for me to abandon the use of Lexical one day. |
@yf-yang I have no idea to handle selection between nested editors correctly, would you like to give me some advices? As what you can see from the screenshot I make from the playground. Clicking a ImageNode in nested editor is not able to deselect the ImageNode in its' parent editor and vice versa. Both |
TBH I'm losing my patience too. I've invested about 18 months here, and found nested editor/decorator element is really bad, I cannot have my own work done without these features if I want to heavily customize an editor based on Lexical. However, that is still acceptable as I trust Facebook's brand. I think I have deep understanding of the core implementation, so I tried to create PRs and invested lots of time modifying the core. The real big problem is, the founder of Lexical has left meta and went to Svelte, he contributes many lines of Lexical code, and most of the core. According to some Discord channel messages, current maintenance team has other stuff to focus on in meta, so they don't have enough time to follow those issues and PRs. Although there are many kind community people who can review some application-wise bugs, no one can even review the core change and give enough feedback, let alone merge or deny them. I'm investigating plate which is built on top of slate now. Not sure how the core works, so not sure its performance comparison with Lexical, but it seems more mature, and I can write element nodes with React. |
@LvChengbin For your selection case specifically, I've encountered similar issue before. I personally recommend you to create something like , which internally globally maintains a variable that records which editor is being selected. This plugin should be used for all the nested editors and listening to each one's selection change. Whenever one changes, check if the editor being changed is the same with the recorded one. If not, then unselect the previous stored editor. Selection is bind with editor state and editor state is bind with editor. Therefore, it is not globally unique. |
@yf-yang Thank you, and yes, I considered about this method, but I thought there may be a better way for this problem, because it'll cause some other problems, for example if I want to select multiple ImageNodes by clicking them with Shift Key pressed. 😵 |
@LvChengbin dm u in the discord server |
@yf-yang I'm sorry I see your reply so late. I just joined into the discord server. |
@yf-yang did you end up switching to plate/slate or something else? A little worried about what you brought up regarding lexical founder, etc 😬 |
@CanRau Works well with plate. Lexical is inspired by prosemirror, so prosemirror is more vanilla JS based. |
I also find this issue needing improvement. Whenever I attempt to wrap Element using existing components, I'm always torn between abandoning current progress or resorting to a more hacky approach of traversing the entire tree for repairs. I haven't used LexicalNestedComposer because it would increase my workload and lead me into brainstorming. I feel that having each Element inherit the decorate method is not in conflict with existing dependency-free solutions. I find Lexical and ProseMirror better in performance optimization compared to frameworks relying on existing views. I hope someone can empower users with the choice. |
I think the 3 points can be summed up as 2 transactions instead of one. This would mean that the user, on these arguably infrequent occasions, has to press ctrl+z 2 times instead of 1. It's not ideal, but in my opinion it's not such a big deal. A solution that I have thought of is that a timestamp could be added to each undomanager transaction, and if they occurred in a very close interval (e.g. 500ms) they are combined (this is what Yjs does) |
The current HistoryStateEntry doesn't track timestamps, and the debouncing in the current implementation wouldn't be able to merge entries from multiple editors. I think generally there are two use cases for nested editors: those that are used to work around deficiencies in Lexical's UI model (what this proposal is trying to solve), and editors that are ephemeral and/or not so related to the parent editor (maybe editing some state related to a specific node, but on save or update that state will be synced to the other document, like labeling an image or adding a comment). The former case is not good DX today and is very error-prone. I think it would be worth it to decouple the DOM and model just enough to allow something like this to work, even if there are some caveats/boilerplate/restrictions, it would be easier than dealing with a "distributed system" of editors when you should really only have one logical root. The latter type probably doesn't really need any infrastructure to support it, because it already works pretty well. The primary concern is just to make sure that events/focus/DOM lookups/etc. go to the correct editor. |
Exactly, the "current". But it is a doable solution.
I have found one more use case. A Lexical editor that can be embedded in two different parent Lexical documents. In Notion, you can embed the same table/DB on two different pages, and if you edit on one, the changes should be reflected on the other. The obvious way to do this is for the table to be a document/editor itself. Then the pages are two different editors that contain a node of type "table-reference" with an id to the subdocument, which is loaded lazy. For reference, another good example is the "Synced block" in Notion. I think there are 2 parallel discussions here:
In (1) my opinion is no. I.e., I think nested editors have use cases. Discussion (2) is not something I have wanted to get into because of my position on (1). Even if an alternative was found to not use nested editors in 90% of the cases, if you still need it in 10% of the cases, you have to find a solution to the problem that works with nested editors. |
I think that's fair, I'm not advocating to get rid of nested editors, they certainly do have their use cases especially for large, block-based, and/or collaborative documents. My point is mostly that they are a very awkward solution to the sorts of use cases where you basically want to add one or more DOM nodes to the editor's DOM but you don't want them to have associated nodes in the document tree or participate directly in things like selection (usually the use case is something like CSS :before and/or :after pseudo-elements, but with interactivity). I think a proposal like this one would be a welcome solution to that problem. Having a better undo manager (for those not already using yjs I guess, since that has a separate history implementation that works a bit better) would be a great idea and much simpler to implement and discuss than this. I think a separate issue or PR with an implementation would be a great place for that discussion. |
Sure, but despite everything I still don't understand what problem a "DecoratorElementNode" would solve. The only problem I see is that of the undomanager, which seems obvious that it should be solved in another way. Is there any other? |
I think you miss the point here. We are not talking about REPLACE nested editors, but adding another type of element node, which could be decorated via modern framework such as React.
I mention the specific use case somewhere. When you are moving blocks around (especially in a drag and drop case), you need to:
Current history plugin does not distinguish editorStates, so I actually tried to create my own version of history plugin to support similar stuff. For others, there are no out of the box utilities. Yes, we can patch all of these stuff (with so much efforts). However, I think the point is, current design is not aligned with real word abstraction, which is —— the document is treated by the user as a whole, so there should be only one editor state. If not, then nested editors is useful. If we ignore the abstraction and think OK we can patch here, here and there, then new problems would occur again and again when the abstraction does not meet the real word demands. |
Let me show a direct example: A nested list Each list item could either be one line of text, or a sub list. You should be able to drag a list to the another and it could become a sublist. It should support to as many as 5 nested lists. Nested list could also be moved out to become a non-nested list. |
Let me take the core point to a standalone comment: The document is treated by the user as a whole, so there should be only one editor state. This should be the correct abstraction of a rich text editor design. |
Would really love to be able to decorate all nodes to re-use components between front-end and backend as we're manually rendering editorstate json on front-end 🤓 |
I think now that we have the (experimental) getDOMSlot API introduced by scrollable tables (#6759) it should be possible to do something like this in a limited way with an ElementNode, although someone needs to do the work to come up with the additional use cases and test them to make sure that they are indeed fully compatible with how the reconciler works. |
I've wanted this feature for a long long time, today I get some ideas to implement it, so I'd like to discuss it here.
People need something like "ReactElementNode"
I've seen such questions again and again. People not only need a plain ElementNode which is a single DOM node, they want to also use other framework's generated DOM node as the parent of a set of Lexical nodes. However, this is not natively supported.
Let's first check other frameworks. Slate.js seamlessly supports it, TipTap breaks the element and the child into
NodeViewWrapper
andNodeViewComponent
, check doc and example. All of them are handle those cases in one editorState.Now let's go back to Lexical. Lexical offers nested editors and LexicalNestedComposer. However, after heavily develop use cases with it, I find it has a lot to improve.
Why nested editor is not a good idea?
Moving contents between editors is painful.
You can't directly move nodes between editors. Serialization is mandatory and I have to deal with at least two
editor.update
s.Create a plugin that supports multiple editors is painful.
Due to previous reason, when I created a drag and drop plugin that supports nested editors, I have to consider if the editor to be dropped is the same with the dragging node's editor. There are two cases, the first one is one
editor.update
, the second one needs twoupdate
call and serialization.Modifying multiple editors SIMULTANEOUSLY is poorly supported.
Suppose I move a node from one editor to the other. This is a transaction that involves two editors. However, both HistoryPlugin and CollaborationPlugin treats them as two separate transactions. I've spent lots of time modifying them.
I've been going deeper with those questions. I think, as an editor framework whose idea originates from React, the editorState should also be singleton, just like how React does. To be more precise, for a single LexicalComposer, all the nodes within it, including those nested editors, should share the same editorState.
Implementation Proposal
What nested editor is doing
I'd like to share some understanding of Lexical. First, Lexical (core) is a framework that only deals with DOM nodes. Each Lexical node is expected to represent exactly one DOM node, so when the editorState (Lexical node tree) updates, the Lexical node tree is mapped to the DOM tree. One Lexical node <=> one DOM node, element node maps to a DOM node contains a sequence of child DOM nodes. TextNode maps a DOM node with no child. Decorator node maps to a DOM node with no child as the portal, the portal can mount, for example, child React elements.
What nested editor does is simple: it adds another root DOM, then mounts its editor's DOM tree to that root. That's all.
It means, if we supports multiple root DOMs in Lexical core, then nested editors will no longer be needed, and we can merge nested node tree into one node tree. Now only one editorState, and we are able to implement
DecoratorElementNode
.How the API will be like
Nested editor needs a nested LexicalContentEditable, which is for setting the root element during the first render. We can take the procedure in the core.
Example: CardNode with two editable areas title and body.
The TypeScript signature of DecoratorElementNode is like:
How the editor update executes
I've not thought about it thoroughly, but the idea is the same with nested editors. During the first render, the DecoratorElementNode's rootElement is null, nothing happens. Later in another micro task, the decorators are rendered by frameworks like React and root element is now available, then queue another micro task to mount the node's children in Lexical. Later on, if the rootElement is available, directly update the children.
Summary
Only one editorState for all nested editors improves DX a lot. We can implement by moving how nested editors are currently rendered to Lexical core.
The text was updated successfully, but these errors were encountered: