NodeView update() On Decoration change

I’m trying to trigger update() on a NodeView using a Decoration but I can’t seem to get it to fire. The use-case here is just adding a number to each heading - kind of like the footnote example but we can’t use css to do it for our purposes.

This code generates the decorations. It’s enclosed in a function that is passed into our EditorView constructor and as a result, is being called every time the document changes.

state.doc.descendants((testNode, pos, parent) => {
    if (testNode.type === schema.nodes.sectionNumber) {
      const spec = {
        sectionNumber: counter
      };
      decos.push(
        Decoration.node(
          pos,
          pos + testNode.nodeSize,
          { style: "color: #ff0000" },
          spec
        )
      );
      counter += 1;
    }
    return true;
  });

I threw the { style: color: #ff0000 } in there just to make sure it was grabbing the correct node. Sure enough, the content (which is added via the dom property in our NodeView constructor) is colored red and the resulting HTML looks correct.

If I select the node and press spacebar, that space gets inserted as a text node as a child of the sectionNumber node and update() IS called. When that happens, the Decoration is passed into update() and has the spec populated as-expected. However, other changes to the document do not trigger an update().

Anyone have a guess as to what’s going wrong? My understanding was that update() should be called because a new Decoration is being constructed and added anytime the document changes.

So the sectionNumber node has a node view, and adding the decoration doesn’t cause its update method to run? Is it being redrawn entirely, maybe? In any case, that doesn’t sound right. Can you try to reduce the problem to the minimal code that reproduces it? I just ran a test and I’m seeing update methods being called when a node decoration is added at least in the simple case I tried.

Just checked and the custom NodeView's constructor is not being called every change. I assume that’s what would happen if it was being redrawn? I’ll create a simplified test and post back here. Glad to hear that it doesn’t look like the approach is totally wrong at least! Thanks!

We ended up taking a pretty different approach to rendering our numbers, but I think in the process I may have stumbled across the issue. Is it possible that update() isn’t called if the newly-created decoration is deep-equal to the old one? When I started randomly tinkering with the created decoration, update started being called. Seems there’s not much point in calling update() if neither the deco nor the parts of the doc it’s decorating have actually changed so I wouldn’t call it a bug.

Oh, yeah, if the decorations (and the node) are equal to the old ones, not calling update is the intended behavior.