fix docs reader anchor navigation
This commit is contained in:
parent
493a1d230c
commit
e84f19b5cb
1 changed files with 36 additions and 11 deletions
|
|
@ -122,7 +122,7 @@
|
||||||
if (!body) return;
|
if (!body) return;
|
||||||
var seen = {};
|
var seen = {};
|
||||||
body.querySelectorAll('h1,h2,h3,h4,h5,h6').forEach(function (h) {
|
body.querySelectorAll('h1,h2,h3,h4,h5,h6').forEach(function (h) {
|
||||||
var base = h.id || slugHeading(h.textContent || '');
|
var base = slugHeading(h.textContent || '') || h.id;
|
||||||
if (!base) return;
|
if (!base) return;
|
||||||
var id = base;
|
var id = base;
|
||||||
var n = seen[base] || 0;
|
var n = seen[base] || 0;
|
||||||
|
|
@ -136,14 +136,41 @@
|
||||||
var body = $('docs-reader-body');
|
var body = $('docs-reader-body');
|
||||||
var reader = $('docs-reader');
|
var reader = $('docs-reader');
|
||||||
if (!body || !reader || !hash) return false;
|
if (!body || !reader || !hash) return false;
|
||||||
var id = String(hash).replace(/^#/, '');
|
var id = decodeURIComponent(String(hash).replace(/^#/, ''));
|
||||||
if (!id) return false;
|
if (!id) return false;
|
||||||
var target = document.getElementById(id);
|
var target = document.getElementById(id);
|
||||||
|
if (!target || !body.contains(target)) {
|
||||||
|
target = Array.prototype.slice.call(body.querySelectorAll('h1,h2,h3,h4,h5,h6')).find(function (h) {
|
||||||
|
return slugHeading(h.textContent || '') === id;
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!target || !body.contains(target)) return false;
|
if (!target || !body.contains(target)) return false;
|
||||||
reader.scrollTop = target.offsetTop - body.offsetTop - 8;
|
var readerBox = reader.getBoundingClientRect();
|
||||||
|
var targetBox = target.getBoundingClientRect();
|
||||||
|
reader.scrollTop += targetBox.top - readerBox.top - 8;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleDocAnchorClick(e) {
|
||||||
|
var link = e.target.closest('a[href]');
|
||||||
|
var body = $('docs-reader-body');
|
||||||
|
if (!link || !body || !body.contains(link)) return;
|
||||||
|
var href = link.getAttribute('href') || '';
|
||||||
|
if (href.charAt(0) !== '#' && link.hash) {
|
||||||
|
try {
|
||||||
|
var u = new URL(link.href, window.location.href);
|
||||||
|
if (u.origin !== window.location.origin || u.pathname !== window.location.pathname) return;
|
||||||
|
href = u.hash;
|
||||||
|
} catch (_) { return; }
|
||||||
|
}
|
||||||
|
if (!href || href.charAt(0) !== '#') return;
|
||||||
|
e.preventDefault();
|
||||||
|
requestAnimationFrame(function () {
|
||||||
|
scrollReaderToHash(href);
|
||||||
|
try { history.replaceState(null, '', href); } catch (_) { window.location.hash = href; }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function loadTree() {
|
function loadTree() {
|
||||||
if (_treeLoaded) return;
|
if (_treeLoaded) return;
|
||||||
fetch('/api/admin/docs/tree', { headers: getAuthHeaders() })
|
fetch('/api/admin/docs/tree', { headers: getAuthHeaders() })
|
||||||
|
|
@ -244,16 +271,14 @@
|
||||||
persistExpanded();
|
persistExpanded();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var docLink = e.target.closest('#docs-reader-body a[href^="#"]');
|
|
||||||
if (docLink) {
|
|
||||||
var href = docLink.getAttribute('href');
|
|
||||||
if (href && scrollReaderToHash(href)) {
|
|
||||||
e.preventDefault();
|
|
||||||
try { history.replaceState(null, '', href); } catch (_) { window.location.hash = href; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var body = $('docs-reader-body');
|
||||||
|
if (body && !body.dataset.anchorsWired) {
|
||||||
|
body.dataset.anchorsWired = '1';
|
||||||
|
body.addEventListener('click', handleDocAnchorClick);
|
||||||
|
}
|
||||||
|
|
||||||
var filter = $('docs-filter');
|
var filter = $('docs-filter');
|
||||||
if (filter) {
|
if (filter) {
|
||||||
var t = null;
|
var t = null;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue