diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-09-24 10:29:54 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-09-24 12:17:22 +0800 |
| commit | 8d161bfb1f014a3184ab7111f7cd39b3c253f552 (patch) | |
| tree | 42f6dadd251fb80bfc32571a1dc4276fc9a4b079 /src | |
| parent | c7ac371639e223282efff546c198428f736232f0 (diff) | |
feat: add focusNextBlock util
Diffstat (limited to 'src')
| -rw-r--r-- | src/dumbymap.mjs | 16 | ||||
| -rw-r--r-- | src/editor.mjs | 11 |
2 files changed, 26 insertions, 1 deletions
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) { | |||
| 175 | this.container.setAttribute("data-layout", nextLayout.name) | 175 | this.container.setAttribute("data-layout", nextLayout.name) |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | function focusNextBlock(reverse = false) { | ||
| 179 | const blocks = this.blocks.filter(b=>b.checkVisibility({ | ||
| 180 | contentVisibilityAuto: true, | ||
| 181 | opacityProperty: true, | ||
| 182 | visibilityProperty: true, | ||
| 183 | })) | ||
| 184 | const currentBlock = blocks.find(b=>b.classList.contains('focus')) | ||
| 185 | const currentIndex = blocks.indexOf(currentBlock) | ||
| 186 | const padding = reverse ? -1 : 1 | ||
| 187 | const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + blocks.length) % blocks.length | ||
| 188 | const nextBlock = blocks[nextIndex] | ||
| 189 | blocks.forEach(b=>b.classList.remove('focus')) | ||
| 190 | nextBlock?.classList?.add('focus') | ||
| 191 | nextBlock.scrollIntoView({behavior: 'smooth', block: "nearest"}) | ||
| 192 | } | ||
| 193 | |||
| 178 | export const generateMaps = (container, callback) => { | 194 | export const generateMaps = (container, callback) => { |
| 179 | container.classList.add('Dumby') | 195 | container.classList.add('Dumby') |
| 180 | const htmlHolder = container.querySelector('.SemanticHtml') ?? container | 196 | const htmlHolder = container.querySelector('.SemanticHtml') ?? container |
diff --git a/src/editor.mjs b/src/editor.mjs index 550f626..f4fadb4 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -552,15 +552,24 @@ document.onkeydown = (e) => { | |||
| 552 | return null | 552 | return null |
| 553 | } | 553 | } |
| 554 | if (!cm.hasFocus()) { | 554 | if (!cm.hasFocus()) { |
| 555 | e.preventDefault() | ||
| 556 | if (!dumbymap) return | 555 | if (!dumbymap) return |
| 557 | 556 | ||
| 558 | if (e.key === 'Tab') { | 557 | if (e.key === 'Tab') { |
| 558 | e.preventDefault() | ||
| 559 | dumbymap.utils.focusNextMap(e.shiftKey) | 559 | dumbymap.utils.focusNextMap(e.shiftKey) |
| 560 | } | 560 | } |
| 561 | if (e.key === 'x') { | 561 | if (e.key === 'x') { |
| 562 | e.preventDefault() | ||
| 562 | dumbymap.utils.switchToNextLayout() | 563 | dumbymap.utils.switchToNextLayout() |
| 563 | } | 564 | } |
| 565 | if (e.key === 'n') { | ||
| 566 | e.preventDefault() | ||
| 567 | dumbymap.utils.focusNextBlock() | ||
| 568 | } | ||
| 569 | if (e.key === 'p') { | ||
| 570 | e.preventDefault() | ||
| 571 | dumbymap.utils.focusNextBlock(true) | ||
| 572 | } | ||
| 564 | } | 573 | } |
| 565 | } | 574 | } |
| 566 | 575 | ||