Passed
Pull Request — master (#67)
by Michael
02:01
created

script/custom/paragraphButtons.js   A

Complexity

Total Complexity 8
Complexity/F 1.33

Size

Lines of Code 65
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 8
eloc 29
nc 1
mnd 1
bc 8
fnc 6
dl 0
loc 65
rs 10
bpm 1.3333
cpm 1.3333
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A paragraphButtons.js ➔ getInsertParagraphButton 0 34 2
A paragraphButtons.js ➔ getInsertParagraphAfterButton 0 3 1
A paragraphButtons.js ➔ getInsertParagraphBeforeButton 0 3 1
1
import { insertParagraphAtPos } from '../customCommands';
2
3
/**
4
 * @param {function} getView function that returns the current EditorView
5
 * @param {string}   direction should be either 'before' or 'after'
6
 * @return HTMLElement
7
 */
8
function getInsertParagraphButton(getView, direction) {
9
    if (typeof jQuery === 'undefined') {
10
        // early return during js schema testing
11
        return null;
12
    }
13
14
    function dispatchInsertingParagraph(event) {
15
        event.preventDefault();
16
        event.stopPropagation();
17
        const view = getView();
18
        const pos = view.posAtDOM(event.target.parentNode);
19
        const command = insertParagraphAtPos(pos, direction);
20
        command(view.state, view.dispatch);
21
    }
22
23
    const $paragraphButton = jQuery('<button>');
24
    $paragraphButton.css('visibility', 'hidden');
25
    $paragraphButton.on('click', dispatchInsertingParagraph);
26
    $paragraphButton.text('Add paragraph');
27
    const $buttonWrapper = jQuery('<div>').append($paragraphButton);
28
    $buttonWrapper.on('mouseleave', () => {
29
        $paragraphButton.css('visibility', 'hidden');
30
    });
31
    $buttonWrapper.on('mouseenter', (event) => {
32
        const view = getView();
33
        const pos = view.posAtDOM(event.target.parentNode);
34
        const command = insertParagraphAtPos(pos, direction);
35
        if (command(view.state)) {
36
            $paragraphButton.css('visibility', 'visible');
37
        }
38
    });
39
40
    return $buttonWrapper.get(0);
41
}
42
43
/**
44
 * Returns a DOM.Node for a div with a button to insert a new paragraph before the node to which it is attached
45
 *
46
 * This button has functionality to only appear when it is actually available
47
 *
48
 * @param {function} getView function that returns the current EditorView
49
 * @return HTMLElement
50
 */
51
export function getInsertParagraphBeforeButton(getView) {
52
    return getInsertParagraphButton(getView, 'before');
53
}
54
55
/**
56
 * Returns a DOM.Node for a div with a button to insert a new paragraph after the node to which it is attached
57
 *
58
 * This button has functionality to only appear when it is actually available
59
 *
60
 * @param {function} getView function that returns the current EditorView
61
 * @return HTMLElement
62
 */
63
export function getInsertParagraphAfterButton(getView) {
64
    return getInsertParagraphButton(getView, 'after');
65
}
66