Add puzzle hints and per-word descriptions with sources
Hint is revealed on demand below the board while playing. After the game ends, each word's description and source link are listed below the puzzle, and hovering a solved word highlights its entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4a2309b589
commit
b2f8e879ab
7 changed files with 141 additions and 21 deletions
|
|
@ -49,11 +49,12 @@ function showLogin(error) {
|
|||
}
|
||||
|
||||
function showEditor(game) {
|
||||
const g = game || { title: '', date: '', groups: COLORS.map((color) => ({ name: '', color, words: ['', '', '', ''] })) };
|
||||
const g = game || { title: '', date: '', hint: '', groups: COLORS.map((color) => ({ name: '', color, words: ['', '', '', ''] })) };
|
||||
const field = (id, label, input, help) =>
|
||||
el('div', { class: 'field' }, el('label', { for: id }, label), input, help ? el('p', { class: 'help' }, help) : null);
|
||||
const title = el('input', { id: 'title', value: g.title, maxlength: 100, required: true });
|
||||
const date = el('input', { id: 'date', type: 'date', value: g.date || '' });
|
||||
const hint = el('input', { id: 'hint', value: g.hint || '', maxlength: 200 });
|
||||
const errBox = el('div');
|
||||
|
||||
const groupEls = g.groups.map((grp, i) => {
|
||||
|
|
@ -61,14 +62,21 @@ function showEditor(game) {
|
|||
const name = el('input', { id: `g${n}-name`, value: grp.name, maxlength: 80, required: true });
|
||||
const color = el('select', { id: `g${n}-color` }, COLORS.map((c) => el('option', { value: c, selected: c === grp.color }, c[0].toUpperCase() + c.slice(1))));
|
||||
const words = grp.words.map((w, j) => el('input', { value: w, maxlength: 30, required: true, 'aria-label': `Group ${n} word ${j + 1}` }));
|
||||
const descs = grp.words.map((_, j) => el('input', { value: grp.details?.[j]?.description || '', maxlength: 500, placeholder: 'Description (optional)', 'aria-label': `Group ${n} word ${j + 1} description` }));
|
||||
const sources = grp.words.map((_, j) => el('input', { type: 'url', value: grp.details?.[j]?.source || '', maxlength: 500, placeholder: 'https://source (optional)', 'aria-label': `Group ${n} word ${j + 1} source link` }));
|
||||
const box = el('fieldset', { class: 'group-edit' },
|
||||
el('legend', { class: 'sr' }, `Group ${n}`),
|
||||
el('div', { class: 'head' },
|
||||
field(`g${n}-name`, el('span', {}, el('span', { class: `swatch ${grp.color}` }), `Group ${n} name`), name),
|
||||
field(`g${n}-color`, 'Color', color)),
|
||||
el('label', {}, 'Words'), el('div', { class: 'words' }, words));
|
||||
el('label', {}, 'Words'),
|
||||
el('p', { class: 'help', style: 'margin-bottom:8px' }, 'Descriptions and sources show below the puzzle once it ends.'),
|
||||
el('div', { class: 'words' }, words.map((w, j) => el('div', { class: 'word-row' }, w, descs[j], sources[j]))));
|
||||
color.addEventListener('change', () => { box.querySelector('.swatch').className = `swatch ${color.value}`; });
|
||||
return { box, read: () => ({ name: name.value, color: color.value, words: words.map((w) => w.value) }) };
|
||||
return { box, read: () => ({
|
||||
name: name.value, color: color.value, words: words.map((w) => w.value),
|
||||
details: words.map((_, j) => ({ description: descs[j].value, source: sources[j].value })),
|
||||
}) };
|
||||
});
|
||||
|
||||
$app.replaceChildren(
|
||||
|
|
@ -78,7 +86,7 @@ function showEditor(game) {
|
|||
el('form', { novalidate: true, onsubmit: async (e) => {
|
||||
e.preventDefault();
|
||||
const btn = e.submitter; btn.disabled = true;
|
||||
const body = { title: title.value, date: date.value || null, groups: groupEls.map((x) => x.read()) };
|
||||
const body = { title: title.value, date: date.value || null, hint: hint.value, groups: groupEls.map((x) => x.read()) };
|
||||
const { ok, data } = await api(game ? `/api/admin/games/${game.id}` : '/api/admin/games', { method: game ? 'PUT' : 'POST', body });
|
||||
btn.disabled = false;
|
||||
if (ok) { toast('Saved'); return showList(); }
|
||||
|
|
@ -89,6 +97,7 @@ function showEditor(game) {
|
|||
errBox,
|
||||
field('title', 'Title', title),
|
||||
field('date', 'Date', date, 'Optional. The puzzle becomes the daily puzzle on this date.'),
|
||||
field('hint', 'Hint', hint, 'Optional. Players can reveal it below the puzzle while playing.'),
|
||||
groupEls.map((x) => x.box),
|
||||
el('div', { class: 'actions', style: 'justify-content:flex-start' }, el('button', { class: 'btn primary', type: 'submit' }, icon('check'), 'Save puzzle'))),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ const $game = document.getElementById('game');
|
|||
const idMatch = location.pathname.match(/^\/play\/(\d+)/);
|
||||
if (!idMatch) document.getElementById('nav-today').setAttribute('aria-current', 'page');
|
||||
|
||||
const state = { game: null, words: [], selected: new Set(), solved: [], mistakes: 0, guesses: [], tried: new Set(), done: false, won: false };
|
||||
const state = { game: null, words: [], selected: new Set(), solved: [], mistakes: 0, guesses: [], tried: new Set(), done: false, won: false, showHint: false };
|
||||
|
||||
const icon = (name) => el('i', { class: `ph-bold ph-${name}`, 'aria-hidden': 'true' });
|
||||
|
||||
async function load() {
|
||||
const { ok, data } = await api(idMatch ? `/api/games/${idMatch[1]}` : '/api/daily');
|
||||
|
|
@ -26,7 +28,8 @@ async function load() {
|
|||
|
||||
function render() {
|
||||
const board = el('div', { class: 'board' },
|
||||
state.solved.map((g) => el('div', { class: `solved ${g.color}` }, el('strong', {}, g.name), el('span', {}, g.words.join(', ')))),
|
||||
state.solved.map((g) => el('div', { class: `solved ${g.color}` }, el('strong', {}, g.name),
|
||||
el('span', {}, g.words.flatMap((w, i) => [i ? ', ' : '', el('span', { class: 'word', 'data-word': w }, w)])))),
|
||||
state.words.length ? el('div', { class: 'row4' }, state.words.map((w) =>
|
||||
el('button', { class: 'tile', 'aria-pressed': String(state.selected.has(w)), disabled: state.done, onclick: () => toggle(w) }, w))) : null,
|
||||
);
|
||||
|
|
@ -34,15 +37,22 @@ function render() {
|
|||
|
||||
if (state.done) {
|
||||
kids.push(resultPanel());
|
||||
const details = detailsPanel();
|
||||
if (details) kids.push(details);
|
||||
} else {
|
||||
kids.push(
|
||||
el('div', { class: 'mistakes', 'aria-label': `${MAX_MISTAKES - state.mistakes} mistakes remaining` },
|
||||
'Mistakes remaining:', [...Array(MAX_MISTAKES)].map((_, i) => el('i', { class: i < state.mistakes ? 'used' : '' }))),
|
||||
el('div', { class: 'actions' },
|
||||
el('button', { class: 'btn', onclick: shuffleWords }, el('i', { class: 'ph-bold ph-shuffle', 'aria-hidden': 'true' }), 'Shuffle'),
|
||||
el('button', { class: 'btn', onclick: shuffleWords }, icon('shuffle'), 'Shuffle'),
|
||||
el('button', { class: 'btn', disabled: !state.selected.size, onclick: () => { state.selected.clear(); render(); } }, 'Deselect all'),
|
||||
el('button', { class: 'btn primary', disabled: state.selected.size !== 4, onclick: submit }, 'Submit')),
|
||||
);
|
||||
if (state.game.hint) {
|
||||
kids.push(state.showHint
|
||||
? el('p', { class: 'hint' }, icon('lightbulb'), el('span', {}, el('strong', {}, 'Hint: '), state.game.hint))
|
||||
: el('div', { class: 'hint-toggle' }, el('button', { class: 'btn', onclick: () => { state.showHint = true; render(); } }, icon('lightbulb'), 'Show hint')));
|
||||
}
|
||||
}
|
||||
// Re-rendering replaces the buttons, so put keyboard focus back on the same one.
|
||||
const focused = $game.contains(document.activeElement) ? document.activeElement.textContent : null;
|
||||
|
|
@ -122,10 +132,37 @@ function resultPanel() {
|
|||
el('p', { class: 'sub' }, state.won ? `Finished with ${state.mistakes} mistake${state.mistakes === 1 ? '' : 's'}.` : 'Here are the groups.'),
|
||||
el('pre', { 'aria-label': 'Your guesses' }, grid),
|
||||
el('div', { class: 'actions' },
|
||||
el('button', { class: 'btn primary', onclick: () => share(text) }, el('i', { class: 'ph-bold ph-copy', 'aria-hidden': 'true' }), 'Share results'),
|
||||
el('button', { class: 'btn primary', onclick: () => share(text) }, icon('copy'), 'Share results'),
|
||||
el('a', { class: 'btn', href: '/puzzles' }, 'More puzzles')));
|
||||
}
|
||||
|
||||
function detailsPanel() {
|
||||
const hasAny = state.solved.some((g) => g.details?.some((d) => d.description || d.source));
|
||||
if (!hasAny) return null;
|
||||
const host = (url) => { try { return new URL(url).hostname.replace(/^www\./, ''); } catch { return 'Source'; } };
|
||||
return el('section', { class: 'details', 'aria-labelledby': 'details-h' },
|
||||
el('h2', { id: 'details-h' }, 'About the words'),
|
||||
state.solved.map((g) => el('div', { class: 'detail-group' },
|
||||
el('h3', {}, el('span', { class: `swatch ${g.color}` }), g.name),
|
||||
el('dl', {}, g.words.map((w, i) => {
|
||||
const d = g.details?.[i] || {};
|
||||
return el('div', { class: 'detail', 'data-word': w },
|
||||
el('dt', {}, w),
|
||||
el('dd', {}, d.description || null,
|
||||
d.source ? el('a', { class: 'source', href: d.source, target: '_blank', rel: 'noopener noreferrer' }, icon('arrow-square-out'), host(d.source)) : null));
|
||||
})))));
|
||||
}
|
||||
|
||||
// After the game ends, hovering a word in a solved row highlights its description.
|
||||
function highlight(e, on) {
|
||||
const word = state.done && e.target.closest?.('.solved .word');
|
||||
if (!word) return;
|
||||
word.classList.toggle('hl', on);
|
||||
$game.querySelector(`.detail[data-word="${CSS.escape(word.dataset.word)}"]`)?.classList.toggle('hl', on);
|
||||
}
|
||||
$game.addEventListener('mouseover', (e) => highlight(e, true));
|
||||
$game.addEventListener('mouseout', (e) => highlight(e, false));
|
||||
|
||||
async function share(text) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
|
|
|
|||
|
|
@ -164,6 +164,41 @@ h1 { font-size: clamp(1.6rem, 4vw, 2.1rem); letter-spacing: -0.03em; line-height
|
|||
.result h2 { margin: 0 0 4px; letter-spacing: -0.02em; }
|
||||
.result pre { font-family: inherit; font-size: 1.4rem; line-height: 1.25; margin: 12px 0 20px; }
|
||||
|
||||
/* Hint (while playing) */
|
||||
.hint-toggle { display: flex; justify-content: center; margin-top: 16px; }
|
||||
.hint {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
align-items: flex-start;
|
||||
margin: 16px 0 0;
|
||||
padding: 12px 16px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
animation: pop 0.3s var(--ease);
|
||||
}
|
||||
.hint i { margin-top: 4px; }
|
||||
|
||||
/* Word descriptions (after the game ends) */
|
||||
.solved .word { border-radius: 4px; padding: 0 2px; transition: background 0.15s; }
|
||||
.solved .word.hl { background: rgb(0 0 0 / 0.14); }
|
||||
.details { margin-top: 32px; animation: pop 0.4s var(--ease); }
|
||||
.details h2 { margin: 0 0 12px; letter-spacing: -0.02em; }
|
||||
.detail-group { margin-bottom: 20px; }
|
||||
.detail-group h3 { font-size: 1rem; margin: 0 0 8px; text-transform: uppercase; letter-spacing: 0.02em; }
|
||||
.detail-group dl { display: grid; gap: 6px; margin: 0; }
|
||||
.detail {
|
||||
padding: 10px 14px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
transition: border-color 0.15s, background 0.15s;
|
||||
}
|
||||
.detail.hl { border-color: var(--ink); background: var(--tile); }
|
||||
.detail dt { font-weight: 700; text-transform: uppercase; }
|
||||
.detail dd { margin: 0; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; }
|
||||
.detail .source { display: inline-flex; gap: 4px; align-items: center; color: var(--ink); font-weight: 600; font-size: 0.9rem; }
|
||||
|
||||
/* Lists and forms */
|
||||
.list { display: grid; gap: 10px; padding: 0; margin: 0; list-style: none; }
|
||||
.list a, .list .item {
|
||||
|
|
@ -198,15 +233,17 @@ input, select {
|
|||
input:focus, select:focus { border-color: var(--ink); outline: none; }
|
||||
.group-edit { min-width: 0; padding: 16px; border-radius: var(--radius); margin-bottom: 14px; border: 1px solid var(--line); background: var(--surface); }
|
||||
.group-edit .head { display: grid; grid-template-columns: 1fr 140px; gap: 10px; margin-bottom: 10px; }
|
||||
.group-edit .words { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
|
||||
.group-edit .swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
|
||||
.group-edit .words { display: grid; gap: 8px; }
|
||||
.group-edit .word-row { display: grid; grid-template-columns: 150px 1fr 1fr; gap: 8px; }
|
||||
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
|
||||
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
||||
.errors { color: var(--error); margin: 0 0 16px; padding-left: 20px; }
|
||||
.row-actions { display: flex; gap: 8px; }
|
||||
.icon-btn { padding: 8px 12px; }
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.group-edit .head, .group-edit .words { grid-template-columns: 1fr 1fr; }
|
||||
.group-edit .head { grid-template-columns: 1fr 1fr; }
|
||||
.group-edit .word-row { grid-template-columns: 1fr; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
|
||||
.topbar nav { gap: 14px; }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue