[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 06/15] xwidget: Simplify functions to scroll to elements
From: |
Ricardo Wurmus |
Subject: |
[PATCH 06/15] xwidget: Simplify functions to scroll to elements |
Date: |
Mon, 24 Oct 2016 18:40:52 +0200 |
* lisp/xwidget.el (xwidget-webkit-show-named-element,
xwidget-webkit-show-id-element,
xwidget-webkit-show-id-or-named-element): Simplify functions by
scrolling exclusively with JavaScript.
---
lisp/xwidget.el | 79 +++++++++++++++++++++++++--------------------------------
1 file changed, 35 insertions(+), 44 deletions(-)
diff --git a/lisp/xwidget.el b/lisp/xwidget.el
index 1333365..e54d1f8 100644
--- a/lisp/xwidget.el
+++ b/lisp/xwidget.el
@@ -339,62 +339,53 @@ XW is the xwidget identifier, TEXT is retrieved from the
webkit."
For example, use this to display an anchor."
(interactive (list (xwidget-webkit-current-session)
(read-string "Element name: ")))
- ;;TODO since an xwidget is an Emacs object, it is not trivial to do
- ;; some things that are taken for granted in a normal browser.
- ;; scrolling an anchor/named-element into view is one such thing.
- ;; This function implements a proof-of-concept for this. Problems
- ;; remaining: - The selected window is scrolled but this is not
- ;; always correct - This needs to be interfaced into browse-url
- ;; somehow. The tricky part is that we need to do this in two steps:
- ;; A: load the base url, wait for load signal to arrive B: navigate
- ;; to the anchor when the base url is finished rendering
-
- ;; This part figures out the Y coordinate of the element
- (let ((y (string-to-number
- (xwidget-webkit-execute-script-rv
- xw
- (format
- "document.getElementsByName('%s')[0].getBoundingClientRect().top"
- element-name)
- 0))))
- ;; Now we need to tell Emacs to scroll the element into view.
- (xwidget-log "scroll: %d" y)
- (set-window-vscroll (selected-window) y t)))
+ ;; TODO: This needs to be interfaced into browse-url somehow. The
+ ;; tricky part is that we need to do this in two steps: A: load the
+ ;; base url, wait for load signal to arrive B: navigate to the
+ ;; anchor when the base url is finished rendering
+ (xwidget-webkit-execute-script
+ xw
+ (format "
+(function (query) {
+ var el = document.getElementsByName(query)[0];
+ if (el !== undefined) {
+ window.scrollTo(0, el.offsetTop);
+ }
+})('%s');"
+ element-name)))
(defun xwidget-webkit-show-id-element (xw element-id)
"Make webkit xwidget XW show an id-element ELEMENT-ID.
For example, use this to display an anchor."
(interactive (list (xwidget-webkit-current-session)
(read-string "Element id: ")))
- (let ((y (string-to-number
- (xwidget-webkit-execute-script-rv
- xw
- (format
"document.getElementById('%s').getBoundingClientRect().top"
- element-id)
- 0))))
- ;; Now we need to tell Emacs to scroll the element into view.
- (xwidget-log "scroll: %d" y)
- (set-window-vscroll (selected-window) y t)))
+ (xwidget-webkit-execute-script
+ xw
+ (format "
+(function (query) {
+ var el = document.getElementById(query);
+ if (el !== null) {
+ window.scrollTo(0, el.offsetTop);
+ }
+})('%s');"
+ element-id)))
(defun xwidget-webkit-show-id-or-named-element (xw element-id)
"Make webkit xwidget XW show a name or element id ELEMENT-ID.
For example, use this to display an anchor."
(interactive (list (xwidget-webkit-current-session)
(read-string "Name or element id: ")))
- (let* ((y1 (string-to-number
- (xwidget-webkit-execute-script-rv
- xw
- (format
"document.getElementsByName('%s')[0].getBoundingClientRect().top" element-id)
- "0")))
- (y2 (string-to-number
- (xwidget-webkit-execute-script-rv
- xw
- (format
"document.getElementById('%s').getBoundingClientRect().top" element-id)
- "0")))
- (y3 (max y1 y2)))
- ;; Now we need to tell Emacs to scroll the element into view.
- (xwidget-log "scroll: %d" y3)
- (set-window-vscroll (selected-window) y3 t)))
+ (xwidget-webkit-execute-script
+ xw
+ (format "
+(function (query) {
+ var el = document.getElementById(query) ||
+ document.getElementsByName(query)[0];
+ if (el !== undefined) {
+ window.scrollTo(0, el.offsetTop);
+ }
+})('%s');"
+ element-id)))
(defun xwidget-webkit-adjust-size-to-content ()
"Adjust webkit to content size."
--
2.10.1
- [PATCH v2 00/15] xwidget webkit improvements, Ricardo Wurmus, 2016/10/24
- [PATCH v2 00/15] xwidget webkit improvements, Ricardo Wurmus, 2016/10/24
- [PATCH 01/15] xwidget: Use WebKit2 API, Ricardo Wurmus, 2016/10/24
- [PATCH 02/15] xwidget: Pass JavaScript return value to optional callback procedure, Ricardo Wurmus, 2016/10/24
- [PATCH 03/15] Remove scrolled window container around WebKit widget, Ricardo Wurmus, 2016/10/24
- [PATCH 04/15] xwidget: Do not use `xwidget-execute-script-rv' to insert string, Ricardo Wurmus, 2016/10/24
- [PATCH 05/15] xwidget: Get title via asynchronous JavaScript., Ricardo Wurmus, 2016/10/24
- [PATCH 06/15] xwidget: Simplify functions to scroll to elements,
Ricardo Wurmus <=
- [PATCH 07/15] xwidget: Add function to find element by CSS selector, Ricardo Wurmus, 2016/10/24
- [PATCH 08/15] xwidget: Get selection with asynchronous JavaScript, Ricardo Wurmus, 2016/10/24
- [PATCH 09/15] xwidget: Get URL asynchronously., Ricardo Wurmus, 2016/10/24
- [PATCH 10/15] xwidget: Remove title hack., Ricardo Wurmus, 2016/10/24
- [PATCH 11/15] Let initial WebKit view fill window, Ricardo Wurmus, 2016/10/24
- [PATCH 12/15] Dynamically resize WebKit widget., Ricardo Wurmus, 2016/10/24
- [PATCH 13/15] Implement zoom for WebKit widget., Ricardo Wurmus, 2016/10/24
- [PATCH 14/15] xwidget: Bind "beginning-of-buffer" and "end-of-buffer", Ricardo Wurmus, 2016/10/24
- [PATCH 15/15] xwidget: Map "previous-line" and "next-line" to scroll, Ricardo Wurmus, 2016/10/24
- Re: [PATCH v2 00/15] xwidget webkit improvements, Paul Eggert, 2016/10/25