From 6c3bd1e8d097a498604a8daa5388d08af180cb80 Mon Sep 17 00:00:00 2001 From: Thedro Neely Date: Tue, 6 Dec 2022 17:18:36 -0500 Subject: public/js/app: Keydown activate dictionary --- public/js/app.js | 65 +++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 45 deletions(-) (limited to 'public') diff --git a/public/js/app.js b/public/js/app.js index 059c355..b8413e6 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -812,55 +812,30 @@ function preload(url) { */ (function () { - addEvent(document, "dblclick", dictionary); - - function addEvent(element, type, listener) { - if (element.addEventListener) { - element.addEventListener(type, listener, false); - return true; - } else if (element.attachEvent) { - return element.attachEvent("on" + type, listener); - } else { - return false; - } - } - - function notInside(element, name) { - if (typeof element.parentElement === "undefined" || - element.parentElement === null - ) { return true; } - var parentTag = element.parentElement; - var tagName = element.tagName; - if (tagName === name) { return false; } - return notInside(parentTag, name); - } + const options = + "targetWindow,width=700,height=500,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes"; - function dictionary(event) { - if ( - event.ctrlKey == true || - event.shiftKey == true || - event.altKey == true - ) { - return; + self.addEventListener("keydown", function (event) { + if (event.repeat && event.key === "d") { + selection(dictionary); } - var word = "" + window.getSelection(); - var container = window.getSelection().anchorNode.parentElement; - - if (word > "" && word.length > 1 && notInside(container, 'PRE')) { - dictionary_access(word); + }); + + function selection(execute) { + let phrase = "" + window.getSelection(); + phrase = phrase.replace(/[!.:?,;"]/g, ""); + phrase = phrase.replace(/^\s*(\S*?)\s*$/g, "$1"); + if (phrase && phrase > "" && phrase.length > 1) { + execute(phrase); } } - function dictionary_access(word) { - word = word.replace(/[!.:?,;"]/g, ""); - word = word.replace(/^\s*(\S*?)\s*$/g, "$1"); - if (word) { - window.open( - "https://www.merriam-webster.com/dictionary/" + - encodeURIComponent(word), - "Definitions", - "targetWindow,width=700,height=500,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes" - ); - } + function dictionary(word) { + window.open( + "https://www.merriam-webster.com/dictionary/" + + encodeURIComponent(word), + "Definitions", + options, + ); } })(); -- cgit v1.2.3