From 1b2843f1854b9350de3c9b2e3d7b94fbe7e0635c Mon Sep 17 00:00:00 2001 From: fantasticit Date: Fri, 3 Jun 2022 18:47:34 +0800 Subject: [PATCH] client: fix tocs scroll listener --- packages/client/src/helpers/throttle.ts | 2 +- packages/client/src/tiptap/editor/tocs/index.tsx | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/client/src/helpers/throttle.ts b/packages/client/src/helpers/throttle.ts index dce6ae87..ede2d336 100644 --- a/packages/client/src/helpers/throttle.ts +++ b/packages/client/src/helpers/throttle.ts @@ -3,7 +3,7 @@ export function throttle(func, wait, options?: { leading: boolean; trailing: boo let timeout = null; let previous = 0; - if (!options) options = { leading: true, trailing: true }; + if (!options) options = { leading: false, trailing: true }; const later = function () { previous = options.leading === false ? 0 : Date.now(); diff --git a/packages/client/src/tiptap/editor/tocs/index.tsx b/packages/client/src/tiptap/editor/tocs/index.tsx index 724f2cf2..734a23ab 100644 --- a/packages/client/src/tiptap/editor/tocs/index.tsx +++ b/packages/client/src/tiptap/editor/tocs/index.tsx @@ -69,11 +69,13 @@ export const Tocs: React.FC<{ editor: Editor; getContainer: () => HTMLElement }> const container = $container.current; if (!container) return; - const activeAnchor = container.querySelector('.semi-anchor-link-title-active'); - if (!activeAnchor) return; - - scrollIntoView(activeAnchor, { behavior: 'smooth', scrollMode: 'if-needed' }); - }, 200); + let target = container.querySelector('.semi-anchor-link-title-active'); + if (!target) { + target = container.querySelector('.semi-anchor-link-title:first-of-type'); + } + if (!target) return; + scrollIntoView(target, { scrollMode: 'if-needed' }); + }, 100); el.addEventListener('scroll', scrollHandler);