From 77e9f0819e5495467e72e17a48e3f0b724034057 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 21 Nov 2022 03:04:48 +0300 Subject: [PATCH] added scroll position to nav history... Signed-off-by: Alex A. Naanou --- pwiki2.html | 35 +++++++++++++++++++++++++++++++---- pwiki2.js | 6 ++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/pwiki2.html b/pwiki2.html index de1a8cf..32dc9cd 100755 --- a/pwiki2.html +++ b/pwiki2.html @@ -375,6 +375,29 @@ require(['./browser'], function(browser){ // NOTE: this seems not to work if the REFRESH_DELAY is too small... setTimeout(function(){ pwiki.location = [path, hash] }, REFRESH_DELAY) }) + + // scroll... + // NOTE: we restore scroll position only on history navigation... + var save_scroll = function(){ + history.replaceState( + { + path: pwiki.location, + // XXX HACK this will work only on full page... + scroll: document.scrollingElement.scrollTop, + }, + '', + // XXX + window.location.hash) } + // save scroll position just after scroll is done... + var scrolling + window.addEventListener('scroll', function(evt){ + scrolling + && clearTimeout(scrolling) + scrolling = setTimeout(save_scroll, 200) }) + // get scroll position from history state... + window.addEventListener('popstate', function(evt){ + pwiki.__scroll = (evt.state ?? {}).scroll }) + pwiki .onBeforeNavigate(function(){ saveAll() }) @@ -382,7 +405,7 @@ require(['./browser'], function(browser){ // NOTE: we do not need to directly update location.hash here as // that will push an extra history item... history.replaceState( - {path: this.location}, + { path: this.location }, '', '#'+this.location +(this.hash ? @@ -416,11 +439,15 @@ require(['./browser'], function(browser){ `${titles.length > 1 ? titles.at(-1).innerText : this.title} — pWiki` - // scroll to anchor element... - this.hash - && this.dom + // scroll... + this.hash ? + // to anchor element... + this.dom .querySelector('#'+ this.hash) .scrollIntoView() + // restore history position... + // XXX HACK this will work only on full page... + : (document.scrollingElement.scrollTop = this.__scroll ?? 0) // XXX MediumEditor... //setupMediumEditor() diff --git a/pwiki2.js b/pwiki2.js index dabff9c..f27537f 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -36,8 +36,6 @@ * - * * -* XXX load new page at .scrollTop = 0 if no anchors are given... -* ...and/or remember scroll position per page... * XXX sort: define a generic sort path argument... * ...this will unify all interfaces (macros/patterns/etc) * XXX macros: else/default macro args essentially mean the same thing, should we @@ -51,6 +49,10 @@ * await pwiki.parse('@source(.)') * This will not: * await pwiki.parse('@source(.)') +* XXX FEATURE: make the pwiki "widget" work bot as an element and as a +* full page (current)... +* ...would need to handle stuff like history, url, nav locally... +* see: pwiki2.html * XXX ASAP test: can we store the file handler with permissions in a ServiceWorker?? * XXX macros: Q: do we need macros for printing errors and the like??? * XXX the parser should handle all action return values, including: