Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Refactor insert mode commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebrock committed Dec 7, 2012
1 parent 1c11737 commit eb8beb6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script src='js/normal_mode/operators.js'></script>
<script src='js/normal_mode/repeat.js'></script>
<script src='js/insert_mode.js'></script>
<script src='js/insert_mode/commands.js'></script>
<script src='js/demo.js'></script>
<script>
window.vimulator = new Vimulator.Demo().init('#vimulator');
Expand Down
22 changes: 10 additions & 12 deletions js/insert_mode.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
(function () {
var escapeCommand = new Vimulator.Command({
callback: function (vim) {
vim.setMode("normal");
vim.moveCursorRelative(0, -1);
},
description: function () {
return "<kbd>ESC</kbd> Return to normal mode";
}
});
var C = Vimulator.Command,
U = Vimulator.Utils;

Expand All @@ -20,12 +11,19 @@
this.vim.registers["."] = "";
};

Vimulator.InsertMode.prototype.commandList = function () {
this.commands = this.commands || new Vimulator.CommandList(
Vimulator.InsertMode.Commands
);
return this.commands;
};

Vimulator.InsertMode.prototype.keyPress = function (key) {
var op;

if (key === U.Keys.ESC) {
op = new Vimulator.Operation;
op.setCommand(escapeCommand);
if (Vimulator.InsertMode.Commands.hasOwnProperty(key)) {
op = new Vimulator.Operation(this.commandList());
op.keyPress(key);
op.execute(this.vim);
return op;
} else {
Expand Down
16 changes: 16 additions & 0 deletions js/insert_mode/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function () {
var C = Vimulator.Command,
U = Vimulator.Utils;

Vimulator.InsertMode.Commands = {};

Vimulator.InsertMode.Commands[U.Keys.ESC] = new C({
callback: function (vim) {
vim.setMode("normal");
vim.moveCursorRelative(0, -1);
},
description: function () {
return "Return to normal mode";
}
});
}());
1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<script src='js/normal_mode/operators.js'></script>
<script src='js/normal_mode/repeat.js'></script>
<script src='js/insert_mode.js'></script>
<script src='js/insert_mode/commands.js'></script>
<script>
window.vimulator = new Vimulator.Base().init('#vimulator');
</script>
Expand Down

0 comments on commit eb8beb6

Please sign in to comment.