Skip to content

Commit

Permalink
chore: 喵喵輸入法 support move candidate cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
jessy1092 committed Jul 12, 2016
1 parent f9b1136 commit 8bd691e
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions example/meow/index.js
Expand Up @@ -43,6 +43,11 @@ function respOnKeyDown(request, state) {
return response;
}

if (state['action'] === 'UPDATE_CANDIDATE') {
response['candidateCursor'] = state['candidateCursor'];
return response;
}

if (state['action'] === 'SELECT_CANDIDATE') {
response['compositionString'] = state['compositionString'];
response['showCandidates'] = state['showCandidates'];
Expand Down Expand Up @@ -70,20 +75,54 @@ function reduceOnKeyDown(request, preState) {
action = '',
compositionString = '',
compositionCursor = 0,
showCandidates = false
showCandidates = false,
candidateCursor = 0
} = preState;

let {keyCode} = request;

if (showCandidates) {

if (keyCode === KEYCODE.VK_UP || keyCode === KEYCODE.VK_ESCAPE) {
if (keyCode === KEYCODE.VK_ESCAPE) {
return Object.assign({}, preState, {
action: 'SHOW_CANDIDATES',
showCandidates: false
});
}

if (keyCode === KEYCODE.VK_DOWN) {
candidateCursor = (candidateCursor + 3) % 4;
return Object.assign({}, preState, {
action: 'UPDATE_CANDIDATE',
candidateCursor
});
}

if (keyCode === KEYCODE.VK_UP) {
candidateCursor = candidateCursor < 3 ? 0 : candidateCursor - 3;
return Object.assign({}, preState, {
action: 'UPDATE_CANDIDATE',
candidateCursor
});
}

if (keyCode === KEYCODE.VK_RIGHT) {
candidateCursor = (candidateCursor + 1) % 4;
return Object.assign({}, preState, {
action: 'UPDATE_CANDIDATE',
candidateCursor
});
}

if (keyCode === KEYCODE.VK_LEFT) {
candidateCursor = candidateCursor == 0 ? 0 : candidateCursor - 1;
return Object.assign({}, preState, {
action: 'UPDATE_CANDIDATE',
candidateCursor
});
}

} else if (keyCode >= '1'.charCodeAt(0) && keyCode <= '4'.charCodeAt(0)) {
if (keyCode >= '1'.charCodeAt(0) && keyCode <= '4'.charCodeAt(0)) {

let selectCandidate = candidateList[keyCode - '1'.charCodeAt(0)];
let cursor = compositionCursor - 1;
Expand All @@ -101,14 +140,33 @@ function reduceOnKeyDown(request, preState) {
});
}

if (keyCode === KEYCODE.VK_RETURN) {

let selectCandidate = candidateList[candidateCursor];
let cursor = compositionCursor - 1;

if (cursor < 0) {
cursor = 0;
}

compositionString = compositionString.substring(0, cursor) + selectCandidate + compositionString.substring(cursor + 1);

return Object.assign({}, preState, {
action: 'SELECT_CANDIDATE',
showCandidates: false,
compositionString
});
}

return Object.assign({}, preState, {action: ''});

} else {

if (keyCode === KEYCODE.VK_DOWN) { // Show Candidate List
return Object.assign({}, preState, {
action: 'SHOW_CANDIDATES',
showCandidates: true
showCandidates: true,
candidateCursor: 0
});
}

Expand Down Expand Up @@ -181,7 +239,8 @@ module.exports = {
action: '',
compositionString: '',
compositionCursor: 0,
showCandidates: false
showCandidates: false,
candidateCursor: 0
});
}

Expand Down

0 comments on commit 8bd691e

Please sign in to comment.