|
2907 | 2907 | // asks for the latest scan time and session activity instead. Every tab keeps its |
2908 | 2908 | // relative times current. |
2909 | 2909 | const synced = $("#synced"); |
| 2910 | + // Newest work first, as the server orders it, by the times the tick just moved. |
| 2911 | + // Whatever is at the top of the window stays where it is on screen. |
| 2912 | + function summaryReorder() { |
| 2913 | + const newest = (time) => time ? new Date(time.dateTime).getTime() || 0 : 0; |
| 2914 | + const order = (items, time) => { |
| 2915 | + const sorted = [...items].sort((a, b) => newest(time(b)) - newest(time(a))); |
| 2916 | + return sorted.some((item, i) => item !== items[i]) ? sorted : null; |
| 2917 | + }; |
| 2918 | + const anchor = $$(".summary-project, .summary-topic").find(el => el.getBoundingClientRect().bottom > 0); |
| 2919 | + const top = anchor?.getBoundingClientRect().top; |
| 2920 | + let moved = false; |
| 2921 | + $$(".summary-toc-list").forEach(list => { |
| 2922 | + const sorted = order([...list.children], li => $("time[data-summary-project]", li)); |
| 2923 | + if (sorted) { list.append(...sorted); moved = true; } |
| 2924 | + }); |
| 2925 | + const links = $$(".summary-toc-list a[href^='#summary-project-']").slice(0, $$(".summary-project[id]").length); |
| 2926 | + const sections = links.map(link => document.getElementById(link.getAttribute("href").slice(1))).filter(Boolean); |
| 2927 | + const content = sections[0]?.parentElement; |
| 2928 | + if (content && sections.some((section, i) => section !== $$(".summary-project[id]")[i])) { content.prepend(...sections); moved = true; } |
| 2929 | + $$(".summary-project[id]").forEach(section => { |
| 2930 | + const topics = $$(":scope > .summary-topic:has(time[data-summary-topic])", section); |
| 2931 | + const sorted = order(topics, topic => $("time[data-summary-topic]", topic)); |
| 2932 | + if (sorted) { (topics[0].previousElementSibling ? topics[0].previousElementSibling.after(...sorted) : section.prepend(...sorted)); moved = true; } |
| 2933 | + }); |
| 2934 | + if (moved && anchor && anchor.isConnected) window.scrollBy(0, anchor.getBoundingClientRect().top - top); |
| 2935 | + } |
2910 | 2936 | async function tick() { |
2911 | 2937 | if (tab === "summary" && synced) { |
2912 | 2938 | try { |
|
2921 | 2947 | if (at && at > new Date(time.dateTime)) { time.dateTime = at.toISOString(); time.title = at.toLocaleString(); } |
2922 | 2948 | }); |
2923 | 2949 | } |
| 2950 | + summaryReorder(); |
2924 | 2951 | } |
2925 | 2952 | } catch {} |
2926 | 2953 | } |
|
0 commit comments