MediaWiki:Common.js: Difference between revisions
No edit summary Tag: Reverted |
Undo revision 4772 by Chandrashekars (talk) Tag: Undo |
||
| Line 182: | Line 182: | ||
// ── TOC active-item highlight + lazy TOC tagging ─────────────── | // ── TOC active-item highlight + lazy TOC tagging ─────────────── | ||
// | // MutationObserver watches Vector 2022 TOC class changes and colours | ||
// the active link orange. This block is not modified for the highlight | |||
// logic, but we additionally tag any TOC text nodes that were not yet | |||
// in the DOM when tagTextNodes() ran at boot (Vector 2022 renders the | |||
// TOC lazily on first scroll). | |||
function watchTocActive() { | function watchTocActive() { | ||
var toc = document.querySelector( '.vector-toc' ); | var toc = document.querySelector( '.vector-toc' ); | ||
| Line 188: | Line 192: | ||
toc._grObserved = true; | toc._grObserved = true; | ||
// Tag any TOC text nodes that appeared after initial tagTextNodes() run | |||
function tagAndApplyToc() { | function tagAndApplyToc() { | ||
var walker = document.createTreeWalker( toc, NodeFilter.SHOW_TEXT ); | var walker = document.createTreeWalker( toc, NodeFilter.SHOW_TEXT ); | ||
var nodes = []; | var nodes = []; | ||
| Line 209: | Line 211: | ||
newSpans.push( span ); | newSpans.push( span ); | ||
} ); | } ); | ||
// Apply current script to any newly tagged spans | |||
if ( currentScript !== 'deva' && newSpans.length ) { | if ( currentScript !== 'deva' && newSpans.length ) { | ||
newSpans.forEach( function ( span ) { | newSpans.forEach( function ( span ) { | ||
| Line 214: | Line 217: | ||
} ); | } ); | ||
} | } | ||
} | } | ||
// Tag immediately | // Tag immediately (catches TOC if it was already rendered) | ||
tagAndApplyToc(); | tagAndApplyToc(); | ||
var observer = new MutationObserver( function ( mutations ) { | |||
mutations.forEach( function ( m ) { | |||
// Tag any new text nodes added to the TOC (lazy render) | |||
if ( m.addedNodes && m.addedNodes.length ) { | |||
tagAndApplyToc(); | |||
if ( m.attributeName | } | ||
// Highlight active item | |||
if ( m.attributeName === 'class' ) { | |||
var li = m.target; | var li = m.target; | ||
var link = li.querySelector( ':scope > .vector-toc-link' ); | var link = li.querySelector( ':scope > .vector-toc-link' ); | ||
| Line 236: | Line 240: | ||
link.style.fontWeight = ''; | link.style.fontWeight = ''; | ||
} | } | ||
} | } | ||
} ); | } ); | ||
} ); | |||
toc.querySelectorAll( '.vector-toc-list-item' ).forEach( function ( li ) { | |||
observer.observe( li, { attributes: true, attributeFilter: [ 'class' ] } ); | |||
} ); | |||
// Also watch for new list items being added (TOC populated lazily) | |||
observer.observe( toc, { childList: true, subtree: true } ); | |||
} | } | ||
// ── Init ──────────────────────────────────────────────────────── | |||
function init() { | |||
var content = document.querySelector( '.mw-parser-output' ); | |||
var alreadyTagged = content && content.querySelector( '[data-deva]' ); | |||
if ( !alreadyTagged ) { | if ( !alreadyTagged ) { | ||
translatableSpans = []; | translatableSpans = []; | ||