gui: a click never moves the page, held for every view in one place (为什么点击一个控件总会滚到页面顶部,能否从根上解决) — picking Live after a day emptied the requests' list, the page got shorter than where the reader was, and the browser pulled it up; any code could also call scrollOnPurpose() and scroll on a redraw, a tab switch or a filter. Now what the reader clicks stays where it is on the screen for the next 4s: the element clicked, or once it's drawn again its nearest neighbour or parent, is held in place against content above it changing, and when content below it shrinks, room is kept at the view's foot (a .view-room spacer, gone once the reader scrolls back up) so the page isn't clamped. Code scrolls only with scrollOnPurpose(e) given the reader's own event, and without one the scroll is refused. The wheel, touch, keys and dragging are the reader's and scroll as before. The Routing page's days stay reachable with no live requests, and the library tabs no longer reset to the top. New click-scroll.test.cjs (Live/day switching with the view scrolled to its end, a jump, growth, redraws and helpers with no event, a scroll asked for, the room kept and removed, the wheel) passes 12/12 on Chromium and WebKit; the old code fails it (picking Live moved the page 294px); menu-scroll 22/22
This commit is contained in:
@@ -233,6 +233,7 @@ body.window .top.cramped .seg { position: relative; left: auto; top: auto; trans
|
||||
.view { flex: 1; min-height: 0; overflow-y: auto; padding: 0 12px 4px; display: flex; flex-direction: column; animation: view-in .24s var(--ease); }
|
||||
@keyframes view-in { from { opacity: 0; transform: translateY(5px); } }
|
||||
body.window .view { padding: 2px 16px 8px; }
|
||||
.view > .view-room { flex: none; pointer-events: none; } /* see "where the reader is" */
|
||||
.view::-webkit-scrollbar { width: 0; }
|
||||
/* Windows and Linux draw a classic scrollbar, arrows and all; a thin rounded
|
||||
thumb instead, like the overlay one macOS shows (left as it is there) */
|
||||
|
||||
+106
-22
@@ -4367,9 +4367,9 @@ function hostOf(u) { try { return new URL(u.includes("://") ? u : "https://" + u
|
||||
|
||||
// the sheet opens below the list: it unrolls on the rows' spring and the
|
||||
// view goes down with it
|
||||
$("#addProvider").onclick = () => {
|
||||
$("#addProvider").onclick = (e) => {
|
||||
adding = true; editing = null; draft = null; renderProviders();
|
||||
unrollSheet($("#view-providers"), $("#addSheet"));
|
||||
unrollSheet($("#view-providers"), $("#addSheet"), e);
|
||||
};
|
||||
|
||||
// unrollSheet opens a sheet just drawn at the foot of a view from nothing to
|
||||
@@ -4380,15 +4380,15 @@ $("#addProvider").onclick = () => {
|
||||
// never runs ahead to be held back and jump, nor stops short; and it is
|
||||
// the view's own scrollTop, which WebKit animates where it won't a smooth
|
||||
// scrollIntoView. The reader scrolling meanwhile has the view from then on.
|
||||
function unrollSheet(view, sheet) {
|
||||
function unrollSheet(view, sheet, e) {
|
||||
const from = view.scrollTop, room = view.scrollHeight - view.clientHeight;
|
||||
const to = Math.max(from, Math.min(from + sheet.getBoundingClientRect().top - view.getBoundingClientRect().top - 12, room));
|
||||
const h = sheet.offsetHeight;
|
||||
// how tall the sheet is when it reaches the view's foot, where the view
|
||||
// can start to move: from there the view goes down as it grows
|
||||
const x0 = Math.max(0, h - (room - from));
|
||||
scrollOnPurpose(ROW_OPEN.ms + 300);
|
||||
if (!h || matchMedia("(prefers-reduced-motion: reduce)").matches) { view.scrollTop = to; return; }
|
||||
const go = scrollOnPurpose(e, ROW_OPEN.ms + 300); // opened by a click, not by code
|
||||
if (!h || matchMedia("(prefers-reduced-motion: reduce)").matches) { if (go) view.scrollTop = to; return; }
|
||||
const pad = getComputedStyle(sheet);
|
||||
sheet.style.overflow = "hidden";
|
||||
const grow = sheet.animate([
|
||||
@@ -4401,7 +4401,7 @@ function unrollSheet(view, sheet) {
|
||||
}
|
||||
let set = from;
|
||||
const follow = () => {
|
||||
if (Math.abs(view.scrollTop - set) > 2) return; // the reader took it
|
||||
if (!go || Math.abs(view.scrollTop - set) > 2) return; // the reader took it
|
||||
const done = grow.playState === "finished";
|
||||
view.scrollTop = Math.round(from + (to - from) * (done ? 1 : Math.min(1, Math.max(0, sheet.offsetHeight - x0) / (h - x0))));
|
||||
set = view.scrollTop; // as far as there was room for
|
||||
@@ -6171,23 +6171,49 @@ function savePrefs(body) {
|
||||
// ---------- header / footer ----------
|
||||
|
||||
// ---------- where the reader is ----------
|
||||
// Each view stays scrolled where the reader put it. Only the reader moves it
|
||||
// — the wheel or trackpad, a touch, the keys that scroll, Tab, a drag — or
|
||||
// code that says so first with scrollOnPurpose(). Anything else that moves
|
||||
// it is put back before it's painted: a part of the page redrawn, and
|
||||
// measured while it was briefly shorter, pulls the page up to what was left
|
||||
// of it (WebKit has no scroll anchoring), and WebKit scrolls a field it
|
||||
// focuses to the middle of the view. Never set a view's scrollTop, or
|
||||
// scrollIntoView inside one, without scrollOnPurpose().
|
||||
let purposeUntil = 0;
|
||||
function scrollOnPurpose(ms = 1000) { purposeUntil = Math.max(purposeUntil, performance.now() + ms); }
|
||||
// Two rules keep the page under the reader, held here for every view so no
|
||||
// part of the app has to remember them:
|
||||
//
|
||||
// - A view moves only for the reader: the wheel or trackpad, a touch, the
|
||||
// keys that scroll, Tab, a drag. Anything else that scrolls it is put
|
||||
// back before it's painted: a part redrawn and measured while briefly
|
||||
// shorter pulls the page up to what was left of it (WebKit has no scroll
|
||||
// anchoring), WebKit scrolls a field it focuses to the middle of the view,
|
||||
// and code sets scrollTop.
|
||||
// - What the reader clicks stays where it is on the screen while what the
|
||||
// click does redraws around it — the part above it grown or shrunk, the
|
||||
// control itself drawn again, a load come in — till the reader scrolls or
|
||||
// clicks again, or it has settled. A click is never a scroll: a tab, a
|
||||
// filter, a day, a toggle leaves the page where it was. When what it does
|
||||
// leaves the page shorter under it (a list emptied), the view keeps room
|
||||
// at its foot for it to stay, room that goes as the reader scrolls back.
|
||||
//
|
||||
// Code moves a view only in answer to a click that asks to go somewhere, and
|
||||
// shows that with the reader's event: scrollOnPurpose(e). Called without one
|
||||
// (from a load, a timer, a helper that other clicks share) it is refused,
|
||||
// and whatever scroll follows is put back.
|
||||
let purposeUntil = 0, held = null;
|
||||
const readerScrolls = (ms) => { purposeUntil = Math.max(purposeUntil, performance.now() + ms); held = null; };
|
||||
function scrollOnPurpose(e, ms = 1000) {
|
||||
if (!e?.isTrusted || performance.now() - e.timeStamp > 1000) {
|
||||
console.warn("magpie: a scroll not asked for by the reader was refused");
|
||||
return false;
|
||||
}
|
||||
readerScrolls(ms);
|
||||
return true;
|
||||
}
|
||||
const SCROLL_KEYS = new Set(["PageUp", "PageDown", "Home", "End", "ArrowUp", "ArrowDown", " "]);
|
||||
addEventListener("wheel", () => scrollOnPurpose(250), { capture: true, passive: true });
|
||||
addEventListener("touchmove", () => scrollOnPurpose(250), { capture: true, passive: true });
|
||||
addEventListener("pointermove", (e) => { if (e.buttons) scrollOnPurpose(250); }, { capture: true, passive: true });
|
||||
addEventListener("wheel", () => readerScrolls(250), { capture: true, passive: true });
|
||||
addEventListener("touchmove", () => readerScrolls(250), { capture: true, passive: true });
|
||||
// a drag, not the tremble of a click
|
||||
let downAt = null;
|
||||
addEventListener("pointerdown", (e) => { downAt = [e.clientX, e.clientY]; }, { capture: true, passive: true });
|
||||
addEventListener("pointermove", (e) => {
|
||||
if (e.buttons && downAt && Math.hypot(e.clientX - downAt[0], e.clientY - downAt[1]) > 6) readerScrolls(250);
|
||||
}, { capture: true, passive: true });
|
||||
addEventListener("keydown", (e) => {
|
||||
const typing = e.target.closest?.("input, textarea, select, [contenteditable]");
|
||||
if (e.key === "Tab" || (!typing && SCROLL_KEYS.has(e.key))) scrollOnPurpose(400);
|
||||
if (e.key === "Tab" || (!typing && SCROLL_KEYS.has(e.key))) readerScrolls(400);
|
||||
}, true);
|
||||
const readerAt = new WeakMap();
|
||||
function backToReader(v) {
|
||||
@@ -6199,13 +6225,71 @@ function backToReader(v) {
|
||||
const f = document.activeElement;
|
||||
if (f && f !== document.body && v.contains(f)) {
|
||||
const r = f.getBoundingClientRect(), b = v.getBoundingClientRect();
|
||||
if (r.bottom > b.bottom || r.top < b.top) { scrollOnPurpose(); f.scrollIntoView({ block: "nearest" }); }
|
||||
if (r.bottom > b.bottom || r.top < b.top) { readerScrolls(1000); f.scrollIntoView({ block: "nearest" }); }
|
||||
}
|
||||
}
|
||||
// Where an element is on the screen in its view. What's held is the element
|
||||
// clicked or, once it's gone or hidden (drawn again), the nearest still
|
||||
// there of its neighbours, its parents and theirs; one moving as it plays
|
||||
// (a row springing open) is passed over, so the page doesn't follow the play.
|
||||
const onScreen = (n, v) => n.getBoundingClientRect().top - v.getBoundingClientRect().top;
|
||||
const atRest = (n) => n.isConnected && n.offsetParent && !n.getAnimations().some((a) => a.playState === "running");
|
||||
// The room is an empty block last in the view (padding at its foot would
|
||||
// count in its height only a frame later), put back when a redraw of the
|
||||
// view takes it out.
|
||||
const room = new WeakMap(); // px kept at a view's foot, past its content
|
||||
const roomOf = (v) => (v.querySelector(":scope > .view-room") ? room.get(v) || 0 : 0);
|
||||
function setRoom(v, px) {
|
||||
px = Math.max(0, Math.round(px));
|
||||
let r = v.querySelector(":scope > .view-room");
|
||||
if (!px) { r?.remove(); room.delete(v); return; }
|
||||
if (!r) { r = document.createElement("div"); r.className = "view-room"; r.setAttribute("aria-hidden", "true"); }
|
||||
if (r !== v.lastElementChild) v.append(r);
|
||||
r.style.height = px + "px";
|
||||
room.set(v, px);
|
||||
}
|
||||
// only as much room as keeps the view where it is: none once the content
|
||||
// reaches the view's foot again
|
||||
function fitRoom(v) {
|
||||
const r = roomOf(v);
|
||||
if (r) setRoom(v, Math.min(r, v.scrollTop + v.clientHeight - (v.scrollHeight - r)));
|
||||
}
|
||||
function hold(h) {
|
||||
const a = h.chain.find(([n]) => atRest(n));
|
||||
if (!a) return;
|
||||
const v = h.v, d = onScreen(a[0], v) - a[1];
|
||||
if (Math.abs(d) >= 1) {
|
||||
const want = v.scrollTop + d, max = v.scrollHeight - v.clientHeight;
|
||||
if (want > max) setRoom(v, roomOf(v) + want - max);
|
||||
v.scrollTop = want;
|
||||
}
|
||||
fitRoom(v);
|
||||
readerAt.set(v, v.scrollTop);
|
||||
}
|
||||
let holding = false; // one frame loop, whatever the clicks
|
||||
function keepHeld() {
|
||||
const h = held;
|
||||
if (h && (h.v.hidden || performance.now() > h.until)) held = null;
|
||||
if (!held) { holding = false; return; }
|
||||
hold(held);
|
||||
requestAnimationFrame(keepHeld);
|
||||
}
|
||||
addEventListener("click", (e) => {
|
||||
purposeUntil = 0; // what came before the click (Space pressed on a button, a tremble) is no scroll
|
||||
const v = e.target.closest?.(".view");
|
||||
if (!v || v.hidden) { held = null; return; }
|
||||
const chain = [];
|
||||
for (let n = e.target; n && n !== v; n = n.parentElement) {
|
||||
for (const m of [n, n.previousElementSibling, n.nextElementSibling]) if (m instanceof HTMLElement && m.offsetParent) chain.push([m, onScreen(m, v)]);
|
||||
}
|
||||
held = chain.length ? { v, chain, until: performance.now() + 4000 } : null;
|
||||
if (held && !holding) { holding = true; requestAnimationFrame(keepHeld); }
|
||||
}, true);
|
||||
for (const v of document.querySelectorAll(".view")) {
|
||||
v.addEventListener("scroll", () => {
|
||||
if (v.hidden) return;
|
||||
if (performance.now() < purposeUntil) readerAt.set(v, v.scrollTop);
|
||||
if (performance.now() < purposeUntil) { fitRoom(v); readerAt.set(v, v.scrollTop); }
|
||||
else if (held?.v === v) hold(held);
|
||||
else backToReader(v);
|
||||
}, { passive: true });
|
||||
}
|
||||
|
||||
@@ -369,7 +369,7 @@
|
||||
["mcp", t("MCP servers") + (counts.mcp ? " · " + counts.mcp : "")],
|
||||
["skills", t("Skills") + (counts.skills ? " · " + counts.skills : "")],
|
||||
["rtk", "RTK"],
|
||||
], tab, (id) => { tab = id; try { localStorage.setItem("magpie.libTab", id); } catch {} render(); page.scrollTop = 0; syncLists(); });
|
||||
], tab, (id) => { tab = id; try { localStorage.setItem("magpie.libTab", id); } catch {} render(); syncLists(); });
|
||||
tabs.classList.add("lib-tabs");
|
||||
head.append(tabs, el("span", "grow"));
|
||||
const more = button("", "lib-more", () => reveal(lib.dir));
|
||||
|
||||
@@ -885,16 +885,16 @@
|
||||
}
|
||||
|
||||
// pick sets the stage to a past request, or back to live with the newest;
|
||||
// the page goes up to the stage only when the reader picked a request in
|
||||
// the list (look), never on a day or Live picked in the list's own bar
|
||||
function pick(r, look = true) {
|
||||
// the page goes up to the stage only for a request the reader clicked in
|
||||
// the list (e, that click), never for a day or Live picked in its bar
|
||||
function pick(r, e) {
|
||||
pinned = r.id === newest()?.id ? null : r;
|
||||
if (rp) { rp = null; rbar.hidden = true; }
|
||||
stopPlays();
|
||||
cur = r;
|
||||
sync(true); renderAll();
|
||||
say(affWhy(r, true) || ruleWhy(r, true) || firstWhy(r));
|
||||
if (pinned && look) { scrollOnPurpose(); box.scrollIntoView({ block: "nearest", behavior: still() ? "auto" : "smooth" }); }
|
||||
if (pinned && e && scrollOnPurpose(e)) { box.scrollIntoView({ block: "nearest", behavior: still() ? "auto" : "smooth" }); }
|
||||
}
|
||||
|
||||
// who answered a request, or what its agent got
|
||||
@@ -924,7 +924,7 @@
|
||||
days = res.days || [];
|
||||
if (d && d === day) { past = res.routes || []; pastCut = !!res.cut; }
|
||||
} catch {}
|
||||
renderDays();
|
||||
renderHist(); // shown once there are days, though none are live
|
||||
}
|
||||
async function lookAt(d) {
|
||||
if (rp) endReplay(true);
|
||||
@@ -933,7 +933,7 @@
|
||||
if (d) await loadDays(d);
|
||||
pinned = null;
|
||||
const r = d ? listed()[0] : newest();
|
||||
if (r && d) pick(r, false);
|
||||
if (r && d) pick(r);
|
||||
else if (r) { stopPlays(); cur = r; sync(true); say(affWhy(r, true) || ruleWhy(r, true) || firstWhy(r)); }
|
||||
renderAll();
|
||||
}
|
||||
@@ -1006,7 +1006,7 @@
|
||||
if (r.tokens) meta.push(t("{n} tokens", { n: tokens(r.tokens) }));
|
||||
b.append(when, asked, to, el("span", "meta", meta.join(" · ")));
|
||||
b.title = `${agentName(r.agent)} · ${r.model} → ${r.provider}`;
|
||||
b.onclick = () => pick(r);
|
||||
b.onclick = (e) => pick(r, e);
|
||||
return b;
|
||||
}));
|
||||
renderActs(rs);
|
||||
@@ -1484,7 +1484,8 @@
|
||||
hubText();
|
||||
list.replaceChildren(el("li", "idle", t("No request yet")));
|
||||
say(t("Every request an agent sends to magpie shows up here, routed for real."));
|
||||
log.hidden = hist.hidden = true;
|
||||
log.hidden = true;
|
||||
renderHist(); // none live, but the days the history keeps are still there to look at
|
||||
layout();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,17 @@ fixtures. It checks session folder/model filters and the main model picker in
|
||||
Chromium and WebKit, including wheel, scrollbar track/thumb, keyboard selection
|
||||
and dismissal. It does not start the backend or read local user configuration.
|
||||
|
||||
`click-scroll.test.cjs` holds the rule that a click never moves the page
|
||||
(see "where the reader is" in `app.js`): on the Routing page, Live and a day
|
||||
picked in turn with the list scrolled to its end; a click whose handler sets
|
||||
scrollTop, grows the page above, redraws itself, or scrolls from a timer or a
|
||||
shared helper; a click that asks to go somewhere with `scrollOnPurpose(e)`;
|
||||
the room kept at the foot going as the reader scrolls back; and the wheel.
|
||||
|
||||
With Node.js and Playwright available:
|
||||
|
||||
```sh
|
||||
node --test internal/gui/tests/menu-scroll.test.cjs
|
||||
node --test internal/gui/tests/menu-scroll.test.cjs internal/gui/tests/click-scroll.test.cjs
|
||||
```
|
||||
|
||||
If Playwright is installed outside the repository, set `NODE_PATH` to the
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// A click never moves the page: what was clicked stays where it is on the
|
||||
// screen, and code scrolls a view only with the reader's click in hand.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
const { test } = require("node:test");
|
||||
const { chromium, webkit } = require("playwright");
|
||||
|
||||
const assets = path.resolve(__dirname, "../assets");
|
||||
const now = new Date();
|
||||
const day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-");
|
||||
const at = (i) => new Date(now.getTime() - (i + 1) * 60e3).toISOString();
|
||||
const who = { id: "fixture-key", provider: "fixture", name: "Fixture", who: "key-1", kind: "key", model: "model-a", routing: "", used: 0 };
|
||||
const routes = Array.from({ length: 12 }, (_, i) => ({
|
||||
id: 100 - i, seq: 100 - i, time: at(i), agent: "fixture", model: "model-a", provider: "fixture",
|
||||
order: [who], tries: [{ id: who.id, model: "model-a", start: at(i), done: true, status: 200, ms: 1200 }],
|
||||
done: true, status: 200, ms: 1200, tokens: 3000,
|
||||
}));
|
||||
const state = { agents: [{ id: "fixture", name: "Fixture", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang: "en", theme: "light" } };
|
||||
|
||||
async function serve(route) {
|
||||
const url = new URL(route.request().url());
|
||||
const json = (data) => route.fulfill({ json: data });
|
||||
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: 'window.bootPrefs = {lang:"en",theme:"light",web:true};' });
|
||||
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||||
if (url.pathname === "/api/state") return json(state);
|
||||
if (url.pathname === "/api/gateway/trace") {
|
||||
// live keeps none: the gateway started a moment ago; the day has them
|
||||
if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
|
||||
return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
|
||||
}
|
||||
if (url.pathname === "/api/gateway/history") {
|
||||
const d = url.searchParams.get("day");
|
||||
return json({ cut: false, days: [{ day, requests: routes.length }], routes: d ? routes : [] });
|
||||
}
|
||||
if (url.pathname === "/api/groups") return json({ groups: [] });
|
||||
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
|
||||
if (url.pathname.startsWith("/api/")) return json({});
|
||||
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
||||
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
||||
await route.fulfill({ body: await fs.readFile(file), contentType });
|
||||
}
|
||||
|
||||
const view = "#view-routing";
|
||||
const top = (page, sel) => page.locator(sel).evaluate((e) => e.getBoundingClientRect().top);
|
||||
const settle = (page) => page.waitForTimeout(900);
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
test(engine + ": a click never moves the page", async (t) => {
|
||||
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
const context = await browser.newContext({ viewport: { width: 1100, height: 640 }, reducedMotion: "reduce" });
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(5000);
|
||||
const errors = [];
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", serve);
|
||||
t.after(async () => {
|
||||
if (process.env.ARTIFACT_DIR) {
|
||||
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
|
||||
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, engine + "-click.png") });
|
||||
}
|
||||
await browser.close();
|
||||
});
|
||||
async function reset() {
|
||||
await page.goto("http://magpie.test/?view=routing");
|
||||
await page.locator(".rt-day").nth(1).waitFor();
|
||||
}
|
||||
// the reader scrolls the view till sel is y under its top, or less
|
||||
async function scrollTo(sel, y = 300) {
|
||||
const box = await page.locator(view).boundingBox();
|
||||
await page.mouse.move(box.x + box.width / 2, box.y + 40);
|
||||
for (let i = 0, last; i < 150 && (await top(page, sel)) - box.y > y; i++) {
|
||||
const now = await page.locator(view).evaluate((v) => v.scrollTop);
|
||||
if (now === last) break; // at the end
|
||||
last = now;
|
||||
await page.mouse.wheel(0, 20);
|
||||
await page.waitForTimeout(20);
|
||||
}
|
||||
await page.waitForTimeout(300);
|
||||
assert((await top(page, sel)) > box.y, "what's clicked must be in sight, or the click itself scrolls to it");
|
||||
assert(await page.locator(view).evaluate((v) => v.scrollTop > 0), "the view must have scrolled");
|
||||
}
|
||||
|
||||
await t.test("Live and a day, picked in turn, stay under the pointer", async () => {
|
||||
await reset();
|
||||
await page.locator(".rt-day").nth(1).click(); // the day's requests, the list full
|
||||
await settle(page);
|
||||
// down to the list's end: Live, with none, leaves the page shorter
|
||||
await scrollTo(".rt-days", 0);
|
||||
assert(await page.locator(view).evaluate((v) => v.scrollTop + v.clientHeight >= v.scrollHeight - 2), "the view must be at its end");
|
||||
for (const i of [0, 1, 0, 1]) {
|
||||
const chip = page.locator(".rt-day").nth(i), was = await top(page, ".rt-days");
|
||||
await chip.click();
|
||||
await settle(page);
|
||||
assert.equal(await page.locator(".rt-day").nth(i).getAttribute("aria-pressed"), "true");
|
||||
const is = await top(page, ".rt-days");
|
||||
assert(Math.abs(is - was) <= 1, `picking ${i ? "the day" : "Live"} moved the page ${Math.round(is - was)}px`);
|
||||
}
|
||||
});
|
||||
|
||||
await t.test("code can't take the page from a click", async () => {
|
||||
await reset();
|
||||
await page.locator(view).evaluate((v) => {
|
||||
const s = document.createElement("div");
|
||||
s.style.cssText = "flex:none;height:1600px";
|
||||
const mk = (id, fn) => { const b = document.createElement("button"); b.id = id; b.textContent = id; b.style.flex = "none"; b.onclick = fn; return b; };
|
||||
const above = document.createElement("div");
|
||||
above.style.flex = "none";
|
||||
v.prepend(above);
|
||||
v.append(s,
|
||||
// sets scrollTop outright
|
||||
mk("jump", () => { v.scrollTop = 0; }),
|
||||
// the page above it grows when a load comes in
|
||||
mk("grow", () => setTimeout(() => { above.style.height = "400px"; }, 300)),
|
||||
// redraws itself, and everything after it
|
||||
mk("redraw", (e) => { const b = e.currentTarget, c = b.cloneNode(true); c.onclick = b.onclick; above.style.height = "120px"; b.replaceWith(c); }),
|
||||
// says it's on purpose, but with no click in hand
|
||||
mk("sneak", () => setTimeout(() => { scrollOnPurpose(); v.scrollTop = 0; }, 50)),
|
||||
// scrollIntoView from a helper a click only shares
|
||||
mk("helper", () => setTimeout(() => document.querySelector("#rt").scrollIntoView(), 50)),
|
||||
document.createElement("div"));
|
||||
v.lastChild.style.cssText = "flex:none;height:600px";
|
||||
});
|
||||
for (const id of ["jump", "grow", "redraw", "sneak", "helper"]) {
|
||||
await scrollTo("#" + id);
|
||||
const was = await top(page, "#" + id);
|
||||
await page.locator("#" + id).click();
|
||||
await settle(page);
|
||||
const is = await top(page, "#" + id);
|
||||
assert(Math.abs(is - was) <= 1, `${id} moved the page ${Math.round(is - was)}px`);
|
||||
}
|
||||
});
|
||||
|
||||
await t.test("a click that asks to go somewhere does", async () => {
|
||||
await reset();
|
||||
await page.locator(view).evaluate((v) => {
|
||||
const s = document.createElement("div");
|
||||
s.style.cssText = "flex:none;height:1600px";
|
||||
const b = document.createElement("button");
|
||||
b.id = "go"; b.textContent = "go"; b.style.flex = "none";
|
||||
b.onclick = (e) => { if (scrollOnPurpose(e)) v.scrollTop = 0; };
|
||||
v.append(s, b);
|
||||
});
|
||||
await scrollTo("#go");
|
||||
await page.locator("#go").click();
|
||||
await settle(page);
|
||||
assert.equal(await page.locator(view).evaluate((v) => v.scrollTop), 0);
|
||||
});
|
||||
|
||||
await t.test("the room kept for a click goes as the reader scrolls back", async () => {
|
||||
await reset();
|
||||
await page.locator(".rt-day").nth(1).click();
|
||||
await settle(page);
|
||||
await scrollTo(".rt-days", 0);
|
||||
await page.locator(".rt-day").nth(0).click(); // Live: none, the page shorter
|
||||
await settle(page);
|
||||
assert(await page.locator(view).evaluate((v) => !!v.querySelector(":scope > .view-room")), "room must be kept");
|
||||
const box = await page.locator(view).boundingBox();
|
||||
await page.mouse.move(box.x + box.width / 2, box.y + 60);
|
||||
for (let i = 0; i < 30; i++) { await page.mouse.wheel(0, -40); await page.waitForTimeout(20); }
|
||||
await page.waitForTimeout(300);
|
||||
assert.equal(await page.locator(view).evaluate((v) => v.querySelector(":scope > .view-room")), null, "the room must go");
|
||||
});
|
||||
|
||||
await t.test("the reader's wheel still scrolls after a click", async () => {
|
||||
await reset();
|
||||
await page.locator(view).evaluate((v) => { const s = document.createElement("div"); s.style.cssText = "flex:none;height:1600px"; v.append(s); });
|
||||
await page.locator(".rt-day").nth(1).click();
|
||||
const was = await page.locator(view).evaluate((v) => v.scrollTop);
|
||||
const box = await page.locator(view).boundingBox();
|
||||
await page.mouse.move(box.x + box.width / 2, box.y + 60);
|
||||
await page.mouse.wheel(0, 300);
|
||||
await page.waitForTimeout(400);
|
||||
assert((await page.locator(view).evaluate((v) => v.scrollTop)) > was + 100, "the wheel must scroll the view");
|
||||
});
|
||||
|
||||
assert.deepEqual(errors, [], "page runtime errors");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user