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