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/library.js CHANGED
@@ -1,5 +1,7 @@
1
+ import { t } from "./i18n.js";
1
2
  import { randomBytes } from "node:crypto";
2
3
  import { normalize } from "./judge.js";
4
+ import { gapsOf } from "./cardline.js";
3
5
 
4
6
  // The card library: decks and their cards. Pure functions over a plain object; saving is the caller's job.
5
7
  //
@@ -7,11 +9,18 @@ import { normalize } from "./judge.js";
7
9
  // note id as their id, so progress recorded by 0.1.0 (keyed by note id) carries over unchanged.
8
10
  // New cards get a "c"-prefixed id, which can never collide with Anki's numeric ids.
9
11
 
10
- export const LIBRARY_VERSION = 1;
12
+ // 2: cloze cards, whose back may be empty (an older Dracosh would call such a library damaged)
13
+ export const LIBRARY_VERSION = 2;
11
14
 
15
+ // Older libraries carry a card type on each deck; every card is front = back now, so it's only kept.
12
16
  export const CARD_TYPES = ["translation", "definition", "qa"];
13
- export const ANSWER_MODES = ["typed", "self"];
14
- const DEFAULT_ANSWER_MODE = { translation: "typed", definition: "typed", qa: "self" };
17
+
18
+ // How a deck is asked: front → back, back → front, or both. Picked after its first card.
19
+ export const DIRECTIONS = ["forward", "backward", "both"];
20
+
21
+ // the sides of a card typed as an answer in the quiz: in the quiz "/" opens the commands, so neither
22
+ // may start with it. A cloze card is asked by its gaps, whatever the deck's direction.
23
+ export const typedSides = (deck) => (deck.directions === "forward" ? ["back"] : deck.directions === "backward" ? ["front"] : ["front", "back"]);
15
24
 
16
25
  export const MAX_DECK_NAME = 40; // it has to fit the card's border and the header
17
26
 
@@ -30,45 +39,55 @@ const isText = (value) => typeof value === "string";
30
39
  const filled = (value) => isText(value) && value.trim() !== "";
31
40
  const validLanguages = (languages) => languages == null || (typeof languages === "object" && filled(languages.front) && filled(languages.back));
32
41
  const validDeck = (deck) => deck && isText(deck.id) && filled(deck.name) && validLanguages(deck.languages);
33
- const validCard = (card) => card && isText(card.id) && isText(card.deckId) && filled(card.front) && Array.isArray(card.back) && card.back.length > 0 && card.back.every(filled);
42
+ // a cloze card's answers are its gaps, so its back (shown as a hint) may be empty
43
+ const validCard = (card) =>
44
+ card && isText(card.id) && isText(card.deckId) && filled(card.front) && Array.isArray(card.back) && (card.cloze ? gapsOf(card.front).length > 0 : card.back.length > 0) && card.back.every(filled);
34
45
  const broken = (why) => Object.assign(new Error(why), { damaged: true });
35
46
 
36
47
  // → the library in its current shape (undefined: there's none yet). Throws on a file written by a newer
37
48
  // Dracosh, so it's never overwritten, and on one that isn't a library, so nothing is lost by saving over it.
38
49
  export function upgradeLibrary(raw) {
39
50
  if (raw === undefined) return emptyLibrary();
40
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw broken("not a library");
51
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw broken(t("errors.why.notLibrary"));
41
52
  if (raw.version > LIBRARY_VERSION) {
42
- throw new Error(`This library was saved by a newer version of Dracosh (format ${raw.version}). Please update Dracosh.`);
53
+ throw new Error(t("errors.newerLibrary", { format: raw.version }));
43
54
  }
44
55
  const decks = raw.decks ?? [];
45
56
  const cards = raw.cards ?? [];
46
- if (!Array.isArray(decks) || !decks.every(validDeck)) throw broken("a deck without a name or an id");
47
- if (!Array.isArray(cards) || !cards.every(validCard)) throw broken("a card without a front or an answer");
57
+ if (!Array.isArray(decks) || !decks.every(validDeck)) throw broken(t("errors.why.deck"));
58
+ if (!Array.isArray(cards) || !cards.every(validCard)) throw broken(t("errors.why.card"));
48
59
  const deckIds = new Set(decks.map((deck) => deck.id));
49
- if (!cards.every((card) => deckIds.has(card.deckId))) throw broken("a card in a deck that doesn't exist");
50
- if (new Set(cards.map((card) => card.id)).size !== cards.length) throw broken("two cards with the same id");
60
+ if (!cards.every((card) => deckIds.has(card.deckId))) throw broken(t("errors.why.cardDeck"));
61
+ if (new Set(cards.map((card) => card.id)).size !== cards.length) throw broken(t("errors.why.cardIds"));
51
62
  return { version: LIBRARY_VERSION, decks, cards };
52
63
  }
