I have such custom block node schema:
const imageNodeSpec = {
attrs: {
src: {},
alt: {
default: null
}
},
inline: false,
group: 'block',
draggable: true,
parseDOM: [{
tag: 'div[image-with-description]',
getAttrs(dom) {
const image = dom.querySelector('img[src]');
return {
src: image.getAttribute('src'),
alt: image.getAttribute('alt')
};
}
}],
toDOM(node) {
return [
'div',
{
class: 'image-with-description-container',
'image-with-description': true
},
[
'img',
{
...node.attrs,
class: 'image'
},
],
[
'p',
{
class: 'image-description'
},
node.attrs.alt
]
];
}
};
It’s an image with description under it.
Each insertion of this node is occurred at the beginning.
If editor is empty and we insert such nodes then each time we will have position index 1.
Insertion is occured this way:
const tr = view.state.tr;
if (!tr.selection.empty) {
tr.deleteSelection();
}
view.dispatch(tr.replaceSelectionWith(type.createAndFill({
...attributes,
src: image.url
})));