dracosh 0.2.0 → 0.3.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.
Files changed (55) hide show
  1. package/README.md +16 -11
  2. package/locales/de.json +1046 -0
  3. package/locales/en.json +1029 -0
  4. package/locales/fr.json +1094 -0
  5. package/locales/pl.json +1117 -0
  6. package/locales/uk.json +1118 -0
  7. package/package.json +2 -2
  8. package/src/alerts.js +3 -1
  9. package/src/backup.js +2 -1
  10. package/src/cardline.js +71 -0
  11. package/src/cli.js +39 -52
  12. package/src/commands.js +14 -12
  13. package/src/demo.js +11 -0
  14. package/src/diff.js +13 -7
  15. package/src/i18n.js +68 -0
  16. package/src/judge.js +24 -6
  17. package/src/library.js +62 -35
  18. package/src/lock.js +2 -1
  19. package/src/manage.js +150 -60
  20. package/src/pause.js +1 -2
  21. package/src/progress.js +76 -59
  22. package/src/quiet.js +3 -2
  23. package/src/scheduler.js +3 -6
  24. package/src/session.js +69 -19
  25. package/src/settings.js +13 -6
  26. package/src/starter.js +36 -27
  27. package/src/store.js +9 -7
  28. package/src/tips.js +12 -3
  29. package/src/ui/AddCard.js +405 -81
  30. package/src/ui/AnswerInput.js +22 -10
  31. package/src/ui/App.js +181 -50
  32. package/src/ui/BadgeUnlock.js +4 -3
  33. package/src/ui/Badges.js +9 -8
  34. package/src/ui/Choice.js +14 -9
  35. package/src/ui/Companion.js +13 -13
  36. package/src/ui/Confetti.js +2 -2
  37. package/src/ui/Decks.js +58 -8
  38. package/src/ui/DieRoll.js +9 -9
  39. package/src/ui/Evolution.js +7 -7
  40. package/src/ui/Hatch.js +7 -6
  41. package/src/ui/Header.js +58 -16
  42. package/src/ui/Help.js +31 -25
  43. package/src/ui/Mascot.js +1 -2
  44. package/src/ui/Missed.js +13 -12
  45. package/src/ui/Preview.js +48 -29
  46. package/src/ui/Reset.js +13 -14
  47. package/src/ui/Settings.js +26 -19
  48. package/src/ui/Stats.js +24 -23
  49. package/src/ui/Summary.js +10 -9
  50. package/src/ui/TheOne.js +7 -6
  51. package/src/ui/icons.js +1 -2
  52. package/src/ui/kit.js +34 -6
  53. package/src/vacation.js +2 -1
  54. package/src/words.js +2 -1
  55. package/data/tips.json +0 -32
package/src/ui/AddCard.js CHANGED
@@ -1,38 +1,94 @@
1
- import { Fragment, useEffect, useState } from "react";
2
- import { Box, Text } from "ink";
3
- import { MAX_DECK_NAME } from "../library.js";
4
- import { SLASH_ANSWER, SLASH_FRONT } from "../manage.js";
1
+ import { Fragment, useEffect, useRef, useState } from "react";
2
+ import { Box, useInput } from "ink";
3
+ import { MAX_DECK_NAME, typedSides } from "../library.js";
4
+ import { blankGap, formatLine, gapsOf, parseBack, parseLine, plainFront } from "../cardline.js";
5
+ import { normalize } from "../judge.js";
5
6
  import { AnswerInput } from "./AnswerInput.js";
6
- import { Choice, CHOICE_KEYS } from "./Choice.js";
7
- import { html, KeyHints, theme, usePanelWidth } from "./kit.js";
7
+ import { Choice, choiceHints } from "./Choice.js";
8
+ import { t } from "../i18n.js";
9
+ import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
8
10
 
9
- const FIELDS = {
10
- deck: { label: "Name your deck", hint: "Polish, Biology, Capitals… whatever you want to learn" },
11
- front: { label: "Front", hint: "the word or question, e.g. kot" },
12
- back: { label: "Back", hint: "the answer; separate several with commas, e.g. cat, kitty" },
13
- example: { label: "Example", hint: "optional: a sentence that helps you remember. Enter skips it" }
14
- };
11
+ const SHOWN_CARDS = 5; // the cards added so far, newest last
12
+ const PASTE_ROWS = 8; // rows of a pasted list on screen at once
13
+ const answers = (back) => back.join(" / ");
15
14
 
