Completed
Push — master ( d64ad1...48757a )
by Michael
15s queued 10s
created

SetDocAttrStep.js ➔ apply   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
dl 0
loc 6
rs 10
nop 1
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