2022-03-01 14:44:21 +00:00
|
|
|
var
|
2022-03-01 15:54:52 +00:00
|
|
|
lastHighlightedPublishedComment = null,
|
2023-01-20 04:49:47 +00:00
|
|
|
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
|
2022-03-01 14:44:21 +00:00
|
|
|
CommentItem = require( './CommentItem.js' ),
|
2023-10-21 14:33:02 +00:00
|
|
|
HeadingItem = require( './HeadingItem.js' ),
|
2022-03-01 14:44:21 +00:00
|
|
|
utils = require( './utils.js' );
|
2022-03-01 14:25:27 +00:00
|
|
|
|
|
|
|
/**
|
2023-10-21 14:33:02 +00:00
|
|
|
* Draw a semi-transparent rectangle on the page to highlight the given thread item.
|
2022-03-01 14:25:27 +00:00
|
|
|
*
|
2022-03-14 14:43:35 +00:00
|
|
|
* @class
|
2023-10-21 14:33:02 +00:00
|
|
|
* @param {ThreadItem|ThreadItem[]} items Thread item(s) to highlight
|
2022-03-01 14:25:27 +00:00
|
|
|
*/
|
2023-10-21 14:33:02 +00:00
|
|
|
function Highlight( items ) {
|
2022-04-20 17:41:44 +00:00
|
|
|
var highlightNodes = [];
|
|
|
|
var ranges = [];
|
2022-03-14 14:43:35 +00:00
|
|
|
|
|
|
|
this.topmostElement = null;
|
|
|
|
|
2023-10-21 14:33:02 +00:00
|
|
|
items = Array.isArray( items ) ? items : [ items ];
|
2022-03-14 14:43:35 +00:00
|
|
|
|
2023-10-21 14:33:02 +00:00
|
|
|
this.rootNode = items[ 0 ] ? items[ 0 ].rootNode : null;
|
2022-06-15 00:56:47 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
items.forEach( ( item ) => {
|
2022-03-14 14:43:35 +00:00
|
|
|
var $highlight = $( '<div>' ).addClass( 'ext-discussiontools-init-highlight' );
|
|
|
|
|
2023-10-21 14:33:02 +00:00
|
|
|
// We insert the highlight in the DOM near the thead item, so that it remains positioned correctly
|
2022-03-14 14:43:35 +00:00
|
|
|
// when it shifts (e.g. collapsing the table of contents), and disappears when it is hidden (e.g.
|
|
|
|
// opening visual editor).
|
2023-12-15 16:22:52 +00:00
|
|
|
var range = item.getRange();
|
2023-08-17 12:25:03 +00:00
|
|
|
// Support: Firefox
|
2022-03-14 14:43:35 +00:00
|
|
|
// The highlight node must be inserted after the start marker node (data-mw-comment-start), not
|
|
|
|
// before, otherwise Node#getBoundingClientRect() returns wrong results.
|
|
|
|
range.insertNode( $highlight[ 0 ] );
|
2022-03-01 14:25:27 +00:00
|
|
|
|
2022-06-15 00:56:47 +00:00
|
|
|
// If the item is a top-level comment wrapped in a frame, highlight outside that frame
|
2023-10-21 14:33:02 +00:00
|
|
|
if ( item.level === 1 ) {
|
|
|
|
var coveredSiblings = utils.getFullyCoveredSiblings( item, item.rootNode );
|
2022-06-15 00:56:47 +00:00
|
|
|
if ( coveredSiblings ) {
|
|
|
|
range.setStartBefore( coveredSiblings[ 0 ] );
|
|
|
|
range.setEndAfter( coveredSiblings[ coveredSiblings.length - 1 ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the item is a heading, highlight the full extent of it (not only the text)
|
2023-10-21 14:33:02 +00:00
|
|
|
if ( item instanceof HeadingItem && !item.placeholderHeading ) {
|
2023-01-20 04:49:47 +00:00
|
|
|
range.selectNode(
|
|
|
|
$highlight.closest( '.mw-heading' )[ 0 ] ||
|
|
|
|
$highlight.closest( 'h1, h2, h3, h4, h5, h6' )[ 0 ]
|
|
|
|
);
|
2022-06-15 00:56:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 17:41:44 +00:00
|
|
|
ranges.push( range );
|
|
|
|
highlightNodes.push( $highlight[ 0 ] );
|
2022-03-14 14:43:35 +00:00
|
|
|
} );
|
|
|
|
|
2022-04-20 17:41:44 +00:00
|
|
|
this.ranges = ranges;
|
|
|
|
this.$element = $( highlightNodes );
|
|
|
|
|
2022-03-14 14:43:35 +00:00
|
|
|
// Events
|
|
|
|
this.updateDebounced = OO.ui.debounce( this.update.bind( this ), 500 );
|
|
|
|
window.addEventListener( 'resize', this.updateDebounced );
|
|
|
|
|
2023-08-17 12:25:03 +00:00
|
|
|
if ( OO.ui.isMobile() ) {
|
|
|
|
// In MobileFrontend, ensure the section we are highlighting within is expanded. This is
|
|
|
|
// often the case as we add the hash fragment to our notification URLs, but not when we highlight
|
|
|
|
// comments across multiple threads.
|
|
|
|
// HACK: Ideally MF would expose Toggler as a public API, but for now just find the correct DOM
|
|
|
|
// node and trigger a fake click.
|
|
|
|
this.$element.parents( '.collapsible-block' ).prev( '.collapsible-heading:not( .open-block )' ).trigger( 'click' );
|
|
|
|
}
|
|
|
|
|
2022-03-14 14:43:35 +00:00
|
|
|
this.update();
|
2022-03-01 14:25:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 14:43:35 +00:00
|
|
|
OO.initClass( Highlight );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update position of highlights, e.g. after window resize
|
|
|
|
*/
|
|
|
|
Highlight.prototype.update = function () {
|
|
|
|
this.$element.css( {
|
|
|
|
'margin-top': '',
|
|
|
|
'margin-left': '',
|
2022-06-15 00:56:47 +00:00
|
|
|
'margin-right': '',
|
2022-03-14 14:43:35 +00:00
|
|
|
width: '',
|
|
|
|
height: ''
|
|
|
|
} );
|
2022-06-15 00:56:47 +00:00
|
|
|
var rootRect = this.rootNode.getBoundingClientRect();
|
2022-03-14 14:43:35 +00:00
|
|
|
this.topmostElement = null;
|
2022-06-15 00:56:47 +00:00
|
|
|
var topmostTop = Infinity;
|
2024-04-18 18:37:58 +00:00
|
|
|
this.ranges.forEach( ( range, i ) => {
|
|
|
|
var $element = this.$element.eq( i );
|
2022-06-15 00:56:47 +00:00
|
|
|
var baseRect = $element[ 0 ].getBoundingClientRect();
|
2022-03-14 14:43:35 +00:00
|
|
|
var rect = RangeFix.getBoundingClientRect( range );
|
|
|
|
// rect may be null if the range is in a detached or hidden node
|
|
|
|
if ( rect ) {
|
2022-06-15 00:56:47 +00:00
|
|
|
// Draw the highlight over the full width of the page, except for very short comments
|
|
|
|
// (less than 1/3 of the available width).
|
|
|
|
//
|
|
|
|
// This lets the far edges of almost all comments align nicely (T309444#7968858),
|
|
|
|
// while still accurately highlighting comments in narrow floating boxes, such as
|
|
|
|
// image captions or {{archive top}}.
|
|
|
|
//
|
|
|
|
// It seems difficult to distinguish the floating boxes from comments that are just
|
|
|
|
// very short or very deeply indented, and this seems to work well enough in practice.
|
|
|
|
var useFullWidth = rect.width > rootRect.width / 3;
|
|
|
|
|
2023-01-20 04:49:47 +00:00
|
|
|
var headingTopAdj = 0;
|
|
|
|
if (
|
|
|
|
featuresEnabled.visualenhancements &&
|
|
|
|
$element.closest( '.ext-discussiontools-init-section' ).length
|
|
|
|
) {
|
|
|
|
// Shift the highlight a little to avoid drawing over the separator border at the top,
|
|
|
|
// and to cover the gap to the first comment at the bottom.
|
|
|
|
headingTopAdj = 10;
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:56:47 +00:00
|
|
|
var top = rect.top - baseRect.top;
|
|
|
|
var width = rect.width;
|
|
|
|
var height = rect.height;
|
|
|
|
var left, right;
|
|
|
|
if ( $element.css( 'direction' ) === 'ltr' ) {
|
|
|
|
left = rect.left - baseRect.left;
|
|
|
|
if ( useFullWidth ) {
|
|
|
|
width = rootRect.width - ( rect.left - rootRect.left );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
right = ( baseRect.left + baseRect.width ) - ( rect.left + rect.width );
|
|
|
|
if ( useFullWidth ) {
|
|
|
|
width = rootRect.width - ( ( rootRect.left + rootRect.width ) - ( rect.left + rect.width ) );
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 14:43:35 +00:00
|
|
|
var padding = 5;
|
2022-06-15 00:56:47 +00:00
|
|
|
$element.css( {
|
2023-01-20 04:49:47 +00:00
|
|
|
'margin-top': top - padding + headingTopAdj,
|
2022-06-15 00:56:47 +00:00
|
|
|
'margin-left': left !== undefined ? left - padding : '',
|
|
|
|
'margin-right': right !== undefined ? right - padding : '',
|
|
|
|
width: width + ( padding * 2 ),
|
|
|
|
height: height + ( padding * 2 )
|
2022-03-14 14:43:35 +00:00
|
|
|
} );
|
2022-06-15 00:56:47 +00:00
|
|
|
|
|
|
|
if ( rect.top < topmostTop ) {
|
2024-04-18 18:37:58 +00:00
|
|
|
this.topmostElement = $element[ 0 ];
|
2022-06-15 00:56:47 +00:00
|
|
|
topmostTop = rect.top;
|
2022-03-14 14:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-10-21 14:33:02 +00:00
|
|
|
* Scroll the topmost highlight into view
|
2022-03-14 14:43:35 +00:00
|
|
|
*/
|
|
|
|
Highlight.prototype.scrollIntoView = function () {
|
|
|
|
if ( this.topmostElement ) {
|
|
|
|
this.topmostElement.scrollIntoView();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the highlight
|
|
|
|
*/
|
|
|
|
Highlight.prototype.destroy = function () {
|
|
|
|
this.$element.remove();
|
|
|
|
window.removeEventListener( 'resize', this.updateDebounced );
|
|
|
|
};
|
|
|
|
|
|
|
|
var highlightedTarget = null;
|
2022-03-01 14:25:27 +00:00
|
|
|
/**
|
2023-10-21 14:33:02 +00:00
|
|
|
* Highlight the thread item(s) on the page associated with the URL hash or query string
|
2022-03-01 14:25:27 +00:00
|
|
|
*
|
|
|
|
* @param {ThreadItemSet} threadItemSet
|
2023-10-21 14:33:02 +00:00
|
|
|
* @param {boolean} [noScroll] Don't scroll to the topmost highlight, e.g. on popstate
|
2023-06-26 15:13:09 +00:00
|
|
|
* @return {Object} Object containing 'requested' IDs, 'requestedSince' ID, and successfully 'highligted' IDs
|
2022-03-01 14:25:27 +00:00
|
|
|
*/
|
|
|
|
function highlightTargetComment( threadItemSet, noScroll ) {
|
2022-03-14 14:43:35 +00:00
|
|
|
if ( highlightedTarget ) {
|
|
|
|
highlightedTarget.destroy();
|
|
|
|
highlightedTarget = null;
|
|
|
|
}
|
2022-03-01 14:25:27 +00:00
|
|
|
|
2022-08-16 11:48:51 +00:00
|
|
|
var targetElement = mw.util.getTargetFromFragment();
|
2022-03-01 14:25:27 +00:00
|
|
|
|
2022-06-20 20:48:24 +00:00
|
|
|
if ( targetElement && targetElement.hasAttribute( 'data-mw-comment-start' ) ) {
|
2023-06-26 15:13:09 +00:00
|
|
|
var threadItemId = targetElement.getAttribute( 'id' );
|
2023-10-21 14:33:02 +00:00
|
|
|
var threadItem = threadItemSet.findCommentById( targetElement.getAttribute( 'id' ) );
|
|
|
|
if ( threadItem ) {
|
|
|
|
highlightedTarget = new Highlight( threadItem );
|
2022-07-22 15:47:09 +00:00
|
|
|
highlightedTarget.$element.addClass( 'ext-discussiontools-init-targetcomment' );
|
|
|
|
highlightedTarget.$element.addClass( 'ext-discussiontools-init-highlight-fadein' );
|
|
|
|
}
|
2023-06-26 15:13:09 +00:00
|
|
|
return {
|
|
|
|
requested: [ threadItemId ],
|
|
|
|
highlighted: [ threadItemId ]
|
|
|
|
};
|
2022-06-20 20:48:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 15:13:09 +00:00
|
|
|
// Single thread item not highlighted yet
|
|
|
|
if ( location.hash.match( /^#(c|h)-/ ) ) {
|
|
|
|
return {
|
|
|
|
requested: [ location.hash.slice( 1 ) ],
|
|
|
|
highlighted: []
|
|
|
|
};
|
2022-06-20 20:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var url = new URL( location.href );
|
2023-06-26 15:13:09 +00:00
|
|
|
return highlightNewComments(
|
2022-06-20 20:48:24 +00:00
|
|
|
threadItemSet,
|
|
|
|
noScroll,
|
|
|
|
url.searchParams.get( 'dtnewcomments' ) && url.searchParams.get( 'dtnewcomments' ).split( '|' ),
|
2023-03-30 15:40:52 +00:00
|
|
|
{
|
|
|
|
newCommentsSinceId: url.searchParams.get( 'dtnewcommentssince' ),
|
2023-03-30 16:04:36 +00:00
|
|
|
inThread: url.searchParams.get( 'dtinthread' ),
|
|
|
|
sinceThread: url.searchParams.get( 'dtsincethread' )
|
2023-03-30 15:40:52 +00:00
|
|
|
}
|
2022-06-20 20:48:24 +00:00
|
|
|
);
|
2022-03-01 14:25:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:44:21 +00:00
|
|
|
/**
|
|
|
|
* Highlight a just-published comment/topic
|
|
|
|
*
|
2022-03-14 14:43:35 +00:00
|
|
|
* These highlights show for a short period of time then tear themselves down.
|
|
|
|
*
|
2022-03-01 14:44:21 +00:00
|
|
|
* @param {ThreadItemSet} threadItemSet Thread item set
|
|
|
|
* @param {string} threadItemId Thread item ID (NEW_TOPIC_COMMENT_ID for the a new topic)
|
|
|
|
*/
|
|
|
|
function highlightPublishedComment( threadItemSet, threadItemId ) {
|
2022-03-14 14:43:35 +00:00
|
|
|
var highlightComments = [];
|
|
|
|
|
2022-03-01 14:44:21 +00:00
|
|
|
if ( threadItemId === utils.NEW_TOPIC_COMMENT_ID ) {
|
|
|
|
// Highlight the last comment on the page
|
|
|
|
var lastComment = threadItemSet.threadItems[ threadItemSet.threadItems.length - 1 ];
|
2022-03-01 15:54:52 +00:00
|
|
|
lastHighlightedPublishedComment = lastComment;
|
2022-03-14 14:43:35 +00:00
|
|
|
highlightComments.push( lastComment );
|
2022-03-01 14:44:21 +00:00
|
|
|
|
|
|
|
// If it's the only comment under its heading, highlight the heading too.
|
|
|
|
// (It might not be if the new discussion topic was posted without a title: T272666.)
|
|
|
|
if (
|
|
|
|
lastComment.parent &&
|
|
|
|
lastComment.parent.type === 'heading' &&
|
|
|
|
lastComment.parent.replies.length === 1
|
|
|
|
) {
|
2022-03-14 14:43:35 +00:00
|
|
|
highlightComments.push( lastComment.parent );
|
2022-03-01 15:54:52 +00:00
|
|
|
lastHighlightedPublishedComment = lastComment.parent;
|
2022-04-20 17:49:00 +00:00
|
|
|
// Change URL to point to this section, like the old section=new wikitext editor does.
|
|
|
|
// This also expands collapsed sections on mobile (T301840).
|
|
|
|
var sectionTitle = lastHighlightedPublishedComment.getLinkableTitle();
|
|
|
|
var urlFragment = mw.util.escapeIdForLink( sectionTitle );
|
|
|
|
// Navigate to fragment without scrolling
|
|
|
|
location.hash = '#' + urlFragment + '-DoesNotExist-DiscussionToolsHack';
|
2022-08-09 12:37:57 +00:00
|
|
|
history.replaceState( null, '', '#' + urlFragment );
|
2022-03-01 14:44:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find the comment we replied to, then highlight the last reply
|
|
|
|
var repliedToComment = threadItemSet.threadItemsById[ threadItemId ];
|
2022-03-14 14:43:35 +00:00
|
|
|
highlightComments.push( repliedToComment.replies[ repliedToComment.replies.length - 1 ] );
|
|
|
|
lastHighlightedPublishedComment = highlightComments[ 0 ];
|
2022-03-01 14:44:21 +00:00
|
|
|
}
|
2022-04-19 14:40:41 +00:00
|
|
|
|
|
|
|
// We may have changed the location hash on mobile, so wait for that to cause
|
|
|
|
// the section to expand before drawing the highlight.
|
2024-04-18 18:37:58 +00:00
|
|
|
setTimeout( () => {
|
2022-04-19 14:40:41 +00:00
|
|
|
var highlight = new Highlight( highlightComments );
|
|
|
|
highlight.$element.addClass( 'ext-discussiontools-init-publishedcomment' );
|
|
|
|
|
|
|
|
// Show a highlight with the same timing as the post-edit message (mediawiki.action.view.postEdit):
|
|
|
|
// show for 3000ms, fade out for 250ms (animation duration is defined in CSS).
|
|
|
|
OO.ui.Element.static.scrollIntoView(
|
|
|
|
highlight.topmostElement,
|
|
|
|
{
|
|
|
|
padding: {
|
|
|
|
// Add padding to avoid overlapping the post-edit notification (above on desktop, below on mobile)
|
|
|
|
top: OO.ui.isMobile() ? 10 : 60,
|
|
|
|
bottom: OO.ui.isMobile() ? 85 : 10
|
|
|
|
},
|
|
|
|
// Specify scrollContainer for compatibility with MobileFrontend.
|
|
|
|
// Apparently it makes `<dd>` elements scrollable and OOUI tried to scroll them instead of body.
|
|
|
|
scrollContainer: OO.ui.Element.static.getRootScrollableElement( highlight.topmostElement )
|
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
).then( () => {
|
2022-04-19 14:40:41 +00:00
|
|
|
highlight.$element.addClass( 'ext-discussiontools-init-highlight-fadein' );
|
2024-04-18 18:37:58 +00:00
|
|
|
setTimeout( () => {
|
2022-04-19 14:40:41 +00:00
|
|
|
highlight.$element.addClass( 'ext-discussiontools-init-highlight-fadeout' );
|
2024-04-18 18:37:58 +00:00
|
|
|
setTimeout( () => {
|
2022-04-19 14:40:41 +00:00
|
|
|
// Remove the node when no longer needed, because it's using CSS 'mix-blend-mode', which
|
|
|
|
// affects the text rendering of the whole page, disabling subpixel antialiasing on Windows
|
|
|
|
highlight.destroy();
|
|
|
|
}, 250 );
|
|
|
|
}, 3000 );
|
|
|
|
} );
|
2022-03-01 14:44:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:25:27 +00:00
|
|
|
/**
|
|
|
|
* Highlight the new comments on the page associated with the query string
|
|
|
|
*
|
|
|
|
* @param {ThreadItemSet} threadItemSet
|
|
|
|
* @param {boolean} [noScroll] Don't scroll to the topmost highlighted comment, e.g. on popstate
|
|
|
|
* @param {string[]} [newCommentIds] A list of comment IDs to highlight
|
2023-03-30 15:40:52 +00:00
|
|
|
* @param {Object} [options] Extra options
|
|
|
|
* @param {string} [options.newCommentsSinceId] Highlight all comments after the comment with this ID
|
|
|
|
* @param {boolean} [options.inThread] When using newCommentsSinceId, only highlight comments in the same thread
|
2023-03-30 16:04:36 +00:00
|
|
|
* @param {boolean} [options.sinceThread] When using newCommentsSinceId, only highlight comments in threads
|
|
|
|
* created since that comment was posted.
|
2023-06-26 15:13:09 +00:00
|
|
|
* @return {Object} Object containing 'requested' comment IDs, 'requestedSince' ID, and successfully 'highligted' IDs
|
2022-03-01 14:25:27 +00:00
|
|
|
*/
|
2023-03-30 15:40:52 +00:00
|
|
|
function highlightNewComments( threadItemSet, noScroll, newCommentIds, options ) {
|
2022-03-14 14:43:35 +00:00
|
|
|
if ( highlightedTarget ) {
|
|
|
|
highlightedTarget.destroy();
|
|
|
|
highlightedTarget = null;
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:25:27 +00:00
|
|
|
newCommentIds = newCommentIds || [];
|
2023-03-30 15:40:52 +00:00
|
|
|
options = options || {};
|
2022-03-01 14:25:27 +00:00
|
|
|
|
2023-03-30 15:40:52 +00:00
|
|
|
if ( options.newCommentsSinceId ) {
|
|
|
|
var newCommentsSince = threadItemSet.findCommentById( options.newCommentsSinceId );
|
2022-03-01 14:25:27 +00:00
|
|
|
if ( newCommentsSince && newCommentsSince instanceof CommentItem ) {
|
|
|
|
var sinceTimestamp = newCommentsSince.timestamp;
|
|
|
|
var threadItems;
|
2023-03-30 15:40:52 +00:00
|
|
|
if ( options.inThread ) {
|
2022-03-01 14:25:27 +00:00
|
|
|
var heading = newCommentsSince.getSubscribableHeading() || newCommentsSince.getHeading();
|
|
|
|
threadItems = heading.getThreadItemsBelow();
|
|
|
|
} else {
|
|
|
|
threadItems = threadItemSet.getCommentItems();
|
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
threadItems.forEach( ( threadItem ) => {
|
2022-03-01 14:25:27 +00:00
|
|
|
if (
|
|
|
|
threadItem instanceof CommentItem &&
|
|
|
|
threadItem.timestamp >= sinceTimestamp
|
|
|
|
) {
|
2023-03-30 16:04:36 +00:00
|
|
|
if ( options.sinceThread ) {
|
|
|
|
// Check that we are in a thread that is newer than `sinceTimestamp`.
|
|
|
|
// Thread age is determined by looking at getOldestReply.
|
|
|
|
var itemHeading = threadItem.getSubscribableHeading() || threadItem.getHeading();
|
|
|
|
var oldestReply = itemHeading.getOldestReply();
|
|
|
|
if ( !( oldestReply && oldestReply.timestamp >= sinceTimestamp ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 14:25:27 +00:00
|
|
|
newCommentIds.push( threadItem.id );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 15:13:09 +00:00
|
|
|
var comments;
|
2022-03-01 14:25:27 +00:00
|
|
|
if ( newCommentIds.length ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
comments = newCommentIds
|
|
|
|
.map( ( id ) => threadItemSet.findCommentById( id ) )
|
|
|
|
.filter( ( cmt ) => !!cmt );
|
2023-06-26 15:13:09 +00:00
|
|
|
if ( comments.length ) {
|
|
|
|
highlightedTarget = new Highlight( comments );
|
|
|
|
highlightedTarget.$element.addClass( 'ext-discussiontools-init-targetcomment' );
|
|
|
|
highlightedTarget.$element.addClass( 'ext-discussiontools-init-highlight-fadein' );
|
2022-03-01 14:25:27 +00:00
|
|
|
|
2023-06-26 15:13:09 +00:00
|
|
|
if ( !noScroll ) {
|
|
|
|
highlightedTarget.scrollIntoView();
|
|
|
|
}
|
2022-03-01 14:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-26 15:13:09 +00:00
|
|
|
return {
|
|
|
|
requested: newCommentIds,
|
|
|
|
requestedSince: options.newCommentsSinceId,
|
|
|
|
highlighted: comments || []
|
|
|
|
};
|
2022-03-01 14:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the highlighting of the comment in the URL hash
|
|
|
|
*
|
|
|
|
* @param {ThreadItemSet} threadItemSet
|
|
|
|
*/
|
|
|
|
function clearHighlightTargetComment( threadItemSet ) {
|
2022-03-29 21:25:21 +00:00
|
|
|
var url = new URL( location.href );
|
2022-08-04 12:58:36 +00:00
|
|
|
|
2022-08-16 11:48:51 +00:00
|
|
|
var targetElement = mw.util.getTargetFromFragment();
|
2022-08-04 12:58:36 +00:00
|
|
|
|
2022-03-01 14:25:27 +00:00
|
|
|
if ( targetElement && targetElement.hasAttribute( 'data-mw-comment-start' ) ) {
|
|
|
|
// Clear the hash from the URL, triggering the 'hashchange' event and updating the :target
|
|
|
|
// selector (so that our code to clear our highlight works), but without scrolling anywhere.
|
|
|
|
// This is tricky because:
|
|
|
|
// * Using history.pushState() does not trigger 'hashchange' or update the :target selector.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#description
|
|
|
|
// https://github.com/whatwg/html/issues/639
|
2022-04-20 17:49:00 +00:00
|
|
|
// * Using location.hash does, but it also scrolls to the target, which is the top of the
|
2022-03-01 14:25:27 +00:00
|
|
|
// page for the empty hash.
|
2022-04-20 17:49:00 +00:00
|
|
|
// Instead, we first use location.hash to navigate to a *different* hash (whose target
|
2022-03-01 14:25:27 +00:00
|
|
|
// doesn't exist on the page, hopefully), and then use history.pushState() to clear it.
|
2022-04-20 17:49:00 +00:00
|
|
|
location.hash += '-DoesNotExist-DiscussionToolsHack';
|
2022-03-29 21:25:21 +00:00
|
|
|
url.hash = '';
|
2022-08-09 12:37:57 +00:00
|
|
|
history.replaceState( null, '', url );
|
2022-03-01 14:25:27 +00:00
|
|
|
} else if (
|
2022-03-29 21:25:21 +00:00
|
|
|
url.searchParams.has( 'dtnewcomments' ) ||
|
|
|
|
url.searchParams.has( 'dtnewcommentssince' )
|
2022-03-01 14:25:27 +00:00
|
|
|
) {
|
2022-03-29 21:25:21 +00:00
|
|
|
url.searchParams.delete( 'dtnewcomments' );
|
|
|
|
url.searchParams.delete( 'dtnewcommentssince' );
|
|
|
|
url.searchParams.delete( 'dtinthread' );
|
2023-03-30 16:04:36 +00:00
|
|
|
url.searchParams.delete( 'dtsincethread' );
|
2022-08-09 12:37:57 +00:00
|
|
|
history.pushState( null, '', url );
|
2022-03-01 14:25:27 +00:00
|
|
|
highlightTargetComment( threadItemSet );
|
2022-05-06 16:44:21 +00:00
|
|
|
} else if ( highlightedTarget ) {
|
|
|
|
// Highlights were applied without changing the URL, e.g. when showing
|
|
|
|
// new comments while drafting. Just clear the highlights.
|
|
|
|
highlightedTarget.destroy();
|
|
|
|
highlightedTarget = null;
|
2022-03-01 14:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:54:52 +00:00
|
|
|
/**
|
|
|
|
* Get the last highlighted just-published comment, if any
|
|
|
|
*
|
2022-03-14 14:43:35 +00:00
|
|
|
* Used to show an auto-subscription popup to first-time users
|
|
|
|
*
|
2022-03-01 15:54:52 +00:00
|
|
|
* @return {ThreadItem|null}
|
|
|
|
*/
|
|
|
|
function getLastHighlightedPublishedComment() {
|
|
|
|
return lastHighlightedPublishedComment;
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:25:27 +00:00
|
|
|
module.exports = {
|
|
|
|
highlightTargetComment: highlightTargetComment,
|
2022-03-01 14:44:21 +00:00
|
|
|
highlightPublishedComment: highlightPublishedComment,
|
2022-02-23 22:41:47 +00:00
|
|
|
highlightNewComments: highlightNewComments,
|
2022-03-01 15:54:52 +00:00
|
|
|
clearHighlightTargetComment: clearHighlightTargetComment,
|
|
|
|
getLastHighlightedPublishedComment: getLastHighlightedPublishedComment
|
2022-03-01 14:25:27 +00:00
|
|
|
};
|