DEV: Minor code improvements.

This commit is contained in:
Dan Ungureanu 2019-03-02 19:07:54 +02:00
parent 3d0008165f
commit fd9671f7fe
No known key found for this signature in database
GPG Key ID: 0AA2A00D6ACC8B84
1 changed files with 10 additions and 18 deletions

View File

@ -76,6 +76,8 @@ const bindings = {
"x t": { click: "#dismiss-topics,#dismiss-topics-top" } // dismiss topics "x t": { click: "#dismiss-topics,#dismiss-topics-top" } // dismiss topics
}; };
const animationDuration = 100;
export default { export default {
bindEvents(keyTrapper, container) { bindEvents(keyTrapper, container) {
this.keyTrapper = keyTrapper; this.keyTrapper = keyTrapper;
@ -420,7 +422,8 @@ export default {
// Pressing a move key (J/K) very quick (i.e. keeping J or K pressed) will // Pressing a move key (J/K) very quick (i.e. keeping J or K pressed) will
// move fast by disabling smooth page scrolling. // move fast by disabling smooth page scrolling.
const now = +new Date(); const now = +new Date();
const fast = this._lastMoveTime && now - this._lastMoveTime < 150; const fast =
this._lastMoveTime && now - this._lastMoveTime < 1.5 * animationDuration;
this._lastMoveTime = now; this._lastMoveTime = now;
const $articles = this._findArticles(); const $articles = this._findArticles();
@ -446,14 +449,9 @@ export default {
const index = $articles.index($selected); const index = $articles.index($selected);
let $article = $articles.eq(index); let $article = $articles.eq(index);
/* // Try doing a page scroll in the context of current post.
* Try doing a page scroll in the context of current post.
*/
if (!fast && direction !== 0 && $article.length > 0) { if (!fast && direction !== 0 && $article.length > 0) {
/** @var Begin and end offsets for current article // The beginning of first article is the beginning of the page.
* The beginning of first article is the beginning of the page.
*/
const beginArticle = const beginArticle =
$article.is(".topic-post") && $article.find("#post_1").length $article.is(".topic-post") && $article.find("#post_1").length
? 0 ? 0
@ -461,7 +459,6 @@ export default {
const endArticle = const endArticle =
$article.offset().top + $article[0].getBoundingClientRect().height; $article.offset().top + $article[0].getBoundingClientRect().height;
/** @var Begin and end offsets for screen */
const beginScreen = $(window).scrollTop(); const beginScreen = $(window).scrollTop();
const endScreen = beginScreen + window.innerHeight; const endScreen = beginScreen + window.innerHeight;
@ -482,10 +479,7 @@ export default {
} }
} }
/* // Try scrolling to post above or below.
* Try scrolling to post above or below.
*/
if ($selected.length !== 0) { if ($selected.length !== 0) {
if (direction === -1 && index === 0) return; if (direction === -1 && index === 0) return;
if (direction === 1 && index === $articles.length - 1) return; if (direction === 1 && index === $articles.length - 1) return;
@ -512,9 +506,7 @@ export default {
); );
} }
/* // Otherwise scroll through the suggested topic list.
* Otherwise scroll through the suggested topic list.
*/
this._scrollList($article, direction); this._scrollList($article, direction);
} }
}, },
@ -522,7 +514,7 @@ export default {
_scrollTo(scrollTop, complete) { _scrollTo(scrollTop, complete) {
$("html, body") $("html, body")
.stop(true, true) .stop(true, true)
.animate({ scrollTop }, { duration: 100, complete }); .animate({ scrollTop }, { duration: animationDuration, complete });
}, },
_scrollList($article) { _scrollList($article) {
@ -554,7 +546,7 @@ export default {
} }
this._scrollAnimation = $("html, body").animate( this._scrollAnimation = $("html, body").animate(
{ scrollTop: scrollPos + "px" }, { scrollTop: scrollPos + "px" },
100 animationDuration
); );
}, },