16
- const splitAnswers = (text) => text.split(",").map((part) => part.trim()).filter(Boolean);
15
+ // How a card will be asked in a deck, for the line under the field: "kot → ? you type cat / kitty"
16
+ function askedAs(fields, directions) {
17
+ if (fields.cloze) return t("add.preview.cloze", { prompt: blankGap(fields.front, 0) + (fields.back.length ? ` (${answers(fields.back)})` : ""), count: gapsOf(fields.front).length });
18
+ const [front, back] = [fields.front, answers(fields.back)];
19
+ if (!directions) return t("add.preview.pair", { front, back });
20
+ if (directions === "backward") return t("add.preview.asks", { prompt: back, answer: front });
21
+ const forward = t("add.preview.asks", { prompt: front, answer: back });
22
+ return directions === "both" ? t("add.preview.both", { forward, backward: t("add.preview.asks", { prompt: back, answer: front }) }) : forward;
23
+ }
24
+
25
+ // what would have to be typed in the quiz starts with "/", which opens the commands there
26
+ function startsWithSlash(fields, directions) {
27
+ const slash = (text) => text.startsWith("/");
28
+ if (fields.cloze) return gapsOf(fields.front).some(slash);
29
+ return typedSides({ directions: directions ?? "forward" }).some((side) => [fields[side]].flat().some(slash));
30
+ }
31
+
32
+ // A pasted list, one card per line: a tab splits the lines when every one of them has one (Anki and
33
+ // Quizlet exports), " = " otherwise. Lines that can't be added as they are say why.
34
+ // A list written with dashes ("apple - jabłko") is taken as it is when most lines have one and none
35
+ // has " = "; each line splits on a tab, or on its first dash
36
+ const DASHES = [" - ", " – ", " — "];
37
+ function separatorFor(lines) {
38
+ const dashed = !lines.some((line) => line.includes(" = ")) && lines.filter((line) => DASHES.some((dash) => line.includes(dash))).length * 2 >= lines.length;
39
+ return (line) => (line.includes("\t") ? "\t" : dashed ? (DASHES.map((dash) => [dash, line.indexOf(dash)]).filter(([, at]) => at >= 0).sort((a, b) => a[1] - b[1])[0]?.[0] ?? " = ") : " = ");
40
+ }
17
41
 
18
- const NEW_DECK = "+ New deck";
42
+ function readPaste(lines, existing, directions) {
43
+ const separatorOf = separatorFor(lines);
44
+ return lines.map((line) => {
45
+ const separator = separatorOf(line);
46
+ const fields = parseLine(line, { separator });
47
+ const issue = issueOf(fields, directions, existing.has(normalize(fields.front)));
48
+ return { line: separator === " = " ? line.trim() : formatLine(fields), fields, issue, dropped: false };
49
+ });
50
+ }
51
+
52
+ const issueOf = (fields, directions, inDeck) =>
53
+ !fields.front ? "noFront" : !fields.cloze && !fields.back.length ? "noBack" : startsWithSlash(fields, directions) ? "slash" : inDeck ? "inDeck" : null;
54
+
55
+ // A line repeating an earlier one (kept, not dropped) waits; drop or fix either and the other is free
56
+ function withRepeats(items) {
57
+ const seen = new Set();
58
+ return items.map((item) => {
59
+ if (item.dropped || item.issue) return item;
60
+ const key = normalize(item.fields.front);
61
+ if (seen.has(key)) return { ...item, issue: "twice" };
62
+ seen.add(key);
63
+ return item;
64
+ });
65
+ }
19
66
 
20
67
  // /add: a small form inside the app. You pick one of your decks or start a new one (your first one
