fix
This commit is contained in:
1 parent
25bb0213d4
commit
31e185664a
19 files changed
+12
-672
No files matched your search
+1
-168
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* AKSRT Page Template
|
||||
*
|
||||
* Handles template-scoped content interactions: TOC, lightbox,
|
||||
* Handles template-scoped content interactions: lightbox,
|
||||
* expandable article bodies, and one-shot query cleanup banners.
|
||||
*
|
||||
* @package AKSRT
|
||||
@@ -10,172 +10,6 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function slugifyHeading(text) {
|
||||
return 'heading-' + String(text || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^\w\u4e00-\u9fff-]/g, '');
|
||||
}
|
||||
|
||||
function initPostToc() {
|
||||
// Accept either explicit data-aksrt-toc="1" or any [data-nav] article body.
|
||||
var content =
|
||||
document.querySelector('.wp-posts-content[data-nav="posts"][data-aksrt-toc="1"]') ||
|
||||
document.querySelector('.wp-posts-content[data-nav]') ||
|
||||
document.querySelector('[data-nav="posts"]') ||
|
||||
document.querySelector('[data-nav]');
|
||||
|
||||
// The article sidebar and mobile drawer share one built-in TOC source.
|
||||
var tocWidgets = Array.prototype.slice.call(
|
||||
document.querySelectorAll('.posts-nav-box.toc-widget')
|
||||
);
|
||||
var tocLists = [];
|
||||
tocWidgets.forEach(function (box) {
|
||||
if (!box.classList.contains('toc-widget')) {
|
||||
box.classList.add('toc-widget');
|
||||
}
|
||||
var list = box.querySelector('.toc-list');
|
||||
if (!list) {
|
||||
list = document.createElement('ol');
|
||||
list.className = 'toc-list';
|
||||
box.appendChild(list);
|
||||
}
|
||||
tocLists.push(list);
|
||||
});
|
||||
|
||||
var mobileToggle = document.querySelector('.toc-float-btn');
|
||||
var drawer = document.querySelector('.toc-drawer');
|
||||
var overlay = document.querySelector('.toc-drawer-overlay');
|
||||
var drawerList = drawer ? drawer.querySelector('.toc-list') : null;
|
||||
var closeBtn = drawer ? drawer.querySelector('.toc-drawer__close') : null;
|
||||
|
||||
if (!content || content.dataset.aksrtTocBound === '1') return;
|
||||
if (!tocLists.length && !drawerList && !mobileToggle) return;
|
||||
|
||||
content.dataset.aksrtTocBound = '1';
|
||||
|
||||
var maxDepth = parseInt(content.dataset.aksrtTocDepth || '3', 10) || 3;
|
||||
// legacy section-navs: only fill when heading count > 2 (≥3).
|
||||
var MIN_HEADINGS = 3;
|
||||
|
||||
function getHeadings() {
|
||||
// legacy scans h1–h4 inside [data-nav]; depth setting still caps lower levels.
|
||||
return Array.prototype.slice.call(content.querySelectorAll('h1, h2, h3, h4')).filter(function (heading) {
|
||||
if (heading.closest('.no-nav') || heading.classList.contains('item-heading')) {
|
||||
return false;
|
||||
}
|
||||
var level = parseInt((heading.tagName || 'H2').replace('H', ''), 10) || 2;
|
||||
// toc_depth is typically 2–4 meaning deepest heading level from h2 baseline;
|
||||
// allow h1 always, then h2..h(1+maxDepth) capped at h4.
|
||||
if (level === 1) return true;
|
||||
return level <= Math.min(4, maxDepth + 1);
|
||||
});
|
||||
}
|
||||
|
||||
function buildToc(container) {
|
||||
if (!container) return false;
|
||||
|
||||
var headings = getHeadings();
|
||||
if (headings.length < MIN_HEADINGS) return false;
|
||||
|
||||
var html = '';
|
||||
headings.forEach(function (heading) {
|
||||
if (!heading.id) {
|
||||
heading.id = slugifyHeading(heading.textContent);
|
||||
}
|
||||
|
||||
var levelClass = '';
|
||||
if (heading.tagName === 'H1') {
|
||||
levelClass = 'toc-h1';
|
||||
} else if (heading.tagName === 'H3') {
|
||||
levelClass = 'toc-h3';
|
||||
} else if (heading.tagName === 'H4') {
|
||||
levelClass = 'toc-h4';
|
||||
}
|
||||
|
||||
html += '<li' + (levelClass ? ' class="' + levelClass + '"' : '') + '><a href="#' + heading.id + '">' + heading.textContent.trim() + '</a></li>';
|
||||
});
|
||||
|
||||
container.innerHTML = html;
|
||||
return true;
|
||||
}
|
||||
|
||||
var hasToc = false;
|
||||
tocLists.forEach(function (list) {
|
||||
if (buildToc(list)) hasToc = true;
|
||||
});
|
||||
if (drawerList && tocLists.indexOf(drawerList) === -1) {
|
||||
if (buildToc(drawerList)) hasToc = true;
|
||||
}
|
||||
|
||||
if (!hasToc) {
|
||||
tocWidgets.forEach(function (box) {
|
||||
var widget = box.closest('.widget');
|
||||
// A built-in TOC is a direct child of the article sidebar. Its parent
|
||||
// is therefore not widget chrome and must never be removed here.
|
||||
if (widget && widget !== document.body && widget.querySelectorAll('.posts-nav-box').length <= 1) {
|
||||
widget.remove();
|
||||
} else {
|
||||
box.remove();
|
||||
}
|
||||
});
|
||||
if (mobileToggle) mobileToggle.remove();
|
||||
if (drawer) drawer.remove();
|
||||
if (overlay) overlay.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
function closeDrawer() {
|
||||
if (drawer) drawer.classList.remove('is-open');
|
||||
if (overlay) overlay.classList.remove('is-open');
|
||||
document.body.style.overflow = '';
|
||||
}
|
||||
|
||||
function openDrawer() {
|
||||
if (drawer) drawer.classList.add('is-open');
|
||||
if (overlay) overlay.classList.add('is-open');
|
||||
document.body.style.overflow = 'hidden';
|
||||
}
|
||||
|
||||
function trackActiveHeading() {
|
||||
var headings = content.querySelectorAll('h1[id], h2[id], h3[id], h4[id]');
|
||||
if (!headings.length) return;
|
||||
|
||||
var activeId = '';
|
||||
var fromTop = window.scrollY + 120;
|
||||
for (var i = headings.length - 1; i >= 0; i -= 1) {
|
||||
if (headings[i].offsetTop <= fromTop) {
|
||||
activeId = headings[i].id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
document.querySelectorAll('.toc-list a').forEach(function (link) {
|
||||
link.classList.toggle('is-active', link.getAttribute('href') === '#' + activeId);
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll('.toc-list a').forEach(function (link) {
|
||||
link.addEventListener('click', function (event) {
|
||||
var target = document.querySelector(link.getAttribute('href'));
|
||||
if (!target) return;
|
||||
|
||||
event.preventDefault();
|
||||
var top = target.getBoundingClientRect().top + window.pageYOffset - 105;
|
||||
window.scrollTo({ top: Math.max(0, top), behavior: 'smooth' });
|
||||
closeDrawer();
|
||||
});
|
||||
});
|
||||
|
||||
if (mobileToggle) mobileToggle.addEventListener('click', openDrawer);
|
||||
if (overlay) overlay.addEventListener('click', closeDrawer);
|
||||
if (closeBtn) closeBtn.addEventListener('click', closeDrawer);
|
||||
|
||||
window.addEventListener('scroll', trackActiveHeading, { passive: true });
|
||||
trackActiveHeading();
|
||||
}
|
||||
|
||||
function initLightbox() {
|
||||
var overlay = document.getElementById('lightbox-overlay');
|
||||
var content = document.querySelector(".wp-posts-content[data-aksrt-lightbox='1'], .entry-content[data-aksrt-lightbox='1']");
|
||||
@@ -316,7 +150,6 @@
|
||||
}
|
||||
|
||||
function init() {
|
||||
initPostToc();
|
||||
initLightbox();
|
||||
initContentExpand();
|
||||
initFlashQueryCleanup();
|
||||
|
||||
Reference in new issue
Block a user