extract key binding into a separate files

This commit is contained in:
Fabian Jakobs 2010-04-12 17:47:10 +02:00
commit 9b8f6c6c95
3 changed files with 176 additions and 173 deletions

View file

@ -29,7 +29,8 @@
<script src="../src/GutterLayer.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/TextLayer.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/MarkerLayer.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/TextInput.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/TextInput.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/KeyBinding.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/Editor.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/VirtualRenderer.js" type="text/javascript" charset="utf-8"></script>
</head>

View file

@ -1,180 +1,12 @@
if (!window.ace)
ace = {};
(function() {
var keys = {
UP : 38,
RIGHT : 39,
DOWN : 40,
LEFT : 37,
PAGEUP : 33,
PAGEDOWN : 34,
POS1 : 36,
END : 35,
DELETE : 46,
BACKSPACE : 8,
TAB : 9,
A : 65,
D: 68
};
var KeyBinding = function(element, host) {
ace.addListener(element, "keydown", function(e) {
var key = e.keyCode;
switch (key) {
case keys.A:
if (e.metaKey) {
host.selectAll();
return ace.stopEvent(e);
}
break;
case keys.D:
if (e.metaKey) {
host.removeLine();
return ace.stopEvent(e);
}
break;
case keys.UP:
if (e.metaKey && e.shiftKey) {
host.selectFileStart();
}
else if (e.metaKey) {
host.navigateFileStart();
}
if (e.shiftKey) {
host.selectUp();
}
else {
host.navigateUp();
}
return ace.stopEvent(e);
case keys.DOWN:
if (e.metaKey && e.shiftKey) {
host.selectFileEnd();
}
else if (e.metaKey) {
host.navigateFileEnd();
}
if (e.shiftKey) {
host.selectDown();
}
else {
host.navigateDown();
}
return ace.stopEvent(e);
case keys.LEFT:
if (e.altKey && e.shiftKey) {
host.selectWordLeft();
}
else if (e.altKey) {
host.navigateWordLeft();
}
else if (e.metaKey && e.shiftKey) {
host.selectLineStart();
}
else if (e.metaKey) {
host.navigateLineStart();
}
else if (e.shiftKey) {
host.selectLeft();
}
else {
host.navigateLeft();
}
return ace.stopEvent(e);
case keys.RIGHT:
if (e.altKey && e.shiftKey) {
host.selectWordRight();
}
else if (e.altKey) {
host.navigateWordRight();
}
else if (e.metaKey && e.shiftKey) {
host.selectLineEnd();
}
else if (e.metaKey) {
host.navigateLineEnd();
}
else if (e.shiftKey) {
host.selectRight();
}
else {
host.navigateRight();
}
return ace.stopEvent(e);
case keys.PAGEDOWN:
if (e.shiftKey) {
host.selectPageDown();
}
else {
host.scrollPageDown();
}
return ace.stopEvent(e);
case keys.PAGEUP:
if (e.shiftKey) {
host.selectPageUp();
}
else {
host.scrollPageUp();
}
return ace.stopEvent(e);
case keys.POS1:
if (e.shiftKey) {
host.selectLineStart();
}
else {
host.navigateLineStart();
}
return ace.stopEvent(e);
case keys.END:
if (e.shiftKey) {
host.selectLineEnd();
}
else {
host.navigateLineEnd();
}
return ace.stopEvent(e);
case keys.DELETE:
host.removeRight();
return ace.stopEvent(e);
case keys.BACKSPACE:
host.removeLeft();
return ace.stopEvent(e);
case keys.TAB:
if (host.hasMultiLineSelection()) {
if (e.shiftKey) {
host.blockOutdent();
} else {
host.blockIndent();
}
} else {
host.onTextInput(" ");
}
return ace.stopEvent(e);
}
});
};
ace.Editor = function(doc, renderer) {
var container = renderer.getContainerElement();
this.renderer = renderer;
this.textInput = new ace.TextInput(container, this);
new KeyBinding(container, this);
new ace.KeyBinding(container, this);
ace.addListener(container, "mousedown", ace
.bind(this.onMouseDown, this));
@ -819,6 +651,4 @@ ace.Editor.prototype.selectLine = function() {
this._moveSelection(function() {
this.moveCursorTo(this.cursor.row + 1, 0);
});
};
})();
};

172
src/KeyBinding.js Normal file
View file

@ -0,0 +1,172 @@
if (!window.ace)
ace = {};
(function() {
var keys = {
UP : 38,
RIGHT : 39,
DOWN : 40,
LEFT : 37,
PAGEUP : 33,
PAGEDOWN : 34,
POS1 : 36,
END : 35,
DELETE : 46,
BACKSPACE : 8,
TAB : 9,
A : 65,
D: 68
};
ace.KeyBinding = function(element, host) {
ace.addListener(element, "keydown", function(e) {
var key = e.keyCode;
switch (key) {
case keys.A:
if (e.metaKey) {
host.selectAll();
return ace.stopEvent(e);
}
break;
case keys.D:
if (e.metaKey) {
host.removeLine();
return ace.stopEvent(e);
}
break;
case keys.UP:
if (e.metaKey && e.shiftKey) {
host.selectFileStart();
}
else if (e.metaKey) {
host.navigateFileStart();
}
if (e.shiftKey) {
host.selectUp();
}
else {
host.navigateUp();
}
return ace.stopEvent(e);
case keys.DOWN:
if (e.metaKey && e.shiftKey) {
host.selectFileEnd();
}
else if (e.metaKey) {
host.navigateFileEnd();
}
if (e.shiftKey) {
host.selectDown();
}
else {
host.navigateDown();
}
return ace.stopEvent(e);
case keys.LEFT:
if (e.altKey && e.shiftKey) {
host.selectWordLeft();
}
else if (e.altKey) {
host.navigateWordLeft();
}
else if (e.metaKey && e.shiftKey) {
host.selectLineStart();
}
else if (e.metaKey) {
host.navigateLineStart();
}
else if (e.shiftKey) {
host.selectLeft();
}
else {
host.navigateLeft();
}
return ace.stopEvent(e);
case keys.RIGHT:
if (e.altKey && e.shiftKey) {
host.selectWordRight();
}
else if (e.altKey) {
host.navigateWordRight();
}
else if (e.metaKey && e.shiftKey) {
host.selectLineEnd();
}
else if (e.metaKey) {
host.navigateLineEnd();
}
else if (e.shiftKey) {
host.selectRight();
}
else {
host.navigateRight();
}
return ace.stopEvent(e);
case keys.PAGEDOWN:
if (e.shiftKey) {
host.selectPageDown();
}
else {
host.scrollPageDown();
}
return ace.stopEvent(e);
case keys.PAGEUP:
if (e.shiftKey) {
host.selectPageUp();
}
else {
host.scrollPageUp();
}
return ace.stopEvent(e);
case keys.POS1:
if (e.shiftKey) {
host.selectLineStart();
}
else {
host.navigateLineStart();
}
return ace.stopEvent(e);
case keys.END:
if (e.shiftKey) {
host.selectLineEnd();
}
else {
host.navigateLineEnd();
}
return ace.stopEvent(e);
case keys.DELETE:
host.removeRight();
return ace.stopEvent(e);
case keys.BACKSPACE:
host.removeLeft();
return ace.stopEvent(e);
case keys.TAB:
if (host.hasMultiLineSelection()) {
if (e.shiftKey) {
host.blockOutdent();
} else {
host.blockIndent();
}
} else {
host.onTextInput(" ");
}
return ace.stopEvent(e);
}
});
};
})();