From 041eacf4fbea1486b00c196343bbbe2df4f0970b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 29 Aug 2017 21:20:42 +0900 Subject: rename command-line files --- src/command-line/command-line.js | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/command-line/command-line.js (limited to 'src/command-line/command-line.js') diff --git a/src/command-line/command-line.js b/src/command-line/command-line.js new file mode 100644 index 0000000..34f3f35 --- /dev/null +++ b/src/command-line/command-line.js @@ -0,0 +1,64 @@ +import './command-line.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(); +}); -- cgit v1.2.3