Hi guys, I am new to ProseMirror and we are switching over from Quill.js.
I have created a custom mark named background
which looks like this.
background: {
parseDOM: [
{
tag: 'span[background]',
getAttrs(node: any): { [key: string]: any } {
return { color: node.attrs.color };
},
},
],
toDOM(mark: Mark): DOMOutputSpec {
return ['span', { style: `background: ${mark.attrs.color};`, background: mark.attrs.color }, 0];
},
},
I use this mark to set a background color with a custom menu which looks like this.
To achieve my goal, I had to create a custom toggle handler for this mark, because I have to recreate the mark with the given color
attribute if I want to set it
The function I use to set the marks looks like this
const { from, to, empty } = this._editorView.state.selection;
if (!empty) {
const colorAttrs = { color };
const tr = this._editorView.state.tr;
// Adding attrs on create call is not working for some reasons
const mark = this._markType.create(colorAttrs);
mark.attrs = colorAttrs;
if (color === '__remove' && this.isActive.value) {
tr.removeMark(from, to, this._markType);
} else {
if (!this.isActive.value) {
tr.addMark(from, to, mark);
} else {
tr.removeMark(from, to, this._markType);
tr.addMark(from, to, mark);
}
}
this._editorView.dispatch(tr);
}
All of this works perfectly as long as I use the editor, but if I extract the JSON from the editor and try to reload it in another session with Node.fromJSON(myDoc)
the attrs
of the background
mark are not set properly.
Here is what the JSON after editing looks like (not matching the screenshot):
{
type: "paragraph",
content: [
{
type: "text",
marks: [{ type: "background", attrs: { color: "red" } }],
text: "this is a test",
},
],
}
and here is what the JSON looks like if I loaded it with Node.fromJSON
:
{
type: "paragraph",
content: [
{
type: "text",
marks: [{ type: "background"}],
text: "this is a test",
},
],
}
As you can see, the attrs
are missing from the mark.
Has anyone an Idea what I am doing wrong in this case?
Thank you very much