mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-24 08:23:52 +00:00
Permalinks: Factor out copyLink clipboard functionality
Change-Id: I8092e8f831b67a32b652a5cb88808f6e463d9b4c
This commit is contained in:
parent
58cb277ced
commit
d4819367aa
|
@ -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 );
|
||||
} );
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue