Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Citizen.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 45: Line 45:
}
}


// Helper: Proper scroll to real heading element, ignoring ghost anchors
// Helper: expand parent, wait two frames, scroll manually
function smoothScrollToHeading(heading) {
function smoothScrollToHeading(heading) {
   if (!heading) return;
   if (!heading) return;


   // If the heading has no visible size (hidden anchor), find the first real heading
   // If heading has 0px height (ghost anchor), find next real heading
   if (heading.offsetHeight === 0) {
   if (heading.offsetHeight === 0) {
     const nextHeading = heading.parentElement.querySelector('h1, h2, h3, h4, h5, h6');
     const nextHeading = heading.parentElement.querySelector('h1, h2, h3, h4, h5, h6');
Line 57: Line 57:
   }
   }


   setTimeout(() => {
   // Wait two animation frames for DOM to reflow fully
     heading.scrollIntoView({ behavior: 'smooth', block: 'start' });
  requestAnimationFrame(() => {
     requestAnimationFrame(() => {
      const rect = heading.getBoundingClientRect();
      const scrollTop = window.scrollY + rect.top - 60; // 60px offset for sticky header


    setTimeout(() => {
       window.scrollTo({ top: scrollTop, behavior: 'smooth' });
      const offset = 60; // Adjust this offset if needed
     });
       const maxScrollableUp = window.scrollY;
   });
 
      if (maxScrollableUp > offset) {
        window.scrollBy(0, -offset);
      } else {
        window.scrollTo({ top: 0, behavior: 'smooth' });
      }
     }, 300);
   }, 100); // Minor wait to ensure layout stabilizes
}
}





Revision as of 12:53, 26 April 2025

/* All JavaScript here will be loaded for users of the Citizen skin */

// Collapse specific headers by default on Desktop
mw.hook('wikipage.content').add(function($content) {
  if (window.innerWidth >= 768) {
    const idsToCollapse = [
      'Muling', 'Misc', 'Anti-ban', 'Anti-PK', 'Trip_Settings', 'Skills_Settings', 'Equipment'
    ];

    idsToCollapse.forEach(function(rawId) {
      const id = rawId.replace(/ /g, "_");
      const heading = document.getElementById(id);

      if (!heading) return;

      const sectionHeading = heading.closest('.citizen-section-heading');
      const content = sectionHeading?.nextElementSibling;

      if (content && content.classList.contains('citizen-section')) {
        content.hidden = 'until-found'; // collapse by default
      }
    });
  }
});

// Helper: Expand any collapsed parent citizen-section containing the heading
function expandCitizenSectionFromHeading(heading) {
  if (!heading) return;

  let current = heading;

  while (current) {
    // Expand if we find a collapsed citizen-section
    if (current.classList && current.classList.contains('citizen-section') && current.hidden) {
      current.hidden = false;
    }

    // Stop walking if we hit a section heading
    if (current.classList && current.classList.contains('citizen-section-heading')) {
      break;
    }

    current = current.parentElement;
  }
}

// Helper: expand parent, wait two frames, scroll manually
function smoothScrollToHeading(heading) {
  if (!heading) return;

  // If heading has 0px height (ghost anchor), find next real heading
  if (heading.offsetHeight === 0) {
    const nextHeading = heading.parentElement.querySelector('h1, h2, h3, h4, h5, h6');
    if (nextHeading) {
      heading = nextHeading;
    }
  }

  // Wait two animation frames for DOM to reflow fully
  requestAnimationFrame(() => {
    requestAnimationFrame(() => {
      const rect = heading.getBoundingClientRect();
      const scrollTop = window.scrollY + rect.top - 60; // 60px offset for sticky header

      window.scrollTo({ top: scrollTop, behavior: 'smooth' });
    });
  });
}


// === Handle direct URL loads with a hash (on page load) ===
mw.hook('wikipage.content').add(function() {
  if (window.innerWidth >= 768) {
    const hash = window.location.hash;
    if (hash && hash.length > 1) {
      const headingId = decodeURIComponent(hash.substring(1));
      setTimeout(() => {
        const heading = document.getElementById(headingId);
        if (heading) {
          expandCitizenSectionFromHeading(heading);

          setTimeout(() => {
            smoothScrollToHeading(heading);
          }, 50);
        }
      }, 100);
    }
  }
});

// === Hook into Citizen TOC (Contents Menu) clicks ===
mw.hook('wikipage.content').add(function() {
  if (window.innerWidth >= 768) {
    mw.loader.using('skins.citizen.toc').then(function() {
      const citizenToc = require('skins.citizen.toc');
      const originalOnHeadingClick = citizenToc.props.onHeadingClick;

      citizenToc.props.onHeadingClick = function(id) {
        const headingId = id.replace(/^toc-/, '');
        const heading = document.getElementById(headingId);
        if (heading) {
          expandCitizenSectionFromHeading(heading);

          setTimeout(() => {
            smoothScrollToHeading(heading);
          }, 50);
        }

        if (originalOnHeadingClick) {
          originalOnHeadingClick(id);
        }
      };
    });
  }
});

// === Safely intercept TOC clicks directly to fix expand timing ===
mw.hook('wikipage.content').add(function() {
  if (window.innerWidth >= 768) {
    setTimeout(() => {
      document.querySelectorAll('#mw-panel-toc a[href^="#"]').forEach(function(anchor) {
        anchor.addEventListener('click', function(e) {
          const hash = decodeURIComponent(this.getAttribute('href')).substring(1);
          const heading = document.getElementById(hash);

          if (heading) {
            e.preventDefault(); // Prevent native jump
            expandCitizenSectionFromHeading(heading);

            setTimeout(() => {
              smoothScrollToHeading(heading);
            }, 50);
          }
        });
      });
    }, 200);
  }
});