added scroll position to nav history...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-21 03:04:48 +03:00
parent 7b78b5133a
commit 77e9f0819e
2 changed files with 35 additions and 6 deletions

View File

@ -375,6 +375,29 @@ require(['./browser'], function(browser){
// NOTE: this seems not to work if the REFRESH_DELAY is too small... // NOTE: this seems not to work if the REFRESH_DELAY is too small...
setTimeout(function(){ setTimeout(function(){
pwiki.location = [path, hash] }, REFRESH_DELAY) }) 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 pwiki
.onBeforeNavigate(function(){ .onBeforeNavigate(function(){
saveAll() }) saveAll() })
@ -382,7 +405,7 @@ require(['./browser'], function(browser){
// NOTE: we do not need to directly update location.hash here as // NOTE: we do not need to directly update location.hash here as
// that will push an extra history item... // that will push an extra history item...
history.replaceState( history.replaceState(
{path: this.location}, { path: this.location },
'', '',
'#'+this.location '#'+this.location
+(this.hash ? +(this.hash ?
@ -416,11 +439,15 @@ require(['./browser'], function(browser){
`${titles.length > 1 ? `${titles.length > 1 ?
titles.at(-1).innerText titles.at(-1).innerText
: this.title} &mdash; pWiki` : this.title} &mdash; pWiki`
// scroll to anchor element... // scroll...
this.hash this.hash ?
&& this.dom // to anchor element...
this.dom
.querySelector('#'+ this.hash) .querySelector('#'+ this.hash)
.scrollIntoView() .scrollIntoView()
// restore history position...
// XXX HACK this will work only on full page...
: (document.scrollingElement.scrollTop = this.__scroll ?? 0)
// XXX MediumEditor... // XXX MediumEditor...
//setupMediumEditor() //setupMediumEditor()

View File

@ -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... * XXX sort: define a generic sort path argument...
* ...this will unify all interfaces (macros/patterns/etc) * ...this will unify all interfaces (macros/patterns/etc)
* XXX macros: else/default macro args essentially mean the same thing, should we * XXX macros: else/default macro args essentially mean the same thing, should we
@ -51,6 +49,10 @@
* await pwiki.parse('<macro src=../tags join=", ">@source(.)</macro>') * await pwiki.parse('<macro src=../tags join=", ">@source(.)</macro>')
* This will not: * This will not:
* await pwiki.parse('<macro src="../tags" join=", ">@source(.)</macro>') * await pwiki.parse('<macro src="../tags" join=", ">@source(.)</macro>')
* 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 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 macros: Q: do we need macros for printing errors and the like???
* XXX the parser should handle all action return values, including: * XXX the parser should handle all action return values, including: