Updating to the TypeScript-based ProseMirror version, I stumbled upon a few plugins in our code base that directly mutate the slices passed in to handlePaste
, handleDrop
, or transformPasted
, for example:
// Strip out any comments when pasting content
handlePaste(view, event, slice) {
slice.content.descendants(node => {
node.marks = node.marks.filter(isCommentMark);
});
return false;
},
or
// Strip out diff marks when pasting content
transformPasted(slice) {
slice.content.descendants(node => {
node.marks = node.marks.filter(isDiffMark);
if (supportsDiff(node.type)) {
node.attrs = {
...node.attrs,
deleted: false,
inserted: false,
modified: false,
};
}
});
return slice;
},
That always worked as far as I know, but the new typings have properties such as attrs
and marks
flagged as readonly
. Which makes sense, but now I am struggling to find a good way to create a new slice from a given slice, potentially changing any of the descendants in the slice.
Does someone have an example for how that is best done in an immutable way?