aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThedro Neely <thedroneely@gmail.com>2022-12-06 17:18:36 -0500
committerThedro Neely <thedroneely@gmail.com>2022-12-06 17:18:36 -0500
commit6c3bd1e8d097a498604a8daa5388d08af180cb80 (patch)
treeeaa011ccfb8c679ccc549129608e26876ae3a18e
parentc1863b682fd1bd950660a7c039d2900d9054ae4a (diff)
downloadthedroneely.com-6c3bd1e8d097a498604a8daa5388d08af180cb80.tar.gz
thedroneely.com-6c3bd1e8d097a498604a8daa5388d08af180cb80.tar.bz2
thedroneely.com-6c3bd1e8d097a498604a8daa5388d08af180cb80.zip
public/js/app: Keydown activate dictionary
-rw-r--r--public/js/app.js65
1 files changed, 20 insertions, 45 deletions
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,
+ );
}
})();