Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use commentDepth options in Follow Comments keyboard navigation #5502

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 45 additions & 8 deletions lib/modules/keyboardNav.js
@@ -1,17 +1,20 @@
/* @flow */

import $ from 'jquery';
import { once, maxBy, without, memoize } from 'lodash-es';
import { maxBy, memoize, once, without } from 'lodash-es';
import { Module } from '../core/module';
import * as Modules from '../core/modules';
import * as Options from '../core/options';
import { i18n, openNewTab } from '../environment';
import {
NAMED_KEYS,
Thing,
SelectedThing,
Thing,
click,
downcast,
currentSubreddit,
currentUserProfile,
downcast,
execRegexes,
filterMap,
getPercentageVisibleYAxis,
hashKeyArray,
Expand All @@ -22,21 +25,21 @@ import {
loggedInUser,
matchesPageLocation,
niceKeyCode,
regexes,
scrollToElement,
string,
watchForElements,
waitForEvent,
watchForElements,
} from '../utils';
import { i18n, openNewTab } from '../environment';
import * as Options from '../core/options';
import * as CommandLine from './commandLine';
import * as CommentDepth from './commentDepth';
import * as CommentNavigator from './commentNavigator';
import * as EasterEgg from './easterEgg';
import * as FilteReddit from './filteReddit';
import * as HideChildComments from './hideChildComments';
import * as NeverEndingReddit from './neverEndingReddit';
import * as Notifications from './notifications';
import * as NoParticipation from './noParticipation';
import * as Notifications from './notifications';
import * as SaveComments from './saveComments';
import * as SettingsNavigation from './settingsNavigation';
import * as ShowImages from './showImages';
Expand Down Expand Up @@ -1201,7 +1204,41 @@ function followPermalink(newWindow = false) {

function followComments(newWindow = false, selected = getSelected()) {
const a = assertElement(selected.getCommentsLink());
navigateTo(a.href, { newWindow });
const url = new URL(a.href, location.href);

// no need to proceed if depth already exists in the query string
if (url.searchParams.has('depth')) {
navigateTo(a.href, { newWindow });
}

if (regexes.commentPermalink.test(url.pathname) && (
!CommentDepth.module.options.commentPermalinks.value ||
!CommentDepth.module.options.commentPermalinksContext.value && url.searchParams.has('context')
)) {
navigateTo(a.href, { newWindow });
}

const matches = execRegexes.comments(url.pathname);
if (!matches) navigateTo(a.href, { newWindow });

// check for subreddit specific values
const subreddit = matches && matches[1].toLowerCase();
const [, commentDepth, minimumComments] = (
CommentDepth.module.options.subredditCommentDepths.value.find(([subreddits]) => subreddits.toLowerCase().split(',').includes(subreddit)) ||
[null, CommentDepth.module.options.defaultCommentDepth.value, CommentDepth.module.options.defaultMinimumComments.value]
);

// NaN or 0 (show everything)
if (!parseInt(commentDepth, 10)) navigateTo(a.href, { newWindow });

// if the comment count is less than the minimum, show everything
const minimumCount = parseInt(minimumComments, 10);
if (minimumCount && selected && selected.isPost() && (selected.getCommentCount() || 0) < minimumCount) {
navigateTo(a.href, { newWindow });
}

url.searchParams.set('depth', commentDepth);
navigateTo(url.href, { newWindow });
}

function vote(way: 'downmod' | 'upmod', preventToggle = false, selected = getSelected()) {
Expand Down
2 changes: 1 addition & 1 deletion locales/locales/en.json
Expand Up @@ -217,7 +217,7 @@
"message": "Custom Comment Depth"
},
"commentDepthDesc": {
"message": "Allows you to set the preferred depth of comments you wish to see when clicking on comments links.\n\n0 = Everything, 1 = Root level, 2 = Responses to root level, 3 = Responses to responses to root level, etc."
"message": "Allows you to set the preferred depth of comments you wish to see when clicking on comments links or navigate using the Follow Comments keyboard shortcut.\n\n0 = Everything, 1 = Root level, 2 = Responses to root level, 3 = Responses to responses to root level, etc."
},
"commentHidePerName": {
"message": "Comment Hide Persistor"
Expand Down