Merge branch 'buffer'

jh-changes
Shin'ya Ueoka 7 years ago
commit 8cabd68b92
  1. 3
      manifest.json
  2. 2
      src/background/commands.js
  3. 72
      src/background/index.js
  4. 1
      src/background/key-queue.js
  5. 49
      src/background/tabs.js
  6. 6
      src/background/zooms.js
  7. 10
      src/console/console-frame.js
  8. 14
      src/console/console.js
  9. 34
      src/content/index.js
  10. 2
      src/shared/actions.js

@ -15,7 +15,8 @@
] ]
}, },
"permissions": [ "permissions": [
"sessions" "sessions",
"tabs"
], ],
"web_accessible_resources": [ "web_accessible_resources": [
"build/console.html" "build/console.html"

@ -1,2 +0,0 @@
export const OPEN = 'open';
export const TABOPEN = 'tabopen';

@ -1,54 +1,50 @@
import * as actions from '../shared/actions'; import * as actions from '../shared/actions';
import * as tabs from './tabs'; import * as tabs from './tabs';
import * as commands from './commands';
import * as zooms from './zooms'; import * as zooms from './zooms';
import KeyQueue from './key-queue'; import KeyQueue from './key-queue';
const queue = new KeyQueue(); const queue = new KeyQueue();
const keyPressHandle = (request, sender, sendResponse) => { const keyPressHandle = (request, sender) => {
let action = queue.push({ let action = queue.push({
code: request.code, code: request.code,
ctrl: request.ctrl ctrl: request.ctrl
}); });
if (!action) { if (!action) {
return; return Promise.resolve();
} }
if (actions.isBackgroundAction(action[0])) { if (actions.isBackgroundAction(action[0])) {
doBackgroundAction(sender, action); return doBackgroundAction(sender, action);
} else if (actions.isContentAction(action[0])) { } else if (actions.isContentAction(action[0])) {
sendResponse(action); return Promise.resolve({
type: 'response.action',
action: action
});
} }
return Promise.resolve();
}; };
const doBackgroundAction = (sender, action) => { const doBackgroundAction = (sender, action) => {
switch(action[0]) { switch(action[0]) {
case actions.TABS_CLOSE: case actions.TABS_CLOSE:
tabs.closeTab(sender.tab.id); return tabs.closeTab(sender.tab.id);
break;
case actions.TABS_REOPEN: case actions.TABS_REOPEN:
tabs.reopenTab(); return tabs.reopenTab();
break;
case actions.TABS_PREV: case actions.TABS_PREV:
tabs.selectPrevTab(sender.tab.index, actions[1] || 1); return tabs.selectPrevTab(sender.tab.index, actions[1] || 1);
break;
case actions.TABS_NEXT: case actions.TABS_NEXT:
tabs.selectNextTab(sender.tab.index, actions[1] || 1); return tabs.selectNextTab(sender.tab.index, actions[1] || 1);
break;
case actions.TABS_RELOAD: case actions.TABS_RELOAD:
tabs.reload(sender.tab, actions[1] || false); return tabs.reload(sender.tab, actions[1] || false);
break;
case actions.ZOOM_IN: case actions.ZOOM_IN:
zooms.zoomIn(); return zooms.zoomIn();
break;
case actions.ZOOM_OUT: case actions.ZOOM_OUT:
zooms.zoomOut(); return zooms.zoomOut();
break;
case actions.ZOOM_NEUTRAL: case actions.ZOOM_NEUTRAL:
zooms.neutral(); return zooms.neutral();
break;
} }
return Promise.resolve();
} }
const normalizeUrl = (string) => { const normalizeUrl = (string) => {
@ -59,28 +55,38 @@ const normalizeUrl = (string) => {
} }
} }
const cmdBuffer = (arg) => {
if (isNaN(arg)) {
return tabs.selectByKeyword(arg);
} else {
let index = parseInt(arg, 10) - 1;
return tabs.selectAt(index);
}
}
const cmdEnterHandle = (request, sender) => { const cmdEnterHandle = (request, sender) => {
let words = request.text.split(' ').filter((s) => s.length > 0); let words = request.text.split(' ').filter((s) => s.length > 0);
switch (words[0]) { switch (words[0]) {
case commands.OPEN: case 'open':
browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) }); return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
return; case 'tabopen':
case commands.TABOPEN: return browser.tabs.create({ url: normalizeUrl(words[1]) });
browser.tabs.create({ url: normalizeUrl(words[1]) }); case 'b':
return; case 'buffer':
return cmdBuffer(words[1]);
} }
throw new Error(words[0] + ' command is not defined');
}; };
browser.runtime.onMessage.addListener((request, sender, sendResponse) => { browser.runtime.onMessage.addListener((request, sender) => {
switch (request.type) { switch (request.type) {
case 'event.keypress': case 'event.keypress':
keyPressHandle(request, sender, sendResponse); return keyPressHandle(request, sender);
break;
case 'event.cmd.enter': case 'event.cmd.enter':
cmdEnterHandle(request, sender, sendResponse); return cmdEnterHandle(request, sender);
break;
case 'event.cmd.suggest': case 'event.cmd.suggest':
// TODO make suggestion and return via sendResponse // TODO make suggestion and return
break; break;
} }
return Promise.resolve();
}); });

@ -4,6 +4,7 @@ const DEFAULT_KEYMAP = {
':': [ actions.CMD_OPEN ], ':': [ actions.CMD_OPEN ],
'o': [ actions.CMD_TABS_OPEN, false ], 'o': [ actions.CMD_TABS_OPEN, false ],
'O': [ actions.CMD_TABS_OPEN, true ], 'O': [ actions.CMD_TABS_OPEN, true ],
'b': [ actions.CMD_BUFFER ],
'k': [ actions.SCROLL_LINES, -1 ], 'k': [ actions.SCROLL_LINES, -1 ],
'j': [ actions.SCROLL_LINES, 1 ], 'j': [ actions.SCROLL_LINES, 1 ],
'<C-E>': [ actions.SCROLL_LINES, -1 ], '<C-E>': [ actions.SCROLL_LINES, -1 ],

@ -1,9 +1,9 @@
const closeTab = (id) => { const closeTab = (id) => {
browser.tabs.remove(id); return browser.tabs.remove(id);
}; };
const reopenTab = () => { const reopenTab = () => {
browser.sessions.getRecentlyClosed({ return browser.sessions.getRecentlyClosed({
maxResults: 1 maxResults: 1
}).then((sessions) => { }).then((sessions) => {
if (sessions.length === 0) { if (sessions.length === 0) {
@ -11,40 +11,69 @@ const reopenTab = () => {
} }
let session = sessions[0]; let session = sessions[0];
if (session.tab) { if (session.tab) {
browser.sessions.restore(session.tab.sessionId); return browser.sessions.restore(session.tab.sessionId);
} else { } else {
browser.sessions.restore(session.window.sessionId); return browser.sessions.restore(session.window.sessionId);
} }
}); });
}; };
const selectAt = (index) => {
return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) {
return;
}
if (index < 0 || tabs.length <= index) {
throw new RangeError(`tab ${index} does not exist`)
}
let id = tabs[index].id;
return browser.tabs.update(id, { active: true })
});
};
const selectByKeyword = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword)
})
if (matched.length == 0) {
throw new RangeError('No matching buffer for ' + keyword);
} else if (matched.length >= 2) {
throw new RangeError('More than one match for ' + keyword);
}
return browser.tabs.update(matched[0].id, { active: true });
});
}
const selectPrevTab = (current, count) => { const selectPrevTab = (current, count) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => { return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) { if (tabs.length < 2) {
return; return;
} }
let select = (current - count) % tabs.length let select = (current - count) % tabs.length
let id = tabs[select].id; let id = tabs[select].id;
chrome.tabs.update(id, { active: true }) return browser.tabs.update(id, { active: true })
}); });
}; };
const selectNextTab = (current, count) => { const selectNextTab = (current, count) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => { return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) { if (tabs.length < 2) {
return; return;
} }
let select = (current + count + tabs.length) % tabs.length let select = (current + count + tabs.length) % tabs.length
let id = tabs[select].id; let id = tabs[select].id;
chrome.tabs.update(id, { active: true }) return browser.tabs.update(id, { active: true })
}); });
}; };
const reload = (current, cache) => { const reload = (current, cache) => {
browser.tabs.reload( return browser.tabs.reload(
current.id, current.id,
{ bypassCache: cache } { bypassCache: cache }
); );
}; };
export { closeTab, reopenTab, selectNextTab, selectPrevTab, reload }; export { closeTab, reopenTab, selectAt, selectByKeyword, selectNextTab, selectPrevTab, reload };

