aboutsummaryrefslogtreecommitdiff
path: root/src/console/components/console.js
blob: d09e252ec1b83cd37298c110555248d780a22393 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import messages from 'shared/messages';
import * as consoleActions from 'console/actions/console';

export default class ConsoleComponent {
  constructor(wrapper, store) {
    this.wrapper = wrapper;
    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('input', this.onInput.bind(this));

    this.hideCommand();
    this.hideMessage();

    store.subscribe(() => {
      this.update();
    });
  }

  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;
    }
  }

  onInput(e) {
    let doc = this.wrapper.ownerDocument;
    let input = doc.querySelector('#vimvixen-console-command-input');
    this.completionOrigin = input.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.mode !== 'command' && state.mode === 'command') {
      this.showCommand(state.consoleText);
    } else if (this.prevState.mode !== 'find' && state.mode === 'find') {
      this.showFind();
    } else if (state.mode !== 'command' && state.mode !== 'find') {
      this.hideCommand();
    }

    if (state.mode === 'error' || state.mode === 'info') {
      this.showMessage(state.mode, state.messageText);
    } else {
      this.hideMessage();
    }

    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) {
    this.showConsole('command', text);
  }

  showFind() {
    this.showConsole('find', '');
  }

  showConsole(mode, initial) {
    let doc = this.wrapper.ownerDocument;
    let command = doc.querySelector('#vimvixen-console-command');
    let input = doc.querySelector('#vimvixen-console-command-input');
    let promptEle = doc.querySelector('.vimvixen-console-command-prompt');

    command.style.display = 'block';
    input.value = initial;
    input.focus();
    promptEle.className = `vimvixen-console-command-prompt prompt-${mode}`;

    window.focus();
    this.onInput({ target: input });
  }

  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;
  }

  showMessage(mode, text) {
    let doc = this.wrapper.ownerDocument;
    let error = doc.querySelector('#vimvixen-console-message');
    error.classList.remove(
      'vimvixen-console-info',
      'vimvixen-console-error'
    );
    error.classList.add('vimvixen-console-' + mode);
    error.textContent = text;
    error.style.display = 'block';
  }

  hideMessage() {
    let doc = this.wrapper.ownerDocument;
    let error = doc.querySelector('#vimvixen-console-message');
    error.style.display = 'none';
  }
}