// A LINK VISIT (umtool/docs/report-video.md, "Link visits"; operator, 4 Nov: "is the linked post read? If not can we // have our transition visit it with a mouse click visual?", "show the post at the link being referred to since it's // context"): a post's words hold a status link to a post the card draws -- the post it quotes (`quoted`, via a link), // a post its words link to (`links[]`), a post it answers -- and the card VISITS it: a pointer glides to the link in // the words, clicks (a ripple), and the linked post opens -- revealed then if it comes in after the link (its read, or // a linked post shown, not read); lit again (an accent ring, at full strength) for `hold` s if it is on screen // already (read earlier: a thread read in date order reads an older post first). // // render.posts.linkVisit = { on?: true, hold?: 1.4 } the cut's // timeline[i].linkVisit = false one item's: none // // PURE (but the page functions, self-contained): the plan carries `follow.visits`, the state at a second carries // `visit` (which, and how far its pointer and its ripple are), and the build and Play draw the same with // applyVisitState after applyFollowState. import { statusIdOf, tileIdOf } from "./post-thread.mjs"; import { cardText, linksOf, parentsOf } from "./post-item.mjs"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const str = (v) => (typeof v === "string" ? v : ""); const r3 = (v) => Math.round(v * 1000) / 1000; export const VISIT_DEFAULTS = Object.freeze({ on: true, hold: 1.4 }); /** The pointer's glide (s), the click's ripple (s) -- the opening lands at the click's end. */ export const VISIT_TIMES = Object.freeze({ move: 0.75, click: 0.3 }); /** One item's visit knobs. */ export function visitKnobs(entry, render = undefined) { const r = isObj(render?.posts?.linkVisit) ? render.posts.linkVisit : {}; const k = { ...VISIT_DEFAULTS, ...(typeof r.on === "boolean" ? { on: r.on } : {}), ...(fin(r.hold) ? { hold: r.hold } : {}) }; return entry?.linkVisit === false ? { ...k, on: false } : k; } /** Every reason the cut's or an item's link-visit knob cannot be built. */ export function validatePostLinkVisit(e) { return e?.linkVisit === undefined || e.linkVisit === false ? [] : [`timeline ${e?.id ?? "?"}.linkVisit must be false (no link visit); absent: the cut's`]; } export function validatePostLinkVisitRender(render) { const v = render?.posts?.linkVisit; if (v === undefined) return []; if (!isObj(v)) return ["render.posts.linkVisit must be { on?, hold? }"]; const errors = Object.keys(v).filter((k) => !(k in VISIT_DEFAULTS)).map((k) => `render.posts.linkVisit.${k} is not a link-visit knob (on, hold)`); if (v.on !== undefined && typeof v.on !== "boolean") errors.push("render.posts.linkVisit.on must be true or false"); if (v.hold !== undefined && !(fin(v.hold) && v.hold >= 0.4 && v.hold <= 5)) errors.push("render.posts.linkVisit.hold must be 0.4–5 seconds"); return errors; } /** The text a tile draws (its words, as the card's wordsHtml marks a link in them). */ function textOfScope(entry, scope) { if (scope === "post") return cardText(entry); if (scope === "quoted") return str(entry?.quoted?.text); const m = /^(parent|linked):(\d)$/.exec(scope); if (!m) return ""; return str((m[1] === "parent" ? parentsOf(entry) : linksOf(entry))[Number(m[2])]?.text); } /** * THE VISITS of a planned card, pure: `[{ from, to, id, at, open, hold }]` -- the tile whose words hold the link * (`from`), the tile it opens (`to`, the post with that `id`), the second the click lands (`at`), and whether the * linked post OPENS there (`open`: it comes in then) or is lit again for `hold`. `thread`: the plan's (threadSchedule); * `blocks`: read-follow's scheduled blocks. A visit needs its link on screen before the click (its tile in, by then). */ export function linkVisits(entry, thread, blocks, render = undefined) { const k = visitKnobs(entry, render); if (!k.on || !thread?.tiles?.length) return []; const rows = thread.tiles; const rowOf = (scope) => rows.find((r) => r.scope === scope) ?? null; const idOf = (r) => (r.mode === "scene" ? r.scope.slice(5) : tileIdOf(entry, r)); const shownAt = (r) => (r.from?.[1] > 0 ? 0 : fin(r.t1) ? r.t1 : 0); const out = []; // A quoted post the archive kept without its id: the status link in the words that names its author. const handleOf = (u) => /(?:x|twitter)\.com\/([^/\s]+)\/status/i.exec(u)?.[1]?.toLowerCase() ?? null; for (const to of rows) { if (to.mode === "hidden" || to.mode === "scene") continue; const own = idOf(to); const qh = to.scope === "quoted" && !own ? str(entry?.quoted?.handle).replace(/^@/, "").toLowerCase() : ""; if (!own && !qh) continue; for (const from of rows) { if (from === to || from.mode === "hidden" || from.mode === "scene") continue; const urls = [...textOfScope(entry, from.scope).matchAll(/https?:\/\/\S+/g)].map((m) => m[0]); const hit = urls.find((u) => (own ? statusIdOf(u) === own : handleOf(u) === qh && !!statusIdOf(u))); if (!hit) continue; const id = statusIdOf(hit); const fromIn = shownAt(from); // It comes in after the link is on screen: the click opens it (its reveal). const reveal = to.from?.[1] === 0 && fin(to.t0) && to.t0 >= fromIn + VISIT_TIMES.move + VISIT_TIMES.click - 1e-6; if (reveal) { out.push({ from: from.scope, to: to.scope, id, at: r3(to.t0), open: true, hold: 0 }); break; } // On screen already: lit again once the link's words are read (the block holding it), else his read's end. const linkBlocks = (blocks ?? []).filter((b) => b.scope === from.scope && fin(b.start)); const last = linkBlocks.sort((a, b) => a.start - b.start).pop(); const at = last && fin(last.end) ? last.end : null; if (at === null || at < fromIn + VISIT_TIMES.move + VISIT_TIMES.click) continue; // A post its words link to, shown but not read: it is not on screen until the click opens it. if (to.scope.startsWith("linked:") && to.mode === "context") { out.push({ from: from.scope, to: to.scope, id, at: r3(at), open: true, hold: 0, reveal: true }); break; } out.push({ from: from.scope, to: to.scope, id, at: r3(at), open: false, hold: k.hold }); break; } } return out.sort((a, b) => a.at - b.at); } /** The latest a visit runs to (its lit hold's end): the plan holds the item at least that long. */ export const visitsEnd = (visits) => Math.max(0, ...(visits ?? []).map((v) => v.at + (v.hold ?? 0) + 0.2)); /** * THE VISIT's STATE at item second `s`, pure: `{ i, move, click, ring }` (null: none up) -- which visit, its pointer's * glide (0..1), its ripple (0..1), the linked tile's ring (0..1: lit again, faded in and out). */ export function visitStateAt(visits, s) { for (let i = 0; i < (visits ?? []).length; i += 1) { const v = visits[i]; const t0 = v.at - VISIT_TIMES.click - VISIT_TIMES.move; const end = v.at + Math.max(0.35, v.hold ?? 0); if (s < t0 || s > end) continue; const move = Math.min(1, Math.max(0, (s - t0) / VISIT_TIMES.move)); const click = s < v.at - VISIT_TIMES.click ? 0 : Math.min(1, (s - (v.at - VISIT_TIMES.click)) / (VISIT_TIMES.click + 0.25)); let ring = 0; if (!v.open && s >= v.at) { const into = s - v.at; ring = Math.min(1, into / 0.25, Math.max(0, (v.hold - into) / 0.35)); } return { i, move: r3(move), click: r3(click), ring: r3(Math.max(0, ring)) }; } return null; } /** * IN THE PAGE (self-contained: the build passes it to page.evaluate; Play calls it on its frame's document): the * pointer and its ripple over the link (`[data-thread-tile=""] [data-link=""]`, where it is now -- after the * follow state), and the linked tile's ring. `arg`: `{ visits, st }` -- the plan's, visitStateAt's (null: none). */ export function applyVisitState(arg, doc = document) { const visits = arg && arg.visits; const st = arg && arg.st; let ptr = doc.querySelector("[data-visit-pointer]"); let rip = doc.querySelector("[data-visit-ripple]"); for (const el of Array.from(doc.querySelectorAll("[data-visit-ring]"))) el.removeAttribute("data-visit-ring"); for (const el of Array.from(doc.querySelectorAll("[data-thread-tile][style*='box-shadow']"))) el.style.boxShadow = ""; if (!st || !visits || !visits[st.i]) { if (ptr) ptr.style.display = "none"; if (rip) rip.style.display = "none"; return; } const v = visits[st.i]; const link = doc.querySelector(`[data-thread-tile="${v.from}"] [data-link="${v.id}"]`) ?? doc.querySelector(`[data-link="${v.id}"]`); const card = doc.querySelector("[data-post-card]"); if (!ptr) { ptr = doc.createElement("div"); ptr.setAttribute("data-visit-pointer", ""); ptr.innerHTML = ''; ptr.setAttribute("style", "position:absolute;z-index:20;pointer-events:none;display:none;filter:drop-shadow(0 2px 3px rgba(0,0,0,.5))"); doc.body.appendChild(ptr); } if (!rip) { rip = doc.createElement("div"); rip.setAttribute("data-visit-ripple", ""); rip.setAttribute("style", "position:absolute;z-index:19;pointer-events:none;display:none;border-radius:50%;border:3px solid #ffcf33"); doc.body.appendChild(rip); } if (!link || !card) { ptr.style.display = "none"; rip.style.display = "none"; return; } const r = link.getBoundingClientRect(); const c = card.getBoundingClientRect(); const tx = r.left + Math.min(r.width * 0.5, 60); const ty = r.top + r.height * 0.55; // From the card's lower right toward the link, eased. const sx = c.right - 40; const sy = c.bottom - 30; const e = st.move < 0.5 ? 4 * st.move ** 3 : 1 - (-2 * st.move + 2) ** 3 / 2; const fade = st.click >= 1 ? Math.max(0, 1 - (st.ring > 0 ? 0 : 1)) : 1; ptr.style.display = st.move > 0 && fade > 0 ? "block" : "none"; ptr.style.left = `${sx + (tx - sx) * e - 4}px`; ptr.style.top = `${sy + (ty - sy) * e - 2}px`; ptr.style.transform = st.click > 0 && st.click < 0.4 ? "scale(0.86)" : ""; if (st.click > 0 && st.click < 1) { const d = 12 + st.click * 52; rip.style.display = "block"; rip.style.width = `${d}px`; rip.style.height = `${d}px`; rip.style.left = `${tx - d / 2}px`; rip.style.top = `${ty - d / 2}px`; rip.style.opacity = String(1 - st.click); } else rip.style.display = "none"; link.setAttribute("data-visit-ring", ""); if (st.ring > 0) { const tile = doc.querySelector(`[data-thread-tile="${v.to}"]`); if (tile) { tile.style.boxShadow = `0 0 0 3px rgba(255, 207, 51, ${st.ring}), 0 0 24px rgba(255, 207, 51, ${st.ring * 0.4})`; tile.style.borderRadius = "12px"; tile.setAttribute("data-visit-ring", ""); } } } /** * The thread's rows with the visits laid on (pure, a copy): a linked post shown, not read, hidden until its click and * opened then (to its context size); a post lit again for its hold (`pulse`: full size and strength over it, eased in * and out -- tileStateAt's). */ export function visitRows(thread, visits) { if (!thread || !visits?.length) return thread; const tiles = thread.tiles.map((r) => { const v = visits.find((x) => x.to === r.scope); if (!v) return r; if (v.reveal) return { ...r, from: [thread.shrink, 0], to: [thread.shrink, thread.dim], t0: r3(Math.max(0, v.at - 0.05)), t1: r3(v.at + 0.35) }; if (!v.open) return { ...r, pulse: { t0: r3(v.at), t1: r3(v.at + 0.3), t2: r3(v.at + Math.max(0.35, v.hold - 0.3)), t3: r3(v.at + Math.max(0.65, v.hold)) } }; return r; }); return { ...thread, tiles }; }