Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/layouts/Walkthrough.astro
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const lcUrl = (slug: string) => `https://leetcode.com/problems/${slug}/`;
</div>
<h1 set:html={en(p.title)} />
<p class="lede" data-i18n={my(p.lede)} set:html={en(p.lede)} />
<nav class="jump">
<nav class="jump" aria-label="On this page">
<a href="#part-1"><b>1</b> <span data-i18n="မေးခွန်း">The question</span></a>
<a href="#part-2"><b>2</b> <span data-i18n="အဖြေ">The answer</span></a>
<a href="#part-3"><b>3</b> <span data-i18n="Code">The code</span></a>
Expand All @@ -101,7 +101,7 @@ const lcUrl = (slug: string) => `https://leetcode.com/problems/${slug}/`;
<section class="question">
<div class="q-statement">
<div class="q-head">
<h2 data-i18n="ပုစ္ဆာ">The problem</h2>
<h3 class="as-h2" data-i18n="ပုစ္ဆာ">The problem</h3>
<span class="q-src" data-i18n="LeetCode မှ ကူးယူဖော်ပြသည်">quoted from LeetCode</span>
</div>
<div class="statement" set:html={p.statement} />
Expand All @@ -117,7 +117,7 @@ const lcUrl = (slug: string) => `https://leetcode.com/problems/${slug}/`;

<div class="q-play panel">
<div class="panel-head">
<h2 data-i18n={my(p.widgetTitle)} set:html={en(p.widgetTitle)} />
<h3 class="as-h2" data-i18n={my(p.widgetTitle)} set:html={en(p.widgetTitle)} />
<span class="note" id="q-label"></span>
</div>
<div id="q-widget"></div>
Expand Down
6 changes: 3 additions & 3 deletions src/lessons/x-sum/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ function drawBrute(s, { k }) {
const cls = (n) => (s.stage === n ? 'active' : s.stage < n ? 'pending' : '');
return stagePanel(pick(L.windowFromScratch(s.lo)), `[${s.win.join(', ')}]`, '')
+ '<div class="bf-steps">'
+ `<div class="bf-step ${cls(1)}"><h3><span class="n">1</span>${pick(L.tallyHead(k))}</h3>`
+ `<div class="bf-step ${cls(1)}"><h5 class="as-h3"><span class="n">1</span>${pick(L.tallyHead(k))}</h5>`
+ `<div class="bars">${s.stage >= 1 ? bars : `<span class="empty">${pick(L.notCounted)}</span>`}</div></div>`
+ `<div class="bf-step ${cls(2)}"><h3><span class="n">2</span>${pick(L.rankHead(s.keep))}</h3>`
+ `<div class="bf-step ${cls(2)}"><h5 class="as-h3"><span class="n">2</span>${pick(L.rankHead(s.keep))}</h5>`
+ `<div class="rank">${s.stage >= 2 ? rankHTML : `<span class="empty">${pick(L.notRanked)}</span>`}</div></div>`
+ `<div class="bf-step ${cls(3)}"><h3><span class="n">3</span>${pick(L.sumHead)}</h3>`
+ `<div class="bf-step ${cls(3)}"><h5 class="as-h3"><span class="n">3</span>${pick(L.sumHead)}</h5>`
+ `<div class="ledger bare"><span class="expr">${s.stage >= 3 ? kept.map((e) => `${e.v}×${e.c}`).join(' + ') : '—'}</span>`
+ `<span class="total">${s.stage >= 3 ? s.sum : '·'}<small>X-SUM</small></span></div></div>`
+ '</div>';
Expand Down
4 changes: 2 additions & 2 deletions src/lessons/x-sum/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
.bf-step.pending{background:transparent;border-style:dashed}
.bf-step.pending, .bf-step.pending *{color:var(--ink-3)}
.bf-step.active{background:var(--surface);border-color:var(--accent);box-shadow:0 0 0 3px var(--accent-soft)}
.bf-step h3{margin-bottom:9px;color:var(--ink-2)}
.bf-step h3 .n{font-family:"IBM Plex Mono",monospace;color:var(--accent);margin-right:6px}
.bf-step h5{margin-bottom:9px;color:var(--ink-2)}
.bf-step h5 .n{font-family:"IBM Plex Mono",monospace;color:var(--accent);margin-right:6px}
.bars{display:flex;flex-direction:column;gap:5px}
.bar-row{display:grid;grid-template-columns:34px 1fr 30px;align-items:center;gap:8px;font-family:"IBM Plex Mono",monospace;font-size:12.5px;font-variant-numeric:tabular-nums}
.bar-track{background:var(--surface);border:1px solid var(--line);border-radius:4px;height:15px;overflow:hidden}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function slots(values, o = {}) {

/* A titled block inside the stage panel, in the page's panel-head style. */
export function stagePanel(title, note, inner) {
return `<div class="panel-head"><h2>${esc(title)}</h2>${note ? `<span class="note">${esc(note)}</span>` : ''}</div>${inner}`;
return `<div class="panel-head"><h4 class="as-h2">${esc(title)}</h4>${note ? `<span class="note">${esc(note)}</span>` : ''}</div>${inner}`;
}

/* A key/value table: hash maps, counters, memo tables. */
Expand Down
100 changes: 81 additions & 19 deletions src/lib/stepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,33 @@ const $ = (sel, el = document) => el.querySelector(sel);
const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
const nameOf = (k) => (typeof k === 'string' ? k : k?.en ?? '');

/* A tab's selection attributes. Only the selected tab sits in the Tab order;
* the arrow keys move between the rest (tabKeys below). */
const tabAttrs = (on) => `aria-selected="${on}" tabindex="${on ? 0 : -1}"`;
const selectTabs = (tabs, isOn) => tabs.forEach((b) => {
const on = isOn(b);
b.setAttribute('aria-selected', String(on));
b.tabIndex = on ? 0 : -1;
});

/* Arrow keys, Home and End inside a tablist: move to that tab and select it. */
function tabKeys(e) {
const tab = e.target.closest('[role="tab"]');
if (!tab || e.altKey || e.ctrlKey || e.metaKey) return;
const tabs = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]')];
const i = tabs.indexOf(tab);
const j = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key];
if (j == null) return;
e.preventDefault();
const to = tabs[(j + tabs.length) % tabs.length];
to.click();
to.focus();
}

