Total Complexity | 7 |
Complexity/F | 1.17 |
Lines of Code | 42 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import { Step, StepResult } from 'prosemirror-transform'; |
||
2 | |||
3 | /** |
||
4 | * Custom step to set attributes of the root `doc` node |
||
5 | * |
||
6 | * @see https://discuss.prosemirror.net/t/changing-doc-attrs/784/17 |
||
7 | */ |
||
8 | export default class SetDocAttrStep extends Step { |
||
9 | constructor(key, value, stepType = 'SetDocAttr') { |
||
10 | super(); |
||
11 | this.stepType = stepType; |
||
12 | this.key = key; |
||
13 | this.value = value; |
||
14 | } |
||
15 | |||
16 | apply(doc) { |
||
17 | this.prevValue = doc.attrs[this.key]; |
||
18 | /* eslint-disable-next-line no-param-reassign */ |
||
19 | doc.attrs[this.key] = this.value; |
||
20 | return StepResult.ok(doc); |
||
21 | } |
||
22 | |||
23 | invert() { |
||
24 | return new SetDocAttrStep(this.key, this.prevValue, 'revertSetDocAttr'); |
||
25 | } |
||
26 | |||
27 | map() { |
||
28 | return null; |
||
29 | } |
||
30 | |||
31 | toJSON() { |
||
32 | return { |
||
33 | stepType: this.stepType, |
||
34 | key: this.key, |
||
35 | value: this.value, |
||
36 | }; |
||
37 | } |
||
38 | |||
39 | static fromJSON(json) { |
||
40 | return new SetDocAttrStep(json.key, json.value, json.stepType); |
||
41 | } |
||
42 | } |
||
43 |