MediaWiki:Gadget-copyTable.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
Line 1:
/**
* Gadget: CopyTable
* Discreet copy icon at the bottomtop-right of every wikitable.
* Copies content as TSV for pasting into Excel / Sheets.
*/
Line 34:
}
 
mw.hook( 'wikipage.content' ).add( function ( $content ) {
$content.find( 'table.wikitable' ).not( '.copy-table-added' ).each( function () {
var $table = $( this ).addClass( 'copy-table-added' );
 
var $btn = $( '<button>' )
.addClass( 'copy-table-btn' )
.attr( 'title', 'Copy table' )
.html( COPY_ICON )
.on( 'click', function () {
var tsv = tableToTSV( $table[ 0 ] );
navigator.clipboard.writeText( tsv ).then( function () {
$btn.html( CHECK_ICON ).addClass( 'copy-table-btn--ok' );
setTimeout( function () {
$btn.html( COPY_ICON ).removeClass( 'copy-table-btn--ok' );
}, 2000 );
} );
} );
} );
 
// If the table sits// inTwo aboxes: horizontalan inner .copy-table-scroll boxthat (thescrolls househorizontally, styleand wraps ita
// in <div style="overflow-x:auto">), wrap// THATnon-scrolling box.copy-table-wrapper sothat anchors the pinned button. livesThe
// OUTSIDEbutton theis scrollinga areaSIBLING andof staysthe pinned.scroller Otherwise wrap(inside the table.wrapper), so it stays at
// the visible top-right while the table scrolls underneath. If the table is
var $parent = $table.parent();
// already inside a scroll box, reuse that box as the scroller.
var $target = /auto|scroll/.test( $parent.css( 'overflow-x' ) ) ? $parent : $table;
var $parent = $table.parent();
 
var $scroller;
$target.wrap( '<div class="copy-table-wrapper">' );
var $target = if ( /auto|scroll/.test( $parent.css( 'overflow-x' ) ) ?) $parent : $table;{
$target.parent().append( $btn ); // button is now a sibling of the scroller
$scroller = $parent;
} else {
$table.wrap( '<div class="copy-table-scroll">' );
$scroller = $table.parent();
}
$targetscroller.wrap( '<div class="copy-table-wrapper">' );
$scroller.parent().append( $btn );
} );
} );
} );
}() );