/* SPDX-License-Identifier: GPL-3.0-or-later */
import { createLocalFileAccessController } from "./local-file-access.js";
// One readiness decision supplies the page heading, probe precondition and
// practice recovery. A library with disabled terms needs enabling, not a reimport.
export function practiceReadiness(options, dictionaries, outcome = null, readerFailed = false) {
const available = dictionaries.some(entry => entry.enabled !== false && entry.termCount > 0);
if (!available) {
const installed = dictionaries.some(entry => entry.termCount > 0);
return {
canProbe: false,
heading: installed ? "Enable a dictionary to try Hachidori" : "Add a dictionary to try Hachidori",
message: installed ? "Your term dictionaries are turned off. Enable one to look up Japanese words." : "Add a term dictionary to look up Japanese words. The recommended dictionaries are a good place to start.",
href: installed ? "settings.html#dictionaries" : "settings.html#add-dictionaries",
action: installed ? "Open Library" : "Add dictionaries",
};
}
if (!options.hoverEnabled) {
return { canProbe: false, heading: "Turn on lookups to try Hachidori",
message: "Lookups are turned off. Enable them in Reading to try the sample.",
href: "settings.html#lookup", action: "Open Reading" };
}
if (readerFailed || outcome === "unavailable") {
return { canProbe: true, heading: "The lookup exercise is unavailable",
message: readerFailed ? "The reader could not load. Reload this page, or review your reading settings." : "The dictionary engine could not answer. Reload this page, or review your reading settings.",
href: "settings.html#lookup", action: "Open Reading" };
}
if (outcome === "missing") {
return { canProbe: true, heading: "Add a dictionary for this sample",
message: "The installed dictionaries do not have the words in this sample yet. Add another term dictionary to try it.",
href: "settings.html#add-dictionaries", action: "Add dictionaries" };
}
return { canProbe: true, heading: outcome === "ready" || outcome === "passage" ? "You’re ready." : "Preparing your first lookup…" };
}
function practiceInstruction(options, enabled, probing, unavailable, shortcut) {
if (enabled) {
const ending = shortcut ? ", or use the lookup button." : ".";
return options.lookupMode !== "hover"
? `Try looking up a word below. Hold ${options.activationKey} and hover over a word${ending}`
: `Try looking up a word below. Hover over Japanese text${ending}`;
}
if (probing) return "Checking what the installed dictionaries can answer…";
if (unavailable) {
return options.lookupMode !== "hover"
? `Hold ${options.activationKey} and hover over Japanese text on any webpage to look it up.`
: "Hover over Japanese text on any webpage to look it up.";
}
return "You can finish setup now and try a lookup later.";
}
// Keep the exercise's text nodes alive while options/inventory updates arrive:
// the ordinary reader anchors its selection, popup and Note draft to them.
export function createPracticeView({ document, onDismiss, loadReader, onReaderSettled = () => {} }) {
const node = document.createElement("div");
node.className = "setup-practice";
node.innerHTML = `