21
- // replaces the tour), then add cards one after another until Esc. `actions` reads and writes the library.
22
- // `startNew` skips the deck list and goes straight to a new deck ("+ New deck" in /decks).
68
+ // replaces the tour), then add cards one line each until Esc: "kot = cat; kitty // example". After a new
69
+ // deck's first card it asks which way the deck is asked. A pasted list opens a review first.
70
+ // `actions` reads and writes the library; `startNew` skips the deck list ("+ New deck" in /decks).
23
71
  export function AddCard({ current, actions, onDone, startNew = false }) {
24
72
  const panel = usePanelWidth();
25
- const [step, setStep] = useState("loading"); // loading | pick | deck | ways | front | back | example | saving
73
+ const [step, setStep] = useState("loading"); // loading | pick | deck | line | back | direction | review | fix | saving
26
74
  const [decks, setDecks] = useState([]);
27
75
  const [picked, setPicked] = useState(0);
28
- const [ways, setWays] = useState(0);
29
- const [bothWays, setBothWays] = useState(false); // the deck's front is an answer too
30
- const [deck, setDeck] = useState(null);
31
- const [newDeck, setNewDeck] = useState(null); // { name, removed } once a first deck was started here
32
- const [draft, setDraft] = useState({ front: "", back: [] });
76
+ const [deck, setDeck] = useState(null); // { name, directions, isNew }: isNew until its direction is picked
77
+ const [newDeck, setNewDeck] = useState(null); // { name, removed } once a deck was started here
78
+ const [added, setAdded] = useState([]); // cards added (or fixed) in this /add, oldest first
79
+ const stale = useRef([]); // progress keys of fixed cards that no longer exist
80
+ const [draft, setDraft] = useState(null); // a front waiting for its back (a line without " = ")
81
+ const [editing, setEditing] = useState(null); // the added card being fixed (↑)
82
+ const [field, setField] = useState({ key: 0, initial: "" }); // a new key clears the field
83
+ const [typed, setTyped] = useState(""); // what's in the field, for the preview
84
+ const [duplicate, setDuplicate] = useState(null);
85
+ const [direction, setDirection] = useState(0);
86
+ const [sample, setSample] = useState(null); // the card the direction question shows
87
+ const busy = useRef(false); // a save in flight: Enter again does nothing
88
+ const [pasted, setPasted] = useState(null); // { items, selected, confirm, fixing }
33
89
  const [message, setMessage] = useState("");
34
90
  const [error, setError] = useState("");
35
- const [added, setAdded] = useState(0);
91
+ const lookup = useRef(0);
36
92
 
37
93
  useEffect(() => {
38
94
  actions.ownDecks().then(
@@ -45,38 +101,81 @@ export function AddCard({ current, actions, onDone, startNew = false }) {
45
101
  );
46
102
  }, []);
47
103
 
48
- // `dropped`: a card was half written (its front typed) when Esc ended /add
49
- const finish = () => onDone({ deck: newDeck?.name ?? null, removed: newDeck?.removed ?? [], added, dropped: Boolean(draft.front) && (step === "back" || step === "example") });
104
+ // `dropped`: a card was half written (its front typed, or a paste not added) when /add ended
105
+ const finish = () => onDone({ deck: newDeck?.name ?? null, removed: newDeck?.removed ?? [], stale: stale.current, added: added.length, dropped: Boolean(draft || pasted) });
50
106
 
51
- async function startDeck(bothWays) {
52
- setStep("saving");
107
+ const clearField = (initial = "") => {
108
+ setField((f) => ({ key: f.key + 1, initial }));
109
+ setTyped(initial);
110
+ setDuplicate(null);
111
+ };
112
+
113
+ // the field changed: the preview follows, and a front already in the deck is flagged as you type
114
+ function onType(value) {
115
+ setTyped(value);
116
+ setError("");
117
+ if (step !== "line") return;
118
+ const { front } = parseLine(value);
119
+ const ask = ++lookup.current;
120
+ if (!front) return setDuplicate(null);
121
+ actions.existingCard(deck.name, front).then((card) => ask === lookup.current && setDuplicate(card && card.id !== editing?.id ? card : null), () => {});
122
+ }
123
+
124
+ async function startDeck(name) {
125
+ if (busy.current) return;
126
+ busy.current = true;
53
127
  try {
54
- const { deck: created, removed } = await actions.startDeck({ name: deck, bothWays });
55
- setDeck(created.name);
56
- setBothWays(bothWays);
128
+ const { deck: created, removed } = await actions.startDeck({ name });
129
+ setDeck({ name: created.name, directions: created.directions, isNew: true });
57
130
  setNewDeck({ name: created.name, removed });
58
- setMessage(`Your deck "${created.name}" is ready. Now its first card:`);
59
- setStep("front");
131
+ setMessage(t("add.deckReady", { deck: created.name }));
132
+ clearField();
133
+ setStep("line");
60
134
  } catch (err) {
61
135
  setError(err.message);
62
- setStep("deck");
136
+ } finally {
137
+ busy.current = false;
63
138
  }
64
139
  }
65
140
 
66
- async function save(example) {
67
- setStep("saving");
141
+ // after a new deck's first card (or first pasted cards): which way it's asked
142
+ const afterSave = (card) => {
143
+ clearField();
144
+ if (deck.isNew && !card.cloze) {
145
+ setDirection(0);
146
+ setSample(card);
147
+ picking.current = false;
148
+ typedAhead.current = "";
149
+ return setStep("direction");
150
+ }
151
+ setStep("line");
152
+ };
153
+
154
+ async function save(fields) {
155
+ if (busy.current) return;
156
+ busy.current = true;
68
157
  try {
69
- const { duplicate } = await actions.addCardTo(deck, { ...draft, example });
70
- if (duplicate) setError(`"${duplicate.front}" is already in ${deck}.`);
71
- else {
72
- setAdded((n) => n + 1);
73
- setMessage(`Added "${draft.front}" to ${deck}. Next card, or Esc to finish.`);
158
+ if (editing) {
159
+ const { card, staleKeys } = await actions.updateCardIn(deck.name, editing.id, fields);
160
+ stale.current.push(...staleKeys);
161
+ setAdded((list) => list.map((c) => (c.id === card.id ? { ...card, fixed: true } : c)));
162
+ setMessage(t("add.fixed", { front: plainFront(card.front) }));
163
+ setEditing(null);
164
+ setDraft(null);
165
+ clearField();
166
+ return setStep("line");
74
167
  }
168
+ const { card, duplicate: existing } = await actions.addCardTo(deck.name, fields);
169
+ if (existing) return setError(t("add.duplicateCard", { front: existing.front, deck: deck.name }));
170
+ setDraft(null);
171
+ setAdded((list) => [...list, card]);
172
+ setMessage("");
173
+ afterSave(card);
75
174
  } catch (err) {
76
175
  setError(err.message);
176
+ } finally {
177
+ busy.current = false;
77
178
  }
78
- setDraft({ front: "", back: [] });
79
- setStep("front");
80
179
  }
81
180
 
82
181
  function submit(text) {
@@ -84,61 +183,228 @@ export function AddCard({ current, actions, onDone, startNew = false }) {
84
183
  setError("");
85
184
  setMessage("");
86
185
  if (step === "deck") {
87
- if (!value) return setError("Your deck needs a name.");
88
- if (Array.from(value).length > MAX_DECK_NAME) return setError(`Keep the name to ${MAX_DECK_NAME} characters or fewer.`);
186
+ if (!value) return setError(t("add.needName"));
187
+ if (Array.from(value).length > MAX_DECK_NAME) return setError(t("add.nameTooLong", { max: MAX_DECK_NAME }));
89
188
  return actions.existingDeck(value).then((existing) => {
90
- if (existing) return setError(`There is already a deck called "${existing.name}".`);
91
- setDeck(value);
92
- setStep("ways");
189
+ if (existing) return setError(t("add.duplicateDeck", { deck: existing.name }));
190
+ startDeck(value);
93
191
  }, (err) => setError(err.message));
94
192
  }
95
- if (step === "front") {
96
- if (!value) return setError("A card needs a front.");
97
- if (bothWays && value.startsWith("/")) return setError(SLASH_FRONT);
98
- return actions.existingCard(deck, value).then((existing) => {
99
- if (existing) return setError(`"${existing.front}" is already in ${deck}.`);
100
- setDraft({ front: value, back: [] });
101
- setStep("back");
102
- }, (err) => setError(err.message));
193
+ if (step === "line") {
194
+ if (!value) return setError(t("add.needFront"));
195
+ const fields = parseLine(value);
196
+ if (!fields.front) return setError(t("add.needFront"));
197
+ if (duplicate) return; // the warning is already under the field
198
+ if (!fields.cloze && !fields.hasBack) {
199
+ setDraft(fields);
200
+ clearField();
201
+ return setStep("back");
202
+ }
203
+ if (!fields.cloze && !fields.back.length) return setError(t("add.needBack"));
204
+ return save(fields);
205
+ }
206
+ if (step === "back") {
207
+ const { back, example } = parseBack(value);
208
+ if (!back.length) return setError(t("add.needBack"));
209
+ return save({ ...draft, back, example: example || draft.example });
103
210
  }
211
+ if (step === "fix") {
212
+ const fields = parseLine(value);
213
+ return actions
214
+ .existingCard(deck.name, fields.front)
215
+ .catch(() => null)
216
+ .then((existing) => {
217
+ setPasted((p) => {
218
+ const issue = issueOf(fields, deck.isNew ? null : deck.directions, Boolean(existing));
219
+ return { ...p, items: p.items.map((item, i) => (i === p.selected ? { line: value, fields, issue, dropped: false } : item)), confirm: false };
220
+ });
221
+ setStep("review");
222
+ });
223
+ }
224
+ }
225
+
226
+ // Esc on an empty field: one step back at a time, and out of /add from the plain card line
227
+ function back() {
228
+ setError("");
104
229
  if (step === "back") {
105
- const answers = splitAnswers(value);
106
- if (!answers.length) return setError("A card needs an answer.");
107
- if (answers.some((a) => a.startsWith("/"))) return setError(SLASH_ANSWER);
108
- setDraft((d) => ({ ...d, back: answers }));
109
- return setStep("example");
230
+ clearField(formatLine(draft));
231
+ setDraft(null);
232
+ return setStep("line");
110
233
  }
111
- if (step === "example") save(value);
234
+ if (step === "fix") return setStep("review");
235
+ if (step === "deck" && decks.length && !startNew) return setStep("pick");
236
+ if (step === "line" && editing) {
237
+ setEditing(null);
238
+ setMessage("");
239
+ return clearField();
240
+ }
241
+ finish();
242
+ }
243
+
244
+ // ↑ on an empty line: the last card comes back into the field, and Enter saves it fixed
245
+ function recall() {
246
+ const last = added.at(-1);
247
+ if (!last || step !== "line") return;
248
+ setEditing(last);
249
+ setMessage(t("add.fixing", { front: plainFront(last.front) }));
250
+ clearField(formatLine(last));
112
251
  }
113
252
 
114
253
  function pickDeck(index) {
115
254
  if (index === decks.length) return setStep("deck");
116
- setDeck(decks[index].name);
117
- setBothWays(decks[index].bothWays);
118
- setStep("front");
255
+ setDeck({ name: decks[index].name, directions: decks[index].directions, isNew: false });
256
+ clearField();
257
+ setStep("line");
119
258
  }
120
259
 
121
- const title = step === "pick" ? "Add cards to…" : step === "deck" || step === "ways" ? (decks.length ? "New deck" : "Start your first deck") : `Add a card · ${deck}`;
122
- const field = FIELDS[step];
260
+ // Keys that aren't an answer to the question (the next card, typed straight away) keep the first
261
+ // choice and go into the field, so nothing typed is lost
262
+ // Typing the next card right away keeps option 1: the keys wait in `typedAhead` until the field is back.
263
+ // `picking` stays set for the rest of the step, so a key that lands while the pick saves can't pick again.
264
+ const typedAhead = useRef("");
265
+ const picking = useRef(false);
266
+ const pickedByChoice = useRef(false);
267
+ const shownAhead = useRef(""); // the typedAhead the field was drawn with // the key Choice just took (1-3, Enter, Esc) isn't the next card's text
268
+ async function pickDirection(index) {
269
+ if (picking.current) return;
270
+ picking.current = true;
271
+ const chosen = ["forward", "backward", "both"][index];
272
+ try {
273
+ await actions.setDirections(deck.name, chosen);
274
+ setDeck((d) => ({ ...d, directions: chosen, isNew: false }));
275
+ setMessage(t("add.directionSet", { how: askedAs(sample, chosen) }));
276
+ } catch (err) {
277
+ setError(err.message);
278
+ }
279
+ const lines = typedAhead.current.split(/\r\n|\r|\n/).filter((line) => line.trim());
280
+ if (lines.length > 1) return void openPaste(lines); // a list pasted in: the review, as on the field
281
+ // `ahead`: the field starts with typedAhead as it is when the field is drawn
282
+ setField((f) => ({ key: f.key + 1, initial: "", ahead: true }));
283
+ setDuplicate(null);
284
+ setStep("line");
285
+ }
286
+
287
+ // a key can still land in typedAhead after the field was drawn and before it listens: draw it again
288
+ useEffect(() => {
289
+ if (!field.ahead) return;
290
+ if (typedAhead.current !== shownAhead.current) return setField((f) => ({ ...f, key: f.key + 1 }));
291
+ setTyped(typedAhead.current);
292
+ }, [field]);
293
+
294
+ // a pasted list: each line checked against the deck before anything is saved
295
+ async function openPaste(lines) {
296
+ const separatorOf = separatorFor(lines);
297
+ const fronts = await Promise.all(lines.map((line) => actions.existingCard(deck.name, parseLine(line, { separator: separatorOf(line) }).front).catch(() => null)));
298
+ const existing = new Set(fronts.filter(Boolean).map((card) => normalize(card.front)));
299
+ setPasted({ items: readPaste(lines, existing, deck.isNew ? null : deck.directions), selected: 0, confirm: false, total: lines.length });
300
+ setError("");
301
+ setMessage("");
302
+ setStep("review");
303
+ }
304
+
305
+ async function addPasted() {
306
+ const ready = withRepeats(pasted.items).filter((item) => !item.dropped && !item.issue);
307
+ if (!ready.length) return setError(t("add.paste.nothing"));
308
+ setStep("saving");
309
+ try {
310
+ const { added: cards } = await actions.addCardsTo(deck.name, ready.map((item) => item.fields));
311
+ setAdded((list) => [...list, ...cards]);
312
+ setPasted(null);
313
+ const skipped = pasted.items.length - ready.length - pasted.items.filter((item) => item.dropped).length;
314
+ setMessage([t("add.paste.added", { count: cards.length, deck: deck.name }), skipped > 0 && t("add.paste.skipped", { count: skipped })].filter(Boolean).join(" "));
315
+ if (cards.length) return afterSave(cards.find((card) => !card.cloze) ?? cards[0]);
316
+ setStep("line");
317
+ } catch (err) {
318
+ setError(err.message);
319
+ setStep("review");
320
+ }
321
+ }
322
+
323
+ useInput(
324
+ (input, key) => {
325
+ if (pickedByChoice.current) return void (pickedByChoice.current = false);
326
+ if (picking.current && (key.backspace || key.delete)) return void (typedAhead.current = typedAhead.current.slice(0, -1));
327
+ if (!input || key.ctrl || key.meta || key.tab || key.escape || key.return) return;
328
+ if (!picking.current && /^[1-3]$/.test(input)) return; // Choice's own key
329
+ typedAhead.current += input;
330
+ pickDirection(0);
331
+ },
332
+ { isActive: step === "direction" }
333
+ );
334
+
335
+ useInput(
336
+ (input, key) => {
337
+ if (key.escape) {
338
+ if (pasted.confirm) {
339
+ setPasted(null);
340
+ setError("");
341
+ return setStep("line");
342
+ }
343
+ return setPasted((p) => ({ ...p, confirm: true }));
344
+ }
345
+ if (pasted.confirm) setPasted((p) => ({ ...p, confirm: false }));
346
+ if (key.upArrow) return setPasted((p) => ({ ...p, selected: Math.max(0, p.selected - 1) }));
347
+ if (key.downArrow) return setPasted((p) => ({ ...p, selected: Math.min(p.items.length - 1, p.selected + 1) }));
348
+ if (input === "x") return setPasted((p) => ({ ...p, items: p.items.map((item, i) => (i === p.selected ? { ...item, dropped: !item.dropped } : item)) }));
349
+ if (input === "e") {
350
+ clearField(pasted.items[pasted.selected].line);
351
+ return setStep("fix");
352
+ }
353
+ if (key.return) addPasted();
354
+ },
355
+ { isActive: step === "review" }
356
+ );
357
+
358
+ const title =
359
+ step === "pick" ? t("add.titlePick") : step === "deck" ? t(decks.length ? "add.titleNew" : "add.titleFirst") : step === "review" || step === "fix" ? t("add.paste.title", { deck: deck.name }) : t("add.titleCard", { deck: deck?.name ?? "" });
360
+ const fieldLabel = step === "deck" ? "deck" : step === "back" ? "back" : step === "line" || step === "fix" ? "line" : null;
361
+ const parsed = step === "line" && typed.trim() ? parseLine(typed) : null;
362
+ const preview = !parsed ? null : parsed.cloze || parsed.back.length ? askedAs(parsed, deck.isNew ? null : deck.directions) : parsed.hasBack ? null : t("add.preview.noBack");
363
+ const shownPaste = pasted ? withRepeats(pasted.items) : [];
364
+ const ready = shownPaste.filter((item) => !item.dropped && !item.issue).length;
365
+ const narrow = panel < 60;
366
+ const firstRow = pasted ? Math.max(0, Math.min(pasted.selected - Math.floor(PASTE_ROWS / 2), pasted.items.length - PASTE_ROWS)) : 0;
367
+ const label = (text) => cutToWidth(text, Math.max(12, Math.floor(panel / 2) - 8)); // a long front or definition shortened in the list
368
+
123
369
  return html`
124
370
  <${Fragment}>
125
371
  <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
126
372
  <${Text} bold color=${theme.accent}>${title}<//>
373
+ ${added.length > 0 && (step === "line" || step === "back" || step === "direction" || step === "saving") &&
374
+ html`
375
+ <${Box} flexDirection="column" marginTop=${1}>
376
+ ${added.length > SHOWN_CARDS && html`<${Text} dimColor>${t("add.more", { count: added.length - SHOWN_CARDS })}<//>`}
377
+ ${added.slice(-SHOWN_CARDS).map((card) => html`<${Text} key=${card.id} dimColor wrap="truncate-end"><${Text} color=${theme.good}>${card.fixed ? "✎" : "✓"}<//> ${card.cloze ? plainFront(card.front) : `${card.front} = ${answers(card.back)}`}<//>`)}
378
+ <//>
379
+ `}
127
380
  ${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
128
- ${draft.front && step !== "front" && html`<${Box} marginTop=${1}><${Text} dimColor>${draft.front}${draft.back.length ? ` → ${draft.back.join(", ")}` : ""}<//><//>`}
129
- ${field &&
381
+ ${step === "back" && draft && html`<${Box} marginTop=${1}><${Text} dimColor>${draft.front} =<//><//>`}
382
+ ${fieldLabel &&
130
383
  html`
131
384
  <${Box} flexDirection="column" marginTop=${1}>
132
- <${Text} bold>${field.label}<//>
133
- <${Text} dimColor>${field.hint}<//>
134
- <${Box} marginTop=${1}><${AnswerInput} key=${step} plain onSubmit=${submit} onExit=${finish} onEdit=${() => setError("")} /><//>
385
+ <${Text} bold>${t(`add.fields.${fieldLabel}.label`)}<//>
386
+ <${Text} dimColor>${t(`add.fields.${fieldLabel}.hint`)}<//>
387
+ <${Box} marginTop=${1}>
388
+ <${AnswerInput}
389
+ key=${`${fieldLabel}-${field.key}`}
390
+ plain
391
+ initial=${field.ahead ? (shownAhead.current = typedAhead.current) : field.initial}
392
+ onSubmit=${submit}
393
+ onExit=${back}
394
+ onChange=${onType}
395
+ onRecall=${step === "line" ? recall : undefined}
396
+ onPaste=${step === "line" ? openPaste : undefined}
397
+ />
398
+ <//>
399
+ ${preview && html`<${Box} paddingLeft=${2}><${Text} dimColor>${preview}<//><//>`}
400
+ ${duplicate && step === "line" && html`<${Box} paddingLeft=${2}><${Text} color=${theme.warn}>${t("add.duplicateCard", { front: duplicate.front, deck: deck.name })}<//><//>`}
135
401
  <//>
136
402
  `}
137
403
  ${step === "pick" &&
138
404
  html`
139
405
  <${Box} marginTop=${1}>
140
406
  <${Choice}
141
- options=${[...decks.map((d) => ({ label: d.name, note: `${d.cards} card${d.cards === 1 ? "" : "s"}` })), { label: NEW_DECK }]}
407
+ options=${[...decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) })), { label: t("decks.newDeck") }]}
142
408
  selected=${picked}
143
409
  onMove=${setPicked}
144
410
  onPick=${pickDeck}
@@ -146,27 +412,85 @@ export function AddCard({ current, actions, onDone, startNew = false }) {
146
412
  />
147
413
  <//>
148
414
  `}
149
- ${step === "ways" &&
415
+ ${step === "direction" &&
416
+ sample &&
150
417
  html`
151
418
  <${Box} flexDirection="column" marginTop=${1}>
152
- <${Text} bold>How should "${deck}" be asked?<//>
419
+ <${Text} bold>${t("add.directionQuestion", { deck: deck.name })}<//>
153
420
  <${Box} marginTop=${1}>
154
421
  <${Choice}
155
422
  options=${[
156
- { label: "Both ways", note: "for words: kot → cat, and cat → kot", key: "y" },
157
- { label: "One way only", note: "for questions: front → back", key: "n" }
423
+ { label: `${label(sample.front)} → ?`, note: t("add.youType", { answer: label(answers(sample.back)) }) },
424
+ { label: `${label(answers(sample.back))} → ?`, note: t("add.youType", { answer: label(sample.front) }) },
425
+ { label: t("add.bothWays"), note: t("add.bothWaysNote") }
158
426
  ]}
159
- selected=${ways}
160
- onMove=${setWays}
161
- onPick=${(i) => startDeck(i === 0)}
162
- onCancel=${finish}
427
+ selected=${direction}
428
+ onMove=${setDirection}
429
+ onPick=${(index) => {
430
+ if (!picking.current) pickedByChoice.current = true;
431
+ pickDirection(index);
432
+ }}
433
+ onCancel=${() => {
434
+ if (!picking.current) pickedByChoice.current = true;
435
+ pickDirection(0);
436
+ }}
163
437
  />
164
438
  <//>
439
+ <${Box} marginTop=${1}><${Text} dimColor>${t("add.directionLater")}<//><//>
440
+ <//>
441
+ `}
442
+ ${(step === "review" || step === "fix") &&
443
+ pasted &&
444
+ html`
445
+ <${Box} flexDirection="column" marginTop=${1}>
446
+ <${Text}>${t("add.paste.summary", { lines: pasted.total, count: ready })}${shownPaste.some((item) => item.issue && !item.dropped) ? t("add.paste.attention", { count: shownPaste.filter((item) => item.issue && !item.dropped).length }) : ""}<//>
447
+ <${Box} flexDirection="column" marginTop=${1}>
448
+ ${shownPaste.slice(firstRow, firstRow + PASTE_ROWS).map((item, i) => {
449
+ const index = firstRow + i;
450
+ const on = index === pasted.selected && step === "review";
451
+ const mark = item.dropped ? "✗" : item.issue ? "!" : "✓";
452
+ const color = item.dropped ? undefined : item.issue ? theme.warn : theme.good;
453
+ return html`
454
+ <${Box} key=${index} flexDirection=${narrow && item.issue && !item.dropped ? "column" : "row"}>
455
+ <${Box}>
456
+ <${Box} flexShrink=${0}>
457
+ <${Text} color=${on ? theme.accent : undefined}>${on ? "❯ " : " "}<//>
458
+ <${Text} color=${color} dimColor=${item.dropped}>${mark} <//>
459
+ <//>
460
+ <${Box} flexGrow=${1}><${Text} dimColor=${item.dropped} strikethrough=${item.dropped} wrap="truncate-end">${item.fields.cloze ? plainFront(item.fields.front) : item.fields.back.length ? `${item.fields.front} = ${answers(item.fields.back)}` : item.fields.front}<//><//>
461
+ <//>
462
+ ${item.issue && !item.dropped && html`<${Box} flexShrink=${0} marginLeft=${narrow ? 4 : 0}><${Text} color=${theme.warn}>${narrow ? "" : " "}${t(`add.paste.issues.${item.issue}`)}<//><//>`}
463
+ <//>
464
+ `;
465
+ })}
466
+ <//>
467
+ ${pasted.items.length > PASTE_ROWS && html`<${Text} dimColor>${t("add.paste.position", { at: pasted.selected + 1, count: pasted.items.length })}<//>`}
468
+ ${pasted.confirm && html`<${Box} marginTop=${1}><${Text} color=${theme.warn}>${t("add.paste.confirm", { count: ready })}<//><//>`}
165
469
  <//>
166
470
  `}
167
471
  ${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
168
472
  <//>
169
- <${Box} paddingX=${1}><${KeyHints} text=${step === "pick" || step === "ways" ? `${CHOICE_KEYS} · esc cancel` : "enter next · esc " + (added || newDeck ? "done" : "cancel")} /><//>
473
+ <${Box} paddingX=${1}>
474
+ <${KeyHints}
475
+ text=${step === "direction"
476
+ ? t("keyHints.directionPick", { choose: t("choice.keys") })
477
+ : step === "pick"
478
+ ? choiceHints("cancel")
479
+ : step === "deck"
480
+ ? t("keyHints.addName")
481
+ : step === "review"
482
+ ? t("keyHints.pasteReview", { count: ready })
483
+ : step === "fix"
484
+ ? t("keyHints.pasteFix")
485
+ : step === "back"
486
+ ? t("keyHints.addBack")
487
+ : step === "line" && editing
488
+ ? t("keyHints.addFixing")
489
+ : step === "line" && added.length
490
+ ? t("keyHints.addMore")
491
+ : t("keyHints.addFirst")}
492
+ />
493
+ <//>
170
494
  <//>
171
495
  `;
172
496
  }