From 316d547ac0d0cfdfbc0b58a0616fcb5a41b2e9ca Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 09:57:00 +0200 Subject: [PATCH 1/8] https://github.com/coolwanglu/pdf2htmlEX/issues/180 Add key handler, and implement page up, page down, ctrl-home and ctrl-end. First commit on github and first javascript coding :) --- share/pdf2htmlEX.js.in | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 2ce4928..2fe571b 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -164,6 +164,8 @@ var pdf2htmlEX = (function(){ //this.zoom_fixer(); + this.add_key_handler(); + // handle links this.$container.add(this.$outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler); @@ -323,6 +325,64 @@ var pdf2htmlEX = (function(){ }); }, + add_key_handler : function () { + var _ = this; + + $(window).on('keydown', function keydown(e) { + + switch (e.keyCode) { + case 33: // Page Down + _.scroll_to_page(_.get_prev_page()); + break; + case 34: // Page Up + _.scroll_to_page(_.get_next_page()); + break; + case 35: // End + if (e.ctrlKey) + _.scroll_to_page(_.pages[_.pages.length-1]); + break; + case 36: // Home + if (e.ctrlKey) + _.scroll_to_page(_.pages[1]); + break; + } + }); + }, + + // Find the first page that is at least half a page below the current position + get_next_page : function() { + var _ = this; + + var position = [0,0]; + var page_height = _.$container.height(); + + var pl = this.pages; + for(var i in pl) { + var page = pl[i]; + var page_position = page.position(); + if (page_position[1] < position[1] - page_height/2) + return page; + } + return undefined; + }, + + // Find the last page that is at least half a page above the current position + get_prev_page : function() { + var _ = this; + + var position = [0,0]; + var page_height = _.$container.height(); + + var pl = this.pages.slice().reverse(); + for(var i in pl) { + var page = pl[i]; + var page_position = page.position(); + if (page_position[1] > position[1] + page_height/2) + return page; + } + return undefined; + }, + rescale : function (ratio, is_relative) { var pl = this.pages; for(var i in pl) { @@ -423,6 +483,16 @@ var pdf2htmlEX = (function(){ } }, + scroll_to_page : function(target_page) { + if (target_page == undefined) return; + + // Show a bit of space above the target page when scrolling + // TODO: do not hardcode! + var page_padding = 10; + + this.scroll_to(target_page.n, [0,-page_padding]); + }, + /* pos=[x,y], where (0,0) is the top-left corner */ scroll_to : function(pageno, pos) { var target_page = this.pages[pageno]; From 40dc097ebd4aca5b4ac8484042279c19c2d9a6d4 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 11:22:42 +0200 Subject: [PATCH 2/8] https://github.com/coolwanglu/pdf2htmlEX/issues/180 Better name for the key handler. --- share/pdf2htmlEX.js.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 2fe571b..2c963aa 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -164,7 +164,7 @@ var pdf2htmlEX = (function(){ //this.zoom_fixer(); - this.add_key_handler(); + this.navigation_key_handler(); // handle links this.$container.add(this.$outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler); @@ -325,7 +325,7 @@ var pdf2htmlEX = (function(){ }); }, - add_key_handler : function () { + navigation_key_handler : function () { var _ = this; $(window).on('keydown', function keydown(e) { From d9bd8106fe13ee8c12d8702f03f487834b364dcf Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 11:27:12 +0200 Subject: [PATCH 3/8] https://github.com/coolwanglu/pdf2htmlEX/issues/180 Call e.preventDefault() when the key was handled. --- share/pdf2htmlEX.js.in | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 2c963aa..e98c450 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -333,17 +333,23 @@ var pdf2htmlEX = (function(){ switch (e.keyCode) { case 33: // Page Down _.scroll_to_page(_.get_prev_page()); + e.preventDefault(); break; case 34: // Page Up _.scroll_to_page(_.get_next_page()); + e.preventDefault(); break; case 35: // End - if (e.ctrlKey) + if (e.ctrlKey) { _.scroll_to_page(_.pages[_.pages.length-1]); + e.preventDefault(); + } break; case 36: // Home - if (e.ctrlKey) + if (e.ctrlKey) { _.scroll_to_page(_.pages[1]); + e.preventDefault(); + } break; } }); From 4bde91423d982d79ff17e37a756655b517352f96 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 11:34:28 +0200 Subject: [PATCH 4/8] https://github.com/coolwanglu/pdf2htmlEX/issues/180 Scroll pages with alt-page up/down, not page up/down. Implement screen scrolling for page up/down. Fix comment about which key is up (code was right, but comments were inverted :D) --- share/pdf2htmlEX.js.in | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index e98c450..9555913 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -331,20 +331,37 @@ var pdf2htmlEX = (function(){ $(window).on('keydown', function keydown(e) { switch (e.keyCode) { - case 33: // Page Down - _.scroll_to_page(_.get_prev_page()); + case 33: // Page UP: + // pageup -> scroll one screen up + // alt-pageup -> scroll one page up + if (e.altKey) { + _.scroll_to_page(_.get_prev_page()); + } + else { + _.$container.scrollTop(_.$container.scrollTop()-_.$container.height()); + } e.preventDefault(); break; - case 34: // Page Up - _.scroll_to_page(_.get_next_page()); + + case 34: // Page DOWN + // pagedown -> scroll one screen down + // alt-pagedown -> scroll one page down + if (e.altKey) { + _.scroll_to_page(_.get_next_page()); + } + else { + _.$container.scrollTop(_.$container.scrollTop()+_.$container.height()); + } e.preventDefault(); break; + case 35: // End if (e.ctrlKey) { _.scroll_to_page(_.pages[_.pages.length-1]); e.preventDefault(); } break; + case 36: // Home if (e.ctrlKey) { _.scroll_to_page(_.pages[1]); From 972c92540e4135534a77aa318e418806a062efc4 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 22:02:53 +0200 Subject: [PATCH 5/8] Use return in the non-handled key cases, and call preventDefault() in a single place, as suggested. https://github.com/coolwanglu/pdf2htmlEX/issues/180 --- share/pdf2htmlEX.js.in | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 9555913..809b4b4 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -340,7 +340,6 @@ var pdf2htmlEX = (function(){ else { _.$container.scrollTop(_.$container.scrollTop()-_.$container.height()); } - e.preventDefault(); break; case 34: // Page DOWN @@ -352,23 +351,28 @@ var pdf2htmlEX = (function(){ else { _.$container.scrollTop(_.$container.scrollTop()+_.$container.height()); } - e.preventDefault(); break; case 35: // End if (e.ctrlKey) { _.scroll_to_page(_.pages[_.pages.length-1]); - e.preventDefault(); } + else + return; break; case 36: // Home if (e.ctrlKey) { _.scroll_to_page(_.pages[1]); - e.preventDefault(); } + else + return; break; + + default: + return; } + e.preventDefault(); }); }, From 08ba884b7fd0def3e42e1ce4ed8da51d56912142 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 22:02:53 +0200 Subject: [PATCH 6/8] Simplify the implementation of ctrl-home, we don't need to care about pages in this case. https://github.com/coolwanglu/pdf2htmlEX/issues/180 --- share/pdf2htmlEX.js.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 809b4b4..669eea6 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -363,7 +363,7 @@ var pdf2htmlEX = (function(){ case 36: // Home if (e.ctrlKey) { - _.scroll_to_page(_.pages[1]); + _.$container.scrollTop(0); } else return; From bf210751f775bee977cc57ee260300c5f82cca12 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Wed, 3 Jul 2013 22:35:49 +0200 Subject: [PATCH 7/8] Added myself in AUTHORS, as requested --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 916c9ff..b74a59c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,4 +1,5 @@ Chris Cinelli +Daniel Bonniot de Ruisselet Deepak filodej hasufell From b78cdd0a9d3b39c1aa6e70fd95d9ac2b4d808fe0 Mon Sep 17 00:00:00 2001 From: Daniel Bonniot Date: Sun, 14 Jul 2013 16:34:02 +0200 Subject: [PATCH 8/8] Use scrollHeight to find out how to scroll to the end for ctrl-end. https://github.com/coolwanglu/pdf2htmlEX/issues/180 --- share/pdf2htmlEX.js.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in index 669eea6..dffb53a 100644 --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -355,7 +355,7 @@ var pdf2htmlEX = (function(){ case 35: // End if (e.ctrlKey) { - _.scroll_to_page(_.pages[_.pages.length-1]); + _.$container.scrollTop(_.$container[0].scrollHeight); } else return;