dracosh 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -8
- package/locales/de.json +359 -11
- package/locales/en.json +359 -11
- package/locales/fr.json +375 -11
- package/locales/pl.json +391 -11
- package/locales/uk.json +391 -11
- package/package.json +1 -1
- package/src/ai.js +245 -0
- package/src/alerts.js +77 -23
- package/src/assist.js +232 -0
- package/src/cli.js +9 -5
- package/src/commands.js +3 -0
- package/src/firstwords.js +49 -0
- package/src/language.js +11 -6
- package/src/library.js +135 -3
- package/src/lock.js +1 -1
- package/src/manage.js +160 -4
- package/src/progress.js +8 -0
- package/src/session.js +38 -6
- package/src/settings.js +11 -1
- package/src/store.js +10 -4
- package/src/transfer.js +22 -5
- package/src/ui/AddCard.js +236 -38
- package/src/ui/Ai.js +284 -0
- package/src/ui/AnswerInput.js +35 -7
- package/src/ui/App.js +263 -18
- package/src/ui/Cards.js +350 -0
- package/src/ui/Choice.js +6 -1
- package/src/ui/Decks.js +181 -15
- package/src/ui/Import.js +316 -0
- package/src/ui/Languages.js +48 -9
- package/src/ui/Preview.js +22 -1
- package/src/ui/Settings.js +23 -7
- package/src/ui/Trash.js +63 -0
- package/src/ui/Voice.js +238 -0
- package/src/ui/Welcome.js +80 -0
- package/src/voice.js +129 -0
- package/src/words.js +27 -4
package/src/ui/Cards.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { Fragment, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { Box, useInput } from "ink";
|
|
3
|
+
import { language, t } from "../i18n.js";
|
|
4
|
+
import { providerName } from "../ai.js";
|
|
5
|
+
import { checkCard } from "../assist.js";
|
|
6
|
+
import { formatLine, parseLine, plainFront } from "../cardline.js";
|
|
7
|
+
import { fold } from "../judge.js";
|
|
8
|
+
import { TRASH_DAYS } from "../library.js";
|
|
9
|
+
import { AnswerInput } from "./AnswerInput.js";
|
|
10
|
+
import { Choice, choiceHints } from "./Choice.js";
|
|
11
|
+
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
12
|
+
|
|
13
|
+
const SHOWN = 8; // rows of the list at a time
|
|
14
|
+
const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
15
|
+
// a card's menu: checked first for a card written by AI help; a check by AI when it's set up
|
|
16
|
+
const menuOf = (card, ai) => [...(card.ai ? ["confirm"] : []), ...(ai && !card.cloze ? ["check"] : []), "edit", "move", "delete"];
|
|
17
|
+
|
|
18
|
+
const look = (text) => fold(String(text).toLowerCase());
|
|
19
|
+
const lineOf = (card) => (card.cloze ? plainFront(card.front) : `${card.front} = ${card.back.join("; ")}`);
|
|
20
|
+
// a card matches what's typed in its front, any answer or its example, accents or not
|
|
21
|
+
const matches = (card, query) => !query || [card.front, ...card.back, card.example].some((text) => look(text).includes(look(query)));
|
|
22
|
+
|
|
23
|
+
// /cards: the cards of a deck, to search (type), fix, move to another deck or delete. A deleted card
|
|
24
|
+
// goes to the trash, and the row on top brings it straight back. `actions`: the library's;
|
|
25
|
+
// `onDeleted`(cardIds) → { entry }, `onRestored`(entry), `onEdited`(staleKeys) let the app keep the
|
|
26
|
+
// progress in step; `onAdd` opens /add for the deck; onClose(changed).
|
|
27
|
+
export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, onClose, ai = null }) {
|
|
28
|
+
const panel = usePanelWidth();
|
|
29
|
+
const [cards, setCards] = useState(null);
|
|
30
|
+
const [decks, setDecks] = useState([]); // your other decks, to move a card to
|
|
31
|
+
const [query, setQuery] = useState("");
|
|
32
|
+
const [selected, setSelected] = useState(0);
|
|
33
|
+
const [open, setOpen] = useState(null); // { card, step: "menu" | "edit" | "move" | "delete", selected }
|
|
34
|
+
const [undo, setUndo] = useState(null); // { entry, card }: the card just deleted
|
|
35
|
+
const [message, setMessage] = useState("");
|
|
36
|
+
const [error, setError] = useState("");
|
|
37
|
+
const changed = useRef(false);
|
|
38
|
+
const busy = useRef(false);
|
|
39
|
+
const checking = useRef(null); // the check by AI in flight: its AbortController
|
|
40
|
+
const [spin, setSpin] = useState(0);
|
|
41
|
+
const menuAt = (card, step) => menuOf(card, ai).indexOf(step);
|
|
42
|
+
const waiting = open?.step === "check" && !open.verdict;
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!waiting) return;
|
|
45
|
+
const timer = setInterval(() => setSpin((n) => n + 1), 100);
|
|
46
|
+
return () => clearInterval(timer);
|
|
47
|
+
}, [waiting]);
|
|
48
|
+
useEffect(() => () => checking.current?.abort(), []);
|
|
49
|
+
|
|
50
|
+
const load = () => actions.cardsIn(deck).then(setCards, (err) => setError(err.message));
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
load();
|
|
53
|
+
actions.ownDecks().then((own) => setDecks(own.filter((d) => d.name !== deck)), () => {});
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
// the cards AI wrote and you haven't checked come first, in their own order
|
|
57
|
+
const found = (cards ?? []).filter((card) => matches(card, query)).sort((a, b) => Number(Boolean(b.ai)) - Number(Boolean(a.ai)));
|
|
58
|
+
const toCheck = (cards ?? []).filter((card) => card.ai).length;
|
|
59
|
+
// the rows: bring back the card just deleted, add cards (with no search), then the cards found
|
|
60
|
+
// (both only with no search: Enter on what's found never brings a card back by accident)
|
|
61
|
+
const rows = [...(undo && !query ? [{ undo: true }] : []), ...(query ? [] : [{ add: true }]), ...found];
|
|
62
|
+
|
|
63
|
+
async function run(work) {
|
|
64
|
+
if (busy.current) return;
|
|
65
|
+
busy.current = true;
|
|
66
|
+
setError("");
|
|
67
|
+
try {
|
|
68
|
+
await work();
|
|
69
|
+
} catch (err) {
|
|
70
|
+
setError(err.message);
|
|
71
|
+
} finally {
|
|
72
|
+
busy.current = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const pick = (row) => {
|
|
77
|
+
setMessage("");
|
|
78
|
+
if (row.undo) return restore();
|
|
79
|
+
if (row.add) return onAdd();
|
|
80
|
+
setOpen({ card: row, step: "menu", selected: 0 });
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const restore = () =>
|
|
84
|
+
run(async () => {
|
|
85
|
+
await onRestored(undo.entry);
|
|
86
|
+
setMessage(t("cards.restored", { card: cutToWidth(lineOf(undo.card), 40) }));
|
|
87
|
+
setUndo(null);
|
|
88
|
+
changed.current = true;
|
|
89
|
+
await load();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const remove = (card) =>
|
|
93
|
+
run(async () => {
|
|
94
|
+
const { entry } = await onDeleted([card.id]);
|
|
95
|
+
setOpen(null);
|
|
96
|
+
setUndo({ entry, card });
|
|
97
|
+
setQuery(""); // the whole list again, the way back on top
|
|
98
|
+
setSelected(2); // on the cards, past the way back and + Add cards: a second Enter can't undo it
|
|
99
|
+
setMessage(t("cards.deleted", { card: cutToWidth(lineOf(card), 40), days: TRASH_DAYS }));
|
|
100
|
+
changed.current = true;
|
|
101
|
+
await load();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const edit = (card, text) =>
|
|
105
|
+
run(async () => {
|
|
106
|
+
const fields = parseLine(text);
|
|
107
|
+
if (!fields.front) throw new Error(t("add.needFront"));
|
|
108
|
+
if (!fields.cloze && !fields.back.length) throw new Error(t("add.needBack"));
|
|
109
|
+
// Enter on the card as it was keeps it marked as AI's: only a change or "It's right" checks it
|
|
110
|
+
const { staleKeys } = await actions.updateCardIn(deck, card.id, { ...fields, ai: card.ai && formatLine(fields) === formatLine(card) });
|
|
111
|
+
await onEdited(staleKeys);
|
|
112
|
+
setOpen(null);
|
|
113
|
+
setMessage(t("add.fixed", { front: cutToWidth(plainFront(fields.front), 40) }));
|
|
114
|
+
changed.current = true;
|
|
115
|
+
await load();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const confirm = (card) =>
|
|
119
|
+
run(async () => {
|
|
120
|
+
await actions.confirmCardIn(deck, card.id);
|
|
121
|
+
setOpen(null);
|
|
122
|
+
setMessage(t("cards.confirmed", { card: cutToWidth(lineOf(card), 40) }));
|
|
123
|
+
changed.current = true;
|
|
124
|
+
await load();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// AI looks the card over: nothing wrong, or what is, with a fix to take or leave. One check at a time,
|
|
128
|
+
// and an answer that comes after Esc (or after the menu was left) changes nothing.
|
|
129
|
+
async function check(card) {
|
|
130
|
+
if (checking.current) return;
|
|
131
|
+
const controller = new AbortController();
|
|
132
|
+
checking.current = controller;
|
|
133
|
+
setError("");
|
|
134
|
+
setOpen((o) => ({ ...o, step: "check", verdict: null, selected: 0 }));
|
|
135
|
+
const name = providerName(ai.provider);
|
|
136
|
+
try {
|
|
137
|
+
const [languages, all] = await Promise.all([actions.languagesOf(deck).catch(() => ({})), actions.cardsIn(deck).catch(() => [])]);
|
|
138
|
+
const verdict = await checkCard(ai, { card, languages, cards: all, language: language(), signal: controller.signal });
|
|
139
|
+
if (controller.signal.aborted) return;
|
|
140
|
+
const still = (o) => o?.step === "check" && !o.verdict && o.card.id === card.id;
|
|
141
|
+
if (!verdict.ok) return setOpen((o) => (still(o) ? { ...o, verdict } : o));
|
|
142
|
+
// an AI card keeps its ✦: AI saying it's right isn't you checking it
|
|
143
|
+
setOpen((o) => (still(o) ? null : o));
|
|
144
|
+
setMessage(t("cards.checkOk", { provider: name, card: cutToWidth(lineOf(card), 40) }));
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (controller.signal.aborted) return;
|
|
147
|
+
setOpen((o) => (o?.step === "check" && !o.verdict ? { ...o, step: "menu", selected: menuAt(o.card, "check") } : o));
|
|
148
|
+
setError(err.code === "unreadable" ? t("cards.checkUnclear", { provider: name }) : err.message);
|
|
149
|
+
} finally {
|
|
150
|
+
if (checking.current === controller) checking.current = null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const useFix = (card, fix) =>
|
|
155
|
+
run(async () => {
|
|
156
|
+
const { staleKeys } = await actions.updateCardIn(deck, card.id, fix);
|
|
157
|
+
await onEdited(staleKeys);
|
|
158
|
+
setOpen(null);
|
|
159
|
+
setMessage(t("add.fixed", { front: cutToWidth(plainFront(fix.front), 40) }));
|
|
160
|
+
changed.current = true;
|
|
161
|
+
await load();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const move = (card, to) =>
|
|
165
|
+
run(async () => {
|
|
166
|
+
const { moved } = await actions.moveCards(deck, [card.id], to);
|
|
167
|
+
if (!moved.length) throw new Error(t("cards.alreadyThere", { deck: to }));
|
|
168
|
+
setOpen(null);
|
|
169
|
+
setMessage(t("cards.moved", { card: cutToWidth(lineOf(card), 40), deck: to }));
|
|
170
|
+
changed.current = true;
|
|
171
|
+
await load();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
useInput((input, key) => {
|
|
175
|
+
if (open?.step === "check" && !open.verdict) {
|
|
176
|
+
if (key.escape || key.leftArrow) {
|
|
177
|
+
checking.current?.abort();
|
|
178
|
+
checking.current = null;
|
|
179
|
+
setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "check") }));
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (open) {
|
|
184
|
+
if (key.leftArrow && open.step === "menu") setOpen(null);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (key.escape) return query ? (setQuery(""), setSelected(0)) : onClose(changed.current);
|
|
188
|
+
if (key.upArrow) return setSelected((i) => Math.max(0, i - 1));
|
|
189
|
+
if (key.downArrow) return setSelected((i) => Math.min(rows.length - 1, i + 1));
|
|
190
|
+
if (key.return) return rows[selected] && pick(rows[selected]);
|
|
191
|
+
if (key.rightArrow) return rows[selected] && !rows[selected].add && !rows[selected].undo && pick(rows[selected]);
|
|
192
|
+
if (key.backspace || key.delete) return query && (setQuery((q) => q.slice(0, -1)), setSelected(0));
|
|
193
|
+
if (key.ctrl && input === "u") return setQuery("");
|
|
194
|
+
const text = input.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
195
|
+
if (text && !key.ctrl && !key.meta) {
|
|
196
|
+
setQuery((q) => q + text);
|
|
197
|
+
setSelected(0);
|
|
198
|
+
setMessage("");
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const frame = (title, body, hints, color = theme.accent) => html`
|
|
203
|
+
<${Fragment}>
|
|
204
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${color} paddingX=${2} width=${panel}>
|
|
205
|
+
<${Text} bold color=${color}>${title}<//>
|
|
206
|
+
${body}
|
|
207
|
+
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
208
|
+
${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
|
|
209
|
+
<//>
|
|
210
|
+
<${Box} paddingX=${1}><${KeyHints} text=${hints} /><//>
|
|
211
|
+
<//>
|
|
212
|
+
`;
|
|
213
|
+
|
|
214
|
+
if (open?.step === "edit")
|
|
215
|
+
return frame(
|
|
216
|
+
t("cards.editTitle", { deck }),
|
|
217
|
+
html`
|
|
218
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
219
|
+
<${Text} dimColor>${t("cards.editHint")}<//>
|
|
220
|
+
<${Box} marginTop=${1}><${AnswerInput} plain escLeaves cardLine initial=${formatLine(open.card)} onSubmit=${(text) => edit(open.card, text)} onExit=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "edit") }))} onChange=${() => setError("")} /><//>
|
|
221
|
+
<//>
|
|
222
|
+
`,
|
|
223
|
+
`${t("keyHints.tabPart")} · ${t("keyHints.addFixing")}`
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
if (open?.step === "move")
|
|
227
|
+
return frame(
|
|
228
|
+
t("cards.moveTitle", { card: cutToWidth(lineOf(open.card), 40) }),
|
|
229
|
+
html`<${Box} marginTop=${1}>${decks.length ? html`<${Choice} options=${decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) }))} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => move(open.card, decks[i].name)} onCancel=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "move") }))} />` : html`<${Text}>${t("cards.noOtherDeck")}<//>`}<//>`,
|
|
230
|
+
choiceHints("back")
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
if (open?.step === "delete")
|
|
234
|
+
return frame(
|
|
235
|
+
t("cards.deleteQuestion", { card: cutToWidth(lineOf(open.card), 50) }),
|
|
236
|
+
html`
|
|
237
|
+
<${Box} flexDirection="column">
|
|
238
|
+
<${Text} dimColor>${t("cards.deleteNote", { days: TRASH_DAYS })}<//>
|
|
239
|
+
<${Box} marginTop=${1}><${Choice} options=${[{ label: t("decks.deleteYes"), danger: true }, { label: t("cards.deleteNo") }]} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (i === 0 ? remove(open.card) : setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "delete") })))} onCancel=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "delete") }))} /><//>
|
|
240
|
+
<//>
|
|
241
|
+
`,
|
|
242
|
+
choiceHints("cancel"),
|
|
243
|
+
theme.bad
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
if (open?.step === "check") {
|
|
247
|
+
const name = providerName(ai.provider);
|
|
248
|
+
const { verdict } = open;
|
|
249
|
+
const back = () => setOpen((o) => ({ ...o, step: "menu", verdict: null, selected: menuAt(o.card, "check") }));
|
|
250
|
+
return frame(
|
|
251
|
+
cutToWidth(lineOf(open.card), panel - 8),
|
|
252
|
+
verdict
|
|
253
|
+
? html`
|
|
254
|
+
<${Box} flexDirection="column">
|
|
255
|
+
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
256
|
+
<${Box} marginTop=${1}><${Box} flexShrink=${0}><${Text} color=${theme.accent}>✦ <//><//><${Text} color=${theme.accent}>${verdict.note || t("cards.checkFix", { provider: name })}<//><//>
|
|
257
|
+
<${Box} marginTop=${1} flexDirection="column" paddingLeft=${2}>
|
|
258
|
+
${verdict.fix && html`<${Fixed} card=${open.card} fix=${verdict.fix} />`}
|
|
259
|
+
<//>
|
|
260
|
+
<${Box} marginTop=${1}><${Choice} options=${verdict.fix ? [{ label: t("cards.useFix") }, { label: t("cards.keepMine") }] : [{ label: t("cards.keepMine") }]} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (verdict.fix && i === 0 ? useFix(open.card, verdict.fix) : back())} onCancel=${back} /><//>
|
|
261
|
+
<//>
|
|
262
|
+
`
|
|
263
|
+
: html`
|
|
264
|
+
<${Box} flexDirection="column">
|
|
265
|
+
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
266
|
+
<${Box} marginTop=${1}><${Text} color=${theme.accent}>${SPIN[spin % SPIN.length]} ${t("cards.checking", { provider: name })}<//><//>
|
|
267
|
+
<//>
|
|
268
|
+
`,
|
|
269
|
+
verdict ? choiceHints("back") : t("keyHints.aiWaiting")
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (open?.step === "menu") {
|
|
274
|
+
const steps = menuOf(open.card, ai);
|
|
275
|
+
const labels = { confirm: t("cards.confirmRow"), check: t("cards.checkRow"), edit: t("cards.editRow"), move: t("cards.moveRow"), delete: t("cards.deleteRow") };
|
|
276
|
+
const options = steps.map((step) => ({ label: labels[step], danger: step === "delete" }));
|
|
277
|
+
return frame(
|
|
278
|
+
cutToWidth(lineOf(open.card), panel - 8),
|
|
279
|
+
html`
|
|
280
|
+
<${Box} flexDirection="column">
|
|
281
|
+
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
282
|
+
${open.card.ai && html`<${Box} marginTop=${1}><${Text} color=${theme.accent}>${t("cards.aiNote")}<//><//>`}
|
|
283
|
+
<${Box} marginTop=${1}><${Choice} options=${options} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (steps[i] === "confirm" ? confirm(open.card) : steps[i] === "check" ? check(open.card) : setOpen((o) => ({ ...o, step: steps[i], selected: steps[i] === "delete" ? 1 : 0 })))} onCancel=${() => setOpen(null)} /><//>
|
|
284
|
+
<//>
|
|
285
|
+
`,
|
|
286
|
+
t("keyHints.choiceLeft", { choose: t("choice.keys") })
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// the list, with the search on top
|
|
291
|
+
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), rows.length - SHOWN));
|
|
292
|
+
const gutter = (cards ?? []).some((card) => card.ai); // room for a ✦ before every card, so they line up
|
|
293
|
+
const rowText = (row) => (row.undo ? t("cards.undoRow", { card: cutToWidth(lineOf(row.undo === true ? undo.card : row), 40) }) : row.add ? t("cards.addRow") : lineOf(row));
|
|
294
|
+
return frame(
|
|
295
|
+
html`<${Fragment}>${t("cards.title", { deck, count: cards?.length ?? 0 })}${toCheck > 0 ? html`<${Text} bold=${false}> ${t("cards.toCheck", { count: toCheck })}<//>` : ""}<//>`,
|
|
296
|
+
html`
|
|
297
|
+
<${Box} flexDirection="column">
|
|
298
|
+
<${Box} marginTop=${1}>
|
|
299
|
+
<${Box} flexShrink=${0}><${Text} dimColor>${t("languages.search")} <//><//>
|
|
300
|
+
<${Text} wrap="truncate-start">${query}<//>
|
|
301
|
+
<${Box} flexShrink=${0}><${Text} inverse> <//><//>
|
|
302
|
+
<//>
|
|
303
|
+
${cards && query && !found.length && html`<${Text} dimColor wrap="truncate-end">${t("cards.noMatch", { query: cutToWidth(query, 20) })}<//>`}
|
|
304
|
+
<${Text} dimColor>${top > 0 ? " ↑" : " "}<//>
|
|
305
|
+
${rows.slice(top, top + SHOWN).map((row, i) => {
|
|
306
|
+
const on = top + i === selected;
|
|
307
|
+
return html`<${Text} key=${row.id ?? (row.undo ? "undo" : "add")} color=${on ? theme.accent : row.undo || row.add ? theme.good : undefined} bold=${on} wrap="truncate-end">${on ? "❯" : " "} ${row.ai ? html`<${Text} color=${theme.accent}>✦ <//>` : gutter && row.id ? " " : ""}${rowText(row)}<//>`;
|
|
308
|
+
})}
|
|
309
|
+
<${Text} dimColor>${top + SHOWN < rows.length ? " ↓" : " "}<//>
|
|
310
|
+
${query && found.length > 0 && html`<${Text} dimColor>${t("cards.found", { count: found.length })}<//>`}
|
|
311
|
+
<//>
|
|
312
|
+
`,
|
|
313
|
+
t("keyHints.cards")
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// the card as AI would fix it, what it changes in the accent colour
|
|
318
|
+
function Fixed({ card, fix }) {
|
|
319
|
+
const changed = (on) => (on ? theme.accent : undefined);
|
|
320
|
+
const parts = fix.back.map((answer, i) => html`<${Text} key=${i} color=${changed(!card.back.includes(answer))}>${i ? "; " : ""}${answer}<//>`);
|
|
321
|
+
return html`
|
|
322
|
+
<${Box} flexDirection="column">
|
|
323
|
+
<${Text} bold wrap="truncate-end"><${Text} color=${changed(fix.front !== card.front)}>${fix.front}<//> = ${parts}<//>
|
|
324
|
+
${fix.example && html`<${Text} italic color=${changed(fix.example !== (card.example ?? ""))} dimColor=${fix.example === (card.example ?? "")}>“${fix.example}”<//>`}
|
|
325
|
+
<//>
|
|
326
|
+
`;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// /cards while several decks are practised together: which deck's cards (`mix`: "all" or names)
|
|
330
|
+
export function CardsDeckPick({ actions, mix, current, onPick, onClose }) {
|
|
331
|
+
const panel = usePanelWidth();
|
|
332
|
+
const [decks, setDecks] = useState(null);
|
|
333
|
+
const [selected, setSelected] = useState(0);
|
|
334
|
+
useEffect(() => {
|
|
335
|
+
actions.ownDecks().then((own) => {
|
|
336
|
+
const list = mix === "all" ? own : own.filter((d) => mix.includes(d.name));
|
|
337
|
+
setDecks(list);
|
|
338
|
+
setSelected(Math.max(0, list.findIndex((d) => d.name === current)));
|
|
339
|
+
}, () => setDecks([]));
|
|
340
|
+
}, []);
|
|
341
|
+
return html`
|
|
342
|
+
<${Fragment}>
|
|
343
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
344
|
+
<${Text} bold color=${theme.accent}>${t("cards.whichDeck")}<//>
|
|
345
|
+
${decks && html`<${Box} marginTop=${1}><${Choice} options=${decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) }))} selected=${selected} onMove=${setSelected} onPick=${(i) => onPick(decks[i].name)} onCancel=${onClose} /><//>`}
|
|
346
|
+
<//>
|
|
347
|
+
<${Box} paddingX=${1}><${KeyHints} text=${choiceHints("back")} /><//>
|
|
348
|
+
<//>
|
|
349
|
+
`;
|
|
350
|
+
}
|
package/src/ui/Choice.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
1
2
|
import { Box, useInput } from "ink";
|
|
2
3
|
import { t } from "../i18n.js";
|
|
3
4
|
import { cellWidth, html, Text, theme, usePanelWidth } from "./kit.js";
|
|
4
5
|
|
|
5
6
|
// A question's answers as a list: ↑/↓ and Enter, a number, or an option's own keys ("t|y" in Polish).
|
|
6
7
|
// Each option: { label, note?, key?, danger? }; `danger` paints it red while it's chosen.
|
|
8
|
+
const SETTLE_MS = 300; // a pick right after the list appears is the second half of the Enter before it
|
|
9
|
+
|
|
7
10
|
export function Choice({ options, selected, onMove, onPick, onCancel }) {
|
|
11
|
+
const shownAt = useRef(Date.now());
|
|
8
12
|
useInput((input, key) => {
|
|
9
13
|
if (key.escape) return onCancel?.();
|
|
10
14
|
if (key.upArrow) return onMove(Math.max(0, selected - 1));
|
|
11
15
|
if (key.downArrow) return onMove(Math.min(options.length - 1, selected + 1));
|
|
16
|
+
if (Date.now() - shownAt.current < SETTLE_MS && (key.return || /^\d$/.test(input))) return;
|
|
12
17
|
if (key.return) return onPick(selected);
|
|
13
18
|
const index = options.findIndex((o, i) => input === String(i + 1) || (o.key && o.key.split("|").includes(input.toLowerCase())));
|
|
14
19
|
if (index >= 0) onPick(index);
|
|
@@ -24,7 +29,7 @@ export function Choice({ options, selected, onMove, onPick, onCancel }) {
|
|
|
24
29
|
const on = i === selected;
|
|
25
30
|
const color = on ? (o.danger ? theme.bad : theme.accent) : undefined;
|
|
26
31
|
return html`
|
|
27
|
-
<${Box} key=${o.label} flexDirection=${stacked ? "column" : "row"}>
|
|
32
|
+
<${Box} key=${`${i}:${o.label}`} flexDirection=${stacked ? "column" : "row"}>
|
|
28
33
|
<${Box} flexShrink=${0}><${Text} color=${color} bold=${on}>${on ? "❯" : " "} ${i + 1}. ${stacked ? o.label : o.label + " ".repeat(width - cellWidth(o.label))}<//><//>
|
|
29
34
|
${o.note && html`<${Box} marginLeft=${stacked ? 5 : 0}><${Text} dimColor>${o.note}<//><//>`}
|
|
30
35
|
<//>
|