Access editor state in node toDOM

Is it possible to access the current editor state from a node spec’s toDOM method?

Here is my use case. I’m new to ProseMirror so maybe I’m way off the rails here…

When I create a new editor I have specific state (an asset library) that I need to associate with the editor. It’s not global to the app, it’s specific to this one instance of an editor. When my custom nodes render toDOM they need to be able to access this editor-specific state.

To get it working I created a plugin that holds the editor specific state. It has a nodeView for my custom node that just adds a reference to the plugin state to each node. This “works”, but feels hacky and roundabout. Also, the nodeView doesn’t seem to run if I try to use a DOMSerializer on a state that has never been in an EditorView so this solution might not even totally work for me.

Is there a better way to accomplish what I need?

You probably shouldn’t, since the rendered node view will survive further updates to the state and thus could get out of date.

You can make the functions you put in nodeViews close over additional data, or even an accessor function that somehow fetches the current state, but you have to take care to take the above issue into account.

Yeah, I ended up enclosing my whole plugin and not using the editor state at all. It made my code a bit simpler and also solved a couple of other problems I was facing.

Thanks for taking the time to reply! :smiley: