-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Propose exporting DOM position mapping #6
Conversation
This is definitely an issue that I've hit before and welcome these new APIs.
The need for an object with both In the past I've wanted to use an API like this to do: const firstParagraph = editorView.root.querySelector("p");
const pos = editorView.posFromDom(firstParagraph); In this case I'd be satisfied for |
What would that refer to, the position before the node, after the node, or somewhere inside it? Many positions (such as those in text) can't be described like this at all. See the way the DOM selection uses
And that's exactly the kind of things that you really shouldn't be doing—for any information that you can get out of the ProseMirror document, you should not use the DOM. It's inefficient and error prone. Just use |
Referring to the position before the node would be fine, so that The scenario where this came up in the past was having a hover menu rendered over a node using a widget decoration, and wanting to find the doc node when a button is triggered. For me the first approach I tried was to feed the DOM event target (or an ancestor of) into a ProseMirror API to find the node. I then wanted to dispatch a transaction to change that node (or plugin state associated with that node). In plugins with state containing references to nodes, I store the position before the node. In the end this approach didn't pan out, and I ended up using Aside: It occurred to me that the documentation for |
I'm already using quite heavily docView's |
FEATURE: The new [`EditorView.posAtDOM` method](##view.EditorView.posAtDOM) can be used to find the document position corresponding to a given DOM position. FEATURE: The new [`EditorView.nodeDOM` method](##view.EditorView.nodeDOM) gives you the DOM node that is used to represent a specific node in the document. Issue ProseMirror/rfcs#6
I've merged this in 4094584 and implemented it in attached patch. |
This proposal adds methods to
EditorView
instances to map between DOM positions and ProseMirror document positions, and to get the DOM node that represents a given document node in the view.Rendered RFC