Compress scrolls, scrollto accept %

Implement Number.clamp to help scrollto
This commit is contained in:
Koushien 2017-10-06 19:22:19 -07:00
parent 8b13b3e7a7
commit 9b42a437d9
No known key found for this signature in database
GPG key ID: 082A69D4DD10690E
5 changed files with 33 additions and 31 deletions

View file

@ -53,29 +53,13 @@ export function normalmode() {state.mode = "NORMAL"}
// Scrolling functions
export function scrollby(x: number, y: number ) { messageActiveTab("scrollpx", [x, y]) }
export function scrolldown(n = 1) { scrollby(0, n) }
export function scrollup(n = 1) { scrolldown(n*-1) }
export function scrollright(n = 1) { scrollby(n, 0) }
export function scrollleft(n = 1) { scrollright(n*-1) }
export function scrolldownline(n = 1) { messageActiveTab("scrollline", [n]) }
export function scrollupline(n = 1) { scrolldownline(n*-1) }
export function scrolldownpage(n = 1) { messageActiveTab("scrollpage", [n]) }
export function scrolluppage(n = 1) { scrolldownpage(n*-1) }
export async function scrolldownhalfpage(n = 1) {
const current_window = await browser.windows.getCurrent()
scrolldown(n*0.5*current_window.height)
}
export function scrolluphalfpage(n = 1) { scrolldownhalfpage(n*-1) }
export function scrollto(x: number, y: number) { messageActiveTab("scrollto", [x, y]) }
export function scrolltobottom() { scrolldown(999999999) } // maximum value scrolldown would respond to
export async function scrolltotop() {
const current_window = await browser.windows.getCurrent()
scrollto(current_window.left, 0)
export function scroll(n = 1) { scrollby(0, n) }
export function scrollx(n = 1) { scrollby(n, 0) }
export function scrollline(n = 1) { messageActiveTab("scrollline", [n]) }
export async function scrollpage(n = 1) {
messageActiveTab("scrollpage", [n*(await browser.windows.getCurrent()).height])
}
export function scrollto(amount: number | [number, number]) { messageActiveTab("scrollto", [amount]) }
// Tab functions

View file

@ -15,10 +15,14 @@ const commands = new Map<string, ContentCommand>([
window.scrollByLines(n)
},
function scrollpage(n: number) {
window.scrollByPages(n)
window.scrollBy(0, n)
},
function scrollto(x: number, y: number) {
window.scrollTo(x, y)
function scrollto(a: number, b?: number) {
console.log(eval('content.document.scrollingElement.scrollHeight'))
window.scrollTo(b ? a : window.scrollX,
b ? b : a.clamp(-100, 100) * eval("content.document.scrollingElement.scrollHeight") / 100)
// window.scrollTo(amount[0] || 0,
// (amount[1] ? amount[1] : amount * eval('content.document.scrollingElement.scrollHeight'))
},
function history(n: number) {
window.history.go(n)

11
src/number.clamp.ts Normal file
View file

@ -0,0 +1,11 @@
/*
* Clamp a number n between two values lo, hi
* such that if n is in [lo, hi], we return n
* otherwise if n < lo, return lo
* else return hi.
*/
Number.prototype.clamp = function (lo: number, hi: number): number {
return Math.max(lo, Math.min(this, hi))
}
export {}

View file

@ -32,12 +32,12 @@ export namespace normalmode {
["o", "showcommandline open"],
["w", "showcommandline winopen"],
["t", "showcommandline tabopen"],
["j", "scrolldownline 10"],
["k", "scrollupline 10"],
["h", "scrollleft"],
["l", "scrollright"],
["G", "scrolltobottom"],
["gg", "scrolltotop"],
["j", "scrollline 10"],
["k", "scrollline -10"],
["h", "scrollx -5"],
["l", "scrollx 5"],
["G", "scrollto 100"],
["gg", "scrollto 0"],
["H", "historyback"],
["L", "historyforward"],
["d", "tabclose"],

3
src/tridactyl.d.ts vendored
View file

@ -5,6 +5,7 @@
interface Number {
mod(n: number): number
clamp(lo: number, hi: number): number
}
// For content.ts
@ -19,6 +20,8 @@ interface Message {
[key: string]: any
}
declare var content: any
// Firefox-specific dom properties
interface Window {
scrollByLines(n: number): void