diff options
Diffstat (limited to 'src/console')
-rw-r--r-- | src/console/console-frame.js | 21 | ||||
-rw-r--r-- | src/console/console.js | 106 |
2 files changed, 50 insertions, 77 deletions
diff --git a/src/console/console-frame.js b/src/console/console-frame.js index f07d8ab..8b4c17a 100644 --- a/src/console/console-frame.js +++ b/src/console/console-frame.js @@ -1,4 +1,5 @@ import './console-frame.scss'; +import * as consoleActions from '../actions/console'; export default class ConsoleFrame { constructor(win) { @@ -16,26 +17,17 @@ export default class ConsoleFrame { showCommand(text) { this.showFrame(); - - let message = { - type: 'vimvixen.console.show.command', - text: text - }; this.errorShown = false; - return browser.runtime.sendMessage(message); + return browser.runtime.sendMessage(consoleActions.showCommand(text)); } showError(text) { this.showFrame(); - let message = { - type: 'vimvixen.console.show.error', - text: text - }; this.errorShown = true; this.element.blur(); - return browser.runtime.sendMessage(message); + return browser.runtime.sendMessage(consoleActions.showError(text)); } showFrame() { @@ -46,6 +38,8 @@ export default class ConsoleFrame { this.element.style.display = 'none'; this.element.blur(); this.errorShown = false; + + return browser.runtime.sendMessage(consoleActions.hide()); } isErrorShown() { @@ -53,9 +47,6 @@ export default class ConsoleFrame { } setCompletions(completions) { - return browser.runtime.sendMessage({ - type: 'vimvixen.console.set.completions', - completions: completions - }); + return browser.runtime.sendMessage(consoleActions.setCompletions(completions)); } } diff --git a/src/console/console.js b/src/console/console.js index 006f640..044aa5f 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,10 +1,12 @@ import './console.scss'; import Completion from './completion'; +import consoleReducer, { defaultState } from '../reducers/console'; // TODO consider object-oriented var prevValue = ""; var completion = null; var completionOrigin = ""; +let state = defaultState; const blurMessage = () => { return { @@ -96,38 +98,6 @@ window.addEventListener('load', () => { input.addEventListener('keyup', handleKeyup); }); -const showCommand = (text) => { - let command = window.document.querySelector('#vimvixen-console-command'); - command.style.display = 'block'; - - let error = window.document.querySelector('#vimvixen-console-error'); - error.style.display = 'none'; - - let input = window.document.querySelector('#vimvixen-console-command-input'); - input.value = text; - input.focus(); - - completion = null; - let container = window.document.querySelector('#vimvixen-console-completion'); - container.innerHTML = ''; - - return browser.runtime.sendMessage(keyupMessage(input)); -} - -const showError = (text) => { - let error = window.document.querySelector('#vimvixen-console-error'); - error.textContent = text; - error.style.display = 'block'; - - let command = window.document.querySelector('#vimvixen-console-command'); - command.style.display = 'none'; - - let completion = window.document.querySelector('#vimvixen-console-completion'); - completion.style.display = 'none'; - - return Promise.resolve(); -} - const createCompletionTitle = (text) => { let li = document.createElement('li'); li.className = 'vimvixen-console-completion-title'; @@ -152,56 +122,68 @@ const createCompletionItem = (icon, caption, url) => { return li; } -const setCompletions = (completions) => { +const selectCompletion = (target) => { let container = window.document.querySelector('#vimvixen-console-completion'); - container.style.display = 'block'; - container.innerHTML = ''; + Array.prototype.forEach.call(container.children, (ele) => { + if (!ele.classList.contains('vimvixen-console-completion-item')) { + return; + } + if (ele === target) { + ele.classList.add('vimvixen-completion-selected'); + } else { + ele.classList.remove('vimvixen-completion-selected'); + } + }); +}; + +const updateCompletions = (completions) => { + let completionsContainer = window.document.querySelector('#vimvixen-console-completion'); + let input = window.document.querySelector('#vimvixen-console-command-input'); + + completionsContainer.innerHTML = ''; let pairs = []; for (let group of completions) { let title = createCompletionTitle(group.name); - container.append(title); + completionsContainer.append(title); for (let item of group.items) { let li = createCompletionItem(item.icon, item.caption, item.url); - container.append(li); + completionsContainer.append(li); pairs.push([item, li]); } } completion = new Completion(pairs); + completionOrigin = input.value.split(' ')[0]; +} +const update = (prevState, state) => { + let error = window.document.querySelector('#vimvixen-console-error'); + let command = window.document.querySelector('#vimvixen-console-command'); let input = window.document.querySelector('#vimvixen-console-command-input'); - completionOrigin = input.value.split(' ')[0]; - return Promise.resolve(); -} + error.style.display = state.errorShown ? 'block' : 'none'; + error.textContent = state.errorText; -const selectCompletion = (target) => { - let container = window.document.querySelector('#vimvixen-console-completion'); - Array.prototype.forEach.call(container.children, (ele) => { - if (!ele.classList.contains('vimvixen-console-completion-item')) { - return; - } - if (ele === target) { - ele.classList.add('vimvixen-completion-selected'); - } else { - ele.classList.remove('vimvixen-completion-selected'); - } - }); -}; + command.style.display = state.commandShown ? 'block' : 'none'; + if (!prevState.commandShown && state.commandShown) { + // setup input on firstly shown + input.value = state.commandText; + input.focus(); + } + + if (JSON.stringify(state.completions) !== JSON.stringify(prevState.completions)) { + updateCompletions(state.completions); + } +} browser.runtime.onMessage.addListener((action) => { - switch (action.type) { - case 'vimvixen.console.show.error': - return showError(action.text); - case 'vimvixen.console.set.completions': - return setCompletions(action.completions); - case 'vimvixen.console.show.command': - return showCommand(action.text); - default: - return Promise.resolve(); + let nextState = consoleReducer(state, action); + if (JSON.stringify(nextState) !== JSON.stringify(state)) { + update(state, nextState); + state = nextState; } }); |