mediawiki-extensions-Discus.../modules/permalinks.js
Ed Sanders ca5157156a ESLint: Autofix no-var rule
Leave rule off for now as manual fixes are required.

Also temporarily disable prefer-const rule as that
will also require some manual fixes.

Change-Id: I8c3478f26f51287acb943bd38c9c1020c06b9f39
2024-05-24 16:49:36 +01:00

57 lines
1.6 KiB
JavaScript

function init( $pageContainer ) {
$pageContainer.find( '.ext-discussiontools-init-timestamplink' ).on( 'click', ( e ) => {
copyLink( e.target.href );
} ).attr( 'data-event-name', 'discussiontools.permalink-copied' );
}
function copyLink( link ) {
const $win = $( window );
const scrollTop = $win.scrollTop();
const $tmpInput = $( '<input>' )
.val( link )
.addClass( 'noime' )
.css( {
position: 'fixed',
top: 0
} )
.appendTo( 'body' )
.trigger( 'focus' );
$tmpInput[ 0 ].setSelectionRange( 0, link.length );
let copied;
try {
copied = document.execCommand( 'copy' );
} catch ( err ) {
copied = false;
}
if ( copied ) {
mw.notify( mw.msg( 'discussiontools-permalink-comment-copied' ) );
}
$tmpInput.remove();
// Restore scroll position, can be changed by setSelectionRange, or hash navigation
function afterNextScroll() {
// On desktop we can restore scroll immediately after the scroll
// event, preventing a scroll flicker.
$win.scrollTop( scrollTop );
// On mobile, we need to wait another execution cycle (setTimeout)
// before the scroll is rendered (and not requestAnimationFrame).
setTimeout( () => {
$win.scrollTop( scrollTop );
} );
}
// Restore scroll position when the scroll event fires.
// setTimeout does't reliably wait long enough for the native
// scroll to happen.
$win.one( 'scroll', afterNextScroll );
// If we happened to be in the exact correct position, 'scroll' won't fire,
// so clear the listener after a short delay
setTimeout( () => {
$win.off( 'scroll', afterNextScroll );
}, 1000 );
}
module.exports = {
init: init
};