Thanks, @Jordan this helped
I have made a quick tweak to the above so you can set and remove individual keys. Might save other people some time.
Usage:
const transaction = state.tr
.step(new SetDocAttr('foo', 'bar'))
.step(new SetDocAttr('bar', ['foo', 'foo']));
dispatch(transaction)
Custom Step:
// @flow
import { Step, StepResult } from 'prosemirror-transform';
class SetDocAttr extends Step {
constructor(key: string, value: any, stepType?: string = 'SetDocAttr') {
super();
this.stepType = stepType;
this.key = key;
this.value = value;
}
apply(doc) {
this.prevValue = doc.attrs[this.key];
doc.attrs[this.key] = this.value;
return StepResult.ok(doc);
}
invert() {
return new SetDocAttr(this.key, this.prevValue, 'revertSetDocAttr');
}
map() {
return null;
}
toJSON() {
return {
stepType: this.stepType,
key: this.key,
value: this.value,
};
}
static fromJSON(json) {
return new SetDocAttr(json.key, json.value, json.stepType);
}
}