Refactor background directories

This commit is contained in:
Shin'ya Ueoka 2018-05-06 11:45:07 +09:00
parent c5c08783d2
commit 98bc2326ee
9 changed files with 19 additions and 21 deletions

View file

@ -1,5 +1,5 @@
import actions from '../actions';
import * as tabs from 'background/tabs';
import * as tabs from '../shared/tabs';
import * as parsers from 'shared/commands/parsers';
import * as properties from 'shared/settings/properties';

View file

@ -3,7 +3,7 @@ import * as commandActions from 'background/actions/command';
import * as settingActions from 'background/actions/setting';
import * as findActions from 'background/actions/find';
import * as tabActions from 'background/actions/tab';
import * as commands from 'shared/commands';
import * as completions from '../shared/completions';
export default class BackgroundComponent {
constructor(store) {
@ -44,7 +44,7 @@ export default class BackgroundComponent {
case messages.SETTINGS_QUERY:
return Promise.resolve(this.store.getState().setting.value);
case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, settings.value);
return completions.complete(message.text, settings.value);
case messages.SETTINGS_RELOAD:
this.store.dispatch(settingActions.load());
return this.broadcastSettingsChanged();

View file

@ -1,7 +1,7 @@
import messages from 'shared/messages';
import operations from 'shared/operations';
import * as tabs from 'background/tabs';
import * as zooms from 'background/zooms';
import * as tabs from '../shared//tabs';
import * as zooms from '../shared/zooms';
export default class BackgroundComponent {
constructor(store) {

View file

@ -0,0 +1,84 @@
import * as tabs from './tabs';
import * as histories from './histories';
const getOpenCompletions = (command, keywords, searchConfig) => {
return histories.getCompletions(keywords).then((pages) => {
let historyItems = pages.map((page) => {
return {
caption: page.title,
content: command + ' ' + page.url,
url: page.url
};
});
let engineNames = Object.keys(searchConfig.engines);
let engineItems = engineNames.filter(name => name.startsWith(keywords))
.map(name => ({
caption: name,
content: command + ' ' + name
}));
let completions = [];
if (engineItems.length > 0) {
completions.push({
name: 'Search Engines',
items: engineItems
});
}
if (historyItems.length > 0) {
completions.push({
name: 'History',
items: historyItems
});
}
return completions;
});
};
const getCompletions = (line, settings) => {
let typedWords = line.trim().split(/ +/);
let typing = '';
if (!line.endsWith(' ')) {
typing = typedWords.pop();
}
if (typedWords.length === 0) {
return Promise.resolve([]);
}
let name = typedWords.shift();
let keywords = typedWords.concat(typing).join(' ');
switch (name) {
case 'o':
case 'open':
case 't':
case 'tabopen':
case 'w':
case 'winopen':
return getOpenCompletions(name, keywords, settings.search);
case 'b':
case 'buffer':
return tabs.getCompletions(keywords).then((gotTabs) => {
let items = gotTabs.map((tab) => {
return {
caption: tab.title,
content: name + ' ' + tab.title,
url: tab.url,
icon: tab.favIconUrl
};
});
return [
{
name: 'Buffers',
items: items
}
];
});
}
return Promise.resolve([]);
};
const complete = (line, settings) => {
return getCompletions(line, settings);
};
export { complete };

View file

@ -0,0 +1,10 @@
const getCompletions = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title && t.title.includes(keyword);
});
return matched;
});
};
export { getCompletions };

View file

@ -66,15 +66,6 @@ const selectByKeyword = (current, keyword) => {
});
};
const getCompletions = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title && t.title.includes(keyword);
});
return matched;
});
};
const selectPrevTab = (current, count) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
if (tabs.length < 2) {
@ -139,7 +130,7 @@ const duplicate = (id) => {
export {
closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword,
getCompletions, selectPrevTab, selectNextTab, selectFirstTab,
selectPrevTab, selectNextTab, selectFirstTab,
selectLastTab, selectPrevSelTab, reload, updateTabPinned,
toggleTabPinned, duplicate
};