53
64
 
54
65
  export const findDeck = (library, nameOrId) =>
55
66
  library.decks.find((deck) => deck.id === nameOrId) ?? library.decks.find((deck) => sameName(deck.name, nameOrId)) ?? null;
56
67
 
57
- export function createDeck(library, { name, type = "translation", languages = { front: "en", back: "pl" }, answerMode, directions = "both", tour = false, now = Date.now() }) {
68
+ export function createDeck(library, { name, type = "translation", languages = { front: "en", back: "pl" }, directions = "both", tour = false, now = Date.now() }) {
58
69
  const trimmed = String(name ?? "").trim();
59
- if (!trimmed) throw new Error("A deck needs a name.");
60
- if (Array.from(trimmed).length > MAX_DECK_NAME) throw new Error(`Keep the deck name to ${MAX_DECK_NAME} characters or fewer.`);
70
+ if (!trimmed) throw new Error(t("errors.deckNeedsName"));
71
+ if (Array.from(trimmed).length > MAX_DECK_NAME) throw new Error(t("errors.deckNameTooLong", { max: MAX_DECK_NAME }));
61
72
  const existing = findDeck(library, trimmed);
62
- if (existing) throw new Error(`There is already a deck called "${existing.name}".`);
73
+ if (existing) throw new Error(t("errors.deckExists", { deck: existing.name }));
63
74
  if (!CARD_TYPES.includes(type)) throw new Error(`Unknown card type "${type}".`);
64
- const mode = answerMode ?? DEFAULT_ANSWER_MODE[type];
65
- if (!ANSWER_MODES.includes(mode)) throw new Error(`Unknown answer mode "${mode}".`);
75
+ if (!DIRECTIONS.includes(directions)) throw new Error(`Unknown direction "${directions}".`);
66
76
 
67
- const deck = { id: newId("d"), name: trimmed, type, languages: languages ? { ...languages } : null, answerMode: mode, directions, tour, created: now };
77
+ const deck = { id: newId("d"), name: trimmed, type, languages: languages ? { ...languages } : null, directions, tour, created: now };
68
78
  library.decks.push(deck);
69
79
  return deck;
70
80
  }
71
81
 
82
+ // Progress is kept per direction, so turning one off only stops asking it: its boxes stay for later.
83
+ export function setDirections(library, deckId, directions) {
84
+ if (!DIRECTIONS.includes(directions)) throw new Error(`Unknown direction "${directions}".`);
85
+ const deck = library.decks.find((d) => d.id === deckId);
86
+ if (!deck) throw new Error("That deck doesn't exist.");
87
+ deck.directions = directions;
88
+ return deck;
89
+ }
90
+
72
91
  // → the ids of the cards that went with it, so their progress can be dropped too
73
92
  export function removeDeck(library, deckId) {
74
93
  const removed = cardsOf(library, deckId).map((card) => card.id);
@@ -86,17 +105,19 @@ export const findDuplicate = (library, deckId, front) => {
86
105
  };
87
106
 
88
107
  // → { card, duplicate }: `duplicate` is the existing card when the front is already in the deck (nothing is added then)
89
- export function addCard(library, deckId, { front, back, example = "", tags = [], id, now = Date.now() }) {
108
+ // `cloze`: the front has {{gaps}}, each asked as a card of its own; the back is then only a hint
109
+ export function addCard(library, deckId, { front, back, example = "", tags = [], cloze = false, id, now = Date.now() }) {
90
110
  if (!library.decks.some((deck) => deck.id === deckId)) throw new Error("That deck doesn't exist.");
91
111
  const cleanFront = String(front ?? "").trim();
92
112
  const cleanBack = cleanList(back);
93
- if (!cleanFront) throw new Error("A card needs a front.");
94
- if (!cleanBack.length) throw new Error("A card needs at least one answer on the back.");
113
+ const isCloze = Boolean(cloze) && gapsOf(cleanFront).length > 0;
114
+ if (!cleanFront) throw new Error(t("errors.cardNeedsFront"));
115
+ if (!cleanBack.length && !isCloze) throw new Error(t("errors.cardNeedsBack"));
95
116
 
96
117
  const duplicate = findDuplicate(library, deckId, cleanFront);
97
118
  if (duplicate) return { card: null, duplicate };
98
119
 
99
- const card = { id: id ? String(id) : newId("c"), deckId, front: cleanFront, back: cleanBack, example: String(example).trim(), tags: cleanList(tags), created: now, updated: now };
120
+ const card = { id: id ? String(id) : newId("c"), deckId, front: cleanFront, back: cleanBack, example: String(example).trim(), tags: cleanList(tags), ...(isCloze && { cloze: true }), created: now, updated: now };
100
121
  if (library.cards.some((c) => c.id === card.id)) throw new Error(`A card with id ${card.id} already exists.`);
101
122
  library.cards.push(card);
102
123
  return { card, duplicate: null };
@@ -105,23 +126,29 @@ export function addCard(library, deckId, { front, back, example = "", tags = [],
105
126
  export function updateCard(library, cardId, patch, now = Date.now()) {
106
127
  const card = library.cards.find((c) => c.id === cardId);
107
128
  if (!card) throw new Error("That card doesn't exist.");
108
- if (patch.front !== undefined) {
109
- const front = String(patch.front).trim();
110
- if (!front) throw new Error("A card needs a front.");
111
- const duplicate = findDuplicate(library, card.deckId, front);
112
- if (duplicate && duplicate.id !== card.id) throw new Error(`"${front}" is already in this deck.`);
113
- card.front = front;
114
- }
115
- if (patch.back !== undefined) {
116
- const back = cleanList(patch.back);
117
- if (!back.length) throw new Error("A card needs at least one answer on the back.");
118
- card.back = back;
119
- }
129
+ const front = patch.front !== undefined ? String(patch.front).trim() : card.front;
130
+ const back = patch.back !== undefined ? cleanList(patch.back) : card.back;
131
+ const cloze = (patch.cloze !== undefined ? Boolean(patch.cloze) : Boolean(card.cloze)) && gapsOf(front).length > 0;
132
+ if (!front) throw new Error(t("errors.cardNeedsFront"));
133
+ if (!back.length && !cloze) throw new Error(t("errors.cardNeedsBack"));
134
+ const duplicate = findDuplicate(library, card.deckId, front);
135
+ if (duplicate && duplicate.id !== card.id) throw new Error(t("errors.frontExists", { front }));
136
+ Object.assign(card, { front, back });
137
+ if (cloze) card.cloze = true;
138
+ else delete card.cloze;
120
139
  if (patch.example !== undefined) card.example = String(patch.example).trim();
121
140
  if (patch.tags !== undefined) card.tags = cleanList(patch.tags);
122
141
  card.updated = now;
123
142
  return card;
124
143
  }
125
144
 
126
- // The quiz's word shape, so the scheduler and session keep working unchanged.
127
- export const toWords = (cards) => cards.map((card) => ({ noteId: card.id, word: card.front, translations: card.back, example: card.example }));
145
+ // The quiz's word shape, so the scheduler and session keep working unchanged. A cloze card is asked one
146
+ // gap at a time ("c1", "c2"…), whatever the deck's direction.
147
+ export const toWords = (cards) =>
148
+ cards.map((card) => ({
149
+ noteId: card.id,
150
+ word: card.front,
151
+ translations: card.back,
152
+ example: card.example,
153
+ ...(card.cloze && { cloze: gapsOf(card.front), directions: gapsOf(card.front).map((_, i) => `c${i + 1}`) })
154
+ }));
package/src/lock.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { t } from "./i18n.js";
1
2
  import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
3
  import { join } from "node:path";
3
4
  import { HOME } from "./store.js";
@@ -29,7 +30,7 @@ export function runningPid() {
29
30
  return pid && pid !== process.pid && alive(pid) ? pid : null; // a lock left by a crash doesn't count
30
31
  }
31
32
 
32
- export const ALREADY_RUNNING = (pid) => `Dracosh is already running in another window (process ${pid}). Close it first: two windows would overwrite each other's progress.`;
33
+ export const ALREADY_RUNNING = (pid) => t("errors.alreadyRunning", { pid });
33
34
 
34
35
  // → a release function; throws when another quiz holds the lock. The lock file is created exclusively
35
36
  // ("wx"), so of two windows started at the same moment only one gets it; a stale one is replaced once.
package/src/manage.js CHANGED
@@ -1,75 +1,165 @@
1
+ import { t } from "./i18n.js";
1
2
  import { backupNow } from "./backup.js";
2
3
  import { seedLibrary } from "./starter.js";
3
- import { addCard, cardsOf, createDeck, emptyLibrary, findDeck, findDuplicate, removeDeck } from "./library.js";
4
+ import { gapsOf } from "./cardline.js";
5
+ import { addCard, cardsOf, createDeck, emptyLibrary, findDeck, findDuplicate, removeDeck, setDirections as setLibraryDirections, typedSides, updateCard } from "./library.js";
6
+ import { itemKey } from "./scheduler.js";
4
7
  import { loadLibrary, saveLibrary } from "./store.js";
5
8
 
6
- // Library changes made from inside the app (/add for now). Each one reads the library fresh, so
7
- // nothing written meanwhile by another Dracosh window is lost.
9
+ export const slashAnswer = () => t("errors.slashAnswer");
10
+ export const slashFront = () => t("errors.slashFront");
8
11
 
9
- // every deck, the tour too, with its card count: /decks switches between them
10
- export async function allDecks() {
11
- const library = await loadLibrary();
12
- return library.decks.map((deck) => ({ name: deck.name, cards: cardsOf(library, deck.id).length, tour: Boolean(deck.tour) }));
13
- }
14
-
15
- // your decks (the tour isn't one), with how many cards each holds: /add lets you pick one
16
- export async function ownDecks() {
17
- const library = await loadLibrary();
18
- return library.decks.filter((deck) => !deck.tour).map((deck) => ({ name: deck.name, cards: cardsOf(library, deck.id).length, bothWays: deck.directions !== "forward" }));
12
+ // in the quiz "/" opens the commands, so nothing you'd have to type may start with it
13
+ const slash = (text) => String(text ?? "").trim().startsWith("/");
14
+ function checkSlashes(deck, fields) {
15
+ if (fields.cloze) {
16
+ if (gapsOf(fields.front).some(slash)) throw new Error(slashAnswer());
17
+ return;
18
+ }
19
+ const typed = typedSides(deck);
20
+ if (typed.includes("back") && [fields.back].flat().some(slash)) throw new Error(slashAnswer());
21
+ if (typed.includes("front") && slash(fields.front)) throw new Error(slashFront());
19
22
  }
20
23
 
21
- // A new deck. Your first one replaces the tour: it's removed (after a backup) and the ids of its cards are
22
- // returned, so their progress can go too.
23
- export async function startDeck({ name, bothWays }) {
24
- const library = await loadLibrary();
25
- const tour = library.decks.find((d) => d.tour);
26
- let removed = [];
27
- if (tour) {
28
- await backupNow("before-delete");
29
- removed = removeDeck(library, tour.id);
24
+ // Library changes made from inside the app (/add, /decks). Each one reads the library fresh, so nothing
25
+ // written meanwhile by another Dracosh window is lost. `load`, `save` and `backup` are the store's; the
26
+ // preview gallery passes its own, so nothing it does is saved.
27
+ export function libraryActions({ load = loadLibrary, save = saveLibrary, backup = backupNow } = {}) {
28
+ async function deckNamed(library, deckName) {
29
+ const deck = findDeck(library, deckName);
30
+ if (!deck) throw new Error(t("errors.deckGone", { deck: deckName }));
31
+ return deck;
30
32
  }
31
- const deck = createDeck(library, { name, type: "translation", languages: null, directions: bothWays ? "both" : "forward" });
32
- await saveLibrary(library);
33
- return { deck, removed };
34
- }
35
33
 
36
- export const SLASH_ANSWER = "An answer can't start with /: in the quiz that opens the commands.";
37
- export const SLASH_FRONT = "In a deck asked both ways the front is an answer too, so it can't start with /.";
34
+ return {
35
+ // every deck, the tour too, with its card count: /decks switches between them
36
+ async allDecks() {
37
+ const library = await load();
38
+ return library.decks.map((deck) => {
39
+ const cards = cardsOf(library, deck.id);
40
+ const sample = cards.find((card) => !card.cloze); // shows the deck's directions in /decks
41
+ return { name: deck.name, cards: cards.length, tour: Boolean(deck.tour), directions: deck.directions, sample: sample ? { front: sample.front, back: sample.back } : null };
42
+ });
43
+ },
38
44
 
39
- // the deck with this name (ignoring case), if any: /add says so before asking anything else
40
- export const existingDeck = async (name) => {
41
- const deck = findDeck(await loadLibrary(), name.trim());
42
- return deck && !deck.tour ? deck : null; // the tour is about to make room, so its name is free
43
- };
45
+ // your decks (the tour isn't one), with how many cards each holds: /add lets you pick one
46
+ async ownDecks() {
47
+ const library = await load();
48
+ return library.decks.filter((deck) => !deck.tour).map((deck) => ({ name: deck.name, cards: cardsOf(library, deck.id).length, directions: deck.directions }));
49
+ },
44
50
 
45
- // the card with this front already in the deck, if any: /add says so before asking for the answer
46
- export async function existingCard(deckName, front) {
47
- const library = await loadLibrary();
48
- const deck = findDeck(library, deckName);
49
- return deck ? findDuplicate(library, deck.id, front) : null;
50
- }
51
+ // A new deck, asked front → back until its first card says otherwise. Your first one replaces the
52
+ // tour: it's removed (after a backup) and the ids of its cards are returned, so their progress can go too.
53
+ async startDeck({ name }) {
54
+ const library = await load();
55
+ const tour = library.decks.find((d) => d.tour);
56
+ let removed = [];
57
+ if (tour) {
58
+ await backup("before-delete");
59
+ removed = removeDeck(library, tour.id);
60
+ }
61
+ const deck = createDeck(library, { name, languages: null, directions: "forward" });
62
+ await save(library);
63
+ return { deck, removed };
64
+ },
51
65
 
52
- // a reset is always preceded by a backup, so `dracosh restore` can undo it
53
- export const backupBeforeReset = () => backupNow("before-reset");
66
+ // → { deck, cardIds }: the session rests the directions no longer asked (session.setDirections).
67
+ // A side that becomes an answer to type may not start with "/", so such a switch is refused.
68
+ async setDirections(deckName, directions) {
69
+ const library = await load();
70
+ const found = await deckNamed(library, deckName);
71
+ const cards = cardsOf(library, found.id);
72
+ const typed = typedSides({ directions });
73
+ const blocked = cards.find((card) => !card.cloze && typed.some((side) => [card[side]].flat().some(slash)));
74
+ if (blocked) throw new Error(t("errors.slashSwitch", { text: [blocked.front, ...blocked.back].find(slash) }));
75
+ const deck = setLibraryDirections(library, found.id, directions);
76
+ await save(library);
77
+ return { deck, cardIds: cards.map((card) => card.id) };
78
+ },
54
79
 
55
- // "Everything" in the reset: your decks and cards go, and the tour comes back as on a fresh install
56
- export async function resetLibrary() {
57
- const library = emptyLibrary();
58
- seedLibrary(library);
59
- await saveLibrary(library);
60
- return library.decks[0].name;
61
- }
80
+ // the deck with this name (ignoring case), if any: /add says so before asking anything else
81
+ async existingDeck(name) {
82
+ const deck = findDeck(await load(), name.trim());
83
+ return deck && !deck.tour ? deck : null; // the tour is about to make room, so its name is free
84
+ },
85
+
86
+ // the card with this front already in the deck, if any: /add says so as you type
87
+ async existingCard(deckName, front) {
88
+ const library = await load();
89
+ const deck = findDeck(library, deckName);
90
+ return deck ? findDuplicate(library, deck.id, front) : null;
91
+ },
62
92
 
63
- // → { card, duplicate }, like addCard
64
- export async function addCardTo(deckName, fields) {
65
- const library = await loadLibrary();
66
- const deck = findDeck(library, deckName);
67
- if (!deck) throw new Error(`The deck "${deckName}" is gone.`);
68
- // in the quiz "/" opens the commands, so nothing you'd have to type may start with it
69
- const slash = (text) => String(text ?? "").trim().startsWith("/");
70
- if ([fields.back].flat().some(slash)) throw new Error(SLASH_ANSWER);
71
- if (deck.directions !== "forward" && slash(fields.front)) throw new Error(SLASH_FRONT);
72
- const result = addCard(library, deck.id, fields);
73
- if (result.card) await saveLibrary(library);
74
- return result;
93
+ // → { card, duplicate }, like addCard
94
+ async addCardTo(deckName, fields) {
95
+ const library = await load();
96
+ const deck = await deckNamed(library, deckName);
97
+ checkSlashes(deck, fields);
98
+ const result = addCard(library, deck.id, fields);
99
+ if (result.card) await save(library);
100
+ return result;
101
+ },
102
+
103
+ // Several cards at once (a pasted list), saved together → { added, duplicates }. Cards that can't be
104
+ // added (a duplicate, a "/" answer) are skipped; the paste review has flagged them already.
105
+ async addCardsTo(deckName, list) {
106
+ const library = await load();
107
+ const deck = await deckNamed(library, deckName);
108
+ const added = [];
109
+ const duplicates = [];
110
+ for (const fields of list) {
111
+ try {
112
+ checkSlashes(deck, fields);
113
+ const { card, duplicate } = addCard(library, deck.id, fields);
114
+ if (card) added.push(card);
115
+ else duplicates.push(duplicate);
116
+ } catch {
117
+ duplicates.push(null);
118
+ }
119
+ }
120
+ if (added.length) await save(library);
121
+ return { added, duplicates };
122
+ },
123
+
124
+ // A card fixed right after adding it (↑ in /add) → { card, staleKeys }: the progress keys it no
125
+ // longer has (a gap removed, a cloze card turned plain or the other way round), for the session to drop
126
+ async updateCardIn(deckName, cardId, fields) {
127
+ const library = await load();
128
+ const deck = await deckNamed(library, deckName);
129
+ checkSlashes(deck, fields);
130
+ const keysOf = (card) => (card.cloze ? gapsOf(card.front).map((_, i) => `c${i + 1}`) : ["en-pl", "pl-en"]).map((direction) => itemKey(card.id, direction));
131
+ const before = library.cards.find((c) => c.id === cardId);
132
+ const oldKeys = before ? keysOf(before) : [];
133
+ const card = updateCard(library, cardId, fields);
134
+ await save(library);
135
+ const kept = new Set(keysOf(card));
136
+ return { card, staleKeys: oldKeys.filter((key) => !kept.has(key)) };
137
+ },
138
+
139
+ // The app's language changed: the tour follows it. → { from, to }, its old and new name, or null
140
+ // when there is no tour or nothing changed.
141
+ async matchTourLanguage() {
142
+ const library = await load();
143
+ const tour = library.decks.find((d) => d.tour);
144
+ const from = tour?.name;
145
+ if (!tour || !seedLibrary(library)) return null;
146
+ await save(library);
147
+ return { from, to: library.decks.find((d) => d.tour).name };
148
+ },
149
+
150
+ // a reset is always preceded by a backup, so `dracosh restore` can undo it
151
+ backupBeforeReset: () => backup("before-reset"),
152
+
153
+ // "Everything" in the reset: your decks and cards go, and the tour comes back as on a fresh install
154
+ async resetLibrary() {
155
+ const library = emptyLibrary();
156
+ seedLibrary(library);
157
+ await save(library);
158
+ return library.decks[0].name;
159
+ }
160
+ };
75
161
  }
162
+
163
+ // the app's own: reads and writes ~/.dracosh
164
+ const actions = libraryActions();
165
+ export const { allDecks, ownDecks, startDeck, setDirections, existingDeck, existingCard, addCardTo, addCardsTo, updateCardIn, matchTourLanguage, backupBeforeReset, resetLibrary } = actions;
package/src/pause.js CHANGED
@@ -1,8 +1,7 @@
1
+ import { t } from "./i18n.js";
1
2
  import { parseSnooze } from "./quiet.js";
2
3
  import { parseVacation } from "./vacation.js";
3
4
 
4
- export const PAUSE_HELP = "Try /pause 30m, /pause 2h, /pause 3d or /pause 24.12 2.01";
5
-
6
5
  // "/pause" arguments, one command for a short break and for days off:
7
6
  // "" → { action: "list" } what's paused or planned
8
7
  // "off" → { action: "off" } questions back, days off cancelled