I’m trying to add background color on anything that is within {{ }} or [[ ]] in codemirror
I got the background on the text but I’m unable to insert spaces after the last }. If it is anything other than space then it shows up and the space is behind that letter.
removing plugin, spaces work fine - Screen-Recording-2022-05-21-at-7.36.39-PM GIF | Gfycat
adding plugin spaces don’t work then - Screen-Recording-2022-05-21-at-7.37.24-PM GIF | Gfycat
here is the codemirror plugin/extension that handles the decoration
const expression = Decoration.mark({
attributes: { class: 'cm-mdb-expression' },
});
function expressionDeco(view: EditorView) {
const builder = new RangeSetBuilder<Decoration>();
for (const { from, to } of view.visibleRanges) {
for (let pos = from; pos <= to; ) {
const line = view.state.doc.lineAt(pos);
const from = line.text.indexOf('{{');
const to = line.text.indexOf('}}');
if (from > -1 && to > -1 && to + 2 <= line.length) {
builder.add(from, to + 2, expression); // if changed to to + 1, then spaces work but then styles are not applied to the last }
}
pos = line.to + 1;
}
}
return builder.finish();
}
const showExpressionPlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = expressionDeco(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) this.decorations = expressionDeco(update.view);
}
},
{
decorations: (v) => v.decorations,
}
);
export function expressionBackground(): Extension {
return [showExpressionPlugin];
}