| 1 |
$( () => { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
function parseId( id ) { |
| 10 |
const matches = id.match( /(.*)-([0-9]+)/ ); |
| 11 |
return { |
| 12 |
prefix: matches[ 1 ], |
| 13 |
number: +matches[ 2 ] |
| 14 |
}; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
function buildId( parsedId ) { |
| 24 |
return parsedId.prefix + '-' + parsedId.number; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
function getLineElement( id ) { |
| 34 |
const line = mw.util.getTargetFromFragment( id ); |
| 35 |
t. |
| 36 |
if ( !$( line ).closest( '.mw-highlight' ).length ) { |
| 37 |
|
| 38 |
return null; |
| 39 |
} |
| 40 |
return line; |
| 41 |
} |
| 42 |
|
| 43 |
let lastLines, lastAnchorLine; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
function onHashChange( scrollIntoView ) { |
| 51 |
const hash = location.hash.slice( 1 ); |
| 52 |
|
| 53 |
const lines = []; |
| 54 |
let anchorLine, focusLine; |
| 55 |
const parts = hash.split( '--' ); |
| 56 |
if ( parts.length === 2 ) { |
| 57 |
anchorLine = getLineElement( parts[ 0 ] ); |
| 58 |
focusLine = getLineElement( parts[ 1 ] ); |
| 59 |
if ( anchorLine && focusLine ) { |
| 60 |
const anchorId = parseId( parts[ 0 ] ); |
| 61 |
const focusId = parseId( parts[ 1 ] ); |
| 62 |
if ( anchorId.prefix === focusId.prefix ) { |
| 63 |
for ( let i = Math.min( anchorId.number, focusId.number ); i <= Math.max( anchorId.number, focusId.number ); i++ ) { |
| 64 |
lines.push( mw.util.getTargetFromFragment( buildId( { prefix: anchorId.prefix, number: i } ) ) ); |
| 65 |
} |
| 66 |
if ( scrollIntoView ) { |
| 67 |
|
| 68 |
lines[ 0 ].scrollIntoView(); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} else { |
| 73 |
anchorLine = getLineElement(); |
| 74 |
if ( anchorLine ) { |
| 75 |
lines.push( anchorLine ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
lastAnchorLine = anchorLine; |
| 80 |
|
| 81 |
if ( lastLines ) { |
| 82 |
lastLines.forEach( ( line ) => line.classList.remove( 'hll' ) ); |
| 83 |
} |
| 84 |
lines.forEach( ( line ) => line.classList.add( 'hll' ) ); |
| 85 |
|
| 86 |
lastLines = lines; |
| 87 |
} |
| 88 |
|
| 89 |
window.addEventListener( 'hashchange', onHashChange ); |
| 90 |
|
| 91 |
$( document.body ).on( 'click', '.mw-highlight .linenos', ( e ) => { |
| 92 |
e.preventDefault(); |
| 93 |
|
| 94 |
const targetUrl = new URL( e.target.parentNode.href ); |
| 95 |
|
| 96 |
if ( e.shiftKey && lastAnchorLine ) { |
| 97 |
const anchorId = parseId( lastAnchorLine.getAttribute( 'id' ) ); |
| 98 |
const focusId = parseId( targetUrl.hash.slice( 1 ) ); |
| 99 |
if ( anchorId.prefix === focusId.prefix ) { |
| 100 |
const hash = buildId( anchorId ) + '--' + buildId( focusId ); |
| 101 |
history.replaceState( null, '', '#' + hash ); |
| 102 |
} else { |
| 103 |
history.replaceState( null, '', targetUrl ); |
| 104 |
} |
| 105 |
} else { |
| 106 |
history.replaceState( null, '', targetUrl ); |
| 107 |
} |
| 108 |
|
| 109 |
onHashChange(); |
| 110 |
} ); |
| 111 |
|
| 112 |
|
| 113 |
onHashChange( true ); |
| 114 |
|
| 115 |
} ); |