-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
89 lines (84 loc) · 2.69 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*jslint nomen: true, vars: true */
/*global define, brackets, $*/
define(function (require, exports, module) {
'use strict';
// Brackets modules
var AppInit = brackets.getModule('utils/AppInit'),
EditorManager = brackets.getModule('editor/EditorManager'),
KeyEvent = brackets.getModule('utils/KeyEvent'),
PreferencesManager = brackets.getModule('preferences/PreferencesManager');
function getSpaceIndentation(inLine) {
var
spacesString = '',
i = 0;
for (i = 0; i < inLine.length; i += 1) {
if (inLine[i].trim() === '') {
spacesString += inLine[i];
} else {
return spacesString;
}
}
}
function fileTypes(fileLang) {
switch (fileLang) {
case 'javascript':
case 'html':
case 'css':
return true;
break;
default:
return false;
break;
}
}
function _keyEventHandler($event, editor, event) {
var
cursorPos = editor.getCursorPos(),
document = editor.document,
currLine = document.getLine(cursorPos.line),
indent = getSpaceIndentation(currLine),
fileLang = editor.document.language._id,
currLineLen = currLine.length;
// On alt+ENTER pressed
if (event.keyCode === KeyEvent.DOM_VK_RETURN && event.shiftKey) {
// only for JavaScript
if (fileTypes(fileLang)) {
editor.setCursorPos(cursorPos.line, currLineLen);
cursorPos = editor.getCursorPos();
if (currLine.substring(currLineLen-1,currLineLen)!==';') {
document.replaceRange(';\n' + indent, cursorPos);
} else {
document.replaceRange('\n' + indent, cursorPos);
}
event.preventDefault();
}
} else {
//on alt+; pressed
if (event.keyCode === KeyEvent.DOM_VK_RETURN && event.altKey) {
if (fileTypes(fileLang)) {
var curCursorPos = editor.getCursorPos();
editor.setCursorPos(cursorPos.line, currLine.length);
cursorPos = editor.getCursorPos();
if (currLine.substring(currLineLen-1,currLineLen)!==';') {
document.replaceRange(';', cursorPos);
editor.setCursorPos(curCursorPos, 0);
}
event.preventDefault();
}
}
}
}
function _activeEditorChangeHandler($event, focusedEditor, lostEditor) {
if (lostEditor) {
$(lostEditor).off('keydown', _keyEventHandler);
}
if (focusedEditor) {
$(focusedEditor).on('keydown', _keyEventHandler);
}
}
AppInit.appReady(function () {
var currentEditor = EditorManager.getActiveEditor();
$(currentEditor).on('keydown', _keyEventHandler);
$(EditorManager).on('activeEditorChange', _activeEditorChangeHandler);
});
});