/* The code-language tabs: `mini` above the live code, plain above part 3. */
const langBar = (langs, active, cls = '') => `
<div class="lang-bar${cls}" role="tablist">${langs.map((l) =>
`<button class="lang" role="tab" data-lang="${l.id}" aria-selected="${l.id === active}">${esc(l.name)}</button>`).join('')}
`<button class="lang" role="tab" data-lang="${l.id}" ${tabAttrs(l.id === active)}>${esc(l.name)}</button>`).join('')}
</div>`;

/* Chrome owned by the engine rather than by any one lesson. */
Expand All @@ -58,6 +81,7 @@ const UI = {
pause: { en: 'Pause', my: 'ရပ်' },
scrub: { en: 'Scrub through steps', my: 'အဆင့်များကို ဆွဲကြည့်ရန်' },
step: { en: 'step', my: 'အဆင့်' },
stepOf: { en: 'Step {i} of {n}', my: 'အဆင့် {i} / {n}' },
answer: { en: 'answer', my: 'answer' },
codeLive: { en: 'The code, live', my: 'အလုပ်လုပ်နေသော code' },
input: { en: 'Input', my: 'Input' },
Expand All @@ -73,6 +97,7 @@ const UI = {
unset: { en: 'not set yet at this step', my: 'ဤအဆင့်တွင် မသတ်မှတ်ရသေး' },
valueNow: { en: 'value at this step', my: 'ဤအဆင့်ရှိ တန်ဖိုး' },
badInput: { en: 'That input does not work here', my: 'ဤ input ကို အသုံးမပြုနိုင်ပါ' },
stale: { en: 'The walkthrough below still shows the last input that worked.', my: 'အောက်ပါ လမ်းညွှန်သည် နောက်ဆုံး အလုပ်လုပ်ခဲ့သော input ကိုသာ ပြနေဆဲ ဖြစ်သည်။' },
};

export function mountLesson(cfg) {
Expand Down Expand Up @@ -108,7 +133,7 @@ export function mountLesson(cfg) {
let steps;
try {
steps = mode().build(structuredClone(state.input)) || [];
showWarn('');
if (!badFields().length) showWarn('');
} catch (err) {
steps = [];
showWarn(`${pick(UI.badInput)}: ${err.message}`);
Expand Down Expand Up @@ -146,7 +171,7 @@ export function mountLesson(cfg) {
</div>` : '';

const tabs = cfg.modes.map((m) => `
<button class="atab" role="tab" data-mode="${m.id}" aria-selected="${m.id === state.mode}">
<button class="atab" role="tab" data-mode="${m.id}" ${tabAttrs(m.id === state.mode)}>
<span class="atab-name">${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}</span>
<span class="atab-desc">${esc(pick(m.desc ?? ''))}</span>
<span class="atab-cost">${esc(pick(m.cost ?? ''))}</span>
Expand All @@ -165,12 +190,12 @@ export function mountLesson(cfg) {
${head('2·2', UI.secRun)}
<div class="player">
<div class="player-row controls">${fields}${presets}
<p class="warn" data-warn hidden></p>
<p class="warn" id="f-warn" data-warn aria-live="polite" hidden></p>
</div>
${cfg.strip ? `
<div class="player-row">
<div class="strip-head">
<h2>${esc(pick(cfg.stripLabel ?? UI.array))}</h2>
<h4 class="as-h2">${esc(pick(cfg.stripLabel ?? UI.array))}</h4>
<span class="op mono" data-op>${esc(pick(UI.ready))}</span>
</div>
<div class="strip" data-strip tabindex="0" role="region" aria-label="${esc(pick(cfg.stripLabel ?? UI.array))}"></div>
Expand All @@ -183,7 +208,7 @@ export function mountLesson(cfg) {
<input type="range" data-scrub min="0" max="0" value="0" aria-label="${esc(pick(UI.scrub))}">
<span class="counter" data-count>0 / 0</span>
</div>
<div class="narration">
<div class="narration" data-narration aria-live="polite" aria-atomic="true">
<span class="tag" data-tag>${esc(pick(UI.step))}</span>
<p class="text" data-note></p>
</div>
Expand All @@ -194,15 +219,15 @@ export function mountLesson(cfg) {
${cfg.answer ? `
<div class="panel answer-card">
<div class="strip-head">
<h2>${esc(pick(UI.answer))}</h2>
<h4 class="as-h2">${esc(pick(UI.answer))}</h4>
<span class="note mono" data-ans-note></span>
</div>
<div class="answer-row" data-answer></div>
</div>` : ''}
</div>
<div class="panel">
<div class="panel-head">
<h2>${esc(pick(UI.codeLive))}</h2>
<h4 class="as-h2">${esc(pick(UI.codeLive))}</h4>
<span class="note" data-code-label></span>
</div>
${langBar(langs, state.lang, ' mini')}
Expand Down Expand Up @@ -253,6 +278,9 @@ export function mountLesson(cfg) {
const scrub = $('[data-scrub]', root);
scrub.max = String(state.steps.length - 1);
scrub.value = String(state.i);
// what a screen reader says for the scrubber: "Step 2 of 12: tally"
const where = pick(UI.stepOf).replace('{i}', state.i + 1).replace('{n}', state.steps.length);
scrub.setAttribute('aria-valuetext', pick(s.tag) ? `${where}: ${pick(s.tag)}` : where);
$('[data-count]', root).textContent = `${state.i + 1} / ${state.steps.length}`;
}

Expand Down Expand Up @@ -287,15 +315,21 @@ export function mountLesson(cfg) {
state.i = Math.max(0, Math.min(state.steps.length - 1, i));
render();
}
// The narration is a live region, so a screen reader reads each step the
// reader moves to. While playing it goes quiet — a step every 700 ms would
// queue up faster than it can be read — and speaks again on stop.
const narrate = (on) => $('[data-narration]', root)?.setAttribute('aria-live', on ? 'polite' : 'off');
function stop() {
clearInterval(state.timer);
state.timer = null;
const b = $('[data-act="play"]', root);
if (b) b.textContent = pick(UI.play);
narrate(true);
}
function play() {
if (state.i >= state.steps.length - 1) state.i = 0;
$('[data-act="play"]', root).textContent = pick(UI.pause);
narrate(false);
state.timer = setInterval(() => {
if (state.i >= state.steps.length - 1) return stop();
go(state.i + 1);
Expand All @@ -310,8 +344,7 @@ export function mountLesson(cfg) {

function setLangAll(id) {
state.lang = id;
document.querySelectorAll('[data-lang]').forEach((b) =>
b.setAttribute('aria-selected', String(b.dataset.lang === id)));
selectTabs(document.querySelectorAll('[data-lang]'), (b) => b.dataset.lang === id);
document.querySelectorAll('[data-pane]').forEach((p) => { p.hidden = p.dataset.pane !== id; });
render();
}
Expand All @@ -321,9 +354,32 @@ export function mountLesson(cfg) {
state.input = structuredClone(input);
(cfg.controls || []).forEach((c) => {
const el = $(`[data-field="${c.key}"]`, root);
if (el) { el.value = format(c, state.input[c.key]); el.classList.remove('bad'); }
if (el) { el.value = format(c, state.input[c.key]); markBad(el, null); }
});
rebuild();
syncStale();
}

/* A field whose text does not parse: marked on the field itself, tied to the
* warning for a screen reader, and — while any field is bad — the player
* dims to say it still shows the last input that worked. */
const badFields = () => [...root.querySelectorAll('[data-field].bad')];
function markBad(el, msg) {
el.classList.toggle('bad', msg != null);
if (msg != null) {
el.dataset.err = msg;
el.setAttribute('aria-invalid', 'true');
el.setAttribute('aria-describedby', 'f-warn');
} else {
delete el.dataset.err;
el.removeAttribute('aria-invalid');
el.removeAttribute('aria-describedby');
}
}
function syncStale() {
const bad = badFields();
$('.player', root)?.classList.toggle('is-stale', bad.length > 0);
if (bad.length) showWarn(`${bad[0].dataset.err} — ${pick(UI.stale)}`);
}

function showWarn(msg) {
Expand All @@ -339,14 +395,15 @@ export function mountLesson(cfg) {
const spec = cfg.controls.find((c) => c.key === el.dataset.field);
try {
state.input[spec.key] = spec.parse ? spec.parse(el.value) : el.value;
el.classList.remove('bad');
markBad(el, null);
} catch (err) {
el.classList.add('bad');
showWarn(err.message);
markBad(el, err.message);
syncStale();
return;
}
stop();
rebuild();
syncStale();
});
});
const scrub = $('[data-scrub]', root);
Expand All @@ -358,8 +415,7 @@ export function mountLesson(cfg) {
if (card) {
stop();
state.mode = card.dataset.mode;
root.querySelectorAll('[data-mode]').forEach((b) =>
b.setAttribute('aria-selected', String(b.dataset.mode === state.mode)));
selectTabs(root.querySelectorAll('[data-mode]'), (b) => b.dataset.mode === state.mode);
rebuild();
return;
}
Expand All @@ -372,10 +428,15 @@ export function mountLesson(cfg) {
});

// Arrow keys and space — never while someone is typing, and only while the
// walkthrough is on screen, so a widget elsewhere keeps its own keys.
// walkthrough is on screen, so a widget elsewhere keeps its own keys. A
// modified key is the browser's (Alt+← is Back), a tab's arrows move between
// tabs, and space on a focused control presses that control, not Play.
root.addEventListener('keydown', tabKeys);
document.addEventListener('keydown', (e) => {
if (e.altKey || e.ctrlKey || e.metaKey) return;
if (e.target.closest('input,textarea,select,[contenteditable]')) return;
if (e.target.closest('#q-widget')) return;
if (e.target.closest('#q-widget, [role="tab"]')) return;
if (e.key === ' ' && e.target.closest('button, a[href], summary, [role="button"]')) return;
const r = root.getBoundingClientRect();
if (r.bottom < 0 || r.top > innerHeight) return;
const name = { ArrowRight: 'next', ArrowLeft: 'prev', ' ': 'play' }[e.key];
Expand Down Expand Up @@ -528,7 +589,7 @@ function renderSolutions(cfg, activeLang) {
return `
<div class="src">
<div class="src-head">
<h2>${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}${meta.tag ? `<span class="tagpill">${esc(meta.tag)}</span>` : ''}</h2>
<h3 class="as-h2">${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}${meta.tag ? `<span class="tagpill">${esc(meta.tag)}</span>` : ''}</h3>
${badge(l.id, m.id)}
<button class="btn copy" data-copy>${esc(pick(UI.copy))}</button>
${meta.desc ? `<p class="sub">${pick(meta.desc)}</p>` : `<p class="sub mono">${esc(pick(m.cost))}</p>`}
Expand All @@ -538,6 +599,7 @@ function renderSolutions(cfg, activeLang) {
}).join('')}
</div>`).join('')}`;

host.onkeydown = tabKeys;
host.onclick = (e) => {
const langBtn = e.target.closest('[data-lang]');
// the live code panel's tab switches every tab and pane on the page
Expand Down
8 changes: 5 additions & 3 deletions src/styles/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ body{
-webkit-font-smoothing:antialiased;
}
.wrap{max-width:1140px;margin:0 auto;padding-inline:20px;padding-block:32px 56px}
h1,h2,h3{font-family:Archivo,"IBM Plex Sans",sans-serif;font-weight:700;text-wrap:balance;margin:0}
/* .as-h2 and .as-h3 give a deeper heading the look of a shallower one, so the
outline can nest properly without the page changing how it looks. */
h1,h2,h3,.as-h2,.as-h3{font-family:Archivo,"IBM Plex Sans",sans-serif;font-weight:700;text-wrap:balance;margin:0}
h1{font-size:clamp(28px,4.6vw,42px);letter-spacing:-.022em;line-height:1.05}
h2{font-size:19px;letter-spacing:-.01em}
h3{font-size:14px;letter-spacing:-.005em;font-weight:600}
h2,.as-h2{font-size:19px;letter-spacing:-.01em}
h3,.as-h3{font-size:14px;letter-spacing:-.005em;font-weight:600}
p{margin:0}
code,.mono{font-family:"IBM Plex Mono",ui-monospace,SFMono-Regular,Menlo,monospace;font-variant-numeric:tabular-nums}
.eyebrow{
Expand Down
Loading
Loading