Permalinks: Factor out copyLink clipboard functionality

Change-Id: I8092e8f831b67a32b652a5cb88808f6e463d9b4c
This commit is contained in:
Ed Sanders 2023-10-05 10:56:12 +01:00
parent 58cb277ced
commit d4819367aa

View file

@ -1,31 +1,37 @@
function init( $pageContainer ) {
$pageContainer.find( '.ext-discussiontools-init-timestamplink' ).on( 'click', function () {
var $win = $( window );
var scrollTop = $win.scrollTop();
var $tmpInput = $( '<input>' )
.val( this.href )
.addClass( 'noime' )
.css( {
position: 'fixed',
top: 0
} )
.appendTo( 'body' )
.trigger( 'focus' );
$tmpInput[ 0 ].setSelectionRange( 0, this.href.length );
var 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
requestAnimationFrame( function () {
$win.scrollTop( scrollTop );
} );
copyLink( this.href );
} );
}
function copyLink( link ) {
var $win = $( window );
var scrollTop = $win.scrollTop();
var $tmpInput = $( '<input>' )
.val( link )
.addClass( 'noime' )
.css( {
position: 'fixed',
top: 0
} )
.appendTo( 'body' )
.trigger( 'focus' );
$tmpInput[ 0 ].setSelectionRange( 0, link.length );
var 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
requestAnimationFrame( function () {
$win.scrollTop( scrollTop );
} );
}