aboutsummaryrefslogtreecommitdiff
path: root/src/command-line/index.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-08-29 20:56:20 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-08-29 20:56:20 +0900
commitdc4286460791c3b76fe29d085502a11c61c78551 (patch)
tree5b9dc747f83e13d6b827a4a0894c57b82b3df456 /src/command-line/index.js
parenta808b2894090f30e65f396f7caa5af36af83442c (diff)
use iframe-ed command-line
Diffstat (limited to 'src/command-line/index.js')
-rw-r--r--src/command-line/index.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/command-line/index.js b/src/command-line/index.js
index 67aac61..f99afa0 100644
--- a/src/command-line/index.js
+++ b/src/command-line/index.js
@@ -1 +1,64 @@
import './index.scss';
+
+const parent = window.parent;
+
+// TODO consider object-oriented
+var prevValue = "";
+
+const blurData = () => {
+ return JSON.stringify({
+ type: 'vimvixen.commandline.blur'
+ });
+};
+
+const keydownData = (input) => {
+ return JSON.stringify({
+ type: 'vimvixen.commandline.enter',
+ value: input.value
+ });
+};
+
+const keyupData = (input) => {
+ return JSON.stringify({
+ type: 'vimvixen.commandline.change',
+ value: input.value
+ });
+};
+
+const handleBlur = () => {
+ parent.postMessage(blurData(), '*');
+};
+
+const handleKeydown = (e) => {
+ switch(e.keyCode) {
+ case KeyboardEvent.DOM_VK_ESCAPE:
+ parent.postMessage(blurData(), '*');
+ break;
+ case KeyboardEvent.DOM_VK_RETURN:
+ parent.postMessage(keydownData(e.target), '*');
+ break;
+ }
+};
+
+const handleKeyup = (e) => {
+ if (e.target.value === prevValue) {
+ return;
+ }
+ parent.postMessage(keyupData(e.target), '*');
+ prevValue = e.target.value;
+};
+
+window.addEventListener('load', () => {
+ let hash = window.location.hash;
+ let initial = '';
+ if (hash.length > 0) {
+ initial = decodeURIComponent(hash.substring(1));
+ }
+
+ let input = window.document.querySelector('#vimvixen-command-line-line-input');
+ input.addEventListener('blur', handleBlur);
+ input.addEventListener('keydown', handleKeydown);
+ input.addEventListener('keyup', handleKeyup);
+ input.value = initial;
+ input.focus();
+});