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

script/customCommands.js   A

Complexity

Total Complexity 14
Complexity/F 2.8

Size

Lines of Code 52
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 14
eloc 29
nc 1
mnd 2
bc 7
fnc 5
dl 0
loc 52
rs 10
bpm 1.4
cpm 2.8
noi 4
c 1
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A customCommands.js ➔ insertParagraphAtPos 0 20 1
A customCommands.js ➔ setBlockTypeNoAttrCheck 0 17 1
1
import { Selection } from 'prosemirror-state';
2
3
/**
4
 * Returns a command that tries to set the selected textblocks to the given node type with the given attributes.
5
 *
6
 * Copied and adjusted from prosemirror-commands::setBlockType to not check for the node attributes
7
 */
8
export function setBlockTypeNoAttrCheck(nodeType, attrs) { // eslint-disable-line import/prefer-default-export
9
    return function setBlockTypeNoAttrCheckDispatch(state, dispatch) {
10
        const { from, to } = state.selection;
11
        let applicable = false;
12
        state.doc.nodesBetween(from, to, (node, pos) => {
13
            if (applicable) return false;
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...
14
            if (!node.isTextblock || node.type === nodeType) return true;
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...
15
            const $pos = state.doc.resolve(pos);
16
            const index = $pos.index();
17
            applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
18
            return true;
19
        });
20
        if (!applicable) return false;
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...
21
        if (dispatch) dispatch(state.tr.setBlockType(from, to, nodeType, attrs).scrollIntoView());
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...
22
        return true;
23
    };
24
}
25
26
/**
27
 * Returns a command that inserts a new paragraph before or after the node at the given position
28
 *
29
 * @param {int}    pos       position near which the paragraph should be inserted
30
 * @param {string} direction should be 'before' or 'after'
31
 * @return {function}
32
 */
33
export function insertParagraphAtPos(pos, direction = 'after') {
34
    return function insertParagraphAtPosCommand(state, dispatch) {
35
        const $pos = state.doc.resolve(pos);
36
        const above = $pos.node(-1);
37
        const beforeOrAfter = direction !== 'after' ? $pos.index(-1) : $pos.indexAfter(-1);
38
        const type = above.contentMatchAt(beforeOrAfter).defaultType;
39
40
        if (!above.canReplaceWith(beforeOrAfter, beforeOrAfter, type)) {
41
            return false;
42
        }
43
44
        if (dispatch) {
45
            const insertPos = direction !== 'after' ? $pos.before() : $pos.after();
46
            const tr = state.tr.replaceWith(insertPos, insertPos, type.createAndFill());
47
            tr.setSelection(Selection.near(tr.doc.resolve(insertPos), 1));
48
            dispatch(tr.scrollIntoView());
49
        }
50
        return true;
51
    };
52
}
53