Created
October 8, 2014 16:59
-
-
Save saschanaz/d742095e46ff093c3805 to your computer and use it in GitHub Desktop.
Atom Touch-scroll Enabler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// removeListeners From http://stackoverflow.com/a/18650831 | |
function removeListeners(el) { | |
//only replace the ancestor element | |
var clone = el.cloneNode(false); | |
//copy children backwards because of the removal | |
for (var index = el.childNodes.length - 1; index >= 0; --index) | |
clone.insertBefore(el.childNodes[index], clone.firstChild); | |
//insert back into DOM | |
el.parentNode.replaceChild(clone, el); | |
return clone; | |
} | |
function transformToScroll(view, linenumber) { | |
var scroll = parseInt(linenumber.style.webkitTransform.match(/translate3d\(0px, -?([0-9]*)px, 0px/)[1]); | |
view.scrollTop = scroll; | |
} | |
function patchEditor(editor) { | |
Array.prototype.forEach.call(editor.querySelectorAll(".horizontal-scrollbar, .vertical-scrollbar"), | |
function (bar) { | |
bar.style.display = "none" | |
}); | |
var view = editor.querySelector(".scroll-view"); | |
var linenumber = editor.querySelector(".line-numbers"); | |
var content = editor.querySelector(".lines.overlayer"); | |
// Preventing any further transforms, by suppressing -webkit- prefixed property. | |
content.style.transform = "translate3d(0px, 0px, 0px)"; | |
view = removeListeners(view); | |
// Getting our scrollbars here. | |
view.style.overflow = "auto"; | |
view.style.height = "100%"; | |
view.addEventListener("scroll", function () { | |
// Match scroll position when touch-scrolled | |
linenumber.style.webkitTransform = "translate3d(0px, -" + view.scrollTop + "px, 0px)"; | |
}); | |
// Match scroll position when cursor-scrolled | |
transformToScroll(view, linenumber); | |
var viewobserver = new MutationObserver(function () { | |
transformToScroll(view, linenumber); | |
}); | |
viewobserver.observe(linenumber, { attributes: true }); | |
} | |
// Patch existing tabs | |
Array.prototype.forEach.call(document.querySelectorAll(".editor"), patchEditor); | |
// Patch future tabs | |
var tabobserver = new MutationObserver(function (mutations) { | |
Array.prototype.forEach.call(mutations[0].addedNodes, patchEditor); | |
}); | |
tabobserver.observe(document.querySelector(".item-views"), { childList: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment