aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/completions.js
blob: 2bf5b3b14774f0850278dd55e0d178fc869b3375 (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
import CompletionItem from '../domains/completion-item';
import CompletionGroup from '../domains/completion-group';
import Completions from '../domains/completions';
import CommandDocs from '../domains/command-docs';
import CompletionRepository from '../repositories/completions';
import * as filters from './filters';
import SettingRepository from '../repositories/setting';
import * as properties from '../../shared/settings/properties';

const COMPLETION_ITEM_LIMIT = 10;

export default class CompletionsInteractor {
  constructor() {
    this.completionRepository = new CompletionRepository();
    this.settingRepository = new SettingRepository();
  }

  queryConsoleCommand(prefix) {
    let keys = Object.keys(CommandDocs);
    let items = keys
      .filter(name => name.startsWith(prefix))
      .map(name => ({
        caption: name,
        content: name,
        url: CommandDocs[name],
      }));

    if (items.length === 0) {
      return Promise.resolve(Completions.empty());
    }
    return Promise.resolve(
      new Completions([new CompletionGroup('Console Command', items)])
    );
  }

  async queryOpen(name, keywords) {
    let groups = [];
    let engines = await this.querySearchEngineItems(name, keywords);
    if (engines.length > 0) {
      groups.push(new CompletionGroup('Search Engines', engines));
    }
    let histories = await this.queryHistoryItems(name, keywords);
    if (histories.length > 0) {
      groups.push(new CompletionGroup('History', histories));
    }
    let bookmarks = await this.queryBookmarkItems(name, keywords);
    if (bookmarks.length > 0) {
      groups.push(new CompletionGroup('Bookmarks', bookmarks));
    }
    return new Completions(groups);
  }

  queryBuffer(name, keywords) {
    return this.queryTabs(name, false, keywords);
  }

  queryBdelete(name, keywords) {
    return this.queryTabs(name, true, keywords);
  }

  queryBdeleteForce(name, keywords) {
    return this.queryTabs(name, false, keywords);
  }

  querySet(name, keywords) {
    let items = Object.keys(properties.docs).map((key) => {
      if (properties.types[key] === 'boolean') {
        return [
          new CompletionItem({
            caption: key,
            content: name + ' ' + key,
            url: 'Enable ' + properties.docs[key],
          }),
          new CompletionItem({
            caption: 'no' + key,
            content: name + ' no' + key,
            url: 'Disable ' + properties.docs[key],
          }),
        ];
      }
      return [
        new CompletionItem({
          caption: key,
          content: name + ' ' + key,
          url: 'Set ' + properties.docs[key],
        })
      ];
    });
    items = items.reduce((acc, val) => acc.concat(val), []);
    items = items.filter((item) => {
      return item.caption.startsWith(keywords);
    });
    if (items.length === 0) {
      return Promise.resolve(Completions.empty());
    }
    return Promise.resolve(
      new Completions([new CompletionGroup('Properties', items)])
    );
  }

  async queryTabs(name, excludePinned, args) {
    let tabs = await this.completionRepository.queryTabs(args, excludePinned);
    let items = tabs.map(tab => new CompletionItem({
      caption: tab.title,
      content: name + ' ' + tab.title,
      url: tab.url,
      icon: tab.favIconUrl
    }));
    if (items.length === 0) {
      return Promise.resolve(Completions.empty());
    }
    return new Completions([new CompletionGroup('Buffers', items)]);
  }

  async querySearchEngineItems(name, keywords) {
    let settings = await this.settingRepository.get();
    let engines = Object.keys(settings.search.engines)
      .filter(key => key.startsWith(keywords));
    return engines.map(key => new CompletionItem({
      caption: key,
      content: name + ' ' + key,
    }));
  }

  async queryHistoryItems(name, keywords) {
    let histories = await this.completionRepository.queryHistories(keywords);
    histories = [histories]
      .map(filters.filterBlankTitle)
      .map(filters.filterHttp)
      .map(filters.filterByTailingSlash)
      .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT))
      .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0]
      .sort((x, y) => x.visitCount < y.visitCount)
      .slice(0, COMPLETION_ITEM_LIMIT);
    return histories.map(page => new CompletionItem({
      caption: page.title,
      content: name + ' ' + page.url,
      url: page.url
    }));
  }

  async queryBookmarkItems(name, keywords) {
    let bookmarks = await this.completionRepository.queryBookmarks(keywords);
    return bookmarks.map(page => new CompletionItem({
      caption: page.title,
      content: name + ' ' + page.url,
      url: page.url
    }));
  }
}