Node.js based forum software built for the modern web
feat: shift-click range selection when selecting posts (#14727)
palmoni5f7668bd6579e
diff --git a/public/src/modules/postSelect.js b/public/src/modules/postSelect.jsindex 3df63d9d8a..0a75355644 100644--- a/public/src/modules/postSelect.js+++ b/public/src/modules/postSelect.js@@ -8,24 +8,38 @@ define('postSelect', ['components'], function (components) {PostSelect.pids = [];let allowMainPostSelect = false;+ let lastSelectedPid;PostSelect.init = function (_onSelect, options) {PostSelect.pids.length = 0;+ lastSelectedPid = null;onSelect = _onSelect;options = options || {};allowMainPostSelect = options.allowMainPostSelect || false;$('#content').on('click', '[component="topic"] [component="post"]', onPostClicked);+ $('#content').on('selectstart', '[component="topic"] [component="post"]', preventSelectOnShift);disableClicksOnPosts();};function onPostClicked(ev) {ev.stopPropagation();const pidClicked = $(this).attr('data-pid');- const postEls = $('[component="topic"] [data-pid="' + pidClicked + '"]');- if (!allowMainPostSelect && parseInt($(this).attr('data-index'), 10) === 0) {+ if (!isSelectable($(this))) {return;}- PostSelect.togglePostSelection(postEls, pidClicked);++ if (ev.shiftKey && lastSelectedPid && lastSelectedPid !== pidClicked) {+ selectRange(pidClicked);+ } else {+ PostSelect.togglePostSelection(getPostEls(pidClicked), pidClicked);+ }+ lastSelectedPid = pidClicked;+ }++ function preventSelectOnShift(ev) {+ if (ev.shiftKey) {+ ev.preventDefault();+ }}PostSelect.disable = function () {@@ -34,29 +48,75 @@ define('postSelect', ['components'], function (components) {});$('#content').off('click', '[component="topic"] [component="post"]', onPostClicked);+ $('#content').off('selectstart', '[component="topic"] [component="post"]', preventSelectOnShift);enableClicksOnPosts();};PostSelect.togglePostSelection = function (postEls, pid) {if (pid) {- const index = PostSelect.pids.indexOf(pid);- if (index === -1) {- PostSelect.pids.push(pid);- postEls.toggleClass('selected', true);- } else {- PostSelect.pids.splice(index, 1);- postEls.toggleClass('selected', false);- }-- if (PostSelect.pids.length) {- PostSelect.pids.sort(function (a, b) { return a - b; });- }- if (typeof onSelect === 'function') {- onSelect();- }+ setPostSelection(pid, PostSelect.pids.indexOf(pid) === -1, postEls);+ onSelectionChanged();}};+ function setPostSelection(pid, isSelected, postEls) {+ const index = PostSelect.pids.indexOf(pid);+ if (isSelected === (index !== -1)) {+ return;+ }++ if (isSelected) {+ PostSelect.pids.push(pid);+ } else {+ PostSelect.pids.splice(index, 1);+ }+ (postEls || getPostEls(pid)).toggleClass('selected', isSelected);+ }++ // selects/deselects every loaded post between the previously clicked post and this one+ function selectRange(pidClicked) {+ const postEls = getSelectablePostEls();+ const start = indexOfPid(postEls, lastSelectedPid);+ const end = indexOfPid(postEls, pidClicked);+ if (start === -1 || end === -1) {+ PostSelect.togglePostSelection(getPostEls(pidClicked), pidClicked);+ return;+ }++ const isSelected = PostSelect.pids.indexOf(pidClicked) === -1;+ postEls.slice(Math.min(start, end), Math.max(start, end) + 1).each(function () {+ setPostSelection($(this).attr('data-pid'), isSelected, $(this));+ });+ onSelectionChanged();+ }++ function onSelectionChanged() {+ if (PostSelect.pids.length) {+ PostSelect.pids.sort(function (a, b) { return a - b; });+ }+ if (typeof onSelect === 'function') {+ onSelect();+ }+ }++ function isSelectable(postEl) {+ return allowMainPostSelect || parseInt(postEl.attr('data-index'), 10) !== 0;+ }++ function getPostEls(pid) {+ return $('[component="topic"] [data-pid="' + pid + '"]');+ }++ function getSelectablePostEls() {+ return $('[component="topic"] [component="post"]').filter(function () {+ return isSelectable($(this));+ });+ }++ function indexOfPid(postEls, pid) {+ return postEls.index(postEls.filter('[data-pid="' + pid + '"]').first());+ }+function disableClicks() {return false;}