Implement ^,$ navigating to 0%,100% in x-direction

This is made possible by also allowing the second
argument to be "x" or "y". We supply "y" as the
default to retain the current, more commonly
desired behavior.
This commit is contained in:
Koushien 2017-12-08 21:11:40 -08:00 committed by Colin Caine
parent 6e9bf93665
commit 850104e0fe
2 changed files with 24 additions and 17 deletions

View file

@ -44,6 +44,9 @@ const DEFAULTS = o({
"l": "scrollpx 50",
"G": "scrollto 100",
"gg": "scrollto 0",
"$": "scrollto 100 x",
// "0": "scrollto 0 x", // will get interpreted as a count
"^": "scrollto 0 x",
"H": "back",
"L": "forward",
"d": "tabclose",

View file

@ -186,21 +186,25 @@ export function scrollpx(a: number, b: number) {
window.scrollBy(a, b)
}
/** If one argument is given, scroll to that percentage down the page.
If two arguments are given, treat as x and y values to give to window.scrollTo
/** If two numbers are given, treat as x and y values to give to window.scrollTo
If one number is given, scroll to that percentage along a chosen axis,
defaulting to the y-axis
*/
//#content
export function scrollto(a: number, b?: number) {
export function scrollto(a: number, b: number | "x" | "y" = "y") {
a = Number(a)
// if b is undefined, Number(b) is NaN.
b = Number(b)
if (b === "y") {
window.scrollTo(
b ? a : window.scrollX,
b
? b
: a.clamp(0, 100) *
(window.document.scrollingElement.scrollHeight / 100)
)
window.scrollX,
a.clamp(0, 100) * window.document.scrollingElement.scrollHeight / 100)
}
else if (b === "x") {
window.scrollTo(
a.clamp(0, 100) * window.document.scrollingElement.scrollWidth / 100,
window.scrollY)
} else {
window.scrollTo(a, Number(b)) // a,b numbers
}
}
//#content