diff options
| author | Ricardo Wurmus | 2016-10-25 23:00:35 -0700 |
|---|---|---|
| committer | Paul Eggert | 2016-10-25 23:07:13 -0700 |
| commit | 74576447b969adc430144d9a3ce57b590f2a2dcc (patch) | |
| tree | 8eba23bf18df27599d3990fb240de475d06b6d45 | |
| parent | cc3b868fbf471b54491ed81f01f2235d50bb5fee (diff) | |
| download | emacs-74576447b969adc430144d9a3ce57b590f2a2dcc.tar.gz emacs-74576447b969adc430144d9a3ce57b590f2a2dcc.zip | |
xwidget: Simplify functions to scroll to elements
* 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.
| -rw-r--r-- | lisp/xwidget.el | 79 |
1 files changed, 35 insertions, 44 deletions
diff --git a/lisp/xwidget.el b/lisp/xwidget.el index 133336533a8..e54d1f80c72 100644 --- a/lisp/xwidget.el +++ b/lisp/xwidget.el | |||
| @@ -339,62 +339,53 @@ XW is the xwidget identifier, TEXT is retrieved from the webkit." | |||
| 339 | For example, use this to display an anchor." | 339 | For example, use this to display an anchor." |
| 340 | (interactive (list (xwidget-webkit-current-session) | 340 | (interactive (list (xwidget-webkit-current-session) |
| 341 | (read-string "Element name: "))) | 341 | (read-string "Element name: "))) |
| 342 | ;;TODO since an xwidget is an Emacs object, it is not trivial to do | 342 | ;; TODO: This needs to be interfaced into browse-url somehow. The |
| 343 | ;; some things that are taken for granted in a normal browser. | 343 | ;; tricky part is that we need to do this in two steps: A: load the |
| 344 | ;; scrolling an anchor/named-element into view is one such thing. | 344 | ;; base url, wait for load signal to arrive B: navigate to the |
| 345 | ;; This function implements a proof-of-concept for this. Problems | 345 | ;; anchor when the base url is finished rendering |
| 346 | ;; remaining: - The selected window is scrolled but this is not | 346 | (xwidget-webkit-execute-script |
| 347 | ;; always correct - This needs to be interfaced into browse-url | 347 | xw |
| 348 | ;; somehow. The tricky part is that we need to do this in two steps: | 348 | (format " |
| 349 | ;; A: load the base url, wait for load signal to arrive B: navigate | 349 | (function (query) { |
| 350 | ;; to the anchor when the base url is finished rendering | 350 | var el = document.getElementsByName(query)[0]; |
| 351 | 351 | if (el !== undefined) { | |
| 352 | ;; This part figures out the Y coordinate of the element | 352 | window.scrollTo(0, el.offsetTop); |
| 353 | (let ((y (string-to-number | 353 | } |
| 354 | (xwidget-webkit-execute-script-rv | 354 | })('%s');" |
| 355 | xw | 355 | element-name))) |
| 356 | (format | ||
| 357 | "document.getElementsByName('%s')[0].getBoundingClientRect().top" | ||
| 358 | element-name) | ||
| 359 | 0)))) | ||
| 360 | ;; Now we need to tell Emacs to scroll the element into view. | ||
| 361 | (xwidget-log "scroll: %d" y) | ||
| 362 | (set-window-vscroll (selected-window) y t))) | ||
| 363 | 356 | ||
| 364 | (defun xwidget-webkit-show-id-element (xw element-id) | 357 | (defun xwidget-webkit-show-id-element (xw element-id) |
| 365 | "Make webkit xwidget XW show an id-element ELEMENT-ID. | 358 | "Make webkit xwidget XW show an id-element ELEMENT-ID. |
| 366 | For example, use this to display an anchor." | 359 | For example, use this to display an anchor." |
| 367 | (interactive (list (xwidget-webkit-current-session) | 360 | (interactive (list (xwidget-webkit-current-session) |
| 368 | (read-string "Element id: "))) | 361 | (read-string "Element id: "))) |
| 369 | (let ((y (string-to-number | 362 | (xwidget-webkit-execute-script |
| 370 | (xwidget-webkit-execute-script-rv | 363 | xw |
| 371 | xw | 364 | (format " |
| 372 | (format "document.getElementById('%s').getBoundingClientRect().top" | 365 | (function (query) { |
| 373 | element-id) | 366 | var el = document.getElementById(query); |
| 374 | 0)))) | 367 | if (el !== null) { |
| 375 | ;; Now we need to tell Emacs to scroll the element into view. | 368 | window.scrollTo(0, el.offsetTop); |
| 376 | (xwidget-log "scroll: %d" y) | 369 | } |
| 377 | (set-window-vscroll (selected-window) y t))) | 370 | })('%s');" |
| 371 | element-id))) | ||
| 378 | 372 | ||
| 379 | (defun xwidget-webkit-show-id-or-named-element (xw element-id) | 373 | (defun xwidget-webkit-show-id-or-named-element (xw element-id) |
| 380 | "Make webkit xwidget XW show a name or element id ELEMENT-ID. | 374 | "Make webkit xwidget XW show a name or element id ELEMENT-ID. |
| 381 | For example, use this to display an anchor." | 375 | For example, use this to display an anchor." |
| 382 | (interactive (list (xwidget-webkit-current-session) | 376 | (interactive (list (xwidget-webkit-current-session) |
| 383 | (read-string "Name or element id: "))) | 377 | (read-string "Name or element id: "))) |
| 384 | (let* ((y1 (string-to-number | 378 | (xwidget-webkit-execute-script |
| 385 | (xwidget-webkit-execute-script-rv | 379 | xw |
| 386 | xw | 380 | (format " |
| 387 | (format "document.getElementsByName('%s')[0].getBoundingClientRect().top" element-id) | 381 | (function (query) { |
| 388 | "0"))) | 382 | var el = document.getElementById(query) || |
| 389 | (y2 (string-to-number | 383 | document.getElementsByName(query)[0]; |
| 390 | (xwidget-webkit-execute-script-rv | 384 | if (el !== undefined) { |
| 391 | xw | 385 | window.scrollTo(0, el.offsetTop); |
| 392 | (format "document.getElementById('%s').getBoundingClientRect().top" element-id) | 386 | } |
| 393 | "0"))) | 387 | })('%s');" |
| 394 | (y3 (max y1 y2))) | 388 | element-id))) |
| 395 | ;; Now we need to tell Emacs to scroll the element into view. | ||
| 396 | (xwidget-log "scroll: %d" y3) | ||
| 397 | (set-window-vscroll (selected-window) y3 t))) | ||
| 398 | 389 | ||
| 399 | (defun xwidget-webkit-adjust-size-to-content () | 390 | (defun xwidget-webkit-adjust-size-to-content () |
| 400 | "Adjust webkit to content size." | 391 | "Adjust webkit to content size." |