How to check decoration attributes in a nodeView update?

I have a plugin that adds a node decoration when the node is selected.

I am now trying to modify the markup of the node based on whether this decoration is present, so I’m using a node view for this.

I’m aware that the update method of a NodeView receives an array of decorations.

I’d like to check whether it contains the specific decoration I’m looking for. Is it possible to access the decoration attributes?

The Decoration type only has from, to, and spec properties - is this correct?

This is what I have so far:

update(node: Node, decorations: readonly Decoration[]) {
    // const hasSelectedDecoration = ???

    if (hasSelectedDecoration) {
        this.dom.appendChild(this.selectedIcon);
    } else {
        this.selectedIcon.remove();
    }

    return true;
}

You’d add some property to the spec of the decoration you’re using, and do decorations.some(d => d.spec.myProperty) or something similar to see whether it is present.

1 Like