completion as action/reducer
This commit is contained in:
parent
7e35d11f65
commit
3c67cc0a00
8 changed files with 75 additions and 47 deletions
11
src/actions/background.js
Normal file
11
src/actions/background.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import actions from '../actions';
|
||||||
|
|
||||||
|
export function requestCompletions(line) {
|
||||||
|
let command = line.split(' ', 1)[0];
|
||||||
|
let keywords = line.replace(command + ' ', '');
|
||||||
|
return {
|
||||||
|
type: actions.BACKGROUND_REQUEST_COMPLETIONS,
|
||||||
|
command,
|
||||||
|
keywords
|
||||||
|
};
|
||||||
|
}
|
|
@ -2,5 +2,7 @@ export default {
|
||||||
CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command',
|
CONSOLE_SHOW_COMMAND: 'vimvixen.console.show.command',
|
||||||
CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions',
|
CONSOLE_SET_COMPLETIONS: 'vimvixen.console.set.completions',
|
||||||
CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error',
|
CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error',
|
||||||
CONSOLE_HIDE: 'vimvixen.console.hide'
|
CONSOLE_HIDE: 'vimvixen.console.hide',
|
||||||
|
|
||||||
|
BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions'
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,6 +2,7 @@ import * as actions from '../shared/actions';
|
||||||
import * as tabs from './tabs';
|
import * as tabs from './tabs';
|
||||||
import * as zooms from './zooms';
|
import * as zooms from './zooms';
|
||||||
import KeyQueue from './key-queue';
|
import KeyQueue from './key-queue';
|
||||||
|
import backgroundReducers from '../reducers/background';
|
||||||
|
|
||||||
const queue = new KeyQueue();
|
const queue = new KeyQueue();
|
||||||
|
|
||||||
|
@ -81,22 +82,11 @@ browser.runtime.onMessage.addListener((request, sender) => {
|
||||||
return keyPressHandle(request, sender);
|
return keyPressHandle(request, sender);
|
||||||
case 'event.cmd.enter':
|
case 'event.cmd.enter':
|
||||||
return cmdEnterHandle(request, sender);
|
return cmdEnterHandle(request, sender);
|
||||||
case 'event.cmd.tabs.completion':
|
|
||||||
return tabs.getCompletions(request.text).then((tabs) => {
|
|
||||||
let items = tabs.map((tab) => {
|
|
||||||
return {
|
|
||||||
caption: tab.title,
|
|
||||||
content: tab.title,
|
|
||||||
url: tab.url,
|
|
||||||
icon: tab.favIconUrl
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
name: "Buffers",
|
|
||||||
items: items
|
|
||||||
};
|
|
||||||
});
|
|
||||||
default:
|
default:
|
||||||
return browser.tabs.sendMessage(sender.tab.id, request);
|
return browser.tabs.sendMessage(sender.tab.id, request);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
browser.runtime.onMessage.addListener((action, sender) => {
|
||||||
|
return backgroundReducers(undefined, action, sender.tab.id);
|
||||||
|
});
|
||||||
|
|
|
@ -45,8 +45,4 @@ export default class ConsoleFrame {
|
||||||
isErrorShown() {
|
isErrorShown() {
|
||||||
return this.element.style.display === 'block' && this.errorShown;
|
return this.element.style.display === 'block' && this.errorShown;
|
||||||
}
|
}
|
||||||
|
|
||||||
setCompletions(completions) {
|
|
||||||
return browser.runtime.sendMessage(consoleActions.setCompletions(completions));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import './console.scss';
|
import './console.scss';
|
||||||
|
import * as backgroundActions from '../actions/background';
|
||||||
import Completion from './completion';
|
import Completion from './completion';
|
||||||
import consoleReducer from '../reducers/console';
|
import consoleReducer from '../reducers/console';
|
||||||
|
|
||||||
|
@ -21,13 +22,6 @@ const keydownMessage = (input) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const keyupMessage = (input) => {
|
|
||||||
return {
|
|
||||||
type: 'vimvixen.command.change',
|
|
||||||
value: input.value
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleBlur = () => {
|
const handleBlur = () => {
|
||||||
return browser.runtime.sendMessage(blurMessage());
|
return browser.runtime.sendMessage(blurMessage());
|
||||||
};
|
};
|
||||||
|
@ -88,7 +82,9 @@ const handleKeyup = (e) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
prevValue = e.target.value;
|
prevValue = e.target.value;
|
||||||
return browser.runtime.sendMessage(keyupMessage(e.target));
|
return browser.runtime.sendMessage(
|
||||||
|
backgroundActions.requestCompletions(e.target.value)
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
|
|
|
@ -68,23 +68,6 @@ window.addEventListener("keypress", (e) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const doCompletion = (line) => {
|
|
||||||
if (line.startsWith('buffer ')) {
|
|
||||||
let keyword = line.replace('buffer ', '');
|
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
|
||||||
type: 'event.cmd.tabs.completion',
|
|
||||||
text: keyword
|
|
||||||
}).then((completions) => {
|
|
||||||
vvConsole.setCompletions([completions]);
|
|
||||||
}).catch((err) => {
|
|
||||||
console.error("Vim Vixen:", err);
|
|
||||||
vvConsole.showError(err.message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return Promise.resolve();
|
|
||||||
};
|
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener((action) => {
|
browser.runtime.onMessage.addListener((action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'vimvixen.command.blur':
|
case 'vimvixen.command.blur':
|
||||||
|
@ -100,8 +83,6 @@ browser.runtime.onMessage.addListener((action) => {
|
||||||
console.error("Vim Vixen:", err);
|
console.error("Vim Vixen:", err);
|
||||||
vvConsole.showError(err.message);
|
vvConsole.showError(err.message);
|
||||||
});
|
});
|
||||||
case 'vimvixen.command.change':
|
|
||||||
return doCompletion(action.value);
|
|
||||||
default:
|
default:
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
38
src/reducers/background.js
Normal file
38
src/reducers/background.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import * as tabs from '../background/tabs';
|
||||||
|
import * as consoleActions from '../actions/console';
|
||||||
|
import actions from '../actions';
|
||||||
|
|
||||||
|
const doCompletion = (command, keywords, sendto) => {
|
||||||
|
if (command === 'buffer') {
|
||||||
|
return tabs.getCompletions(keywords).then((tabs) => {
|
||||||
|
let items = tabs.map((tab) => {
|
||||||
|
return {
|
||||||
|
caption: tab.title,
|
||||||
|
content: tab.title,
|
||||||
|
url: tab.url,
|
||||||
|
icon: tab.favIconUrl
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let completions = {
|
||||||
|
name: "Buffers",
|
||||||
|
items: items
|
||||||
|
};
|
||||||
|
return browser.tabs.sendMessage(
|
||||||
|
sendto,
|
||||||
|
consoleActions.setCompletions([completions]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export default function reducer(state, action = {}, sendto) {
|
||||||
|
// TODO hide sendto object
|
||||||
|
switch (action.type) {
|
||||||
|
case actions.BACKGROUND_REQUEST_COMPLETIONS:
|
||||||
|
return doCompletion(action.command, action.keywords, sendto);
|
||||||
|
default:
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
14
test/actions/background.test.js
Normal file
14
test/actions/background.test.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { expect } from "chai";
|
||||||
|
import actions from '../../src/actions';
|
||||||
|
import * as backgroundActions from '../../src/actions/background';
|
||||||
|
|
||||||
|
describe("background actions", () => {
|
||||||
|
describe("requestCompletions", () => {
|
||||||
|
it('create BACKGROUND_REQUEST_COMPLETIONS action', () => {
|
||||||
|
let action = backgroundActions.requestCompletions('buffer hoge fuga');
|
||||||
|
expect(action.type).to.equal(actions.BACKGROUND_REQUEST_COMPLETIONS);
|
||||||
|
expect(action.command).to.equal('buffer');
|
||||||
|
expect(action.keywords).to.equal('hoge fuga');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue