Test Setup Failed
Push — master ( 8ee19e...25278d )
by recca
05:07 queued 46s
created

resources/assets/js/commands/vi.js   A

Size

Lines of Code 100

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
nc 1
dl 0
loc 100
rs 10
c 2
b 1
f 0
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A vi.js ➔ ??? 0 13 1
1
'use strict';
2
3
import Command from './command';
4
import CodeMirror from 'codemirror';
5
import 'codemirror/addon/dialog/dialog';
6
import 'codemirror/addon/search/searchcursor';
7
import 'codemirror/addon/mode/loadmode';
8
import 'codemirror/addon/edit/matchbrackets';
9
import 'codemirror/addon/display/fullscreen';
10
import 'codemirror/mode/clike/clike';
11
import 'codemirror/mode/meta';
12
import 'codemirror/keymap/vim';
13
import 'codemirror/mode/php/php';
14
import 'codemirror/mode/css/css';
15
import 'codemirror/mode/javascript/javascript';
16
import 'codemirror/mode/htmlmixed/htmlmixed';
17
import 'codemirror/mode/xml/xml';
18
19
export default class Vi extends Command {
20
    constructor(options) {
21
        super(options);
22
        let textarea = $('<textarea style="display: none;"></textarea>').appendTo(document.body).get(0);
23
        this.editor = CodeMirror.fromTextArea(textarea, {
24
            lineNumbers: true,
25
            matchBrackets: true,
26
            keyMap: 'vim',
27
            showCursorWhenSelecting: true,
28
            theme: 'monokai'
29
        });
30
        this.editor.getWrapperElement().className += ' CodeMirror-fullscreen';
31
        $(this.editor.getWrapperElement()).hide();
32
    }
33
34
    match(name) {
35
        return name === 'vi';
36
    }
37
38
    call(cmd) {
39
        this.api.$term.pause();
40
        this.makeRequest(cmd.command).then((response) => {
41
            let path = cmd.rest;
42
            let editor = this.editor;
43
            let m, info, mode, spec;
44
            if (m = path.match(/.+\.([^.]+)$/)) {
45
                info = CodeMirror.findModeByExtension(m[1]);
46
                if (info) {
47
                    mode = info.mode;
48
                    spec = info.mime;
49
                }
50
            } else if (/\//.test(path)) {
51
                info = info = CodeMirror.findModeByMIME(path);
0 ignored issues
show
Unused Code introduced by
The assignment to variable info seems to be never used. Consider removing it.
Loading history...
52
                if (info) {
53
                    mode = info.mode;
54
                    spec = info.mime;
55
                }
56
            };
57
58
            if (['htmlmixed', 'css', 'javascript', 'php'].includes(mode) === false) {
0 ignored issues
show
Bug introduced by
The variable mode seems to not be initialized for all possible execution paths. Are you sure includes handles undefined variables?
Loading history...
59
                mode = 'php';
60
                spec = 'application/x-httpd-php';
61
            }
62
63
            if (mode) {
64
                editor.setOption("mode", spec);
0 ignored issues
show
Bug introduced by
The variable spec seems to not be initialized for all possible execution paths. Are you sure setOption handles undefined variables?
Loading history...
65
                CodeMirror.autoLoadMode(editor, mode);
66
            }
67
            $(editor.getWrapperElement()).show();
68
            let doc = editor.getDoc();
69
            let cm = doc.cm;
70
            doc.setValue(response.result);
71
            doc.setCursor(0);
72
            editor.focus();
73
            cm.focus();
74
            let save = () => {
75
                let value = JSON.stringify(doc.getValue());
76
                cmd.command += ` --text=${value}`;
77
                this.makeRequest(cmd.command).then(() => {}, () => {});
78
            }
79
            let quit = () => {
80
                $(editor.getWrapperElement()).hide();
81
                this.api.$term.resume();
82
                this.api.$term.focus();
83
            }
84
            CodeMirror.Vim.defineEx('q', 'q', function() {
85
                quit();
86
            });
87
            CodeMirror.Vim.defineEx('w', 'w', function() {
88
                save();
89
            });
90
            CodeMirror.Vim.defineEx('wq', 'wq', function() {
91
                save();
92
                quit();
93
            });
94
        }, (response) => {
95
            this.api.loading.hide();
96
            this.api.echo(response.result);
97
            this.api.serverInfo();
98
        });
99
    }
100
}
101