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

ltTabBehavior   B

Complexity

Conditions 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
dl 0
loc 24
rs 8
c 0
b 0
f 0
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