Injecting dynamic default attrs to nodes

Hey there,

I’m trying do figure out what the best way might be to reference a dynamic global state when rendering nodes.

The obvious solution would be to create a truly global state (e.g. window.state) and then refer to that in the toDOM callback of my node specs. But that doesn’t seem like a very clean way to do this. On the other hand, building a view plugin that iterates through the dom and applies the default attributes on each update might affect performance too much.

Another idea I had was storing the state in the editor state, but there seems to be no way to access that state from toDom either.

To make it a bit clearer what I’m trying to accomplish:

  1. I have a schema with a bunch of node types (e. g. my-node-type)
  2. I have a state that contains default attrs per node type (e.g. {"my-node-type": {"background": "blue"}).
  3. I want to be able to use this state in the toDom callback
  4. I want to be able to modify the state and then have that reflected in the affected nodes

Thank you in advance for any pointers!

Depending on how often this state changes, you could reconfigure the editor to provide new node view constructor functions that close over the new value whenever you need to update it. Or, alternatively, use node decorations to communicate the state to node views, and have a plugin update those decorations whenever this state changes.