import { scorerSpanToOffsets } from "./columns.js"; import { byId, el, prefersReducedMotion, tweenNumber } from "./dom.js"; import { tokenizeLines } from "./highlight.js"; import { NOTES, scoreWord } from "./labels.js"; const NOTE_STAGGER_MS = 30; const NOTE_STAGGER_STEPS = 7; const SCORE_TWEEN_MS = 600; const TAB_COLUMNS = 4; function leadingColumns(text) { let columns = 0; for (const char of text) { if (char === " ") { columns += 1; } else if (char === "\t") { columns += TAB_COLUMNS; } else { break; } } return columns; } function textNodes(text, tokens, start, end) { const nodes = []; let pos = start; for (const token of tokens) { if (token.to <= start || token.from >= end) { continue; } const from = Math.max(token.from, start); const to = Math.min(token.to, end); if (from > pos) { nodes.push(text.slice(pos, from)); } nodes.push(el("span", { class: token.className, text: text.slice(from, to) })); pos = to; } if (pos < end) { nodes.push(text.slice(pos, end)); } return nodes; } function leadingLength(text) { return text.length - text.trimStart().length; } function bodyNodes(text, tokens, start, squiggle) { const from = squiggle ? Math.max(squiggle.from, start) : start; if (!squiggle || squiggle.to <= from) { return textNodes(text, tokens, start, text.length); } return [ ...textNodes(text, tokens, start, from), el("span", { class: "squiggle" }, textNodes(text, tokens, from, squiggle.to)), ...textNodes(text, tokens, squiggle.to, text.length), ]; } function srcNode(text, tokens, squiggle) { const lead = leadingLength(text); const children = bodyNodes(text, tokens, lead, squiggle); if (lead > 0) { children.unshift(el("span", { class: "lead", text: text.slice(0, lead) })); } return el("span", { class: "src", style: `--indent: ${leadingColumns(text)}` }, children); } function lineNode({ number, text, tokens, className = "line", squiggle = null, note = "" }) { return el("div", { class: className }, [ el("span", { class: "ln", text: number === null ? "" : String(number) }), srcNode(text, tokens, squiggle), note ? el("span", { class: "note", text: note }) : "", ]); } function flaggedLines(lines, squiggles) { const byLine = new Map(); let previous = null; for (const squiggle of squiggles) { const text = lines[squiggle.line - 1]; if (text === undefined) { continue; } const continues = previous && previous.kind === squiggle.kind && previous.line === squiggle.line - 1; byLine.set(squiggle.line, { span: scorerSpanToOffsets(text, squiggle.col_start, squiggle.col_end), note: continues ? "" : NOTES[squiggle.kind], }); previous = squiggle; } return byLine; } export function paintLines(container, { text, filename, squiggles, enter }) { const lines = text.split("\n"); const tokens = tokenizeLines(text, filename); const flagged = flaggedLines(lines, squiggles); const stagger = enter && !prefersReducedMotion(); let order = 0; const nodes = lines.map((lineText, index) => { const flag = flagged.get(index + 1); const node = lineNode({ number: index + 1, text: lineText, tokens: tokens[index], className: flag ? "line is-flagged" : "line", squiggle: flag?.span, note: flag?.note, }); if (flag && stagger) { node.style.setProperty("--delay", `${Math.min(order, NOTE_STAGGER_STEPS) * NOTE_STAGGER_MS}ms`); order += 1; } return node; }); container.classList.toggle("is-entering", stagger); container.replaceChildren(...nodes); } function scrollIntoStage(code, line) { const offset = line.getBoundingClientRect().top - code.getBoundingClientRect().top + code.scrollTop; code.scrollTop = Math.max(0, offset - code.clientHeight / 3); } export function createStage() { const path = byId("stage-path"); const status = byId("stage-status"); const code = byId("stage-code"); const scoreBox = byId("stage-score"); let shownPoints = null; function setStatus(text) { status.textContent = text; } function setScore(points, animate) { const value = scoreBox.querySelector(".stage-score-value"); const word = scoreBox.querySelector(".stage-score-word"); const { word: label, className } = scoreWord(points); tweenNumber(value, shownPoints ?? points, points, animate ? SCORE_TWEEN_MS : 0); shownPoints = points; word.textContent = label; word.className = `stage-score-word ${className}`.trim(); } function renderPlain({ text, filename, squiggles, enter }) { paintLines(code, { text, filename, squiggles, enter }); const first = code.querySelector(".is-flagged"); if (enter) { code.scrollTop = 0; if (first) { scrollIntoStage(code, first); } } } return { setHead({ filename, statusText, points, animate = false }) { const slash = filename.lastIndexOf("/"); path.title = filename; path.replaceChildren( slash >= 0 ? el("span", { class: "path-dir", text: filename.slice(0, slash + 1) }) : "", filename.slice(slash + 1), ); setStatus(statusText); setScore(points, animate); }, resetScore() { shownPoints = null; }, renderPlain, }; }