-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortcuts.js
215 lines (184 loc) · 5.14 KB
/
shortcuts.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function isEditable(t)
{
if ($(t).is("input")
|| $(t).is("select")
|| $(t).is("textarea")
|| $(t).is("button")) return true;
return false;
}
//
// global shortcuts handler. Receives events on both keydown and keyup too
//
function processGlobalShortcuts(event)
{
switch(event.which)
{
case 13: // Enter
propSuggestHide();
break;
case 9: // Tab
if (event.type=='keydown') // ignore tab keyup
propSuggestHide();
break;
case 27: // Esc
if (event.type=='keydown')
{
propSuggestHide();
if ($('#modalq').is(":visible")) modalqDestroy();
else modalDestroy();
}
if (event.type=='keyup')
{
autocompleteHide();
}
break;
}
// Most of Global keyboard shortcuts are
// only processed if focus is not on a form element
if (isEditable(event.target)) return;
switch(event.which)
{
case 27: // Esc
msg();
break;
case 46: // Del
deleteKeyPressed();
break;
case 65: // Ctrl+A, A
if (event.ctrlKey) // Ctrl+A only
{
deselectAll();
for(var i=0; i<g.states.length; i++)
selectObject(g.states[i]);
refreshToolbars()
}
break;
case 90: // Ctrl+Z, Z
if (event.ctrlKey) // Ctrl+Z only
{
event.preventDefault();
if (event.type!='keyup') historyUndo();
}
break;
case 67: // Ctrl+C, C
if (event.ctrlKey) // Ctrl+C only
{
event.preventDefault();
if (event.type!='keyup') controlCopy();
}
break;
case 86: // Ctrl+V, V
if (event.ctrlKey) // Ctrl+V only
{
event.preventDefault();
if (event.type!='keyup') controlPaste();
}
break;
case 88: // Ctrl+X, X
if (event.ctrlKey) // Ctrl+V only
{
event.preventDefault();
if (event.type!='keyup')
{
controlCopy();
deleteKeyPressed();
}
}
break;
}
}
// callback a function if ENTER key is pressed on an INPUT field
function processInputSpecialKeys(ev)
{
var t=ev.target;
var key=ev.which;
var enter=$(t).attr('data-enter');
if (enter && key==13) eval(enter+"(t)");
}
// on each keypress, keydown and keyup, validate if the key/character
// is allowed on the INPUT element. If not, display error message
// and don't let the user to type that key/char
function validateInput(ev, keyIsChar)
{
var t=ev.target;
var char=ev.which;
var filter=$(t).attr('data-filter');
if (!filter) return;
if (filter.match('number')) // we want to allow only numbers
{
if (!keyIsChar)
{
if ($(t).val().match(/.[-]/)) // fix double minus signs
$(t).val("-"+$(t).val().replace(/[-]/g,''));
if ($(t).val().split(/[.,]/).length > 2) // fix double dot
$(t).val($(t).val().replace(/[.,]/g,'')+'.');
}
if (keyIsChar)
if (char>=32 && (char<48 || char>57) && char!=44 && char!=45 && char!=46)
{
msg("Only numbers allowed here","error");
ev.preventDefault();
return;
}
}
if (filter.match('noregex'))
{
if (char>=32 && char!=44 && keyIsChar)
if ((char>=33 && char<=47) || (char>=58 && char<=64) || (char>=91 && char<=96) || (char>=123 && char<=127))
{
msg("Such character is not valid here","error");
ev.preventDefault();
return;
}
}
if (filter.match('funcname') || filter.match('varname'))
{
// number must not be first
if ($(t).val().match(/^[0-9]/) || (keyIsChar && char>=48 && char<=57 && $(t).val()==''))
{
msg("Identifier cannot start with a number ","error");
$(t).val($(t).val().replace(/^[0-9]/g,''));
ev.preventDefault();
return;
}
// only allow valid characters
if (char>=32 && keyIsChar)
if (char<48 || (char>=58 && char<=64) || (char>=91 && char<=94) || char==96 || char>122 )
{
msg("Such character is not valid here","error");
ev.preventDefault();
return;
}
}
if (char!=13 && char!=16 && (char<32 || keyIsChar)) msg("");
propUpdate();
refreshToolbarButtons();
}
// Update textarea rows count according to number of newlines
// in the value.
function textareaRowsUpdateOnly(t)
{
$(t).attr("rows",Math.max(2, $(t).val().split("\n").length+1) );
fixEditboxPos();
// TODO: this is intricate. Sometimes it works sometimes it does not. Lets skip this for now
/*
// fix height of the element with long lines
// needs to be called after other rendering is complete
setTimeout(function(){
var scroll=$('#editboxscrollablearea').scrollTop();
var origHeight=$(t).height();
if (origHeight==0) return;
$(t).height(1);
var scrollHeight=$(t).prop("scrollHeight");
if (scrollHeight) $(t).height(Math.max(30,scrollHeight-8)+10);
else $(t).height(origHeight);
$('#editboxscrollablearea').scrollTop(scroll);
},10);
*/
}
function textareaRowsUpdate(t)
{
textareaRowsUpdateOnly(t);
propUpdate();
refreshToolbarButtons();
}