BlueGit

projects-timfalken-com verhalenboek

Merge branch 'cursor/day-scroll-top-916c' into 'master'

Scroll naar boven bij wisselen van dag

See merge request Timfa/Verhalenboek!8

timfa Tim Falken · pushed by timfa

Authored 2026-10-08 · 5 weeks ago

Hash
0b60d5677ff1458549af73adc1357a91a8c9e676
3 files changed +107 −4
web/assets/app.js +102 −3
@@ -21,6 +21,79 @@ function pathNow() {
21 21 return raw.startsWith('/') ? raw : '/' + raw;
22 22 }
23 23
24 function isDayRoute(path) {
25 return /^\/dag\/\d{4}-\d{2}-\d{2}$/.test(path);
26 }
27
28 // Hash-navigatie blijft één document. De browser onthoudt het scrollpunt per
29 // geschiedenisregel; bij handmatig herstel zetten we een nieuw geopende dag
30 // bovenaan en geven terug/vooruit de opgeslagen plek terug.
31 let historyTraversal = false;
32 let scrollGeneration = 0;
33
34 function savedScrollY() {
35 const entry = history.state;
36 if (!entry || typeof entry !== 'object' || typeof entry.scrollY !== 'number') return null;
37 return entry.scrollY;
38 }
39
40 function saveScrollPosition() {
41 const y = window.scrollY || document.documentElement.scrollTop || 0;
42 const entry = history.state && typeof history.state === 'object' ? history.state : {};
43 if (entry.scrollY === y) return;
44 try { history.replaceState({ ...entry, scrollY: y }, ''); }
45 catch (error) { /* een volle geschiedenis mag het lezen niet blokkeren */ }
46 }
47
48 function scrollToPosition(y) {
49 const root = document.documentElement;
50 const body = document.body;
51 // De stylesheet zet scroll-behavior op smooth. Een kale scrollTo leest dat
52 // en glijdt vanaf de onderkant omhoog; de nieuwe dag moet meteen bovenaan staan.
53 root.style.scrollBehavior = 'auto';
54 if (body) body.style.scrollBehavior = 'auto';
55 root.style.overflowAnchor = 'none';
56 if (body) body.style.overflowAnchor = 'none';
57 try {
58 window.scrollTo({ top: y, left: 0, behavior: 'instant' });
59 } catch (error) {
60 window.scrollTo(0, y);
61 }
62 if (root.scrollTop !== y) root.scrollTop = y;
63 if (body && body.scrollTop !== y) body.scrollTop = y;
64 requestAnimationFrame(() => {
65 root.style.scrollBehavior = '';
66 if (body) body.style.scrollBehavior = '';
67 root.style.overflowAnchor = '';
68 if (body) body.style.overflowAnchor = '';
69 });
70 }
71
72 function settleScroll(path, traversal) {
73 let y = null;
74 if (traversal) y = savedScrollY();
75 if (y === null && isDayRoute(path)) y = 0;
76 if (y === null) {
77 // Weekwissel en overige pagina's houden hun scrollpunt. Wel vastleggen,
78 // zodat terug/vooruit die plek terugzet.
79 saveScrollPosition();
80 return;
81 }
82 const generation = ++scrollGeneration;
83 scrollToPosition(y);
84 saveScrollPosition();
85 requestAnimationFrame(() => {
86 if (generation !== scrollGeneration || pathNow() !== path) return;
87 scrollToPosition(y);
88 saveScrollPosition();
89 });
90 }
91
92 function navigateHash(hash) {
93 saveScrollPosition();
94 location.hash = hash;
95 }
96
24 97 function el(tag, attrs, children) {
25 98 const node = document.createElement(tag);
26 99 Object.entries(attrs || {}).forEach(([key, value]) => {
@@ -461,7 +534,7 @@ async function paintEditor(date) {
461 534 if (!window.confirm('Deze dag en de bijbehorende verhalen verwijderen? Dit kan niet ongedaan worden.')) return;
462 535 try {
463 536 await api('DELETE', '/days/' + date);
464 location.hash = '#/beheer';
537 navigateHash('#/beheer');
465 538 } catch (failure) { error.textContent = failure.message; }
466 539 });
467 540 form.append(remove);
@@ -474,7 +547,7 @@ async function paintEditor(date) {
474 547 try {
475 548 if (creating) {
476 549 const created = await api('POST', '/days', { date: dateInput.value, ...payload });
477 location.hash = '#/beheer/' + created.date;
550 navigateHash('#/beheer/' + created.date);
478 551 } else {
479 552 await api('PUT', '/days/' + date, payload);
480 553 toast('Opgeslagen.');
@@ -608,6 +681,8 @@ function paintKeys(keys) {
608 681 async function render() {
609 682 const token = ++renderToken;
610 683 const path = pathNow();
684 const traversal = historyTraversal;
685 historyTraversal = false;
611 686 if (path !== '/sleutels') {
612 687 state.freshKey = null;
613 688 state.revealedKey = null;
@@ -615,6 +690,12 @@ async function render() {
615 690 closeMenu();
616 691 highlight(path);
617 692 document.title = 'Verhalenboek';
693 let settled = false;
694 const finishScroll = () => {
695 if (settled || token !== renderToken) return;
696 settled = true;
697 settleScroll(path, traversal);
698 };
618 699 try {
619 700 const weekMatch = path.match(/^\/week\/(\d{4}-\d{2}-\d{2})$/);
620 701 if (path === '/' || path === '' || weekMatch) {
@@ -642,12 +723,14 @@ async function render() {
642 723 const data = await api('GET', '/keys');
643 724 if (token !== renderToken) return;
644 725 paintKeys(data.keys);
645 } else if (/^\/dag\/\d{4}-\d{2}-\d{2}$/.test(path)) {
726 } else if (isDayRoute(path)) {
646 727 const date = path.slice(5);
647 728 const day = await api('GET', '/days/' + date);
648 729 if (token !== renderToken) return;
649 730 paintDay(day);
650 731 document.title = day.title + ' · Verhalenboek';
732 // Vóór de lees-POST, anders schildert de browser de nieuwe dag nog onderaan.
733 finishScroll();
651 734 if (!day.read) {
652 735 try { await api('POST', '/days/' + date + '/read'); } catch (error) { /* de pagina blijft leesbaar */ }
653 736 }
@@ -658,6 +741,8 @@ async function render() {
658 741 if (token !== renderToken) return;
659 742 if (error.status === 401) { window.location.href = 'index.php'; return; }
660 743 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]), el('a', { href: '#/' }, ['Naar het overzicht']));
744 } finally {
745 finishScroll();
661 746 }
662 747 }
663 748
@@ -673,7 +758,21 @@ async function boot() {
673 758 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]));
674 759 return;
675 760 }
761 if ('scrollRestoration' in history) history.scrollRestoration = 'manual';
762 window.addEventListener('popstate', () => { historyTraversal = true; });
676 763 window.addEventListener('hashchange', () => { render(); });
764 document.addEventListener('click', (event) => {
765 if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
766 const link = event.target instanceof Element ? event.target.closest('a[href]') : null;
767 if (!link || !(link.getAttribute('href') || '').startsWith('#')) return;
768 saveScrollPosition();
769 }, true);
770 let scrollSaveTimer = 0;
771 window.addEventListener('scroll', () => {
772 clearTimeout(scrollSaveTimer);
773 scrollSaveTimer = setTimeout(saveScrollPosition, 120);
774 }, { passive: true });
775 window.addEventListener('pagehide', saveScrollPosition);
677 776 if (!location.hash) location.replace('#/');
678 777 else render();
679 778 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
@@ -21,6 +21,79 @@ function pathNow() {
21 return raw.startsWith('/') ? raw : '/' + raw;
21 return raw.startsWith('/') ? raw : '/' + raw;
22}
22}
23
23
24function isDayRoute(path) {
25 return /^\/dag\/\d{4}-\d{2}-\d{2}$/.test(path);
26}
27
28// Hash-navigatie blijft één document. De browser onthoudt het scrollpunt per
29// geschiedenisregel; bij handmatig herstel zetten we een nieuw geopende dag
30// bovenaan en geven terug/vooruit de opgeslagen plek terug.
31let historyTraversal = false;
32let scrollGeneration = 0;
33
34function savedScrollY() {
35 const entry = history.state;
36 if (!entry || typeof entry !== 'object' || typeof entry.scrollY !== 'number') return null;
37 return entry.scrollY;
38}
39
40function saveScrollPosition() {
41 const y = window.scrollY || document.documentElement.scrollTop || 0;
42 const entry = history.state && typeof history.state === 'object' ? history.state : {};
43 if (entry.scrollY === y) return;
44 try { history.replaceState({ ...entry, scrollY: y }, ''); }
45 catch (error) { /* een volle geschiedenis mag het lezen niet blokkeren */ }
46}
47
48function scrollToPosition(y) {
49 const root = document.documentElement;
50 const body = document.body;
51 // De stylesheet zet scroll-behavior op smooth. Een kale scrollTo leest dat
52 // en glijdt vanaf de onderkant omhoog; de nieuwe dag moet meteen bovenaan staan.
53 root.style.scrollBehavior = 'auto';
54 if (body) body.style.scrollBehavior = 'auto';
55 root.style.overflowAnchor = 'none';
56 if (body) body.style.overflowAnchor = 'none';
57 try {
58 window.scrollTo({ top: y, left: 0, behavior: 'instant' });
59 } catch (error) {
60 window.scrollTo(0, y);
61 }
62 if (root.scrollTop !== y) root.scrollTop = y;
63 if (body && body.scrollTop !== y) body.scrollTop = y;
64 requestAnimationFrame(() => {
65 root.style.scrollBehavior = '';
66 if (body) body.style.scrollBehavior = '';
67 root.style.overflowAnchor = '';
68 if (body) body.style.overflowAnchor = '';
69 });
70}
71
72function settleScroll(path, traversal) {
73 let y = null;
74 if (traversal) y = savedScrollY();
75 if (y === null && isDayRoute(path)) y = 0;
76 if (y === null) {
77 // Weekwissel en overige pagina's houden hun scrollpunt. Wel vastleggen,
78 // zodat terug/vooruit die plek terugzet.
79 saveScrollPosition();
80 return;
81 }
82 const generation = ++scrollGeneration;
83 scrollToPosition(y);
84 saveScrollPosition();
85 requestAnimationFrame(() => {
86 if (generation !== scrollGeneration || pathNow() !== path) return;
87 scrollToPosition(y);
88 saveScrollPosition();
89 });
90}
91
92function navigateHash(hash) {
93 saveScrollPosition();
94 location.hash = hash;
95}
96
24function el(tag, attrs, children) {
97function el(tag, attrs, children) {
25 const node = document.createElement(tag);
98 const node = document.createElement(tag);
26 Object.entries(attrs || {}).forEach(([key, value]) => {
99 Object.entries(attrs || {}).forEach(([key, value]) => {
@@ -461,7 +534,7 @@ async function paintEditor(date) {
461 if (!window.confirm('Deze dag en de bijbehorende verhalen verwijderen? Dit kan niet ongedaan worden.')) return;
534 if (!window.confirm('Deze dag en de bijbehorende verhalen verwijderen? Dit kan niet ongedaan worden.')) return;
462 try {
535 try {
463 await api('DELETE', '/days/' + date);
536 await api('DELETE', '/days/' + date);
464 location.hash = '#/beheer';
537 navigateHash('#/beheer');
465 } catch (failure) { error.textContent = failure.message; }
538 } catch (failure) { error.textContent = failure.message; }
466 });
539 });
467 form.append(remove);
540 form.append(remove);
@@ -474,7 +547,7 @@ async function paintEditor(date) {
474 try {
547 try {
475 if (creating) {
548 if (creating) {
476 const created = await api('POST', '/days', { date: dateInput.value, ...payload });
549 const created = await api('POST', '/days', { date: dateInput.value, ...payload });
477 location.hash = '#/beheer/' + created.date;
550 navigateHash('#/beheer/' + created.date);
478 } else {
551 } else {
479 await api('PUT', '/days/' + date, payload);
552 await api('PUT', '/days/' + date, payload);
480 toast('Opgeslagen.');
553 toast('Opgeslagen.');
@@ -608,6 +681,8 @@ function paintKeys(keys) {
608async function render() {
681async function render() {
609 const token = ++renderToken;
682 const token = ++renderToken;
610 const path = pathNow();
683 const path = pathNow();
684 const traversal = historyTraversal;
685 historyTraversal = false;
611 if (path !== '/sleutels') {
686 if (path !== '/sleutels') {
612 state.freshKey = null;
687 state.freshKey = null;
613 state.revealedKey = null;
688 state.revealedKey = null;
@@ -615,6 +690,12 @@ async function render() {
615 closeMenu();
690 closeMenu();
616 highlight(path);
691 highlight(path);
617 document.title = 'Verhalenboek';
692 document.title = 'Verhalenboek';
693 let settled = false;
694 const finishScroll = () => {
695 if (settled || token !== renderToken) return;
696 settled = true;
697 settleScroll(path, traversal);
698 };
618 try {
699 try {
619 const weekMatch = path.match(/^\/week\/(\d{4}-\d{2}-\d{2})$/);
700 const weekMatch = path.match(/^\/week\/(\d{4}-\d{2}-\d{2})$/);
620 if (path === '/' || path === '' || weekMatch) {
701 if (path === '/' || path === '' || weekMatch) {
@@ -642,12 +723,14 @@ async function render() {
642 const data = await api('GET', '/keys');
723 const data = await api('GET', '/keys');
643 if (token !== renderToken) return;
724 if (token !== renderToken) return;
644 paintKeys(data.keys);
725 paintKeys(data.keys);
645 } else if (/^\/dag\/\d{4}-\d{2}-\d{2}$/.test(path)) {
726 } else if (isDayRoute(path)) {
646 const date = path.slice(5);
727 const date = path.slice(5);
647 const day = await api('GET', '/days/' + date);
728 const day = await api('GET', '/days/' + date);
648 if (token !== renderToken) return;
729 if (token !== renderToken) return;
649 paintDay(day);
730 paintDay(day);
650 document.title = day.title + ' · Verhalenboek';
731 document.title = day.title + ' · Verhalenboek';
732 // Vóór de lees-POST, anders schildert de browser de nieuwe dag nog onderaan.
733 finishScroll();
651 if (!day.read) {
734 if (!day.read) {
652 try { await api('POST', '/days/' + date + '/read'); } catch (error) { /* de pagina blijft leesbaar */ }
735 try { await api('POST', '/days/' + date + '/read'); } catch (error) { /* de pagina blijft leesbaar */ }
653 }
736 }
@@ -658,6 +741,8 @@ async function render() {
658 if (token !== renderToken) return;
741 if (token !== renderToken) return;
659 if (error.status === 401) { window.location.href = 'index.php'; return; }
742 if (error.status === 401) { window.location.href = 'index.php'; return; }
660 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]), el('a', { href: '#/' }, ['Naar het overzicht']));
743 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]), el('a', { href: '#/' }, ['Naar het overzicht']));
744 } finally {
745 finishScroll();
661 }
746 }
662}
747}
663
748
@@ -673,7 +758,21 @@ async function boot() {
673 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]));
758 app.replaceChildren(el('p', { class: 'form-error' }, [error.message]));
674 return;
759 return;
675 }
760 }
761 if ('scrollRestoration' in history) history.scrollRestoration = 'manual';
762 window.addEventListener('popstate', () => { historyTraversal = true; });
676 window.addEventListener('hashchange', () => { render(); });
763 window.addEventListener('hashchange', () => { render(); });
764 document.addEventListener('click', (event) => {
765 if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
766 const link = event.target instanceof Element ? event.target.closest('a[href]') : null;
767 if (!link || !(link.getAttribute('href') || '').startsWith('#')) return;
768 saveScrollPosition();
769 }, true);
770 let scrollSaveTimer = 0;
771 window.addEventListener('scroll', () => {
772 clearTimeout(scrollSaveTimer);
773 scrollSaveTimer = setTimeout(saveScrollPosition, 120);
774 }, { passive: true });
775 window.addEventListener('pagehide', saveScrollPosition);
677 if (!location.hash) location.replace('#/');
776 if (!location.hash) location.replace('#/');
678 else render();
777 else render();
679 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
778 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
web/assets/pwa.js +4 −0
@@ -1,5 +1,9 @@
1 1 'use strict';
2 2
3 // Zelfde document bij een hash-wissel. Zonder dit zet de browser het oude
4 // scrollpunt terug, ook als de shell uit de service worker komt.
5 if ('scrollRestoration' in history) history.scrollRestoration = 'manual';
6
3 7 function appScope() {
4 8 let path = window.location.pathname;
5 9 if (!path.endsWith('/')) {
@@ -1,5 +1,9 @@
1'use strict';
1'use strict';
2
2
3// Zelfde document bij een hash-wissel. Zonder dit zet de browser het oude
4// scrollpunt terug, ook als de shell uit de service worker komt.
5if ('scrollRestoration' in history) history.scrollRestoration = 'manual';
6
3function appScope() {
7function appScope() {
4 let path = window.location.pathname;
8 let path = window.location.pathname;
5 if (!path.endsWith('/')) {
9 if (!path.endsWith('/')) {
web/lib/bootstrap.php +1 −1
@@ -247,7 +247,7 @@ function pwaHead(): void {
247 247 <meta name="apple-mobile-web-app-capable" content="yes">
248 248 <meta name="apple-mobile-web-app-title" content="Verhalenboek">
249 249 <meta name="apple-mobile-web-app-status-bar-style" content="default">
250 <script src="{$script}" defer></script>
250 <script src="{$script}"></script>
251 251
252 252 HTML;
253 253 }
@@ -247,7 +247,7 @@ function pwaHead(): void {
247<meta name="apple-mobile-web-app-capable" content="yes">
247<meta name="apple-mobile-web-app-capable" content="yes">
248<meta name="apple-mobile-web-app-title" content="Verhalenboek">
248<meta name="apple-mobile-web-app-title" content="Verhalenboek">
249<meta name="apple-mobile-web-app-status-bar-style" content="default">
249<meta name="apple-mobile-web-app-status-bar-style" content="default">
250<script src="{$script}" defer></script>
250<script src="{$script}"></script>
251
251
252HTML;
252HTML;
253}
253}