Fill a lesson's Reword boxes with the words Ask for suggestions sends

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-29 10:49:42 -08:00
1 parent e0f82cc36b
commit 834b71e82d
7 files changed
+143 -7

No files matched your search

+33 -2
View File
@@ -43,6 +43,7 @@ import {
lessonEditStart,
lessonEditField,
lessonReworded,
lessonSuggestion,
startsTicked,
} from "../answers.js";
import { el, hiddenText, icon, md, stateMark } from "../render.js";
@@ -1758,7 +1759,9 @@ function lessonsDraft(item) {
}
const picks = d && d.picks && typeof d.picks === "object" ? d.picks : {};
const edits = d && d.edits && typeof d.edits === "object" ? d.edits : {};
return { picks, edits, leaveAll: Boolean(d && d.leaveAll) };
// Which suggested words were already put in a row's boxes, so they go in once.
const applied = d && d.applied && typeof d.applied === "object" ? d.applied : {};
return { picks, edits, applied, leaveAll: Boolean(d && d.leaveAll) };
}
// The answer the draft stands for, or null with the field still missing.
@@ -1829,8 +1832,10 @@ function buildLessons(card, item, staged) {
const how = lessonsCheckedLine(item.checked);
if (how) card.appendChild(el("p", "lessons-checked", how));
const list = el("ul", "lessons-rows");
const filled = lessonsFillSuggested(item, draft);
for (const row of item.rows || []) list.appendChild(lessonRow(item, row, draft, commit));
card.appendChild(list);
if (filled) commit(false);
const { missing } = lessonsAnswer(item, draft);
error.hidden = !missing;
@@ -1908,6 +1913,32 @@ function lessonCheckLines(li, item, row, draft, commit) {
li.appendChild(box);
}
// A suggestion with words (Ask for suggestions) fills the boxes of a row whose
// edit is open on the suggested choice: once per suggestion, whether the edit
// was open when it landed or opened after, so typing afterwards stays.
function lessonsFillSuggested(item, draft) {
let filled = false;
for (const row of item.rows || []) {
const sug = lessonSuggestion(item, row);
if (!sug || (!sug.text && !sug.why) || draft.picks[row.id] !== sug.choice) continue;
const stamp = JSON.stringify([sug.text, sug.why]);
if (draft.applied[row.id] === stamp) continue;
const edit = draft.edits[row.id] || lessonEditStart(row);
if (sug.text) {
const key = lessonEditField(row).key;
// A log line that would become several lessons keeps them: one
// suggested sentence never stands in for the whole list.
if (key !== "lessons") edit[key] = sug.text;
else if (!Array.isArray(edit.lessons) || edit.lessons.length <= 1) edit.lessons = [sug.text];
}
if (sug.why && "why" in edit) edit.why = sug.why;
draft.edits[row.id] = edit;
draft.applied[row.id] = stamp;
filled = true;
}
return filled;
}
// One waiting item: what it is, why, where it came from, and its own choices.
function lessonRow(item, row, draft, commit) {
const pick = draft.picks[row.id] || "";
@@ -1977,7 +2008,7 @@ function lessonRow(item, row, draft, commit) {
group.setAttribute("aria-label", "Choices for: " + (row.text || row.id));
// The agent's suggestion (Ask for suggestions) outlines its word; it is
// never a pick, so nothing is chosen for the person.
const suggested = item.suggested && typeof item.suggested === "object" ? item.suggested[row.id] : null;
const suggested = lessonSuggestion(item, row)?.choice ?? null;
choices.forEach((word, i) => {
const on = pick === word;
const hint = suggested === word;