message passing by helper functions

jh-changes
Shin'ya Ueoka 7 years ago
parent 95c095f366
commit adcaf2aa48
  1. 27
      src/console/console.js
  2. 11
      src/content/index.js
  3. 19
      src/shared/messages.js
  4. 25
      test/shared/messages.test.js

@ -1,41 +1,42 @@
import './console.scss';
import * as messages from '../shared/messages';
const parent = window.parent;
// TODO consider object-oriented
var prevValue = "";
const blurData = () => {
return JSON.stringify({
const blurMessage = () => {
return {
type: 'vimvixen.commandline.blur'
});
};
};
const keydownData = (input) => {
return JSON.stringify({
const keydownMessage = (input) => {
return {
type: 'vimvixen.commandline.enter',
value: input.value
});
};
};
const keyupData = (input) => {
return JSON.stringify({
const keyupMessage = (input) => {
return {
type: 'vimvixen.commandline.change',
value: input.value
});
};
};
const handleBlur = () => {
parent.postMessage(blurData(), '*');
messages.send(parent, blurMessage());
};
const handleKeydown = (e) => {
switch(e.keyCode) {
case KeyboardEvent.DOM_VK_ESCAPE:
parent.postMessage(blurData(), '*');
messages.send(parent, blurMessage());
break;
case KeyboardEvent.DOM_VK_RETURN:
parent.postMessage(keydownData(e.target), '*');
messages.send(parent, keydownMessage(e.target));
break;
}
};
@ -44,7 +45,7 @@ const handleKeyup = (e) => {
if (e.target.value === prevValue) {
return;
}
parent.postMessage(keyupData(e.target), '*');
messages.send(parent, keyupMessage(e.target));
prevValue = e.target.value;
};

@ -1,6 +1,7 @@
import * as scrolls from './scrolls';
import * as histories from './histories';
import * as actions from '../shared/actions';
import * as messages from '../shared/messages';
import ConsoleFrame from '../console/console-frame';
import Follow from './follow';
@ -71,15 +72,7 @@ window.addEventListener("keypress", (e) => {
});
});
window.addEventListener('message', (e) => {
let message;
try {
message = JSON.parse(e.data);
} catch (e) {
// ignore message posted by author of web page
return;
}
messages.receive(window, (message) => {
switch (message.type) {
case 'vimvixen.commandline.blur':
if (cmd) {

@ -0,0 +1,19 @@
const receive = (win, callback) => {
win.addEventListener('message', (e) => {
let message;
try {
message = JSON.parse(e.data);
} catch (e) {
// ignore message posted by author of web page
return;
}
callback(message);
})
}
const send = (win, message) => {
win.postMessage(JSON.stringify(message), '*');
}
export { receive, send };

@ -0,0 +1,25 @@
import { expect } from "chai";
import * as messages from '../../src/shared/messages';
describe('messages', () => {
describe('#receive', () => {
it('received a message', (done) => {
messages.receive(window, (message) => {
expect(message).to.deep.equal({ type: 'vimvixen.test' });
done();
});
window.postMessage(JSON.stringify({ type: 'vimvixen.test' }), '*');
});
});
describe('#send', () => {
it('sends a message', (done) => {
window.addEventListener('message', (e) => {
let json = JSON.parse(e.data);
expect(json).to.deep.equal({ type: 'vimvixen.test' });
done();
});
messages.send(window, { type: 'vimvixen.test' });
});
});
});