@ -10,7 +10,7 @@ const ZOOM_SETTINGS = [
]; ];
const zoomIn = (tabId = undefined) => { const zoomIn = (tabId = undefined) => {
browser.tabs.getZoom(tabId).then((factor) => { return browser.tabs.getZoom(tabId).then((factor) => {
for (let f of ZOOM_SETTINGS) { for (let f of ZOOM_SETTINGS) {
if (f > factor) { if (f > factor) {
browser.tabs.setZoom(tabId, f); browser.tabs.setZoom(tabId, f);
@ -21,7 +21,7 @@ const zoomIn = (tabId = undefined) => {
}; };
const zoomOut = (tabId = undefined) => { const zoomOut = (tabId = undefined) => {
browser.tabs.getZoom(tabId).then((factor) => { return browser.tabs.getZoom(tabId).then((factor) => {
for (let f of [].concat(ZOOM_SETTINGS).reverse()) { for (let f of [].concat(ZOOM_SETTINGS).reverse()) {
if (f < factor) { if (f < factor) {
browser.tabs.setZoom(tabId, f); browser.tabs.setZoom(tabId, f);
@ -32,7 +32,7 @@ const zoomOut = (tabId = undefined) => {
}; };
const neutral = (tabId = undefined) => { const neutral = (tabId = undefined) => {
browser.tabs.setZoom(tabId, 1); return browser.tabs.setZoom(tabId, 1);
}; };
export { zoomIn, zoomOut, neutral }; export { zoomIn, zoomOut, neutral };

@ -10,6 +10,8 @@ export default class ConsoleFrame {
this.element = element; this.element = element;
this.errorShown = true;
this.hide(); this.hide();
} }
@ -21,6 +23,7 @@ export default class ConsoleFrame {
text: text text: text
}; };
messages.send(this.element.contentWindow, message); messages.send(this.element.contentWindow, message);
this.errorShown = false;
} }
showError(text) { showError(text) {
@ -31,6 +34,8 @@ export default class ConsoleFrame {
text: text text: text
}; };
messages.send(this.element.contentWindow, message); messages.send(this.element.contentWindow, message);
this.errorShown = true;
this.element.blur();
} }
showFrame() { showFrame() {
@ -40,5 +45,10 @@ export default class ConsoleFrame {
hide() { hide() {
this.element.style.display = 'none'; this.element.style.display = 'none';
this.element.blur(); this.element.blur();
this.errorShown = false;
}
isErrorShown() {
return this.element.style.display === 'block' && this.errorShown;
} }
} }

@ -8,20 +8,20 @@ var prevValue = "";
const blurMessage = () => { const blurMessage = () => {
return { return {
type: 'vimvixen.commandline.blur' type: 'vimvixen.command.blur'
}; };
}; };
const keydownMessage = (input) => { const keydownMessage = (input) => {
return { return {
type: 'vimvixen.commandline.enter', type: 'vimvixen.command.enter',
value: input.value value: input.value
}; };
}; };
const keyupMessage = (input) => { const keyupMessage = (input) => {
return { return {
type: 'vimvixen.commandline.change', type: 'vimvixen.command.change',
value: input.value value: input.value
}; };
}; };
@ -57,15 +57,15 @@ window.addEventListener('load', () => {
}); });
const showCommand = (text) => { const showCommand = (text) => {
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = text;
input.focus();
let command = window.document.querySelector('#vimvixen-console-command'); let command = window.document.querySelector('#vimvixen-console-command');
command.style.display = 'block'; command.style.display = 'block';
let error = window.document.querySelector('#vimvixen-console-error'); let error = window.document.querySelector('#vimvixen-console-error');
error.style.display = 'none'; error.style.display = 'none';
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = text;
input.focus();
} }
const showError = (text) => { const showError = (text) => {

@ -7,7 +7,7 @@ import Follow from './follow';
let vvConsole = new ConsoleFrame(window); let vvConsole = new ConsoleFrame(window);
const invokeEvent = (action) => { const doAction = (action) => {
if (typeof action === 'undefined' || action === null) { if (typeof action === 'undefined' || action === null) {
return; return;
} }
@ -24,6 +24,9 @@ const invokeEvent = (action) => {
vvConsole.showCommand('open '); vvConsole.showCommand('open ');
} }
break; break;
case actions.CMD_BUFFER:
vvConsole.showCommand('buffer ');
break;
case actions.SCROLL_LINES: case actions.SCROLL_LINES:
scrolls.scrollLines(window, action[1]); scrolls.scrollLines(window, action[1]);
break; break;
@ -54,6 +57,18 @@ const invokeEvent = (action) => {
} }
} }
const handleResponse = (response) => {
if (!response) {
return;
}
switch(response.type) {
case 'response.action':
doAction(response.action);
break;
}
};
window.addEventListener("keypress", (e) => { window.addEventListener("keypress", (e) => {
if (e.target instanceof HTMLInputElement) { if (e.target instanceof HTMLInputElement) {
return; return;
@ -66,27 +81,34 @@ window.addEventListener("keypress", (e) => {
} }
browser.runtime.sendMessage(request) browser.runtime.sendMessage(request)
.then(invokeEvent, .then(handleResponse)
(err) => { .catch((err) => {
vvConsole.showError(err.message);
console.log(`Vim Vixen: ${err}`); console.log(`Vim Vixen: ${err}`);
}); });
}); });
messages.receive(window, (message) => { messages.receive(window, (message) => {
switch (message.type) { switch (message.type) {
case 'vimvixen.commandline.blur': case 'vimvixen.command.blur':
if (!vvConsole.isErrorShown()) {
vvConsole.hide(); vvConsole.hide();
}
break; break;
case 'vimvixen.commandline.enter': case 'vimvixen.command.enter':
browser.runtime.sendMessage({ browser.runtime.sendMessage({
type: 'event.cmd.enter', type: 'event.cmd.enter',
text: message.value text: message.value
}).catch((e) => {
vvConsole.showError(e.message);
}); });
break; break;
case 'vimvixen.commandline.change': case 'vimvixen.command.change':
browser.runtime.sendMessage({ browser.runtime.sendMessage({
type: 'event.cmd.suggest', type: 'event.cmd.suggest',
text: message.value text: message.value
}).catch((e) => {
vvConsole.showError(e.message);
}); });
break; break;
default: default:

@ -1,5 +1,6 @@
export const CMD_OPEN = 'cmd.open'; export const CMD_OPEN = 'cmd.open';
export const CMD_TABS_OPEN = 'cmd.tabs.open'; export const CMD_TABS_OPEN = 'cmd.tabs.open';
export const CMD_BUFFER = 'cmd.buffer';
export const TABS_CLOSE = 'tabs.close'; export const TABS_CLOSE = 'tabs.close';
export const TABS_REOPEN = 'tabs.reopen'; export const TABS_REOPEN = 'tabs.reopen';
export const TABS_PREV = 'tabs.prev'; export const TABS_PREV = 'tabs.prev';
@ -32,6 +33,7 @@ const BACKGROUND_ACTION_SET = new Set([
const CONTENT_ACTION_SET = new Set([ const CONTENT_ACTION_SET = new Set([
CMD_OPEN, CMD_OPEN,
CMD_TABS_OPEN, CMD_TABS_OPEN,
CMD_BUFFER,
SCROLL_LINES, SCROLL_LINES,
SCROLL_PAGES, SCROLL_PAGES,
SCROLL_TOP, SCROLL_TOP,