commit
c5529958d5
20 changed files with 481 additions and 358 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 |
||||
}; |
||||
} |
@ -0,0 +1,43 @@ |
||||
import operations from '../operations'; |
||||
import messages from '../messages'; |
||||
import * as consoleActions from './console'; |
||||
import * as tabs from '../background/tabs'; |
||||
import * as zooms from '../background/zooms'; |
||||
|
||||
export function exec(operation, tab) { |
||||
switch (operation.type) { |
||||
case operations.TABS_CLOSE: |
||||
return tabs.closeTab(tab.id); |
||||
case operations.TABS_REOPEN: |
||||
return tabs.reopenTab(); |
||||
case operations.TABS_PREV: |
||||
return tabs.selectPrevTab(tab.index, operation.count); |
||||
case operations.TABS_NEXT: |
||||
return tabs.selectNextTab(tab.index, operation.count); |
||||
case operations.TABS_RELOAD: |
||||
return tabs.reload(tab, operation.cache); |
||||
case operations.ZOOM_IN: |
||||
return zooms.zoomIn(); |
||||
case operations.ZOOM_OUT: |
||||
return zooms.zoomOut(); |
||||
case operations.ZOOM_NEUTRAL: |
||||
return zooms.neutral(); |
||||
case operations.COMMAND_OPEN: |
||||
return consoleActions.showCommand(''); |
||||
case operations.COMMAND_TABS_OPEN: |
||||
if (operations.alter) { |
||||
// alter url
|
||||
return consoleActions.showCommand('open ' + tab.url); |
||||
} else { |
||||
return consoleActions.showCommand('open '); |
||||
} |
||||
case operations.COMMAND_BUFFER: |
||||
return consoleActions.showCommand('buffer '); |
||||
default: |
||||
return browser.tabs.sendMessage(tab.id, { |
||||
type: messages.CONTENT_OPERATION, |
||||
operation |
||||
}); |
||||
} |
||||
} |
||||
|
@ -1,45 +1,75 @@ |
||||
import * as keys from './keys'; |
||||
import * as inputActions from '../actions/input'; |
||||
import backgroundReducers from '../reducers/background'; |
||||
import commandReducer from '../reducers/command'; |
||||
import inputReducers from '../reducers/input'; |
||||
import * as operationActions from '../actions/operation'; |
||||
import * as commandActions from '../actions/command'; |
||||
import * as consoleActions from '../actions/console'; |
||||
import reducers from '../reducers'; |
||||
import messages from '../messages'; |
||||
import * as store from '../store' |
||||
|
||||
let inputState = inputReducers(undefined, {}); |
||||
let prevInput = []; |
||||
const backgroundStore = store.createStore(reducers, (e, sender) => { |
||||
console.error('Vim-Vixen:', e); |
||||
if (sender) { |
||||
backgroundStore.dispatch(consoleActions.showError(e.message), sender); |
||||
} |
||||
}); |
||||
backgroundStore.subscribe((sender) => { |
||||
let currentInput = backgroundStore.getState().input |
||||
if (JSON.stringify(prevInput) === JSON.stringify(currentInput)) { |
||||
return |
||||
} |
||||
prevInput = currentInput; |
||||
|
||||
const keyQueueChanged = (sender, prevState, state) => { |
||||
if (state.keys.length === 0) { |
||||
return Promise.resolve(); |
||||
if (currentInput.keys.length === 0) { |
||||
return; |
||||
} |
||||
if (sender) { |
||||
return keyQueueChanged(backgroundStore.getState(), sender); |
||||
} |
||||
}); |
||||
backgroundStore.subscribe((sender) => { |
||||
if (sender) { |
||||
return browser.tabs.sendMessage(sender.tab.id, { |
||||
type: messages.STATE_UPDATE, |
||||
state: backgroundStore.getState() |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
let prefix = keys.asKeymapChars(state.keys); |
||||
const keyQueueChanged = (state, sender) => { |
||||
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(), sender); |
||||
backgroundStore.dispatch(inputActions.clearKeys(), sender); |
||||
return Promise.resolve(); |
||||
} else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) { |
||||
return Promise.resolve(); |
||||
} |
||||
let action = keys.defaultKeymap[matched]; |
||||
return handleMessage(inputActions.clearKeys(), sender).then(() => { |
||||
return backgroundReducers(undefined, action, sender).then(() => { |
||||
return browser.tabs.sendMessage(sender.tab.id, action); |
||||
}); |
||||
}); |
||||
backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender); |
||||
backgroundStore.dispatch(inputActions.clearKeys(), sender); |
||||
}; |
||||
|
||||
const handleMessage = (action, sender) => { |
||||
let nextInputState = inputReducers(inputState, action); |
||||
if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) { |
||||
let prevState = inputState; |
||||
inputState = nextInputState; |
||||
return keyQueueChanged(sender, prevState, inputState); |
||||
} |
||||
return backgroundReducers(undefined, action, sender).then(() => { |
||||
return commandReducer(undefined, action, sender).then(() => { |
||||
return browser.tabs.sendMessage(sender.tab.id, action); |
||||
}); |
||||
}); |
||||
}; |
||||
const handleMessage = (message, sender) => { |
||||
switch (message.type) { |
||||
case messages.KEYDOWN: |
||||
return backgroundStore.dispatch(inputActions.keyPress(message.code, message.ctrl), sender); |
||||
case messages.CONSOLE_BLURRED: |
||||
return backgroundStore.dispatch(consoleActions.hide(), sender); |
||||
case messages.CONSOLE_ENTERED: |
||||
return backgroundStore.dispatch(commandActions.exec(message.text), sender); |
||||
case messages.CONSOLE_CHANGEED: |
||||
return backgroundStore.dispatch(commandActions.complete(message.text), sender); |
||||
} |
||||
} |
||||
|
||||
browser.runtime.onMessage.addListener(handleMessage); |
||||
browser.runtime.onMessage.addListener((message, sender) => { |
||||
try { |
||||
handleMessage(message, sender); |
||||
} catch (e) { |
||||
backgroundStore.dispatch(consoleActions.showError(e.message), sender); |
||||
} |
||||
}); |
||||
|
@ -0,0 +1,10 @@ |
||||
export default { |
||||
STATE_UPDATE: 'state.update', |
||||
CONTENT_OPERATION: 'content.operation', |
||||
|
||||
CONSOLE_BLURRED: 'console.blured', |
||||
CONSOLE_ENTERED: 'console.entered', |
||||
CONSOLE_CHANGEED: 'console.changed', |
||||
|
||||
KEYDOWN: 'keydown' |
||||
}; |
@ -0,0 +1,26 @@ |
||||
export default { |
||||
// command
|
||||
COMMAND_OPEN: 'cmd.open', |
||||
COMMAND_TABS_OPEN: 'cmd.tabs.open', |
||||
COMMAND_BUFFER: 'cmd.buffer', |
||||
|
||||
SCROLL_LINES: 'scroll.lines', |
||||
SCROLL_PAGES: 'scroll.pages', |
||||
SCROLL_TOP: 'scroll.top', |
||||
SCROLL_BOTTOM: 'scroll.bottom', |
||||
SCROLL_LEFT: 'scroll.left', |
||||
SCROLL_RIGHT: 'scroll.right', |
||||
FOLLOW_START: 'follow.start', |
||||
HISTORY_PREV: 'history.prev', |
||||
HISTORY_NEXT: 'history.next', |
||||
|
||||
// background
|
||||
TABS_CLOSE: 'tabs.close', |
||||
TABS_REOPEN: 'tabs.reopen', |
||||
TABS_PREV: 'tabs.prev', |
||||
TABS_NEXT: 'tabs.next', |
||||
TABS_RELOAD: 'tabs.reload', |
||||
ZOOM_IN: 'zoom.in', |
||||
ZOOM_OUT: 'zoom.out', |
||||
ZOOM_NEUTRAL: 'zoom.neutral', |
||||
} |
@ -1,53 +0,0 @@ |
||||
import * as tabs from '../background/tabs'; |
||||
import * as zooms from '../background/zooms'; |
||||
import * as consoleActions from '../actions/console'; |
||||
import actions from '../actions'; |
||||
|
||||
const doCompletion = (command, keywords, sender) => { |
||||
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( |
||||
sender, |
||||
consoleActions.setCompletions([completions])); |
||||
}); |
||||
} |
||||
return Promise.resolve(); |
||||
}; |
||||
|
||||
export default function reducer(state, action = {}, sender) { |
||||
// TODO hide sender object
|
||||
switch (action.type) { |
||||
case actions.BACKGROUND_REQUEST_COMPLETIONS: |
||||
return doCompletion(action.command, action.keywords, sender.tab.id); |
||||
case actions.TABS_CLOSE: |
||||
return tabs.closeTab(sender.tab.id); |
||||
case actions.TABS_REOPEN: |
||||
return tabs.reopenTab(); |
||||
case actions.TABS_PREV: |
||||
return tabs.selectPrevTab(sender.tab.index, action.count); |
||||
case actions.TABS_NEXT: |
||||
return tabs.selectNextTab(sender.tab.index, action.count); |
||||
case actions.TABS_RELOAD: |
||||
return tabs.reload(sender.tab, action.cache); |
||||
case actions.ZOOM_IN: |
||||
return zooms.zoomIn(); |
||||
case actions.ZOOM_OUT: |
||||
return zooms.zoomOut(); |
||||
case actions.ZOOM_NEUTRAL: |
||||
return zooms.neutral(); |
||||
default: |
||||
return Promise.resolve(); |
||||
} |
||||
} |
@ -1,24 +0,0 @@ |
||||
import * as tabs from '../background/tabs'; |
||||
import actions from '../actions'; |
||||
|
||||
const cmdBuffer = (sender, arg) => { |
||||
if (isNaN(arg)) { |
||||
return tabs.selectByKeyword(sender.tab, arg); |
||||
} else { |
||||
let index = parseInt(arg, 10) - 1; |
||||
return tabs.selectAt(index); |
||||
} |
||||
} |
||||
|
||||
export default function reducer(state, action, sender) { |
||||
switch (action.type) { |
||||
case actions.COMMAND_OPEN_URL: |
||||
return browser.tabs.update(sender.tab.id, { url: action.url }); |
||||
case actions.COMMAND_TABOPEN_URL: |
||||
return browser.tabs.create({ url: action.url }); |
||||
case actions.COMMAND_BUFFER: |
||||
return cmdBuffer(sender, action.keywords); |
||||
default: |
||||
return Promise.resolve(); |
||||
} |
||||
} |
@ -1,48 +0,0 @@ |
||||
import * as consoleFrames from '../console/frames'; |
||||
import * as histories from '../content/histories'; |
||||
import * as scrolls from '../content/scrolls'; |
||||
import Follow from '../content/follow'; |
||||
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 '); |
||||
case actions.SCROLL_LINES: |
||||
scrolls.scrollLines(window, action.count); |
||||
break; |
||||
case actions.SCROLL_PAGES: |
||||
scrolls.scrollPages(window, action.count); |
||||
break; |
||||
case actions.SCROLL_TOP: |
||||
scrolls.scrollTop(window); |
||||
break; |
||||
case actions.SCROLL_BOTTOM: |
||||
scrolls.scrollBottom(window); |
||||
break; |
||||
case actions.SCROLL_LEFT: |
||||
scrolls.scrollLeft(window); |
||||
break; |
||||
case actions.SCROLL_RIGHT: |
||||
scrolls.scrollRight(window); |
||||
break; |
||||
case actions.FOLLOW_START: |
||||
new Follow(window.document, action.newTab); |
||||
break; |
||||
case actions.HISTORY_PREV: |
||||
histories.prev(window); |
||||
break; |
||||
case actions.HISTORY_NEXT: |
||||
histories.next(window); |
||||
break; |
||||
} |
||||
} |
@ -0,0 +1,14 @@ |
||||
import inputReducer from '../reducers/input'; |
||||
import consoleReducer from '../reducers/console'; |
||||
|
||||
const defaultState = { |
||||
input: inputReducer(undefined, {}), |
||||
console: consoleReducer(undefined, {}) |
||||
}; |
||||
|
||||
export default function reducer(state = defaultState, action = {}) { |
||||
return Object.assign({}, state, { |
||||
input: inputReducer(state.input, action), |
||||
console: consoleReducer(state.console, action) |
||||
}); |
||||
} |
@ -0,0 +1,51 @@ |
||||
class Store { |
||||
constructor(reducer, catcher) { |
||||
this.reducer = reducer; |
||||
this.catcher = catcher; |
||||
this.subscribers = []; |
||||
try { |
||||
this.state = this.reducer(undefined, {}); |
||||
} catch (e) { |
||||
catcher(e); |
||||
} |
||||
} |
||||
|
||||
dispatch(action, sender) { |
||||
if (action instanceof Promise) { |
||||
action.then((a) => { |
||||
this.transitNext(a, sender); |
||||
}).catch((e) => { |
||||
this.catcher(e, sender); |
||||
}); |
||||
} else { |
||||
try { |
||||
this.transitNext(action, sender); |
||||
} catch (e) { |
||||
this.catcher(e, sender); |
||||
} |
||||
} |
||||
return action |
||||
} |
||||
|
||||
getState() { |
||||
return this.state; |
||||
} |
||||
|
||||
subscribe(callback) { |
||||
this.subscribers.push(callback); |
||||
} |
||||
|
||||
transitNext(action, sender) { |
||||
let newState = this.reducer(this.state, action); |
||||
if (JSON.stringify(this.state) !== JSON.stringify(newState)) { |
||||
this.state = newState; |
||||
this.subscribers.forEach(f => f(sender)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
const empty = () => {}; |
||||
|
||||
export function createStore(reducer, catcher = empty) { |
||||
return new Store(reducer, catcher); |
||||
} |
@ -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'); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,111 @@ |
||||
import { expect } from "chai"; |
||||
import { createStore } from '../../src/store'; |
||||
|
||||
describe("Store class", () => { |
||||
const reducer = (state, action) => { |
||||
if (state == undefined) { |
||||
return 0; |
||||
} |
||||
return state + action; |
||||
}; |
||||
|
||||
describe("#dispatch", () => { |
||||
it('transit status by immediate action', () => { |
||||
let store = createStore(reducer); |
||||
store.dispatch(10); |
||||
expect(store.getState()).to.equal(10); |
||||
|
||||
store.dispatch(-20); |
||||
expect(store.getState()).to.equal(-10); |
||||
}); |
||||
|
||||
it('returns next state by immediate action', () => { |
||||
let store = createStore(reducer); |
||||
let dispatchedAction = store.dispatch(11); |
||||
expect(dispatchedAction).to.equal(11); |
||||
}); |
||||
|
||||
it('transit status by Promise action', () => { |
||||
let store = createStore(reducer); |
||||
let p1 = Promise.resolve(10); |
||||
|
||||
return store.dispatch(p1).then(() => { |
||||
expect(store.getState()).to.equal(10); |
||||
}).then(() => { |
||||
store.dispatch(Promise.resolve(-20)); |
||||
}).then(() => { |
||||
expect(store.getState()).to.equal(-10); |
||||
}); |
||||
}); |
||||
|
||||
it('returns next state by promise action', () => { |
||||
let store = createStore(reducer); |
||||
let dispatchedAction = store.dispatch(Promise.resolve(11)); |
||||
return dispatchedAction.then((value) => { |
||||
expect(value).to.equal(11); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe("#subscribe", () => { |
||||
it('invoke callback', (done) => { |
||||
let store = createStore(reducer); |
||||
store.subscribe(() => { |
||||
expect(store.getState()).to.equal(15); |
||||
done(); |
||||
}); |
||||
store.dispatch(15); |
||||
}); |
||||
|
||||
it('propagate sender object', (done) => { |
||||
let store = createStore(reducer); |
||||
store.subscribe((sender) => { |
||||
expect(sender).to.equal('sender'); |
||||
done(); |
||||
}); |
||||
store.dispatch(15, 'sender'); |
||||
}); |
||||
}) |
||||
|
||||
describe("catcher", () => { |
||||
it('catch an error in reducer on initializing by immediate action', (done) => { |
||||
let store = createStore(() => { |
||||
throw new Error(); |
||||
}, (e) => { |
||||
expect(e).to.be.an('error'); |
||||
done(); |
||||
}); |
||||
}); |
||||
|
||||
it('catch an error in reducer on initializing by immediate action', (done) => { |
||||
let store = createStore((state, action) => { |
||||
if (state === undefined) return 0; |
||||
throw new Error(); |
||||
}, (e) => { |
||||
expect(e).to.be.an('error'); |
||||
done(); |
||||
}); |
||||
store.dispatch(20); |
||||
}); |
||||
|
||||
it('catch an error in reducer on initializing by promise action', (done) => { |
||||
let store = createStore((state, action) => { |
||||
if (state === undefined) return 0; |
||||
throw new Error(); |
||||
}, (e) => { |
||||
expect(e).to.be.an('error'); |
||||
done(); |
||||
}); |
||||
store.dispatch(Promise.resolve(20)); |
||||
}); |
||||
|
||||
it('catch an error in promise action', (done) => { |
||||
let store = createStore((state, action) => 0, (e) => { |
||||
expect(e).to.be.an('error'); |
||||
done(); |
||||
}); |
||||
store.dispatch(new Promise(() => { throw new Error() })); |
||||
}); |
||||
}) |
||||
}); |
||||
|
Reference in new issue