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

script/plugins/Menu/MenuItems/PageMacroMenuItemDispatcher.js   A

Complexity

Total Complexity 10
Complexity/F 1.43

Size

Lines of Code 59
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 10
eloc 41
c 1
b 0
f 0
nc 1
mnd 1
bc 8
fnc 7
dl 0
loc 59
rs 10
bpm 1.1428
cpm 1.4285
noi 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A PageMacroMenuItemDispatcher.js ➔ constructor 0 4 1
A PageMacroMenuItemDispatcher.js ➔ isAvailable 0 3 1
A PageMacroMenuItemDispatcher.js ➔ getMenuItem 0 38 2
1
import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher';
2
import MenuItem from '../MenuItem';
3
import SetDocAttrStep from '../../../custom/SetDocAttrStep';
4
import { svgIcon } from '../MDI';
5
6
export default class PageMacroMenuItemDispatcher extends AbstractMenuItemDispatcher {
7
    /**
8
     * Create an MenuItemDispatcher to toggle the state of a page macro
9
     *
10
     * @param {string} macro The page macro, should be either NOCACHE or NOTOC
11
     */
12
    constructor(macro) {
13
        super();
14
        this.macro = macro;
15
    }
16
17
    isAvailable({ nodes }) {
18
        return !!nodes.doc.attrs[this.macro.toLowerCase()];
19
    }
20
21
    getMenuItem({ nodes }) {
22
        if (!this.isAvailable({ nodes })) {
23
            throw new Error('Plugin block not available in schema!');
24
        }
25
        const macroAttrName = this.macro.toLowerCase();
26
        return new MenuItem({
27
            command: (state, dispatch) => {
28
                if (dispatch) {
29
                    const newState = !state.doc.attrs[macroAttrName];
30
                    const { tr } = state;
31
                    tr.step(new SetDocAttrStep(macroAttrName, newState));
32
                    dispatch(tr);
33
                }
34
                return true;
35
            },
36
            render: () => {
37
                const label = LANG.plugins.prosemirror[`label:${macroAttrName}`];
0 ignored issues
show
Bug introduced by
The variable LANG seems to be never declared. If this is a global, consider adding a /** global: LANG */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
38
                const blankIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-blank-outline'), label)
39
                    .addClass('is-false');
40
                const checkedIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-marked-outline'), label)
41
                    .addClass('is-true');
42
                const dom = jQuery('<span>').addClass('menuitem');
43
                dom.append(blankIcon);
44
                dom.append(checkedIcon);
45
                dom.append(jQuery('<span>')
46
                    .addClass('menulabel')
47
                    .text(label));
48
                return dom.get(0);
49
            },
50
            update: (editorView, dom) => {
51
                const $dom = jQuery(dom);
52
                const attrState = editorView.state.doc.attrs[macroAttrName];
53
                $dom.find('.is-true').toggle(attrState);
54
                $dom.find('.is-false').toggle(!attrState);
55
            },
56
            isActive: state => state.doc.attrs[macroAttrName],
57
        });
58
    }
59
}
60