/
SetDocAttrStep.js
56 lines (46 loc) · 1.18 KB
/
SetDocAttrStep.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow
import {Node} from 'prosemirror-model';
import {Step, StepResult} from 'prosemirror-transform';
type SetDocAttrStepJSONValue = {
key: string,
stepType: string,
value: any,
};
// https://discuss.prosemirror.net/t/changing-doc-attrs/784/17
class SetDocAttrStep extends Step {
key: string;
stepType: string;
value: any;
constructor(key: string, value: any, stepType?: string = 'SetDocAttr') {
super();
this.stepType = stepType;
this.key = key;
this.value = value;
}
apply(doc: Node): void {
this.prevValue = doc.attrs[this.key];
const attrs = {
...doc.attrs,
[this.key]: this.value,
};
const docNew = doc.type.create(attrs, doc.content, doc.marks);
return StepResult.ok(docNew);
}
invert(): SetDocAttrStep {
return new SetDocAttrStep(this.key, this.prevValue, 'revertSetDocAttr');
}
map(): ?Object {
return null;
}
toJSON(): SetDocAttrStepJSONValue {
return {
stepType: this.stepType,
key: this.key,
value: this.value,
};
}
static fromJSON(json: SetDocAttrStepJSONValue) {
return new SetDocAttrStep(json.key, json.value, json.stepType);
}
}
export default SetDocAttrStep;