Passed
Push — master ( 360ca5...ca6dc3 )
by Andreas
04:42 queued 01:57
created

script/plugins/disableDefaultTabBehavior.js   A

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 31
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 7
mnd 2
bc 2
fnc 5
bpm 0.4
cpm 1.4
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B disableDefaultTabBehavior.js ➔ disableDefaultTabBehavior 0 24 7
1
import { Plugin } from 'prosemirror-state';
2
3
/**
4
 * Disable the browser's default behavior on tab, which is required to support indentation using Tab/Shift-Tab
5
 */
6
export function disableDefaultTabBehavior() {
7
    function preventTab(e) {
8
        if (e.key === 'Tab') {
9
            e.preventDefault();
10
            e.stopPropagation();
11
        }
12
    }
13
14
    let setupComplete = false;
15
16
    return new Plugin({
17
        view: () => ({
18
            update: () => {
19
                if (setupComplete) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
21
                window.addEventListener('keydown', preventTab);
22
                setupComplete = true;
23
            },
24
            destroy: () => {
25
                window.removeEventListener('keydown', preventTab);
26
            },
27
        }),
28
    });
29
}
30
31
export default disableDefaultTabBehavior;
32