2019-11-10 17:38:47 +00:00
|
|
|
import {
|
|
|
|
getSettings,
|
|
|
|
Settings,
|
|
|
|
createElementFromString,
|
|
|
|
querySelector
|
|
|
|
} from '../utilities';
|
|
|
|
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
const settings: Settings = await getSettings();
|
|
|
|
if (
|
|
|
|
!settings.features.jumpToNewComment ||
|
|
|
|
!window.location.pathname.startsWith('/~') ||
|
|
|
|
document.querySelectorAll('.comment.is-comment-new').length === 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Jump To New Comment button.
|
|
|
|
const jumpToNewCommentButton: HTMLAnchorElement = createElementFromString(
|
|
|
|
'<a id="trx-jump-to-new-comment" class="btn btn-primary">Jump To New Comment</a>'
|
|
|
|
);
|
|
|
|
jumpToNewCommentButton.addEventListener('click', clickHandler);
|
|
|
|
document.body.append(jumpToNewCommentButton);
|
|
|
|
})();
|
|
|
|
|
2020-01-12 21:36:42 +00:00
|
|
|
let previousComment: HTMLElement | null = null;
|
|
|
|
|
2019-11-10 17:38:47 +00:00
|
|
|
function clickHandler(): void {
|
2020-01-12 21:36:42 +00:00
|
|
|
if (previousComment !== null) {
|
|
|
|
previousComment.classList.remove('is-comment-new');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no new comments left, remove the button.
|
|
|
|
if (document.querySelectorAll('.comment.is-comment-new').length === 0) {
|
|
|
|
const jumpToNewCommentButton: HTMLAnchorElement = querySelector(
|
|
|
|
'#trx-jump-to-new-comment'
|
|
|
|
);
|
|
|
|
jumpToNewCommentButton.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
const newestComment: HTMLElement | null = document.querySelector(
|
|
|
|
'.comment.is-comment-new'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newestComment === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the newest comment is invisible, expand all comments to make it visible.
|
2020-01-12 21:17:45 +00:00
|
|
|
if (newestComment.offsetParent === null) {
|
|
|
|
// TODO: Instead of expanding all comments, only expand the ones necessary
|
|
|
|
// to make the comment visible.
|
|
|
|
const expandAllButton: HTMLButtonElement = querySelector(
|
|
|
|
'[data-js-comment-expand-all-button]'
|
|
|
|
);
|
|
|
|
expandAllButton.click();
|
|
|
|
}
|
|
|
|
|
2019-11-10 17:38:47 +00:00
|
|
|
newestComment.scrollIntoView({behavior: 'smooth'});
|
2020-01-12 21:36:42 +00:00
|
|
|
previousComment = newestComment;
|
2019-11-10 17:38:47 +00:00
|
|
|
}
|