Passed
Push — master ( 578b04...dc97f6 )
by Michael
03:06
created

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

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 46
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 7
eloc 30
c 1
b 0
f 0
nc 1
mnd 1
bc 8
fnc 3
dl 0
loc 46
rs 10
bpm 2.6666
cpm 2.3333
noi 0

2 Functions

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