parent
6127fdc285
commit
83cb277ba2
17 changed files with 171 additions and 236 deletions
@ -1,11 +0,0 @@ |
||||
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 |
||||
}; |
||||
} |
@ -1,55 +1,81 @@ |
||||
import * as keys from './keys'; |
||||
import * as inputActions from '../actions/input'; |
||||
import * as operationActions from '../actions/operation'; |
||||
import backgroundReducers from '../reducers/background'; |
||||
import * as commandActions from '../actions/command'; |
||||
import * as consoleActions from '../actions/console'; |
||||
import reducers from '../reducers'; |
||||
import commandReducer from '../reducers/command'; |
||||
import messages from '../messages'; |
||||
import * as store from '../store' |
||||
|
||||
let prevInput = []; |
||||
const backgroundStore = store.createStore(reducers, (e) => { |
||||
console.error('Vim-Vixen:', e); |
||||
}); |
||||
backgroundStore.subscribe(() => { |
||||
let currentInput = backgroundStore.getState().input |
||||
if (JSON.stringify(prevInput) === JSON.stringify(currentInput)) { |
||||
return |
||||
} |
||||
prevInput = currentInput; |
||||
|
||||
if (currentInput.keys.length === 0) { |
||||
return; |
||||
} |
||||
|
||||
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { |
||||
if (tabs.length > 0) { |
||||
return keyQueueChanged(tabs[0], backgroundStore.getState()); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
const keyQueueChanged = (sendToTab, state) => { |
||||
if (state.input.keys.length === 0) { |
||||
return Promise.resolve(); |
||||
backgroundStore.subscribe(() => { |
||||
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { |
||||
if (tabs.length > 0) { |
||||
return browser.tabs.sendMessage(tabs[0].id, { |
||||
type: 'state.changed', |
||||
state: backgroundStore.getState() |
||||
}); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
const keyQueueChanged = (sendToTab, state) => { |
||||
let prefix = keys.asKeymapChars(state.input.keys); |
||||
let matched = Object.keys(keys.defaultKeymap).filter((keys) => { |
||||
return keys.startsWith(prefix); |
||||
}); |
||||
if (matched.length == 0) { |
||||
return handleMessage(inputActions.clearKeys(), sendToTab); |
||||
backgroundStore.dispatch(inputActions.clearKeys()); |
||||
return Promise.resolve(); |
||||
} else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) { |
||||
return Promise.resolve(); |
||||
} |
||||
let action = keys.defaultKeymap[matched]; |
||||
backgroundStore.dispatch(operationActions.exec(action, sendToTab)); |
||||
return handleMessage(inputActions.clearKeys(), sendToTab).then(() => { |
||||
return backgroundReducers(undefined, action, sendToTab).then(() => { |
||||
backgroundStore.dispatch(inputActions.clearKeys()); |
||||
return browser.tabs.sendMessage(sendToTab.id, action); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
const handleMessage = (action, sendToTab) => { |
||||
backgroundStore.dispatch(action); |
||||
|
||||
return backgroundReducers(undefined, action, sendToTab).then(() => { |
||||
return commandReducer(undefined, action, sendToTab).then(() => { |
||||
return browser.tabs.sendMessage(sendToTab.id, action); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
browser.runtime.onMessage.addListener((action, sender) => { |
||||
handleMessage(action, sender.tab); |
||||
}); |
||||
|
||||
browser.runtime.onMessage.addListener((message) => { |
||||
switch (message.type) { |
||||
case messages.CONSOLE_BLURRED: |
||||
backgroundStore.dispatch(consoleActions.hide()); |
||||
break; |
||||
case messages.CONSOLE_ENTERED: |
||||
backgroundStore.dispatch(commandActions.exec(message.text)); |
||||
break; |
||||
case messages.CONSOLE_CHANGEED: |
||||
backgroundStore.dispatch(commandActions.complete(message.text)); |
||||
break; |
||||
} |
||||
}); |
||||
|
@ -0,0 +1,5 @@ |
||||
export default { |
||||
CONSOLE_BLURRED: 'console.blured', |
||||
CONSOLE_ENTERED: 'console.entered', |
||||
CONSOLE_CHANGEED: 'console.changed' |
||||
}; |
@ -1,36 +0,0 @@ |
||||
import * as tabs from '../background/tabs'; |
||||
import * as consoleActions from '../actions/console'; |
||||
import actions from '../actions'; |
||||
|
||||
const doCompletion = (command, keywords, tabId) => { |
||||
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( |
||||
tabId, |
||||
consoleActions.setCompletions([completions])); |
||||
}); |
||||
} |
||||
return Promise.resolve(); |
||||
}; |
||||
|
||||
export default function reducer(state, action = {}, sendToTab) { |
||||
// TODO hide sender object
|
||||
switch (action.type) { |
||||
case actions.BACKGROUND_REQUEST_COMPLETIONS: |
||||
return doCompletion(action.command, action.keywords, sendToTab.id); |
||||
default: |
||||
return Promise.resolve(); |
||||
} |
||||
} |
@ -1,24 +0,0 @@ |
||||
import * as tabs from '../background/tabs'; |
||||
import actions from '../actions'; |
||||
|
||||
const cmdBuffer = (tab, arg) => { |
||||
if (isNaN(arg)) { |
||||
return tabs.selectByKeyword(tab, arg); |
||||
} else { |
||||
let index = parseInt(arg, 10) - 1; |
||||
return tabs.selectAt(index); |
||||
} |
||||
} |
||||
|
||||
export default function reducer(state, action, sendToTab) { |
||||
switch (action.type) { |
||||
case actions.COMMAND_OPEN_URL: |
||||
return browser.tabs.update(sendToTab.id, { url: action.url }); |
||||
case actions.COMMAND_TABOPEN_URL: |
||||
return browser.tabs.create({ url: action.url }); |
||||
case actions.COMMAND_BUFFER: |
||||
return cmdBuffer(sendToTab, action.keywords); |
||||
default: |
||||
return Promise.resolve(); |
||||
} |
||||
} |
@ -1,18 +0,0 @@ |
||||
import * as consoleFrames from '../console/frames'; |
||||
import actions from '../actions'; |
||||
|
||||
export default function reducer(state, action = {}) { |
||||
switch (action.type) { |
||||
case actions.CMD_OPEN: |
||||
return consoleFrames.showCommand(''); |
||||
case actions.CMD_TABS_OPEN: |
||||
if (action.alter) { |
||||
// alter url
|
||||
return consoleFrames.showCommand('open ' + window.location.href); |
||||
} else { |
||||
return consoleFrames.showCommand('open '); |
||||
} |
||||
case actions.CMD_BUFFER: |
||||
return consoleFrames.showCommand('buffer '); |
||||
} |
||||
} |
@ -1,11 +1,14 @@ |
||||
import inputReducer from '../reducers/input'; |
||||
import consoleReducer from '../reducers/console'; |
||||
|
||||
const defaultState = { |
||||
input: inputReducer(undefined, {}) |
||||
input: inputReducer(undefined, {}), |
||||
console: consoleReducer(undefined, {}) |
||||
}; |
||||
|
||||
export default function reducer(state = defaultState, action = {}) { |
||||
return Object.assign({}, state, { |
||||
input: inputReducer(state.input, action) |
||||
input: inputReducer(state.input, action), |
||||
console: consoleReducer(state.console, action) |
||||
}); |
||||
} |
||||
|
@ -1,14 +0,0 @@ |
||||
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'); |
||||
}); |
||||
}); |
||||
}); |
@ -1,51 +0,0 @@ |
||||
import { expect } from "chai"; |
||||
import actions from '../../src/actions'; |
||||
import * as commandActions from '../../src/actions/command'; |
||||
|
||||
describe("command actions", () => { |
||||
describe("exec", () => { |
||||
context("open command", () => { |
||||
it('create COMMAND_OPEN_URL acion with a full url', () => { |
||||
let action = commandActions.exec("open https://github.com/") |
||||
expect(action.type).to.equal(actions.COMMAND_OPEN_URL); |
||||
expect(action.url).to.equal('https://github.com/'); |
||||
}); |
||||
|
||||
it('create COMMAND_OPEN_URL acion with a domain name', () => { |
||||
let action = commandActions.exec("open github.com") |
||||
expect(action.type).to.equal(actions.COMMAND_OPEN_URL); |
||||
expect(action.url).to.equal('http://github.com'); |
||||
}); |
||||
}); |
||||
|
||||
context("tabopen command", () => { |
||||
it('create COMMAND_TABOPEN_URL acion with a full url', () => { |
||||
let action = commandActions.exec("tabopen https://github.com/") |
||||
expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); |
||||
expect(action.url).to.equal('https://github.com/'); |
||||
}); |
||||
|
||||
it('create COMMAND_TABOPEN_URL acion with a domain name', () => { |
||||
let action = commandActions.exec("tabopen github.com") |
||||
expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); |
||||
expect(action.url).to.equal('http://github.com'); |
||||
}); |
||||
}); |
||||
|
||||
context("buffer command", () => { |
||||
it('create COMMAND_BUFFER acion with a keywords', () => { |
||||
let action = commandActions.exec("buffer foo bar") |
||||
expect(action.type).to.equal(actions.COMMAND_BUFFER); |
||||
expect(action.keywords).to.equal('foo bar'); |
||||
}); |
||||
}); |
||||
|
||||
context("b command", () => { |
||||
it('create COMMAND_BUFFER acion with a keywords', () => { |
||||
let action = commandActions.exec("b foo bar") |
||||
expect(action.type).to.equal(actions.COMMAND_BUFFER); |
||||
expect(action.keywords).to.equal('foo bar'); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
Reference in new issue