Skip to content

Commit

Permalink
feature(vim) add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Sep 7, 2017
1 parent dc79ab4 commit 6dc1bbe
Show file tree
Hide file tree
Showing 9 changed files with 539 additions and 11 deletions.
8 changes: 8 additions & 0 deletions client/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ function CmdProto() {
return Cmd;
};

this.unselectFile = (currentFile) => {
const current = currentFile || DOM.getCurrentFile();

current.classList.remove(SELECTED_FILE);

return Cmd;
};

this.toggleSelectedFile = (currentFile) => {
const current = currentFile || DOM.getCurrentFile();
const name = DOM.getCurrentName(current);
Expand Down
19 changes: 13 additions & 6 deletions client/key/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const exec = require('execon');
const Events = require('../dom/events');
const Buffer = require('../dom/buffer');
const KEY = require('./key');
const vim = require('./vim');
const setCurrentByChar = require('./set-current-by-char');
const fullstore = require('fullstore/legacy');
const Chars = fullstore();
Expand Down Expand Up @@ -71,15 +72,19 @@ function KeyProto() {
char = isSymbol;
}

/* in case buttons can be processed */
if (!Key.isBind())
return;

if (!isNumpad && !alt && !ctrl && !meta && (isBetween || isSymbol))
const isVim = CloudCmd.config('vim');

if (!isVim && !isNumpad && !alt && !ctrl && !meta && (isBetween || isSymbol))
return setCurrentByChar(char, Chars);

Chars([]);
switchKey(event);

if (isVim)
vim(char, event);
}

function getSymbol(shift, keyCode) {
Expand All @@ -106,6 +111,8 @@ function KeyProto() {
function switchKey(event) {
let i, isSelected, prev, next;
let current = Info.element;
let dataName;

const name = Info.name;

const {Operation} = CloudCmd;
Expand Down Expand Up @@ -301,9 +308,9 @@ function KeyProto() {

event.preventDefault();

const attr = Info.panel.getAttribute('data-name');
dataName = Info.panel.getAttribute('data-name');

if (attr === 'js-right')
if (dataName === 'js-right')
DOM.duplicatePanel();

break;
Expand All @@ -314,9 +321,9 @@ function KeyProto() {

event.preventDefault();

name = Info.panel.getAttribute('data-name');
dataName = Info.panel.getAttribute('data-name');

if (name === 'js-left')
if (dataName === 'js-left')
DOM.duplicatePanel();

break;
Expand Down
9 changes: 9 additions & 0 deletions client/key/key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
BACKSPACE : 8,
TAB : 9,
Expand All @@ -20,13 +22,20 @@ module.exports = {

ZERO : 48,

SEMICOLON : 52,

COLON : 54,

A : 65,

C : 67,
D : 68,

G : 71,

J : 74,
K : 75,

M : 77,

O : 79,
Expand Down
3 changes: 1 addition & 2 deletions client/key/set-current-by-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

const Info = DOM.CurrentInfo;

const {escapeRegExp} = require('../../common/util');

module.exports = function setCurrentByChar(char, charStore) {
Expand Down Expand Up @@ -56,5 +55,5 @@ module.exports = function setCurrentByChar(char, charStore) {
DOM.setCurrentFile(firstByName);
charStore([char]);
}
}
};

157 changes: 157 additions & 0 deletions client/key/vim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
'use strict';
/* global CloudCmd, DOM */

const Info = DOM.CurrentInfo;
const KEY = require('./key');

const fullstore = require('fullstore/legacy');
const store = fullstore('');
const visual = fullstore(false);

const stopVisual = () => {
visual(false);
};

const end = () => {
store('');
};

const rmFirst = (a) => {
return a
.split('')
.slice(1)
.join('');
};

module.exports = (key, event) => {
const current = Info.element;
const keyCode = event.keyCode;
const prevStore = store();

const value = store(prevStore.concat(key));

if (keyCode === KEY.ENTER)
return end();

if (keyCode === KEY.ESC) {
DOM.unselectFiles();
visual(false);
return end();
}

if (key === 'j') {
move('next', {
prevStore,
current,
});

return end();
}

if (key === 'k') {
move('previous', {
prevStore,
current,
});

return end();
}

if (/gg/.test(value)) {
move('previous', {
current,
prevStore,
max: Infinity,
});

return end();
}

if (key === 'd' && (visual() || prevStore === 'd')) {
CloudCmd.Operation.show('delete');
stopVisual();
return end();
}

if (key === 'G') {
move('next', {
current,
prevStore,
max: Infinity,
});

return end();
}

if (/v/i.test(key)) {
DOM.toggleSelectedFile(current);
visual(!visual());

return end();
}
};

module.exports.selectFile = selectFile;

function move(sibling, {max, current, prevStore}) {
const isDelete = prevStore[0] === 'd';

if (isDelete) {
visual(true);
prevStore = rmFirst(prevStore);
}

const n = max || getNumber(prevStore);

if (isNaN(n))
return;

setCurrent({
n,
current,
sibling,
visual: visual(),
});

if (isDelete)
CloudCmd.Operation.show('delete');
}

function getNumber(value) {
if (!value)
return 1;

if (value === 'g')
return 1;

return parseInt(value);
}

function selectFile(current) {
const name = DOM.getCurrentName(current);

if (name === '..')
return;

DOM.selectFile(current);
}

function setCurrent({n, current, visual, sibling}) {
const select = visual ? selectFile : DOM.unselectFile;

select(current);

const position = `${sibling}Sibling`;
for (let i = 0; i < n; i++) {
const next = current[position];

if (!next)
break;

current = next;
select(current);
}

DOM.setCurrentFile(current);
}

3 changes: 2 additions & 1 deletion json/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"console": true,
"terminal": false,
"terminalPath": "",
"showConfig": "false"
"showConfig": "false",
"vim": "true"
}

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"fix:js:eslint:client": "redrun eslint:client -- --fix",
"fix:js:eslint:server": "redrun eslint:server -- --fix",
"test": "tape 'test/**/*.js'",
"test:client": "tape 'test/client/**/*.js'",
"spell": "yaspeller .",
"wisdom": "redrun build",
"wisdom:type": "bin/release.js",
Expand Down Expand Up @@ -91,7 +92,8 @@
"watch:server": "nodemon bin/cloudcmd.js",
"watch:lint": "nodemon -w client -w server -w webpack.config.js -x 'redrun lint:js'",
"watch:lint:client": "nodemon -w client -w webpack.config.js -x 'redrun lint:js:eslint:client'",
"watch:test": "nodemon -w server -w test -w common -x \"npm run test\"",
"watch:test": "nodemon -w server -w test -w common -x \"npm test\"",
"watch:test:client": "nodemon -w client -w test/client -x \"npm run test:client\"",
"watch:coverage": "nodemon -w server -w test -w common -x \"npm run coverage\"",
"w:c": "redrun watch:client",
"w:c:d": "redrun watch:client:dev",
Expand Down
Loading

0 comments on commit 6dc1bbe

Please sign in to comment.