keep scroll position on install/delete

This commit is contained in:
tophf 2017-12-14 01:13:16 +03:00
parent 78c14dd93c
commit f048649e58

View File

@ -534,7 +534,9 @@ window.addEventListener('showStyles:done', function _() {
function onUninstallClicked(event) {
event.stopPropagation();
const entry = this.closest('.search-result');
deleteStyleSafe({id: entry._result.installedStyleId});
saveScrollPosition(entry);
deleteStyleSafe({id: entry._result.installedStyleId})
.then(restoreScrollPosition);
}
/** Installs the current userstyleSearchResult into Stylus. */
@ -546,6 +548,7 @@ window.addEventListener('showStyles:done', function _() {
const installButton = $('.search-result-install', entry);
showSpinner(entry);
saveScrollPosition(entry);
installButton.disabled = true;
entry.style.setProperty('pointer-events', 'none', 'important');
@ -570,6 +573,7 @@ window.addEventListener('showStyles:done', function _() {
$.remove('.lds-spinner', entry);
installButton.disabled = false;
entry.style.pointerEvents = '';
restoreScrollPosition();
});
function fetchStyleSettings(result) {
@ -581,6 +585,21 @@ window.addEventListener('showStyles:done', function _() {
}
}
function saveScrollPosition(entry) {
dom.scrollPosition = entry.getBoundingClientRect().top;
dom.scrollPositionElement = entry;
}
function restoreScrollPosition() {
const t0 = performance.now();
new MutationObserver((mutations, observer) => {
if (performance.now() - t0 < 1000) {
window.scrollBy(0, dom.scrollPositionElement.getBoundingClientRect().top - dom.scrollPosition);
}
observer.disconnect();
}).observe(document.body, {childList: true, subtree: true, attributes: true});
}
//endregion
//region USO API wrapper