Passed
Pull Request — master (#66)
by Michael
02:50
created

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

Complexity

Total Complexity 10
Complexity/F 1.43

Size

Lines of Code 57
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

3 Functions

Rating   Name   Duplication   Size   Complexity  
A PageMacroMenuItemDispatcher.js ➔ constructor 0 4 1
A PageMacroMenuItemDispatcher.js ➔ getMenuItem 0 36 2
A PageMacroMenuItemDispatcher.js ➔ isAvailable 0 3 1
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.tr;
31
                    tr.step(new SetDocAttrStep(macroAttrName, newState));
32
                    dispatch(tr);
33
                }
34
                return true;
35
            },
36
            render: (editorView) => {
0 ignored issues
show
Unused Code introduced by
The parameter editorView is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

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