script/plugins/Menu/MenuItems/FootnoteMenuItemDispatcher.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 49
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 49
rs 10
wmc 7
mnd 4
bc 4
fnc 3
bpm 1.3333
cpm 2.3333
noi 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
B FootnoteMenuItemDispatcher.getMenuItem 0 36 6
A FootnoteMenuItemDispatcher.isAvailable 0 3 1
1
import { Schema } from 'prosemirror-model';
2
3
import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher';
4
import MenuItem from '../MenuItem';
5
import { svgIcon } from '../MDI';
6
import getFootnoteSpec from '../../../nodeviews/Footnote/footnoteSchema';
7
8
export default class FootnoteMenuItemDispatcher extends AbstractMenuItemDispatcher {
9
    static isAvailable(schema) {
10
        return !!schema.nodes.footnote;
11
    }
12
13
    static getMenuItem(schema) {
14
        if (!this.isAvailable(schema)) {
15
            throw new Error('Footnote not available in schema!');
16
        }
17
        return new MenuItem({
18
            command: (state, dispatch) => {
19
                const { $from } = state.selection;
20
21
                const index = $from.index();
22
                if (!$from.parent.canReplaceWith(index, index, schema.nodes.footnote)) {
23
                    return false;
24
                }
25
26
                const selectedContent = state.selection.content().content.toJSON();
27
                const footnoteDoc = {
28
                    type: 'doc',
29
                    content: selectedContent || [{ type: 'paragraph' }],
30
                };
31
                try {
32
                    const footnoteSchema = new Schema(getFootnoteSpec());
33
                    footnoteSchema.nodeFromJSON(footnoteDoc);
34
                } catch (e) {
35
                    return false;
36
                }
37
38
                if (dispatch) {
39
                    const footnoteNode = schema.nodes.footnote.create({ contentJSON: JSON.stringify(footnoteDoc) });
40
                    dispatch(state.tr.replaceSelectionWith(footnoteNode));
41
                }
42
43
                return true;
44
            },
45
            icon: svgIcon('note-plus-outline'),
46
            label: LANG.plugins.prosemirror['label:footnote'],
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...
47
        });
48
    }
49
}
50