Spaces:
Sleeping
Sleeping
File size: 7,736 Bytes
a1415e8 161cb23 a1415e8 161cb23 a1415e8 5c265c1 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 5c265c1 161cb23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | import { byId, copyWithIcon, prefersReducedMotion } from "./dom.js";
import { isFullRepoUrl, parseRepoSpec, specLabel } from "./github.js";
import { SOURCE_ERRORS, issuesText, toHundred } from "./labels.js";
import { createNavigator } from "./navigator.js";
import { boardLink, rankErrorText, rankLabel, requestRank } from "./ranking.js";
import { fetchStats } from "./refiner.js";
import { paintSample } from "./sample.js";
import { createScorer } from "./scoring.js";
import { loadExamples, loadFolder, loadRepo, loadSnippet } from "./sources.js";
import { createStage } from "./stage.js";
import { hideBannerStats, renderBanner, reportScan } from "./telemetry.js";
const nodes = {
sample: byId("sample"),
stage: byId("stage"),
input: byId("source-input"),
error: byId("source-error"),
progress: byId("source-progress"),
copy: byId("copy"),
copyIcon: byId("copy-icon"),
rank: byId("rank"),
rankOpen: byId("rank-open"),
rankText: byId("rank-text"),
};
const narrowScreen = window.matchMedia("(max-width: 640px)");
const scorer = createScorer();
const stage = createStage();
const fileNav = createNavigator(selectFile);
const app = {
repoSpec: null,
sourceKind: "examples",
files: [],
shown: null,
ingest: null,
standing: null,
ingestAbort: null,
};
function renderProgress() {
nodes.progress.hidden = !app.ingest;
nodes.progress.textContent = app.ingest ?? "";
}
function renderActions(file) {
nodes.copy.hidden = !file;
const rankable = app.sourceKind === "repo" && Boolean(app.repoSpec);
nodes.rank.hidden = !rankable || Boolean(app.standing);
nodes.rankOpen.hidden = !rankable || !app.standing;
}
function paintStanding(standing) {
nodes.rankText.textContent = rankLabel(standing);
nodes.rankOpen.href = boardLink(standing.repo);
const points = Math.round(standing.score);
nodes.rankOpen.title = `${standing.repo}: ${points}/100, #${standing.rank} of ${standing.total}`;
}
function setBusy(button, busy) {
button.disabled = busy;
button.classList.toggle("is-busy", busy);
button.setAttribute("aria-busy", String(busy));
}
function showSourceError(message) {
nodes.error.hidden = !message;
nodes.error.textContent = message;
}
async function rankRepo() {
const spec = app.repoSpec;
showSourceError("");
setBusy(nodes.rank, true);
try {
const standing = await requestRank(spec);
if (app.repoSpec === spec) {
app.standing = standing;
paintStanding(standing);
}
} catch (error) {
showSourceError(rankErrorText(error));
} finally {
setBusy(nodes.rank, false);
renderActions(app.shown);
}
}
function showFile(file, { enter = true, freshTree = false } = {}) {
app.shown = file;
stage.resetScore();
if (freshTree) {
fileNav.render(app.files, file);
} else {
fileNav.update(app.files, file);
}
stage.setHead({
filename: file.path,
statusText: issuesText(file.result.squiggles.length),
points: toHundred(file.result.score),
});
stage.renderPlain({
text: file.content,
filename: file.path,
squiggles: file.result.squiggles,
enter,
});
renderActions(file);
}
function selectFile(file) {
showFile(file);
}
function bringStageIntoView() {
if (narrowScreen.matches) {
const behavior = prefersReducedMotion() ? "auto" : "smooth";
nodes.stage.scrollIntoView({ behavior, block: "start" });
}
}
function revealStage(update) {
if (!nodes.stage.hidden) {
update();
return;
}
const swap = () => {
nodes.sample.hidden = true;
nodes.stage.hidden = false;
update();
};
if (!document.startViewTransition || prefersReducedMotion()) {
swap();
bringStageIntoView();
return;
}
document.startViewTransition(swap).finished.then(bringStageIntoView);
}
function syncUrl() {
const url = new URL(window.location.href);
url.searchParams.delete("repo");
if (app.repoSpec) {
url.searchParams.set("repo", specLabel(app.repoSpec));
}
url.search = url.searchParams.toString().replaceAll("%2F", "/").replaceAll("%40", "@");
window.history.replaceState(null, "", url);
}
function startFiles({ files, sourceKind, repoSpec = null }) {
Object.assign(app, { files, sourceKind, repoSpec, standing: null, ingest: null });
syncUrl();
renderProgress();
reportScan(sourceKind, files);
revealStage(() => showFile(files[0], { enter: true, freshTree: true }));
}
async function startExamples() {
showSourceError("");
app.ingestAbort?.abort();
const files = await loadExamples(scorer.scoreAll);
startFiles({ files, sourceKind: "examples" });
}
async function startRepo(spec) {
showSourceError("");
app.ingestAbort?.abort();
const aborter = new AbortController();
app.ingest = "Fetching";
app.ingestAbort = aborter;
renderProgress();
try {
const files = await loadRepo(spec, {
signal: aborter.signal,
scoreAll: scorer.scoreAll,
onProgress: (text) => {
app.ingest = text;
renderProgress();
},
});
startFiles({ files, sourceKind: "repo", repoSpec: spec });
} catch (error) {
if (error.name === "AbortError") {
return;
}
app.ingest = null;
renderProgress();
showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.offline);
}
}
async function startFolder(fileList) {
showSourceError("");
app.ingestAbort?.abort();
app.ingest = "Reading";
renderProgress();
try {
const files = await loadFolder(fileList, {
scoreAll: scorer.scoreAll,
onProgress: (text) => {
app.ingest = text;
renderProgress();
},
});
nodes.input.value = "";
startFiles({ files, sourceKind: "folder" });
} catch (error) {
app.ingest = null;
renderProgress();
showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.no_files);
}
}
async function startSnippet(text) {
showSourceError("");
app.ingestAbort?.abort();
const files = await loadSnippet(text, scorer.scoreAll);
startFiles({ files, sourceKind: "snippet" });
}
function submitLink(text) {
const spec = parseRepoSpec(text);
if (!spec) {
showSourceError(SOURCE_ERRORS.invalid);
return;
}
nodes.input.value = spec.slug;
startRepo(spec);
}
function handlePaste(event) {
const text = event.clipboardData?.getData("text/plain") ?? "";
if (text.trim().includes("\n")) {
event.preventDefault();
startSnippet(text);
return;
}
if (isFullRepoUrl(text)) {
event.preventDefault();
nodes.input.value = text.trim();
submitLink(text);
}
}
function bindControls() {
byId("source").addEventListener("submit", (event) => {
event.preventDefault();
submitLink(nodes.input.value);
});
nodes.copy.addEventListener("click", () => copyWithIcon(nodes.copy, nodes.copyIcon, app.shown.content));
nodes.rank.addEventListener("click", rankRepo);
byId("command-copy").addEventListener("click", () => copyWithIcon(
byId("command-copy"),
byId("command-copy-icon"),
byId("command-text").textContent,
));
byId("sample-run").addEventListener("click", startExamples);
const uploadInput = byId("upload-input");
byId("upload").addEventListener("click", () => uploadInput.click());
uploadInput.addEventListener("change", () => {
if (uploadInput.files.length > 0) {
startFolder([...uploadInput.files]);
}
uploadInput.value = "";
});
document.addEventListener("paste", handlePaste);
}
function boot() {
bindControls();
fetchStats().then(renderBanner, hideBannerStats);
paintSample(scorer.scoreAll);
const repoParam = new URLSearchParams(window.location.search).get("repo");
const spec = repoParam ? parseRepoSpec(repoParam) : null;
if (spec) {
nodes.input.value = spec.slug;
startRepo(spec);
}
}
boot();
|