diff options
| author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-08 15:19:25 +0900 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-08 15:19:25 +0900 | 
| commit | 0183161145d36cbafb7dbd86ca3a1aac6faca43f (patch) | |
| tree | aed757bfb5f8789156439d1e1fdff4e221376aaa /src/console/components | |
| parent | 0f54a203dba38acdd080a928cee95f875fe84706 (diff) | |
| parent | 57f798044d32ba7f9dc10a34ac31ad5dbdbf56ae (diff) | |
Merge pull request #22 from ueokande/separate-domains
Refactor: Separate domains
Diffstat (limited to 'src/console/components')
| -rw-r--r-- | src/console/components/completion.js | 61 | ||||
| -rw-r--r-- | src/console/components/console.js | 148 | 
2 files changed, 209 insertions, 0 deletions
| diff --git a/src/console/components/completion.js b/src/console/components/completion.js new file mode 100644 index 0000000..5033b5c --- /dev/null +++ b/src/console/components/completion.js @@ -0,0 +1,61 @@ +export default class Completion { +  constructor(wrapper, store) { +    this.wrapper = wrapper; +    this.store = store; +    this.prevState = {}; +  } + +  update() { +    let state = this.store.getState(); +    if (JSON.stringify(this.prevState) === JSON.stringify(state)) { +      return; +    } + +    this.wrapper.innerHTML = ''; + +    for (let i = 0; i < state.completions.length; ++i) { +      let group = state.completions[i]; +      let title = this.createCompletionTitle(group.name); +      this.wrapper.append(title); + +      for (let j = 0; j < group.items.length; ++j) { +        let item = group.items[j]; +        let li = this.createCompletionItem(item.icon, item.caption, item.url); +        this.wrapper.append(li); + +        if (i === state.groupSelection && j === state.itemSelection) { +          li.classList.add('vimvixen-completion-selected'); +        } +      } +    } + +    this.prevState = state; +  } + +  createCompletionTitle(text) { +    let doc = this.wrapper.ownerDocument; +    let li = doc.createElement('li'); +    li.className = 'vimvixen-console-completion-title'; +    li.textContent = text; +    return li; +  } + +  createCompletionItem(icon, caption, url) { +    let doc = this.wrapper.ownerDocument; + +    let captionEle = doc.createElement('span'); +    captionEle.className = 'vimvixen-console-completion-item-caption'; +    captionEle.textContent = caption; + +    let urlEle = doc.createElement('span'); +    urlEle.className = 'vimvixen-console-completion-item-url'; +    urlEle.textContent = url; + +    let li = doc.createElement('li'); +    li.style.backgroundImage = 'url(' + icon + ')'; +    li.className = 'vimvixen-console-completion-item'; +    li.append(captionEle); +    li.append(urlEle); +    return li; +  } +} diff --git a/src/console/components/console.js b/src/console/components/console.js new file mode 100644 index 0000000..9023d91 --- /dev/null +++ b/src/console/components/console.js @@ -0,0 +1,148 @@ +import messages from 'shared/messages'; +import * as consoleActions from 'console/actions/console'; + +export default class ConsoleComponent { +  constructor(wrapper, store) { +    this.wrapper = wrapper; +    this.prevValue = ''; +    this.prevState = {}; +    this.completionOrigin = ''; +    this.store = store; + +    let doc = this.wrapper.ownerDocument; +    let input = doc.querySelector('#vimvixen-console-command-input'); +    input.addEventListener('blur', this.onBlur.bind(this)); +    input.addEventListener('keydown', this.onKeyDown.bind(this)); +    input.addEventListener('keyup', this.onKeyUp.bind(this)); + +    this.hideCommand(); +    this.hideError(); +  } + +  onBlur() { +    return browser.runtime.sendMessage({ +      type: messages.CONSOLE_BLURRED, +    }); +  } + +  onKeyDown(e) { +    let doc = this.wrapper.ownerDocument; +    let input = doc.querySelector('#vimvixen-console-command-input'); + +    switch (e.keyCode) { +    case KeyboardEvent.DOM_VK_ESCAPE: +      return input.blur(); +    case KeyboardEvent.DOM_VK_RETURN: +      return browser.runtime.sendMessage({ +        type: messages.CONSOLE_ENTERED, +        text: e.target.value +      }).then(this.onBlur); +    case KeyboardEvent.DOM_VK_TAB: +      if (e.shiftKey) { +        this.store.dispatch(consoleActions.completionPrev()); +      } else { +        this.store.dispatch(consoleActions.completionNext()); +      } +      e.stopPropagation(); +      e.preventDefault(); +      break; +    } +  } + +  onKeyUp(e) { +    if (e.keyCode === KeyboardEvent.DOM_VK_TAB) { +      return; +    } +    if (e.target.value === this.prevValue) { +      return; +    } + +    let doc = this.wrapper.ownerDocument; +    let input = doc.querySelector('#vimvixen-console-command-input'); +    this.completionOrigin = input.value; + +    this.prevValue = e.target.value; +    return browser.runtime.sendMessage({ +      type: messages.CONSOLE_QUERY_COMPLETIONS, +      text: e.target.value +    }).then((completions) => { +      this.store.dispatch(consoleActions.setCompletions(completions)); +    }); +  } + +  update() { +    let state = this.store.getState(); +    if (!this.prevState.commandShown && state.commandShown) { +      this.showCommand(state.commandText); +    } else if (!state.commandShown) { +      this.hideCommand(); +    } + +    if (state.errorShown) { +      this.setErrorText(state.errorText); +      this.showError(); +    } else { +      this.hideError(); +    } + +    if (state.groupSelection >= 0 && state.itemSelection >= 0) { +      let group = state.completions[state.groupSelection]; +      let item = group.items[state.itemSelection]; +      this.setCommandValue(item.content); +    } else if (state.completions.length > 0 && +      JSON.stringify(this.prevState.completions) === +        JSON.stringify(state.completions)) { +      // Reset input only completion groups not changed (unselected an item in +      // completion) in order to avoid to override previous input +      this.setCommandCompletionOrigin(); +    } + +    this.prevState = state; +  } + +  showCommand(text) { +    let doc = this.wrapper.ownerDocument; +    let command = doc.querySelector('#vimvixen-console-command'); +    let input = doc.querySelector('#vimvixen-console-command-input'); + +    command.style.display = 'block'; +    input.value = text; +    input.focus(); +  } + +  hideCommand() { +    let doc = this.wrapper.ownerDocument; +    let command = doc.querySelector('#vimvixen-console-command'); +    command.style.display = 'none'; +  } + +  setCommandValue(value) { +    let doc = this.wrapper.ownerDocument; +    let input = doc.querySelector('#vimvixen-console-command-input'); +    input.value = value; +  } + +  setCommandCompletionOrigin() { +    let doc = this.wrapper.ownerDocument; +    let input = doc.querySelector('#vimvixen-console-command-input'); +    input.value = this.completionOrigin; +  } + +  setErrorText(text) { +    let doc = this.wrapper.ownerDocument; +    let error = doc.querySelector('#vimvixen-console-error'); +    error.textContent = text; +  } + +  showError() { +    let doc = this.wrapper.ownerDocument; +    let error = doc.querySelector('#vimvixen-console-error'); +    error.style.display = 'block'; +  } + +  hideError() { +    let doc = this.wrapper.ownerDocument; +    let error = doc.querySelector('#vimvixen-console-error'); +    error.style.display = 'none'; +  } +} | 
