From de9509cd31d63b036f63fc56697e018263e52eda Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 1 Dec 2018 22:07:00 +0300 Subject: [PATCH] hotkeys: simplify prefix timeout handling --- js/feedlist.js | 3 ++- js/functions.js | 50 +++++++++++++++++++++---------------------------- js/prefs.js | 2 -- js/viewfeed.js | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/js/feedlist.js b/js/feedlist.js index c295bf22b..bff62043f 100644 --- a/js/feedlist.js +++ b/js/feedlist.js @@ -1,3 +1,5 @@ +/* global notify,__,dijit */ + const Feeds = { counters_last_request: 0, _active_feed_id: 0, @@ -208,7 +210,6 @@ const Feeds = { Utils.setLoadingProgress(50); document.onkeydown = () => { App.hotkeyHandler(event) }; - window.setInterval(() => { hotkeyPrefixTimeout() }, 3 * 1000); window.setInterval(() => { Headlines.catchupBatchedArticles() }, 10 * 1000); if (!this.getActiveFeedId()) { diff --git a/js/functions.js b/js/functions.js index b6d86b51c..04a81d925 100755 --- a/js/functions.js +++ b/js/functions.js @@ -5,9 +5,6 @@ let _label_base_index = -1024; let loading_progress = 0; let notify_hide_timerid = false; -let hotkey_prefix = 0; -let hotkey_prefix_pressed = false; - Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap( function (callOriginal, options) { @@ -58,6 +55,9 @@ Array.prototype.remove = function(s) { const Utils = { _rpc_seq: 0, + hotkey_prefix: 0, + hotkey_prefix_pressed: false, + hotkey_prefix_timeout: 0, next_seq: function() { this._rpc_seq += 1; return this._rpc_seq; @@ -75,30 +75,34 @@ const Utils = { Element.hide("overlay"); }, - keyeventToAction: function(e) { + keyeventToAction: function(event) { const hotkeys_map = getInitParam("hotkeys"); - const keycode = e.which; + const keycode = event.which; const keychar = String.fromCharCode(keycode).toLowerCase(); if (keycode == 27) { // escape and drop prefix - hotkey_prefix = false; + this.hotkey_prefix = false; } if (keycode == 16 || keycode == 17) return; // ignore lone shift / ctrl - if (!hotkey_prefix && hotkeys_map[0].indexOf(keychar) != -1) { + if (!this.hotkey_prefix && hotkeys_map[0].indexOf(keychar) != -1) { const date = new Date(); const ts = Math.round(date.getTime() / 1000); - hotkey_prefix = keychar; - hotkey_prefix_pressed = ts; - + this.hotkey_prefix = keychar; $("cmdline").innerHTML = keychar; Element.show("cmdline"); - e.stopPropagation(); + window.clearTimeout(this.hotkey_prefix_timeout); + this.hotkey_prefix_timeout = window.setTimeout(() => { + this.hotkey_prefix = false; + Element.hide("cmdline"); + }, 3 * 1000); + + event.stopPropagation(); return false; } @@ -108,13 +112,13 @@ const Utils = { let hotkey_name = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")"; // ensure ^*char notation - if (e.shiftKey) hotkey_name = "*" + hotkey_name; - if (e.ctrlKey) hotkey_name = "^" + hotkey_name; - if (e.altKey) hotkey_name = "+" + hotkey_name; - if (e.metaKey) hotkey_name = "%" + hotkey_name; + if (event.shiftKey) hotkey_name = "*" + hotkey_name; + if (event.ctrlKey) hotkey_name = "^" + hotkey_name; + if (event.altKey) hotkey_name = "+" + hotkey_name; + if (event.metaKey) hotkey_name = "%" + hotkey_name; - const hotkey_full = hotkey_prefix ? hotkey_prefix + " " + hotkey_name : hotkey_name; - hotkey_prefix = false; + const hotkey_full = this.hotkey_prefix ? this.hotkey_prefix + " " + hotkey_name : hotkey_name; + this.hotkey_prefix = false; let action_name = false; @@ -1008,18 +1012,6 @@ function strip_tags(s) { return s.replace(/<\/?[^>]+(>|$)/g, ""); } -function hotkeyPrefixTimeout() { - const date = new Date(); - const ts = Math.round(date.getTime() / 1000); - - if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) { - console.log("hotkey_prefix seems to be stuck, aborting"); - hotkey_prefix_pressed = false; - hotkey_prefix = false; - Element.hide('cmdline'); - } -} - // noinspection JSUnusedGlobalSymbols function uploadIconHandler(rc) { switch (rc) { diff --git a/js/prefs.js b/js/prefs.js index 8308a747f..1541d49d0 100755 --- a/js/prefs.js +++ b/js/prefs.js @@ -85,8 +85,6 @@ const App = { editFeed(param) }, 100); } - - setInterval(() => { hotkeyPrefixTimeout() }, 5 * 1000); }, hotkeyHandler: function (event) { if (event.target.nodeName == "INPUT" || event.target.nodeName == "TEXTAREA") return; diff --git a/js/viewfeed.js b/js/viewfeed.js index b091e5ccb..01e5cb325 100755 --- a/js/viewfeed.js +++ b/js/viewfeed.js @@ -1,4 +1,4 @@ -/* global dijit, __, ngettext */ +/* global dijit, __, ngettext, notify */ const ArticleCache = { has_storage: 'sessionStorage' in window && window['sessionStorage'] !== null,