File size: 5,345 Bytes
161cb23
 
 
a1415e8
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1415e8
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
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,
  };
}