From 8d161bfb1f014a3184ab7111f7cd39b3c253f552 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 24 Sep 2024 10:29:54 +0800 Subject: feat: add focusNextBlock util --- src/dumbymap.mjs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/dumbymap.mjs') diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 01463c0..d7c822a 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs @@ -175,6 +175,22 @@ function switchToNextLayout(reverse = false) { this.container.setAttribute("data-layout", nextLayout.name) } +function focusNextBlock(reverse = false) { + const blocks = this.blocks.filter(b=>b.checkVisibility({ + contentVisibilityAuto: true, + opacityProperty: true, + visibilityProperty: true, + })) + const currentBlock = blocks.find(b=>b.classList.contains('focus')) + const currentIndex = blocks.indexOf(currentBlock) + const padding = reverse ? -1 : 1 + const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + blocks.length) % blocks.length + const nextBlock = blocks[nextIndex] + blocks.forEach(b=>b.classList.remove('focus')) + nextBlock?.classList?.add('focus') + nextBlock.scrollIntoView({behavior: 'smooth', block: "nearest"}) +} + export const generateMaps = (container, callback) => { container.classList.add('Dumby') const htmlHolder = container.querySelector('.SemanticHtml') ?? container -- cgit v1.2.3-70-g09d2