MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 178: | Line 178: | ||
} | } | ||
// ── TOC active-item highlight | // ── TOC active-item highlight ──────────────────────────────────── | ||
// FIX: Instead of relying on CSS :active selectors (which fail because | |||
// Common.js wraps text nodes in <span data-deva>, making > a or > .link | |||
// selectors not match the coloured text), we use a MutationObserver to | |||
// watch each <li> for class changes and imperatively apply/remove the | |||
// orange colour via inline style on the .vector-toc-link inside it. | |||
// This is immune to DOM depth and span nesting. | |||
function watchTocActive() { | function watchTocActive() { | ||
var toc = document.querySelector( '.vector-toc' ); | var toc = document.querySelector( '.vector-toc' ); | ||
if ( !toc | if ( !toc ) return; | ||
if ( !window.MutationObserver ) return; | |||
/* Guard only the structObserver — attach it once per toc element. | |||
Per-<li> attachment is already guarded by _grHighlightAttached, | |||
so calling watchTocActive() multiple times is safe and picks up | |||
any <li> items that weren't in the DOM on the first call. */ | |||
if ( toc._grObserved ) { | |||
/* Re-scan for any <li> items that arrived after first call */ | |||
toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight ); | |||
return; | |||
} | |||
toc._grObserved = true; | toc._grObserved = true; | ||
if ( ! | |||
var ACTIVE_COLOR = '#f57c00'; | |||
var ACTIVE_WEIGHT = '700'; | |||
function setLinkActive( li, active ) { | |||
var link = li.querySelector( '.vector-toc-link' ) || li.querySelector( 'a' ); | |||
if ( !link ) return; | |||
if ( active ) { | |||
link.style.setProperty( 'color', ACTIVE_COLOR, 'important' ); | |||
link.style.setProperty( 'font-weight', ACTIVE_WEIGHT, 'important' ); | |||
/* Also colour nested spans (e.g. data-deva spans from transliteration) */ | |||
link.querySelectorAll( 'span' ).forEach( function ( s ) { | |||
s.style.setProperty( 'color', ACTIVE_COLOR, 'important' ); | |||
} ); | |||
} else { | |||
link.style.removeProperty( 'color' ); | |||
link.style.removeProperty( 'font-weight' ); | |||
link.querySelectorAll( 'span' ).forEach( function ( s ) { | |||
s.style.removeProperty( 'color' ); | |||
} ); | |||
} | |||
} | |||
function attachHighlight( li ) { | function attachHighlight( li ) { | ||
if ( li._grHighlightAttached ) return; | |||
li._grHighlightAttached = true; | |||
liObserver.observe( li, { attributes: true, attributeFilter: [ 'class' ] } ); | |||
/* Apply immediately if already active on attachment */ | |||
if ( li.classList.contains( 'vector-toc-list-item-active' ) ) { | |||
setLinkActive( li, true ); | |||
} | |||
} | } | ||
var | var liObserver = new MutationObserver( function ( mutations ) { | ||
mutations.forEach( function ( m ) { | mutations.forEach( function ( m ) { | ||
if ( m.attributeName !== 'class' ) return; | |||
var li = m.target; | |||
var isActive = li.classList.contains( 'vector-toc-list-item-active' ); | |||
setLinkActive( li, isActive ); | |||
if ( | |||
/* Scroll active item into view if it's off screen */ | |||
if ( isActive ) { | |||
var container = document.querySelector( '.vector-sticky-pinned-container' ); | |||
if ( container ) { | |||
var liRect = li.getBoundingClientRect(); | |||
var cRect = container.getBoundingClientRect(); | |||
if ( liRect.top < cRect.top + 8 || liRect.bottom > cRect.bottom - 8 ) { | |||
li.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); | |||
} | } | ||
} | |||
} | |||
} ); | |||
} ); | |||
/ | var structObserver = new MutationObserver( function ( mutations ) { | ||
mutations.forEach( function ( m ) { | |||
if ( m.type !== 'childList' ) return; | |||
m.addedNodes.forEach( function ( n ) { | |||
if ( n.nodeType !== 1 ) return; | |||
/* Attach highlight observer to newly added list items */ | |||
if ( n.classList && n.classList.contains( 'vector-toc-list-item' ) ) { | |||
attachHighlight( n ); | |||
} | |||
if ( n.querySelectorAll ) { | |||
n.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight ); | |||
} | |||
/* Tag any new .vector-toc-text spans for transliteration */ | |||
var newSpans = []; | |||
if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) newSpans.push( n ); | |||
if ( n.querySelectorAll ) { | |||
n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) { newSpans.push( s ); } ); | |||
} | |||
newSpans.forEach( function ( span ) { | |||
if ( span.hasAttribute( 'data-deva' ) ) return; | |||
var orig = span.textContent; | |||
if ( !orig.trim() ) return; | |||
span.setAttribute( 'data-deva', orig ); | |||
if ( currentScript !== 'deva' ) { | |||
span.textContent = transliterateText( orig, currentScript ); | |||
} | } | ||
translatableSpans.push( span ); | |||
} ); | } ); | ||
} ); | |||
} ); | } ); | ||
} ); | } ); | ||
toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight ); | toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight ); | ||
structObserver.observe( toc, { childList: true, subtree: true } ); | |||
/ | /* On initial load, scroll to the already-active item */ | ||
setTimeout( function () { | setTimeout( function () { | ||
var active = toc.querySelector( '.vector-toc-list-item-active' ); | var active = toc.querySelector( '.vector-toc-list-item-active' ); | ||
if ( active ) active.scrollIntoView({ block: 'nearest', behavior: 'auto' }); | if ( active ) { | ||
}, | setLinkActive( active, true ); | ||
active.scrollIntoView({ block: 'nearest', behavior: 'auto' }); | |||
} | |||
}, 300 ); | |||
} | } | ||
// ── Init | // ── Init ──────────────────────────────────────────────────────── | ||
function init() { | function init() { | ||
var content = document.querySelector( '.mw-parser-output' ); | var content = document.querySelector( '.mw-parser-output' ); | ||
| Line 261: | Line 307: | ||
tagTextNodes(); | tagTextNodes(); | ||
} else { | } else { | ||
document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) { | document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) { | ||
var orig = span.textContent; | var orig = span.textContent; | ||
| Line 270: | Line 315: | ||
} | } | ||
var saved = ( function () { | var saved = ( function () { | ||
try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; } | try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; } | ||
| Line 281: | Line 325: | ||
watchTocActive(); | watchTocActive(); | ||
/* Retry for Vector 2022 TOC which renders after DOMContentLoaded */ | |||
setTimeout( watchTocActive, 300 ); | |||
setTimeout( watchTocActive, 800 ); | |||
} | } | ||
// ── React to toolbar dropdown changes | // ── React to toolbar dropdown changes ────────────────────────── | ||
window.addEventListener( 'gr-script-change', function ( e ) { | window.addEventListener( 'gr-script-change', function ( e ) { | ||
var script = e && e.detail && e.detail.script; | var script = e && e.detail && e.detail.script; | ||
| Line 289: | Line 336: | ||
} ); | } ); | ||
// ── MediaWiki hook (SPA-style navigation | // ── MediaWiki hook (SPA-style navigation) ─────────────────────── | ||
if ( window.mw ) { | if ( window.mw ) { | ||
mw.hook( 'wikipage.content' ).add( function () { | mw.hook( 'wikipage.content' ).add( function () { | ||
setTimeout( function () { | setTimeout( function () { | ||
var content | var content = document.querySelector( '.mw-parser-output' ); | ||
var alreadyTagged = content && content.querySelector( '[data-deva]' ); | var alreadyTagged = content && content.querySelector( '[data-deva]' ); | ||
if ( !alreadyTagged ) { | if ( !alreadyTagged ) { | ||
| Line 299: | Line 346: | ||
tagTextNodes(); | tagTextNodes(); | ||
} else { | } else { | ||
document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) { | document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) { | ||
var orig = span.textContent; | var orig = span.textContent; | ||
| Line 307: | Line 353: | ||
} ); | } ); | ||
} | } | ||
if ( currentScript !== 'deva' ) applyScript( currentScript ); | |||
if ( currentScript !== 'deva' ) | |||
watchTocActive(); | watchTocActive(); | ||
}, 150 ); | }, 150 ); | ||
| Line 316: | Line 359: | ||
} | } | ||
if ( document.readyState === 'loading' ) { | if ( document.readyState === 'loading' ) { | ||
document.addEventListener( 'DOMContentLoaded', init ); | document.addEventListener( 'DOMContentLoaded', init ); | ||