From d8914c38a722f188609660f939884b77b3ada00a Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 2 Aug 2018 21:10:43 +0300 Subject: [PATCH] highlight edited style on history back in FF --- manage/manage.js | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/manage/manage.js b/manage/manage.js index 6db745d6..3354d4d7 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -73,7 +73,7 @@ function initGlobalEvents() { installed.addEventListener('mouseover', handleEvent.lazyAddEntryTitle); installed.addEventListener('mouseout', handleEvent.lazyAddEntryTitle); - document.addEventListener('visibilitychange', rememberScrollPosition); + document.addEventListener('visibilitychange', onVisibilityChange); $$('[data-toggle-on-click]').forEach(el => { // dataset on SVG doesn't work in Chrome 49-??, works in 57+ @@ -115,7 +115,6 @@ function initGlobalEvents() { `)); } - function showStyles(styles = [], matchUrlIds) { const sorted = sorter.sort({ styles: styles.map(style => ({ @@ -164,14 +163,7 @@ function showStyles(styles = [], matchUrlIds) { if ('scrollY' in (history.state || {}) && !sessionStorage.justEditedStyleId) { setTimeout(window.scrollTo, 0, 0, history.state.scrollY); } - if (sessionStorage.justEditedStyleId) { - const entry = $(ENTRY_ID_PREFIX + sessionStorage.justEditedStyleId); - delete sessionStorage.justEditedStyleId; - if (entry) { - animateElement(entry); - requestAnimationFrame(() => scrollElementIntoView(entry)); - } - } + highlightEditedStyle(); } } @@ -416,7 +408,7 @@ Object.assign(handleEvent, { }); } } else { - rememberScrollPosition(); + onVisibilityChange(); getActiveTab().then(tab => { sessionStorageHash('manageStylesHistory').set(tab.id, url); location.href = url; @@ -687,8 +679,28 @@ function switchUI({styleOnly} = {}) { } -function rememberScrollPosition() { - history.replaceState({scrollY: window.scrollY}, document.title); +function onVisibilityChange() { + switch (document.visibilityState) { + // page restored without reloading via history navigation (currently only in FF) + case 'visible': + highlightEditedStyle(); + break; + // going away + case 'hidden': + history.replaceState({scrollY: window.scrollY}, document.title); + break; + } +} + + +function highlightEditedStyle() { + if (!sessionStorage.justEditedStyleId) return; + const entry = $(ENTRY_ID_PREFIX + sessionStorage.justEditedStyleId); + delete sessionStorage.justEditedStyleId; + if (entry) { + animateElement(entry); + requestAnimationFrame(() => scrollElementIntoView(entry)); + } }