In my example , i defined this custom node which is a block node (h1) with an inline content and which has an attribute “data-before-content” which is responsible of displaying a css rule’s ::before dynamic content :
const editorTitle = {
name: "editorTitle",
content: "inline*",
isTextblock: true,
attrs: {
class: { default: "editorTitleId" },
"data-before-content": { default: undefined },
},
group: "block",
parseDOM: [
{
priority: 51, // must be higher than the default image spec
tag: "h1[class][data-before-content]",
getAttrs(dom) {
const nodeClass = dom.getAttribute("class");
if (nodeClass != "editorTitleId") {
return false;
}
return {
class: nodeClass,
"data-before-content": dom.getAttribute("data-before-content"),
};
},
},
],
toDOM(node) {
const attrs = node.attrs;
if (
!node.content.content[0] ||
(node.content.content[0] && !node.content.content[0].text)
) {
attrs["data-before-content"] = "Untitled item";
} else {
attrs["data-before-content"] = undefined;
}
return ["h1", { ...attrs }, 0];
},
};
the css rule : h1.editorTitleId::before { content: attr(data-before-content); opacity: 0.5; }
When editing the editorTitle content, the toDom is executed and changes the “data-before-content” value but the dom related element attribute never get updated.
I reproduced the bug in this codesandbox
As you can see in the demo code, i’ve tried to update the view state inside the dispatchTransaction as the following using view.updateState(newState); or even setState(newState); but the problem is still there in either cases (i prefer just update the view state though and not the react’s)
dispatchTransaction={transaction => {
const { view } = useProseMirrorComp;
let newState = view.state.apply(transaction);
proseMirrorOnChangeHandler(state, newState, mySchema, props);
view.updateState(newState);
// setState(newState);
}}
toDom only displays the correct “data-before-content” attribute in the initial display whether the editorTitle has content or not (by changing the textPageContent value in the code):
-
with content : when
let textPageContent = '<h1 class="editorTitleId">4</h1><p></p>';
“data-before-content” will be equal to an empty string in this case and only the editorTitle content will be displayed -
without content : when
let textPageContent = "";
==> “data-before-content” will be equal to ‘Untitled item’
So why the toDom runs but doesn’t update the dom element attribute ? what am i doing wrong ?! Cheers