diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-16 23:32:19 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-16 23:32:19 +0900 |
commit | c5529958d53146c8c6826673abe6431a19f1924d (patch) | |
tree | 4952c7ac1ded91d52fd6e424c229022b61b67aa3 /src/actions | |
parent | b2cddcd69b4ae06770d66808624fc43f3dcbcb0e (diff) | |
parent | ae394e28c0cbc8710d4937238c97328afddbca0f (diff) |
Merge branch 'more-redux'
Diffstat (limited to 'src/actions')
-rw-r--r-- | src/actions/background.js | 11 | ||||
-rw-r--r-- | src/actions/command.js | 81 | ||||
-rw-r--r-- | src/actions/index.js | 29 | ||||
-rw-r--r-- | src/actions/operation.js | 43 |
4 files changed, 108 insertions, 56 deletions
diff --git a/src/actions/background.js b/src/actions/background.js deleted file mode 100644 index 40b901b..0000000 --- a/src/actions/background.js +++ /dev/null @@ -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 - }; -} diff --git a/src/actions/command.js b/src/actions/command.js index c983278..03f1e83 100644 --- a/src/actions/command.js +++ b/src/actions/command.js @@ -1,4 +1,5 @@ -import actions from '../actions'; +import * as tabs from '../background/tabs'; +import * as consoleActions from './console'; const normalizeUrl = (string) => { try { @@ -8,28 +9,76 @@ const normalizeUrl = (string) => { } } -export function exec(line) { - let name = line.split(' ')[0]; - let remaining = line.replace(name + ' ', ''); +const openCommand = (url) => { + return browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + if (tabs.length > 0) { + return browser.tabs.update(tabs[0].id, { url: url }); + } + }); +} +const tabopenCommand = (url) => { + return browser.tabs.create({ url: url }); +} + +const bufferCommand = (keywords) => { + return browser.tabs.query({ active: true, currentWindow: true }).then((tabss) => { + if (tabss.length > 0) { + if (isNaN(keywords)) { + return tabs.selectByKeyword(tabss[0], keywords); + } else { + let index = parseInt(keywords, 10) - 1; + return tabs.selectAt(index); + } + } + }); +} + +const doCommand = (name, remaining) => { switch (name) { case 'open': // TODO use search engined and pass keywords to them - return { - type: actions.COMMAND_OPEN_URL, - url: normalizeUrl(remaining) - }; + return openCommand(normalizeUrl(remaining)); case 'tabopen': - return { - type: actions.COMMAND_TABOPEN_URL, - url: normalizeUrl(remaining) - }; + return tabopenCommand(normalizeUrl(remaining)); case 'b': case 'buffer': - return { - type: actions.COMMAND_BUFFER, - keywords: remaining - }; + return bufferCommand(remaining); } throw new Error(name + ' command is not defined'); } + +const getCompletions = (command, keywords) => { + switch (command) { + case '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 + } + }); + return [{ + name: "Buffers", + items: items + }]; + }); + } + return Promise.resolve([]); +}; + +export function exec(line) { + let name = line.split(' ')[0]; + let remaining = line.replace(name + ' ', ''); + return doCommand(name, remaining).then(() => { + return consoleActions.hide(); + }); +} + +export function complete(line) { + let command = line.split(' ', 1)[0]; + let keywords = line.replace(command + ' ', ''); + return getCompletions(command, keywords).then(consoleActions.setCompletions); +} diff --git a/src/actions/index.js b/src/actions/index.js index 63d5f6f..977b3c2 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -5,36 +5,7 @@ export default { CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', CONSOLE_HIDE: 'vimvixen.console.hide', - // Background commands - BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions', - 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', - - // content commands - CMD_OPEN: 'cmd.open', - CMD_TABS_OPEN: 'cmd.tabs.open', - CMD_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', - // User input INPUT_KEY_PRESS: 'input.key,press', INPUT_CLEAR_KEYS: 'input.clear.keys', - - COMMAND_OPEN_URL: 'command.open.url', - COMMAND_TABOPEN_URL: 'command.tabopen.url', - COMMAND_BUFFER: 'command.buffer', }; diff --git a/src/actions/operation.js b/src/actions/operation.js new file mode 100644 index 0000000..e589b89 --- /dev/null +++ b/src/actions/operation.js @@ -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 + }); + } +} + |