I’m trying to implement links on images by using the tr.setNodeMarkup
api, which seems allowed given that marks is a possible last parameter to that method.
I’ve also modified the image spec such that it allows for link marks:
const imageSpec = {
attrs: {
src: { default: null },
alt: { default: null },
title: { default: null }
},
inline: false,
group: "block",
marks: "link",
draggable: true,
parseDOM: [
{
tag: "img[src]",
getAttrs(img) {
return { src: img.src, alt: img.alt, title: img.title };
}
}
],
toDOM(node) {
return ["img", { ...node.attrs }];
}
};
And the snippet that applies the mark is
if (selection instanceof NodeSelection) {
const { node, from } = selection;
console.log("node", node.type.name);
if (node.type === schema.nodes.image) {
console.log("before", node.marks);
tr.setNodeMarkup(from, null, node.attrs, [
schema.marks.link.create({ href })
]);
console.log("after", tr.doc.resolve(from).nodeAfter.marks);
}
}
The issue I’m running into is that the mark seems to be correctly applied when I set breakpoints within setNodeMarkup
but checking the doc both by logging it after the setNodeMarkup
and after the transaction is dispatch, the block level link doesn’t seem to get applied.
Reproducible sandbox: snowy-butterfly-cmwpw - CodeSandbox