-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
back where we started closes #2
- Loading branch information
Showing
9 changed files
with
112 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"singleQuote": true, | ||
"printWidth": 120, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Ctrl from './ctrl'; | ||
|
||
const lpvs = new Set<Ctrl>(); | ||
let viewTarget: Ctrl | undefined; | ||
|
||
initEventHandlers(); | ||
|
||
export function stepwiseScroll(inner: (e: WheelEvent, scroll: boolean) => void): (e: WheelEvent) => void { | ||
let scrollTotal = 0; | ||
return (e: WheelEvent) => { | ||
scrollTotal += e.deltaY * (e.deltaMode ? 40 : 1); | ||
if (Math.abs(scrollTotal) >= 4) { | ||
inner(e, true); | ||
scrollTotal = 0; | ||
} else { | ||
inner(e, false); | ||
} | ||
}; | ||
} | ||
|
||
export function eventRepeater(action: () => void, e: Event) { | ||
const repeat = () => { | ||
action(); | ||
delay = Math.max(100, delay - delay / 15); | ||
timeout = setTimeout(repeat, delay); | ||
}; | ||
let delay = 350; | ||
let timeout = setTimeout(repeat, 500); | ||
action(); | ||
const eventName = e.type == 'touchstart' ? 'touchend' : 'mouseup'; | ||
document.addEventListener(eventName, () => clearTimeout(timeout), { once: true }); | ||
} | ||
|
||
export function addCtrl(ctrl: Ctrl) { | ||
lpvs.add(ctrl); | ||
adjustViewTarget(); // after all lpvs are added, we only do this again after scrolling. | ||
} | ||
|
||
function initEventHandlers() { | ||
let scrollTimeout = 0; | ||
const debouncer = () => { | ||
if (scrollTimeout) window.clearTimeout(scrollTimeout); | ||
scrollTimeout = window.setTimeout(adjustViewTarget, 125); | ||
}; | ||
window.visualViewport.addEventListener('scroll', debouncer); | ||
window.visualViewport.addEventListener('resize', debouncer); | ||
document.addEventListener('keydown', onKeyDown); | ||
} | ||
|
||
function suppressKeyNavOn(e: KeyboardEvent): boolean { | ||
const ae = document.activeElement; | ||
if ( | ||
!viewTarget || | ||
e.getModifierState('Shift') || | ||
e.getModifierState('Alt') || | ||
e.getModifierState('Control') || | ||
e.getModifierState('Meta') | ||
) | ||
return true; | ||
else if (ae instanceof HTMLInputElement) | ||
switch ((ae as HTMLInputElement).type) { | ||
case 'button': | ||
case 'checkbox': | ||
case 'color': | ||
case 'image': | ||
case 'radio': | ||
case 'submit': | ||
case 'file': | ||
return false; | ||
default: | ||
return true; | ||
} | ||
else return ae instanceof HTMLTextAreaElement; | ||
} | ||
|
||
function onKeyDown(e: KeyboardEvent) { | ||
if (suppressKeyNavOn(e)) return; | ||
else if (e.key == 'ArrowLeft') viewTarget?.goTo('prev'); | ||
else if (e.key == 'ArrowRight') viewTarget?.goTo('next'); | ||
else if (e.key == 'f' && !(document.activeElement instanceof HTMLSelectElement)) viewTarget?.flip(); | ||
} | ||
|
||
function adjustViewTarget() { | ||
let largestOnscreenLpvHeight = 0; | ||
viewTarget = undefined; | ||
lpvs.forEach((v: Ctrl) => { | ||
const r = v.div!.getBoundingClientRect(); | ||
const onscreenLpvHeight = Math.min(window.innerHeight, r.bottom) - Math.max(0, r.top); | ||
if (onscreenLpvHeight > largestOnscreenLpvHeight) { | ||
largestOnscreenLpvHeight = onscreenLpvHeight; | ||
viewTarget = v; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters