client: fix tocs scroll listener

This commit is contained in:
fantasticit 2022-06-03 18:47:34 +08:00
parent c20c9794f9
commit 1b2843f185
2 changed files with 8 additions and 6 deletions

View File

@ -3,7 +3,7 @@ export function throttle(func, wait, options?: { leading: boolean; trailing: boo
let timeout = null; let timeout = null;
let previous = 0; let previous = 0;
if (!options) options = { leading: true, trailing: true }; if (!options) options = { leading: false, trailing: true };
const later = function () { const later = function () {
previous = options.leading === false ? 0 : Date.now(); previous = options.leading === false ? 0 : Date.now();

View File

@ -69,11 +69,13 @@ export const Tocs: React.FC<{ editor: Editor; getContainer: () => HTMLElement }>
const container = $container.current; const container = $container.current;
if (!container) return; if (!container) return;
const activeAnchor = container.querySelector('.semi-anchor-link-title-active'); let target = container.querySelector('.semi-anchor-link-title-active');
if (!activeAnchor) return; if (!target) {
target = container.querySelector('.semi-anchor-link-title:first-of-type');
scrollIntoView(activeAnchor, { behavior: 'smooth', scrollMode: 'if-needed' }); }
}, 200); if (!target) return;
scrollIntoView(target, { scrollMode: 'if-needed' });
}, 100);
el.addEventListener('scroll', scrollHandler); el.addEventListener('scroll', scrollHandler);