User:Enterprisey/diff-context.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Enterprisey/diff-context. |
// vim: ts=4 sw=4 et ai
// TODO: compat w/ User:Writ Keeper/Scripts/commonHistory.js
( function () {
var wikitextCache = {};
function getWikitext( revision ) {
if( wikitextCache[ revision ] ) {
return $.when( wikitextCache[ revision ] );
}
return $.getJSON(
mw.util.wikiScript( "api" ),
{
format: "json",
action: "query",
prop: "revisions",
rvprop: "content",
rvslots: "main",
rvlimit: 1,
rvstartid: revision,
titles: mw.config.get( "wgPageName" ),
formatversion: 2,
}
).then( function ( data ) {
var revObj = Object.values( data.query.pages )[0].revisions[0];
var wikitext = revObj.slots.main.content;
wikitextCache[ revision ] = wikitext;
return wikitext;
} );
}
function lineNumFromLineNumCell( cell ) {
return parseInt( cell.textContent.replace( /,/g, "" ).match( /Line (\d+):/ )[1] );
}
function findLineNumberRowIdx( diffTable, direction, rowIdx ) {
var rows = Array.prototype.slice.call( diffTable.rows );
var searchRows;
if( direction === "next" ) {
searchRows = rows.slice( rowIdx );
} else {
searchRows = rows.slice( 0, rowIdx );
searchRows.reverse();
}
return searchRows.find( function ( row ) {
return row.cells[0].className === "diff-lineno";
} );
}
function showMore( direction, rowWithButton, numRowsToShow, targetRow, diffTable ) {
var rows = Array.prototype.slice.call( diffTable.rows );
var buttonRowIdx = rows.indexOf( rowWithButton );
if( direction === "next" ) {
targetRow = findLineNumberRowIdx( diffTable, "previous", buttonRowIdx );
}
var currLineNum = lineNumFromLineNumCell( targetRow.cells[1] );
var targetRowIdx = Array.prototype.indexOf.call( diffTable.rows, targetRow );
var currRowIdx = direction === "next" ? buttonRowIdx - 1 : targetRowIdx + 1;
if( direction === "next" ) {
currLineNum += buttonRowIdx - rows.indexOf( targetRow ) - 2;
}
return getWikitext( mw.config.get( "wgDiffNewId" ) ).then( function ( wikitext ) {
var lines = wikitext.split( "\n" );
var startLineIdx, endLineIdx;
switch( direction ) {
case "next":
startLineIdx = currLineNum + 1;
endLineIdx = currLineNum + numRowsToShow + 1;
var nextLineNumberRow = findLineNumberRowIdx( diffTable, "next", buttonRowIdx );
if( nextLineNumberRow ) {
var limitLineNum = lineNumFromLineNumCell( nextLineNumberRow.cells[1] ) - 1;
endLineIdx = Math.min( endLineIdx, limitLineNum );
}
break;
case "previous":
startLineIdx = currLineNum - numRowsToShow - 1;
endLineIdx = currLineNum - 1;
var prevLineNumberRow = findLineNumberRowIdx( diffTable, "previous", buttonRowIdx );
if( prevLineNumberRow ) {
var prevLineNumberRowIdx = rows.indexOf( prevLineNumberRow );
var limitLineNum = lineNumFromLineNumCell( prevLineNumberRow.cells[1] ) + ( buttonRowIdx - prevLineNumberRowIdx ) - 2;
startLineIdx = Math.max( startLineIdx, limitLineNum );
}
break;
}
startLineIdx = Math.max( 0, startLineIdx );
endLineIdx = Math.min( lines.length - 1, endLineIdx );
if( endLineIdx - startLineIdx === 0 ) {
return true; // no more work can be done
}
var actualNumLines = endLineIdx - startLineIdx;
var linesToShow = lines.slice( startLineIdx, endLineIdx );
var insertionRow = currRowIdx + +( direction === "next" );
for( var lineCounter = 0; lineCounter < actualNumLines; lineCounter++ ) {
var lineIdx = direction === "previous" ? lineCounter : actualNumLines - lineCounter - 1;
var currLine = "<div>" + lines[ startLineIdx + lineIdx ].replace( /</g, "<" ) + "</div>";
var newRow = diffTable.insertRow( insertionRow );
newRow.insertCell( 0 ).appendChild( document.createTextNode( "." ) );
var newCell = newRow.insertCell( 1 );
newCell.className = "diff-context";
newCell.innerHTML = currLine;
newRow.insertCell( 2 ).appendChild( document.createTextNode( "." ) );
var newCell2 = newRow.insertCell( 3 );
newCell2.className = "diff-context";
newCell2.innerHTML = currLine;
}
if( direction === "previous" ) {
targetRow.cells[0].textContent = "Line " + ( currLineNum - actualNumLines ) + ":";
targetRow.cells[1].textContent = "Line " + ( currLineNum - actualNumLines ) + ":";
}
mw.hook( "diff-update" ).fire( diffTable );
if( actualNumLines < numRowsToShow ) {
return true; // we're done
}
return false;
} );
}
function makeDropdown() {
var sel = document.createElement( "select" );
function add( num ) {
var opt = document.createElement( "option" );
opt.value = num;
opt.textContent = num;
sel.appendChild( opt );
}
add( 1 );
add( 10 );
add( 50 );
add( 100 );
//add( "all" );
return sel;
}
function addLinks( diffTable ) {
function makeRow( index, direction, targetRow ) {
var newRow = diffTable.insertRow( index );
newRow.className = "context " + direction;
var newCell = newRow.insertCell( 0 );
newCell.setAttribute( "colspan", "4" );
var container = document.createElement( "div" );
container.className = "container";
container.appendChild( document.createTextNode( "Show " + direction + " " ) );
var dropdown = makeDropdown();
container.appendChild( dropdown );
container.appendChild( document.createTextNode( " lines: " ) );
var go = document.createElement( "button" );
go.textContent = "Go";
go.addEventListener( "click", function () {
var numRowsToShow = parseInt( dropdown.value );
showMore( direction, newRow, numRowsToShow, targetRow, diffTable ).then( function ( done ) {
if( done ) {
this.disabled = true;
}
}.bind( this ) );
return false;
} );
container.appendChild( go );
newCell.appendChild( container );
}
var firstTime = true;
for( var rowIdx = 0, rowCount = diffTable.rows.length; rowIdx < rowCount; rowIdx++ ) {
var row = diffTable.rows[rowIdx];
if( row.cells[0].className !== "diff-lineno" ) continue;
if( !firstTime ) {
makeRow( rowIdx, "next", row );
rowIdx++;
} else {
firstTime = false;
}
makeRow( rowIdx, "previous", row );
rowIdx++;
}
var lastLineNumRow = Array.prototype.slice.call( diffTable.querySelectorAll( "td.diff-lineno" ), -1 )[0].parentNode;
makeRow( -1, "next", lastLineNumRow );
}
$( function () {
var diffTable = document.querySelector( "table.diff" );
if( mw.config.get( "wgDiffNewId" ) && diffTable && diffTable.rows.length > 2 ) {
mw.loader.addStyleTag( "tr.context td { text-align: center; }" );
mw.loader.addStyleTag( "tr.context td div.container { display: inline-block; border: thin solid gray; padding: 0.5em; }" );
addLinks( diffTable );
}
mw.hook( "new-diff-table" ).add( addLinks );
} );
} )();