Hello,
First of all, thanks a lot for the great lib’
I am encountering an issue at the moment. I want to add a specific class (adding blue border), using plugin & decorations, only on TextNode nodes which have a specific mark and which are inside the current selection. I did the following but it only works in certain cases (see screenshots):
return [
new Plugin({
key: new PluginKey("PluginKeyFocus"),
props: {
decorations: ({ doc, selection }) => {
const decorations: Decoration[] = [];
const { from, to } = selection;
const activeMarks: { mark: Mark; $pos: ResolvedPos }[] = [];
doc.descendants((node, pos) => {
node.marks.forEach((mark) => {
if (
mark.type.name === CustomLink.name &&
mark.attrs.id &&
mark.attrs.type
) {
const markRange = getMarkRange(
doc.resolve(pos),
mark.type,
mark.attrs
);
if (
markRange &&
markRange.to >= from &&
markRange.from <= to
) {
activeMarks.push({ mark, $pos: doc.resolve(pos) });
}
}
});
});
activeMarks.forEach(({ mark, $pos }) => {
const markRange = getMarkRange($pos, mark.type, mark.attrs);
if (markRange) {
decorations.push(
Decoration.inline(markRange.from, markRange.to, {
class: "has-focus",
})
);
}
});
return DecorationSet.create(doc, decorations);
},
},
}),
];
It works when only the TextNode with the mark is present:
Also, it works when there is text after the TextNode with the mark:
But, if there is text before the TextNode with the mark, it does not work:
Is this the way to go or am I missing something?
Thank you for the help!