I have an array of “variables” in a variablesStateField. Each variable item contains extra info so I can colour the decorations based on the category. If a variable isn’t in the list, I style the widget to show it’s missing.
When the list of valid variables changes (outside of the editor instance), I dispatch an effect to update the variables:
view.dispatch({
effects: variablesStateEffect.of(variables), // new variables array
});
But, I can’t figure out how to get the match decorator to re-run the decoration process so it can update the styling of the widgets.
const placeholderMatcher = new MatchDecorator({
regexp: variableInputRegex,
decoration: (match, view) => { // How to get this to run again when the state effect runs?
const variables = view.state.field(variablesStateField);
const variableName = match[1];
const variable = variables.find((variable) => variable.name === variableName);
if (variableName == null) {
return null;
}
return Decoration.replace({
widget: new PlaceholderWidget(variableName, variable),
});
},
});
Sounds like you need to fully recreate the decoration set with createDeco when the set of variable changes. I.e. set up the view plugin that maintains the decorations to check the value of that state field, and only use updateDeco when it didn’t change in an update.
When I dispatch the effect I see a call flow which looks correct in terms of the right data flow:
variablesStateField.update // new variables
placeholders.update // using new variables list
placeholderMatcher.decoration // creates new PlaceholderWidget
placeholders.decorations
But the editor view doesn’t update with the new widget. Do I need to trigger an update in the view so it re-renders with the new widgets?
If I can get that working, then I can change placeholders.update to check if the variables have changed, rather than calling createDeco on every update.
No. If you dispatch an update, and your view plugin updates its decorations in its update method, the view will use those. Are you sure the variables in the state field are changing as intended?