Skip to content

Node.js based forum software built for the modern web

feat: navigate thumbs by keyboard

Barış Soner Uşaklı0b684d5b15da
diff --git a/public/src/client/topic.js b/public/src/client/topic.js
index d6ac87fa97..3a42120f36 100644
--- a/public/src/client/topic.js
+++ b/public/src/client/topic.js
@@ -199,11 +199,42 @@ define('forum/topic', [
backdrop: true,
message: html,
});
+ function selectThumbByIndex(index) {
+ const thumbEls = modal.find('[component="topic/thumb/select"]');
+ if (thumbEls.length) {
+ const normalizedIndex = (((index % thumbEls.length) + thumbEls.length) % thumbEls.length);
+ const selectedThumb = thumbEls.eq(normalizedIndex);
+ thumbEls.removeClass('border-primary');
+ selectedThumb.addClass('border-primary');
+ const newSrc = selectedThumb.find('img').attr('src');
+ modal.find('[component="topic/thumb/current"]')
+ .attr('src', newSrc)
+ .parent().attr('href', newSrc);
+ }
+ }
+
modal.on('click', '[component="topic/thumb/select"]', function () {
- $('[component="topic/thumb/select"]').removeClass('border-primary');
- $(this).addClass('border-primary');
- $('[component="topic/thumb/current"]')
- .attr('src', $(this).find('img').attr('src'));
+ const thumbEls = modal.find('[component="topic/thumb/select"]');
+ selectThumbByIndex(thumbEls.index(this));
+ });
+
+ const keydownEventName = `keydown.topic-thumbs-${Date.now()}`;
+ const onThumbKeydown = function (ev) {
+ if (ev.key !== 'ArrowLeft' && ev.key !== 'ArrowRight') {
+ return;
+ }
+
+ ev.preventDefault();
+ const thumbEls = modal.find('[component="topic/thumb/select"]');
+ const selectedIndex = thumbEls.index(thumbEls.filter('.border-primary').first());
+ const currentIndex = selectedIndex >= 0 ? selectedIndex : 0;
+ const direction = ev.key === 'ArrowRight' ? 1 : -1;
+ selectThumbByIndex(currentIndex + direction);
+ };
+
+ $(document).on(keydownEventName, onThumbKeydown);
+ modal.on('hidden.bs.modal', function () {
+ $(document).off(keydownEventName, onThumbKeydown);
});
}
});