How to get widgets from DecorationSet?

I have a StateField that contains DecorationSet. Inside the update method of the spec object that I passed to StateField.define method, in some cases, I need to enumerate all existing decorations and get the widgets from them. Is there a way to do it?

I use replace decorations, they replace specific strings of text, and I want to make them appear selectable, just like text, so I want to regenerate the DOM and change the styles there so that the widgets appear selected.

thanks in advance

Decoration sets are immutable and widget DOM should be a pure function of the widget decorations, so you can’t, but you can regenerate the decorations when something about them should change, and you can even use the widget’s updateDOM method to optimize the DOM updates.

Thank you @marijn . My decorations use the effect value in the toDOM method. I want to update the decorations when the selection changes too. But in this case, the transaction does not have any effects. There is no value that I can use…

    update(templates, {changes, effects, selection, startState}) {
        let newTemplates =;

        for (const effect of effects) {
            if ( {
                const {
                    value: {template, fields, from, to}
                } = effect;

                if (from < to) {
                    const decoration = Decoration.replace({widget: new TemplateWidget(template, fields)});
                    newTemplates = newTemplates.update({add: [decoration.range(from, to)]});
        // HERE I want to update `newTemplates` that are inside the `selection`,
        // I can check the `selection` when I create the ones that are in the `effects` but
        // this method is invoked also when there are no effects in the transition...

        console.log('selection', JSON.stringify(selection));
        console.log('startState', JSON.stringify(startState));

        return newTemplates;

I’ve used DecorationSet.between to iterate the decorations in the set: CodeMirror 6 Reference Manual