
/**
 * Copied from `element-scroll-to`, but edited to account for `window` being
 * inside of iframes.
 *
 * https://github.com/webmodules/element-scroll-to
 */

import getWindow from 'get-window'

function scrollWrapperElements(element, options) {
  let window = getWindow(element)
  let elementRect = element.getBoundingClientRect()
  let wrapper = element.parentNode

  while (wrapper != window.document.body) {
    let wrapperRect = wrapper.getBoundingClientRect()
    let margin = options.margin
    let deltaX = 0
    let deltaY = 0

    if (elementRect.top < wrapperRect.top + margin) {
      deltaY = elementRect.top - wrapperRect.top - margin
    } else if (elementRect.bottom > wrapperRect.bottom - margin) {
      deltaY = elementRect.bottom - wrapperRect.bottom + margin
    }
    if (elementRect.left < wrapperRect.left + margin) {
      deltaX = elementRect.left - wrapperRect.left - margin
    } else if (elementRect.right > wrapperRect.right - margin) {
      deltaX = elementRect.right - wrapperRect.right + margin
    }
    wrapper.scrollTop += deltaY
    wrapper.scrollLeft += deltaX
    wrapper = wrapper.parentNode
  }

  return elementRect
}

function scrollWindow(element, options, elementRect) {
  let window = getWindow(element)
  let margin = options.margin
  let deltaX = 0
  let deltaY = 0

  if (elementRect.top < 0 + margin) {
    deltaY = elementRect.top - margin
  } else if (elementRect.bottom > window.innerHeight - margin) {
    deltaY = elementRect.bottom - window.innerHeight + margin
  }
  if (elementRect.left < 0 + margin) {
    deltaX = elementRect.left - margin
  } else if (elementRect.right > window.innerWidth - margin) {
    deltaX = elementRect.right - window.innerWidth + margin
  }

  window.scrollBy(deltaX, deltaY)
}

function scrollTo(element, options) {
  options = options || {}
  options.margin = options.margin || 0
  let rect = scrollWrapperElements(element, options)
  scrollWindow(element, options, rect)
}

export default scrollTo

@@ -5,597 +5,474 @@
 %0A * 
-Copied from %60element-scroll-to%60, but edited to account for %60window%60 being%0A * inside of iframes.%0A *%0A * https://github.com/webmodules/element-scroll-to%0A */%0A%0Aimport getWindow from 'get-window'%0A%0Afunction scrollWrapperElements(element, options) %7B%0A  let window = getWindow(element)
+Helps scroll the cursor into the middle of view if it isn't in view%0A */%0A%0Aimport getWindow from 'get-window'%0A%0Afunction scrollWindow(window, cursorTop, cursorLeft, cursorHeight) %7B%0A  let deltaX = 0
 %0A  let 
+d
 el
-ementRect = element.getBoundingClientRect()%0A  let wrapper = element.parentNode%0A%0A  while (wrapper != window.document.body) %7B%0A    let wrapperRect = wrapper.getBoundingClientRect()%0A    let margin = options.margin%0A    let deltaX = 0%0A    let deltaY = 0%0A%0A    if (elementRect.top %3C wrapperRect.top + margin
+taY = 0%0A  let cursorBottom = cursorTop + cursorHeight%0A%0A  if (cursorTop %3C 0 %7C%7C cursorBottom %3E window.innerHeight) %7B%0A    deltaY = cursorTop - window.scrollY + window.innerHeight / 2 - cursorHeight / 2%0A  %7D%0A%0A  if (cursorLeft %3C 0 %7C%7C cursorLeft %3E window.innerWidth
 ) %7B%0A
-  
     
@@ -480,518 +480,481 @@
 elta
-Y = elementRect.top - wrapperRect.top - margin%0A    %7D else if (elementRect.bottom %3E wrapperRect.bottom - margin) %7B%0A      deltaY = elementRect.bottom - wrapperRect.bottom + margin%0A    %7D%0A    if (elementRect.left %3C wrapperRect.left + margin) %7B%0A      deltaX = elementRect.left - wrapperRect.left - margin%0A    %7D else if (elementRect.right %3E wrapperRect.right - margin) %7B%0A      deltaX = elementRect.right - wrapp
+X = cursorLeft - window.scrollX + window.innerWidth / 2%0A  %7D%0A%0A  window.scrollBy(deltaX, deltaY)%0A%7D%0A%0Afunction scrollTo(element) %7B%0A  let window = getWindow(element)%0A  let s = window.getSelection()%0A  if (s.rangeCount %3E 0) %7B%0A    let selectionRect = s.getRangeAt(0).getBoundingClientRect()%0A    let innerRect = selectionRect%0A    let wrapper = element%0A    let cursorHeight = inn
 erRect.
-r
+he
 ight
- + margin%0A    %7D%0A    wrapper.scroll
+%0A    let cursor
 Top 
-+
 = 
-deltaY%0A    wrapper.scroll
+innerRect.top%0A    let cursor
 Left 
-+
 = 
-deltaX%0A    
+innerRect.left%0A%0A    while (
 wrapper 
 = wr
@@ -953,820 +953,703 @@
 per 
+!
 = w
-rapper.parentNode%0A  %7D%0A%0A  return elementRect%0A%7D%0A%0Afunction scrollWindow(element, options, elementRect) %7B%0A  let window = getWindow(element)%0A  let margin = options.margin%0A  let deltaX = 0%0A  let deltaY = 0%0A%0A  if (elementRect.top %3C 0 + margin) %7B%0A    deltaY = elementRect.top - margin%0A  %7D else if (elementRect.bottom %3E window.innerHeight - margin) %7B%0A    deltaY = elementRect.bottom - window.innerHeight + margin%0A
+indow.document.body) %7B%0A      let wrapperRect = wrapper.getBoundingClientRect()%0A      let currentY = cursorTop%0A      let cursorBottom = cursorTop + cursorHeight%0A      if (cursorTop %3C wrapperRect.top %7C%7C cursorBottom %3E wrapperRect.bottom) %7B%0A        cursorTop = wrapperRect.top + wrapperRect.height / 2 - cursorHeight / 2%0A        wrapper.scrollTop += currentY - cursorTop%0A    
   %7D%0A
+%0A
   
-if (elementRect.left %3C 0 + margin) %7B%0A    deltaX = elementRect.left - margin%0A  %7D else if (elementRect.right %3E window.innerWidth - margin) %7B%0A    deltaX = element
+    let currentLeft = cursorLeft%0A      if (cursorLeft %3C wrapperRect.left %7C%7C cursorLeft %3E wrapperRect.right) %7B%0A        cursorLeft = wrapper
 Rect.
-right - window.innerWidth + margin%0A  %7D%0A%0A  window.scrollBy(deltaX, deltaY)%0A%7D%0A%0Afunction scrollTo(element, options) %7B%0A  options = options %7C%7C %7B
+left + wrapperRect.width / 2%0A        wrapper.scrollLeft += currentLeft - cursorLeft%0A      
 %7D%0A
+%0A
   
-options.margin = options.margin %7C%7C 0%0A  let rect = scrollWrapperElements(element, options)%0A
+    innerRect = wrapperRect%0A      wrapper = wrapper.parentNode%0A    %7D%0A%0A 
   
+ 
 scro
@@ -1661,31 +1661,56 @@
 dow(
-element, options, rect)
+window, cursorTop, cursorLeft, cursorHeight)%0A  %7D
 %0A%7D%0A%0A

Fix jumpy scroll behaviour (#607)