aboutsummaryrefslogtreecommitdiff
path: root/src/actions/command.js
blob: 03f1e83e833088eee37557aa395c5735f5dc6b87 (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
import * as tabs from '../background/tabs';
import * as consoleActions from './console';

const normalizeUrl = (string) => {
  try {
    return new URL(string).href
  } catch (e) {
    return 'http://' + string;
  }
}

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 openCommand(normalizeUrl(remaining));
  case 'tabopen':
    return tabopenCommand(normalizeUrl(remaining));
  case 'b':
  case 'buffer':
    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);
}