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
|
import { injectable } from 'tsyringe';
import * as operations from '../../shared/operations';
import * as parsers from './parsers';
import * as urls from '../../shared/urls';
import TabPresenter from '../presenters/TabPresenter';
import WindowPresenter from '../presenters/WindowPresenter';
import HelpPresenter from '../presenters/HelpPresenter';
import SettingRepository from '../repositories/SettingRepository';
import BookmarkRepository from '../repositories/BookmarkRepository';
import ConsoleClient from '../infrastructures/ConsoleClient';
import ContentMessageClient from '../infrastructures/ContentMessageClient';
import RepeatUseCase from '../usecases/RepeatUseCase';
@injectable()
export default class CommandIndicator {
constructor(
private tabPresenter: TabPresenter,
private windowPresenter: WindowPresenter,
private helpPresenter: HelpPresenter,
private settingRepository: SettingRepository,
private bookmarkRepository: BookmarkRepository,
private consoleClient: ConsoleClient,
private contentMessageClient: ContentMessageClient,
private repeatUseCase: RepeatUseCase,
) {
}
async open(keywords: string): Promise<browser.tabs.Tab> {
let url = await this.urlOrSearch(keywords);
this.repeatUseCase.storeLastOperation({
type: operations.INTERNAL_OPEN_URL,
url,
});
return this.tabPresenter.open(url);
}
async tabopen(keywords: string): Promise<browser.tabs.Tab> {
let url = await this.urlOrSearch(keywords);
this.repeatUseCase.storeLastOperation({
type: operations.INTERNAL_OPEN_URL,
url,
newTab: true,
});
return this.tabPresenter.create(url);
}
async winopen(keywords: string): Promise<browser.windows.Window> {
let url = await this.urlOrSearch(keywords);
this.repeatUseCase.storeLastOperation({
type: operations.INTERNAL_OPEN_URL,
url,
newWindow: true,
});
return this.windowPresenter.create(url);
}
// eslint-disable-next-line max-statements
async buffer(keywords: string): Promise<any> {
if (keywords.length === 0) {
return;
}
if (!isNaN(Number(keywords))) {
let tabs = await this.tabPresenter.getAll();
let index = parseInt(keywords, 10) - 1;
if (index < 0 || tabs.length <= index) {
throw new RangeError(`tab ${index + 1} does not exist`);
}
return this.tabPresenter.select(tabs[index].id as number);
} else if (keywords.trim() === '%') {
// Select current window
return;
} else if (keywords.trim() === '#') {
// Select last selected window
let lastId = await this.tabPresenter.getLastSelectedId();
if (typeof lastId === 'undefined' || lastId === null) {
throw new Error('No last selected tab');
}
return this.tabPresenter.select(lastId);
}
let current = await this.tabPresenter.getCurrent();
let tabs = await this.tabPresenter.getByKeyword(keywords);
if (tabs.length === 0) {
throw new RangeError('No matching buffer for ' + keywords);
}
for (let tab of tabs) {
if (tab.index > current.index) {
return this.tabPresenter.select(tab.id as number);
}
}
return this.tabPresenter.select(tabs[0].id as number);
}
async bdelete(force: boolean, keywords: string): Promise<any> {
let excludePinned = !force;
let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned);
if (tabs.length === 0) {
throw new Error('No matching buffer for ' + keywords);
} else if (tabs.length > 1) {
throw new Error('More than one match for ' + keywords);
}
return this.tabPresenter.remove([tabs[0].id as number]);
}
async bdeletes(force: boolean, keywords: string): Promise<any> {
let excludePinned = !force;
let tabs = await this.tabPresenter.getByKeyword(keywords, excludePinned);
let ids = tabs.map(tab => tab.id as number);
return this.tabPresenter.remove(ids);
}
async quit(): Promise<any> {
let tab = await this.tabPresenter.getCurrent();
return this.tabPresenter.remove([tab.id as number]);
}
async quitAll(): Promise<any> {
let tabs = await this.tabPresenter.getAll();
let ids = tabs.map(tab => tab.id as number);
this.tabPresenter.remove(ids);
}
async addbookmark(title: string): Promise<any> {
let tab = await this.tabPresenter.getCurrent();
let item = await this.bookmarkRepository.create(title, tab.url as string);
let message = 'Saved current page: ' + item.url;
return this.consoleClient.showInfo(tab.id as number, message);
}
async set(keywords: string): Promise<any> {
if (keywords.length === 0) {
return;
}
let [name, value] = parsers.parseSetOption(keywords);
await this.settingRepository.setProperty(name, value);
return this.contentMessageClient.broadcastSettingsChanged();
}
help(): Promise<void> {
return this.helpPresenter.open();
}
private async urlOrSearch(keywords: string): Promise<any> {
let settings = await this.settingRepository.get();
return urls.searchUrl(keywords, settings.search);
}
}
|