Total Complexity | 7 |
Complexity/F | 1.17 |
Lines of Code | 41 |
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 | doc.attrs[this.key] = this.value; |
||
19 | return StepResult.ok(doc); |
||
20 | } |
||
21 | |||
22 | invert() { |
||
23 | return new SetDocAttrStep(this.key, this.prevValue, 'revertSetDocAttr'); |
||
24 | } |
||
25 | |||
26 | map() { |
||
27 | return null; |
||
28 | } |
||
29 | |||
30 | toJSON() { |
||
31 | return { |
||
32 | stepType: this.stepType, |
||
33 | key: this.key, |
||
34 | value: this.value, |
||
35 | }; |
||
36 | } |
||
37 | |||
38 | static fromJSON(json) { |
||
39 | return new SetDocAttrStep(json.key, json.value, json.stepType); |
||
40 | } |
||
41 | } |
||
42 |