dracosh 0.1.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.
- package/README.md +49 -22
- package/assets/sounds/badge-1.wav +0 -0
- package/assets/sounds/badge-2.wav +0 -0
- package/assets/sounds/badge-3.wav +0 -0
- package/assets/sounds/badge-4.wav +0 -0
- package/locales/de.json +1046 -0
- package/locales/en.json +1029 -0
- package/locales/fr.json +1094 -0
- package/locales/pl.json +1117 -0
- package/locales/uk.json +1118 -0
- package/package.json +2 -2
- package/src/alerts.js +7 -4
- package/src/backup.js +96 -0
- package/src/cardline.js +71 -0
- package/src/cli.js +106 -38
- package/src/commands.js +15 -13
- package/src/demo.js +11 -0
- package/src/diff.js +13 -7
- package/src/i18n.js +68 -0
- package/src/judge.js +51 -12
- package/src/library.js +154 -0
- package/src/lock.js +64 -0
- package/src/manage.js +165 -0
- package/src/pause.js +22 -0
- package/src/progress.js +202 -93
- package/src/quiet.js +4 -3
- package/src/scheduler.js +39 -30
- package/src/session.js +181 -33
- package/src/settings.js +15 -13
- package/src/starter.js +47 -0
- package/src/store.js +58 -17
- package/src/tips.js +17 -25
- package/src/ui/AddCard.js +496 -0
- package/src/ui/AnswerInput.js +28 -13
- package/src/ui/App.js +360 -89
- package/src/ui/BadgeUnlock.js +51 -0
- package/src/ui/Badges.js +79 -20
- package/src/ui/Choice.js +38 -0
- package/src/ui/Companion.js +24 -39
- package/src/ui/Confetti.js +2 -2
- package/src/ui/Decks.js +99 -0
- package/src/ui/DieRoll.js +104 -0
- package/src/ui/Evolution.js +11 -10
- package/src/ui/Hatch.js +9 -7
- package/src/ui/Header.js +62 -19
- package/src/ui/Help.js +74 -0
- package/src/ui/Mascot.js +89 -40
- package/src/ui/Missed.js +15 -13
- package/src/ui/Preview.js +123 -29
- package/src/ui/Reset.js +71 -0
- package/src/ui/Settings.js +34 -23
- package/src/ui/Stats.js +28 -24
- package/src/ui/Summary.js +18 -10
- package/src/ui/TheOne.js +122 -0
- package/src/ui/icons.js +156 -25
- package/src/ui/kit.js +87 -2
- package/src/vacation.js +9 -3
- package/src/words.js +22 -14
- package/assets/sounds/badge.wav +0 -0
- package/data/tips.json +0 -1362
- package/src/ui/Tip.js +0 -25
|
@@ -0,0 +1,496 @@
|
|
|
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 { AnswerInput } from "./AnswerInput.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";
|
|
10
|
+
|
|
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(" / ");
|
|
14
|
+
|
|
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
|
+
}
|
|
41
|
+
|
|
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
|
+
}
|
|
66
|
+
|
|
67
|
+
// /add: a small form inside the app. You pick one of your decks or start a new one (your first one
|
|
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).
|
|
71
|
+
export function AddCard({ current, actions, onDone, startNew = false }) {
|
|
72
|
+
const panel = usePanelWidth();
|
|
73
|
+
const [step, setStep] = useState("loading"); // loading | pick | deck | line | back | direction | review | fix | saving
|
|
74
|
+
const [decks, setDecks] = useState([]);
|
|
75
|
+
const [picked, setPicked] = useState(0);
|
|
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 }
|
|
89
|
+
const [message, setMessage] = useState("");
|
|
90
|
+
const [error, setError] = useState("");
|
|
91
|
+
const lookup = useRef(0);
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
actions.ownDecks().then(
|
|
95
|
+
(own) => {
|
|
96
|
+
setDecks(own);
|
|
97
|
+
setPicked(Math.max(0, own.findIndex((d) => d.name === current)));
|
|
98
|
+
setStep(own.length && !startNew ? "pick" : "deck");
|
|
99
|
+
},
|
|
100
|
+
(err) => setError(err.message)
|
|
101
|
+
);
|
|
102
|
+
}, []);
|
|
103
|
+
|
|
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) });
|
|
106
|
+
|
|
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;
|
|
127
|
+
try {
|
|
128
|
+
const { deck: created, removed } = await actions.startDeck({ name });
|
|
129
|
+
setDeck({ name: created.name, directions: created.directions, isNew: true });
|
|
130
|
+
setNewDeck({ name: created.name, removed });
|
|
131
|
+
setMessage(t("add.deckReady", { deck: created.name }));
|
|
132
|
+
clearField();
|
|
133
|
+
setStep("line");
|
|
134
|
+
} catch (err) {
|
|
135
|
+
setError(err.message);
|
|
136
|
+
} finally {
|
|
137
|
+
busy.current = false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
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;
|
|
157
|
+
try {
|
|
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");
|
|
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);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
setError(err.message);
|
|
176
|
+
} finally {
|
|
177
|
+
busy.current = false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function submit(text) {
|
|
182
|
+
const value = text.trim();
|
|
183
|
+
setError("");
|
|
184
|
+
setMessage("");
|
|
185
|
+
if (step === "deck") {
|
|
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 }));
|
|
188
|
+
return actions.existingDeck(value).then((existing) => {
|
|
189
|
+
if (existing) return setError(t("add.duplicateDeck", { deck: existing.name }));
|
|
190
|
+
startDeck(value);
|
|
191
|
+
}, (err) => setError(err.message));
|
|
192
|
+
}
|
|
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 });
|
|
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("");
|
|
229
|
+
if (step === "back") {
|
|
230
|
+
clearField(formatLine(draft));
|
|
231
|
+
setDraft(null);
|
|
232
|
+
return setStep("line");
|
|
233
|
+
}
|
|
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));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function pickDeck(index) {
|
|
254
|
+
if (index === decks.length) return setStep("deck");
|
|
255
|
+
setDeck({ name: decks[index].name, directions: decks[index].directions, isNew: false });
|
|
256
|
+
clearField();
|
|
257
|
+
setStep("line");
|
|
258
|
+
}
|
|
259
|
+
|
|
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
|
+
|
|
369
|
+
return html`
|
|
370
|
+
<${Fragment}>
|
|
371
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
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
|
+
`}
|
|
380
|
+
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
381
|
+
${step === "back" && draft && html`<${Box} marginTop=${1}><${Text} dimColor>${draft.front} =<//><//>`}
|
|
382
|
+
${fieldLabel &&
|
|
383
|
+
html`
|
|
384
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
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 })}<//><//>`}
|
|
401
|
+
<//>
|
|
402
|
+
`}
|
|
403
|
+
${step === "pick" &&
|
|
404
|
+
html`
|
|
405
|
+
<${Box} marginTop=${1}>
|
|
406
|
+
<${Choice}
|
|
407
|
+
options=${[...decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) })), { label: t("decks.newDeck") }]}
|
|
408
|
+
selected=${picked}
|
|
409
|
+
onMove=${setPicked}
|
|
410
|
+
onPick=${pickDeck}
|
|
411
|
+
onCancel=${finish}
|
|
412
|
+
/>
|
|
413
|
+
<//>
|
|
414
|
+
`}
|
|
415
|
+
${step === "direction" &&
|
|
416
|
+
sample &&
|
|
417
|
+
html`
|
|
418
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
419
|
+
<${Text} bold>${t("add.directionQuestion", { deck: deck.name })}<//>
|
|
420
|
+
<${Box} marginTop=${1}>
|
|
421
|
+
<${Choice}
|
|
422
|
+
options=${[
|
|
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") }
|
|
426
|
+
]}
|
|
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
|
+
}}
|
|
437
|
+
/>
|
|
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 })}<//><//>`}
|
|
469
|
+
<//>
|
|
470
|
+
`}
|
|
471
|
+
${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
|
|
472
|
+
<//>
|
|
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
|
+
<//>
|
|
494
|
+
<//>
|
|
495
|
+
`;
|
|
496
|
+
}
|
package/src/ui/AnswerInput.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { t } from "../i18n.js";
|
|
1
2
|
import { useState } from "react";
|
|
2
|
-
import { Box,
|
|
3
|
+
import { Box, useInput, usePaste } from "ink";
|
|
3
4
|
import { COMMANDS, matchCommands } from "../commands.js";
|
|
4
|
-
import { html, theme } from "./kit.js";
|
|
5
|
+
import { html, Text, theme } from "./kit.js";
|
|
5
6
|
|
|
6
7
|
function Palette({ matches, selected }) {
|
|
7
8
|
return html`
|
|
@@ -9,7 +10,7 @@ function Palette({ matches, selected }) {
|
|
|
9
10
|
${matches.map(
|
|
10
11
|
(command, i) => html`
|
|
11
12
|
<${Box} key=${command.name}>
|
|
12
|
-
<${Text} color=${i === selected ? theme.accent : undefined} bold=${i === selected} dimColor=${i !== selected}>${i === selected ? "❯" : " "} ${command.name.padEnd(12)}
|
|
13
|
+
<${Box} flexShrink=${0}><${Text} color=${i === selected ? theme.accent : undefined} bold=${i === selected} dimColor=${i !== selected}>${i === selected ? "❯" : " "} ${command.name.padEnd(12)}<//><//>
|
|
13
14
|
<${Text} dimColor>${command.hint}<//>
|
|
14
15
|
<//>
|
|
15
16
|
`
|
|
@@ -18,17 +19,21 @@ function Palette({ matches, selected }) {
|
|
|
18
19
|
`;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
// everything after the command name: "/
|
|
22
|
+
// everything after the command name: "/pause 30m" → "30m"
|
|
22
23
|
const args = (value) => value.trim().split(/\s+/).slice(1).join(" ");
|
|
23
24
|
|
|
24
25
|
// Answer line with a movable cursor (←/→, Ctrl+A/E). Typing "/" opens a command palette:
|
|
25
26
|
// ↑/↓ choose, Tab completes, Enter runs.
|
|
26
27
|
// `commandOnly` is the between-questions variant: it starts at "/" and Esc or erasing the "/" closes it.
|
|
27
|
-
|
|
28
|
+
// `plain` is a text field with no commands at all ("and/or" is just text there).
|
|
29
|
+
// `onChange` hears every edit; `onRecall` is ↑ on an empty plain field (/add brings back the last card);
|
|
30
|
+
// `onPaste` takes a paste of several lines (/add reviews them as cards), anything else is typed in.
|
|
31
|
+
export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, onChange, onRecall, onPaste, commands = COMMANDS, initial = "", commandOnly = false, plain = false }) {
|
|
28
32
|
const [value, setValue] = useState(initial);
|
|
29
33
|
const [cursor, setCursor] = useState(initial.length);
|
|
30
34
|
const [selected, setSelected] = useState(0);
|
|
31
|
-
const
|
|
35
|
+
const isCommand = !plain && value.startsWith("/");
|
|
36
|
+
const matches = isCommand ? matchCommands(value, commands) : [];
|
|
32
37
|
const pick = Math.min(selected, Math.max(matches.length - 1, 0));
|
|
33
38
|
|
|
34
39
|
const edit = (next, at = next.length) => {
|
|
@@ -36,17 +41,29 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, com
|
|
|
36
41
|
setCursor(Math.max(0, Math.min(at, next.length)));
|
|
37
42
|
setSelected(0);
|
|
38
43
|
onEdit?.();
|
|
44
|
+
onChange?.(next);
|
|
39
45
|
};
|
|
40
46
|
|
|
47
|
+
// pasted text arrives whole (bracketed paste); lines become spaces unless /add takes them as cards
|
|
48
|
+
const paste = (text) => {
|
|
49
|
+
const lines = /[\r\n]/.test(text) ? text.split(/\r\n|\r|\n/).filter((line) => line.trim()) : [text];
|
|
50
|
+
if (onPaste && lines.length > 1) return onPaste(lines);
|
|
51
|
+
const typed = lines.join(" ");
|
|
52
|
+
edit(value.slice(0, cursor) + typed + value.slice(cursor), cursor + typed.length);
|
|
53
|
+
};
|
|
54
|
+
usePaste(paste);
|
|
55
|
+
|
|
41
56
|
useInput((input, key) => {
|
|
42
57
|
if (key.escape) return commandOnly ? onCancel() : value ? edit("") : onExit();
|
|
43
58
|
if (key.return) {
|
|
44
|
-
|
|
59
|
+
// no command starts like this: on a card it's simply the answer (some answers begin with "/")
|
|
60
|
+
if (!isCommand || (!matches.length && !commandOnly)) return onSubmit(value);
|
|
45
61
|
const [token, ...rest] = value.trim().split(/\s+/);
|
|
46
62
|
edit("");
|
|
47
63
|
return onCommand(matches[pick]?.name ?? token, rest.join(" "));
|
|
48
64
|
}
|
|
49
65
|
if (key.tab) return matches.length ? edit(`${matches[pick].name}${args(value) ? ` ${args(value)}` : ""}`) : undefined;
|
|
66
|
+
if (key.upArrow && !value && onRecall) return onRecall();
|
|
50
67
|
if (key.upArrow) return setSelected(Math.max(0, pick - 1));
|
|
51
68
|
if (key.downArrow) return setSelected(Math.min(matches.length - 1, pick + 1));
|
|
52
69
|
if (key.leftArrow) return setCursor((c) => Math.max(0, c - 1));
|
|
@@ -60,10 +77,8 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, com
|
|
|
60
77
|
if (key.ctrl && input === "a") return setCursor(0);
|
|
61
78
|
if (key.ctrl && input === "e") return setCursor(value.length);
|
|
62
79
|
if (key.ctrl || key.meta || key.pageUp || key.pageDown) return;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
edit(value.slice(0, cursor) + typed + value.slice(cursor), cursor + typed.length);
|
|
66
|
-
}
|
|
80
|
+
// a terminal without bracketed paste sends a paste as typed text, line breaks and all
|
|
81
|
+
if (input) paste(input);
|
|
67
82
|
});
|
|
68
83
|
|
|
69
84
|
const before = value.slice(0, cursor);
|
|
@@ -72,11 +87,11 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, com
|
|
|
72
87
|
return html`
|
|
73
88
|
<${Box} flexDirection="column">
|
|
74
89
|
<${Box}>
|
|
75
|
-
<${Text} color=${theme.accent}>❯
|
|
90
|
+
<${Box} flexShrink=${0}><${Text} color=${theme.accent}>❯ <//><//>
|
|
76
91
|
<${Text}>${before}<${Text} inverse>${at}<//>${after}<//>
|
|
77
92
|
<//>
|
|
78
93
|
${matches.length > 0 && html`<${Palette} matches=${matches} selected=${pick} />`}
|
|
79
|
-
${
|
|
94
|
+
${isCommand && matches.length === 0 && html`<${Box} marginTop=${1}>${commandOnly ? html`<${Text} color=${theme.bad}>${t("input.unknownCommand")}<//>` : html`<${Text} dimColor>${t("input.notACommand")}<//>`}<//>`}
|
|
80
95
|
<//>
|
|
81
96
|
`;
|
|
82
97
|
}
|