postext 1.62.0 → 1.63.1

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 (36) hide show
  1. package/dist/__tests__/exercises/config.test.js +32 -0
  2. package/dist/__tests__/exercises/config.test.js.map +1 -1
  3. package/dist/__tests__/exports.test.js +2 -0
  4. package/dist/__tests__/exports.test.js.map +1 -1
  5. package/dist/__tests__/pipeline/spanHeadingMidPage.test.js +99 -0
  6. package/dist/__tests__/pipeline/spanHeadingMidPage.test.js.map +1 -1
  7. package/dist/__tests__/pipeline/trailingBand.test.js +25 -0
  8. package/dist/__tests__/pipeline/trailingBand.test.js.map +1 -1
  9. package/dist/defaults/exercises.d.ts +13 -2
  10. package/dist/defaults/exercises.d.ts.map +1 -1
  11. package/dist/defaults/exercises.js +63 -11
  12. package/dist/defaults/exercises.js.map +1 -1
  13. package/dist/exercises/configWarnings.d.ts.map +1 -1
  14. package/dist/exercises/configWarnings.js +6 -2
  15. package/dist/exercises/configWarnings.js.map +1 -1
  16. package/dist/exercises/digital.d.ts +8 -3
  17. package/dist/exercises/digital.d.ts.map +1 -1
  18. package/dist/exercises/digital.js +6 -0
  19. package/dist/exercises/digital.js.map +1 -1
  20. package/dist/exercises/runtime.d.ts +68 -0
  21. package/dist/exercises/runtime.d.ts.map +1 -1
  22. package/dist/exercises/runtime.js +194 -21
  23. package/dist/exercises/runtime.js.map +1 -1
  24. package/dist/index.d.ts +2 -2
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/pipeline/bandCaps.d.ts.map +1 -1
  29. package/dist/pipeline/bandCaps.js +8 -1
  30. package/dist/pipeline/bandCaps.js.map +1 -1
  31. package/dist/pipeline/build.d.ts.map +1 -1
  32. package/dist/pipeline/build.js +15 -4
  33. package/dist/pipeline/build.js.map +1 -1
  34. package/dist/types.d.ts +32 -4
  35. package/dist/types.d.ts.map +1 -1
  36. package/package.json +1 -1
@@ -19,6 +19,11 @@
19
19
  * script (`EXERCISE_SCRIPT`, its source text). It keeps to syntax every
20
20
  * reading system and transpiler leaves as it is: no spread, no `for…of`,
21
21
  * no `async`.
22
+ *
23
+ * A host may keep the answers somewhere else than `localStorage` (`store`:
24
+ * a SCORM package keeps them in the LMS) and hear every check (`onResult`,
25
+ * #1267); a style's limits (#1268) bound the checks and fresh starts of an
26
+ * attempt and when its solution shows.
22
27
  */
23
28
  /* eslint-disable @typescript-eslint/no-explicit-any -- the DOM side works on untyped JSON and elements of any document */
24
29
  function exerciseRuntime() {
@@ -191,12 +196,39 @@ function exerciseRuntime() {
191
196
  }
192
197
  return { check, solution, reset };
193
198
  }
199
+ /** What an exercise's score counts out of with nothing answered: its
200
+ * graded slots (a word of a mark exercise not to be marked counts only
201
+ * once it is marked). */
202
+ function pointsOf(record) {
203
+ let n = 0;
204
+ for (let i = 0; i < record.slots.length; i++) {
205
+ const s = record.slots[i];
206
+ if (!isGraded(s, record))
207
+ continue;
208
+ if (s.kind === 'mark' && (s.correct || []).indexOf(1) < 0)
209
+ continue;
210
+ n++;
211
+ }
212
+ return n;
213
+ }
194
214
  // --- Storage -----------------------------------------------------------------
195
- function load(key, persist) {
215
+ function storeOf(store) {
216
+ if (store)
217
+ return store;
218
+ try {
219
+ return window.localStorage;
220
+ }
221
+ catch (e) {
222
+ void e;
223
+ return null;
224
+ }
225
+ }
226
+ function load(key, persist, store) {
196
227
  let text = null;
197
228
  if (persist) {
198
229
  try {
199
- text = window.localStorage.getItem(key);
230
+ const st = storeOf(store);
231
+ text = st ? st.getItem(key) : null;
200
232
  }
201
233
  catch (e) {
202
234
  void e;
@@ -215,24 +247,28 @@ function exerciseRuntime() {
215
247
  return null;
216
248
  }
217
249
  }
218
- function save(key, persist, state) {
250
+ function save(key, persist, state, store) {
219
251
  const text = JSON.stringify(state);
220
252
  memory[key] = text;
221
253
  if (!persist)
222
254
  return;
223
255
  try {
224
- window.localStorage.setItem(key, text);
256
+ const st = storeOf(store);
257
+ if (st)
258
+ st.setItem(key, text);
225
259
  }
226
260
  catch (e) {
227
261
  void e;
228
262
  }
229
263
  }
230
- function forget(key, persist) {
264
+ function forget(key, persist, store) {
231
265
  delete memory[key];
232
266
  if (!persist)
233
267
  return;
234
268
  try {
235
- window.localStorage.removeItem(key);
269
+ const st = storeOf(store);
270
+ if (st)
271
+ st.removeItem(key);
236
272
  }
237
273
  catch (e) {
238
274
  void e;
@@ -434,15 +470,28 @@ function exerciseRuntime() {
434
470
  }
435
471
  if (outers.length === 0 && found.length === 0)
436
472
  return null;
437
- const persist = opts.persist !== undefined ? opts.persist : data.persist !== false;
473
+ const persist = opts.persist !== undefined ? opts.persist : !!opts.store || data.persist !== false;
474
+ const store = opts.store;
438
475
  const instant = (opts.check || data.check) === 'instant';
439
476
  const words = data.words || {};
440
477
  const look = (data.styles && data.styles[rec.style]) || { correct: '#1e8e3e', wrong: '#c5221f', answer: '#d6246e', ink: 'currentColor', marker: 'none', trueFalse: words.trueFalse || ['T', 'F'] };
441
478
  const tf = look.trueFalse;
442
479
  const exName = (words.exercise || 'Exercise') + ' ' + rec.number;
443
480
  const key = base + ':' + (rec.anchor || '#' + rec.ordinal + '.' + rec.id);
444
- const saved = load(key, persist) || {};
445
- const state = { v: saved.v || {}, checked: !!saved.checked, revealed: false };
481
+ const saved = load(key, persist, store) || {};
482
+ // Checks of the attempt (`n`), Try again used (`r`), the last check all
483
+ // right (`all`), the attempt ended by its solution (`e`).
484
+ const state = { v: saved.v || {}, checked: !!saved.checked, revealed: false, n: saved.n || 0, r: saved.r || 0, all: !!saved.all, ended: !!saved.e };
485
+ // The style's limits (#1268); none with instant checking but the
486
+ // solution's.
487
+ const limits = look.limits || null;
488
+ const maxChecks = limits && !instant ? limits.maxChecks || 0 : 0;
489
+ const allowReset = limits ? limits.reset !== false : true;
490
+ const maxResets = limits ? limits.maxResets || 0 : 0;
491
+ const showWhen = limits ? limits.showSolution || 'always' : 'always';
492
+ /** The attempt takes no more answers. */
493
+ const locked = () => maxChecks > 0 && (state.n >= maxChecks || state.ended);
494
+ const fields = [];
446
495
  const ppt = data.pxPerPt || 1;
447
496
  const body = data.bodyPx || 16;
448
497
  const flow = data.flow === true;
@@ -578,6 +627,7 @@ function exerciseRuntime() {
578
627
  const input = given || own(el('input', 'pt-ex-input pt-ex-ui', target));
579
628
  if (given)
580
629
  addClass(given, 'pt-ex-ui');
630
+ fields.push(input);
581
631
  input.setAttribute('type', 'text');
582
632
  input.setAttribute('autocomplete', 'off');
583
633
  input.setAttribute('spellcheck', 'false');
@@ -975,6 +1025,7 @@ function exerciseRuntime() {
975
1025
  * order. */
976
1026
  function area(slot, box) {
977
1027
  const ta = own(el('textarea', 'pt-ex-input pt-ex-ui', box));
1028
+ fields.push(ta);
978
1029
  ta.setAttribute('aria-label', (words.blank || 'blank') + ' ' + (++blankN) + ', ' + exName);
979
1030
  ta.setAttribute('spellcheck', 'false');
980
1031
  const line = (slot.linePt || 0) * ppt || body * 1.35;
@@ -1026,6 +1077,7 @@ function exerciseRuntime() {
1026
1077
  f.style.fontFamily = textFont;
1027
1078
  colours(f);
1028
1079
  const input = el('input', 'pt-ex-input' + (grid.kind === 'crossword' ? ' pt-ex-cell' : ''), f);
1080
+ fields.push(input);
1029
1081
  input.setAttribute('type', 'text');
1030
1082
  input.setAttribute('autocomplete', 'off');
1031
1083
  input.setAttribute('spellcheck', 'false');
@@ -1443,6 +1495,8 @@ function exerciseRuntime() {
1443
1495
  let panel = null;
1444
1496
  let solutionBtn = null;
1445
1497
  let hintBtn = null;
1498
+ let checkBtn = null;
1499
+ let resetBtn = null;
1446
1500
  if (outer) {
1447
1501
  const bar = own(el('div', 'pt-ex-bar pt-ex-ui' + (flow ? ' pt-ex-flow' : ''), outer));
1448
1502
  bar.setAttribute('role', 'group');
@@ -1463,11 +1517,11 @@ function exerciseRuntime() {
1463
1517
  if (hasHints)
1464
1518
  hintBtn = button(words.hint || 'Hint', () => showPanel(panel && panel.getAttribute('data-show') === 'hint' ? null : 'hint'));
1465
1519
  if (!instant && checkable)
1466
- button(words.check || 'Check', () => { state.checked = true; persistNow(); feedback(); });
1467
- if (solvable)
1520
+ checkBtn = button(words.check || 'Check', check);
1521
+ if (solvable && showWhen !== 'never')
1468
1522
  solutionBtn = button(words.showSolution || 'Show solution', () => reveal(!state.revealed));
1469
- if (resettable)
1470
- button(words.reset || 'Try again', reset);
1523
+ if (resettable && allowReset)
1524
+ resetBtn = button(words.reset || 'Try again', reset);
1471
1525
  panel = el('div', 'pt-ex-panel pt-ex-ui' + (flow ? ' pt-ex-flow' : ''), outer);
1472
1526
  own(panel);
1473
1527
  panel.hidden = true;
@@ -1538,7 +1592,48 @@ function exerciseRuntime() {
1538
1592
  widgets[i].paint(valueOf(widgets[i]));
1539
1593
  widgets[i].lock(state.revealed);
1540
1594
  }
1595
+ // An attempt with no checks left keeps its answers as they are.
1596
+ const frozen = state.revealed || locked();
1597
+ for (let i = 0; i < fields.length; i++)
1598
+ fields[i].readOnly = frozen;
1541
1599
  refreshColumns();
1600
+ refreshBar();
1601
+ }
1602
+ /** The bar's buttons as the limits stand: the checks left on Check, the
1603
+ * fresh starts left on Try again, the solution once it may show. */
1604
+ function refreshBar() {
1605
+ if (checkBtn) {
1606
+ const spent = locked();
1607
+ checkBtn.disabled = spent;
1608
+ checkBtn.textContent = maxChecks > 0
1609
+ ? (spent ? words.noChecksLeft || 'No checks left' : String(words.checkLeft || 'Check ({n} left)').replace('{n}', String(maxChecks - state.n)))
1610
+ : words.check || 'Check';
1611
+ }
1612
+ if (resetBtn) {
1613
+ const left = maxResets > 0 ? maxResets - state.r : -1;
1614
+ resetBtn.hidden = left === 0;
1615
+ resetBtn.textContent = left > 0 ? String(words.resetLeft || 'Try again ({n} left)').replace('{n}', String(left)) : words.reset || 'Try again';
1616
+ }
1617
+ if (solutionBtn) {
1618
+ const may = showWhen === 'always' || state.revealed
1619
+ || (showWhen === 'afterCheck' && (state.n > 0 || state.ended))
1620
+ || (showWhen === 'noChecksLeft' && (locked() || state.all));
1621
+ solutionBtn.hidden = !may;
1622
+ }
1623
+ }
1624
+ /** The answers checked: one check of the attempt spent. */
1625
+ function check() {
1626
+ if (locked() || state.revealed)
1627
+ return;
1628
+ state.n++;
1629
+ state.checked = true;
1630
+ const sc = feedback();
1631
+ state.all = sc.total > 0 && sc.right === sc.total;
1632
+ persistNow();
1633
+ refreshBar();
1634
+ if (locked())
1635
+ paintAll();
1636
+ report('check');
1542
1637
  }
1543
1638
  function verdict(slot) {
1544
1639
  const v = slot.kind === 'word' ? state.v._found : state.v[slot.key];
@@ -1546,6 +1641,17 @@ function exerciseRuntime() {
1546
1641
  }
1547
1642
  /** Right and wrong on every slot, and the score. */
1548
1643
  function feedback() {
1644
+ const sc = score();
1645
+ for (let i = 0; i < widgets.length; i++)
1646
+ widgets[i].mark(state.revealed ? null : verdict(widgets[i].slot), !state.revealed);
1647
+ if (scoreEl) {
1648
+ const text = String(words.score || '{right} of {total} right').replace('{right}', String(sc.right)).replace('{total}', String(sc.total));
1649
+ scoreEl.textContent = sc.total === 0 ? '' : (sc.right === sc.total ? (words.correct || '') + ' ' : '') + text;
1650
+ }
1651
+ return sc;
1652
+ }
1653
+ /** Graded slots right, and graded slots. */
1654
+ function score() {
1549
1655
  let right = 0;
1550
1656
  let total = 0;
1551
1657
  for (let i = 0; i < rec.slots.length; i++) {
@@ -1563,11 +1669,34 @@ function exerciseRuntime() {
1563
1669
  if (verdict(s) === true)
1564
1670
  right++;
1565
1671
  }
1566
- for (let i = 0; i < widgets.length; i++)
1567
- widgets[i].mark(state.revealed ? null : verdict(widgets[i].slot), !state.revealed);
1568
- if (scoreEl) {
1569
- const score = String(words.score || '{right} of {total} right').replace('{right}', String(right)).replace('{total}', String(total));
1570
- scoreEl.textContent = total === 0 ? '' : (right === total ? (words.correct || '') + ' ' : '') + score;
1672
+ return { right, total };
1673
+ }
1674
+ /** The exercise as it stands, to the host (#1267). */
1675
+ function report(event) {
1676
+ if (!opts.onResult)
1677
+ return;
1678
+ const sc = score();
1679
+ const slots = [];
1680
+ for (let i = 0; i < rec.slots.length; i++) {
1681
+ const sl = rec.slots[i];
1682
+ if (!isGraded(sl, rec))
1683
+ continue;
1684
+ slots.push({ key: sl.key, kind: sl.kind, value: sl.kind === 'word' ? state.v._found : state.v[sl.key], right: verdict(sl), solution: solutionValue(sl, rec, tf) });
1685
+ }
1686
+ const result = {
1687
+ event, key, id: rec.id, number: rec.number, kind: rec.kind, style: rec.style,
1688
+ right: sc.right, total: sc.total, checks: state.n, resets: state.r,
1689
+ checked: state.checked || instant, locked: locked(), slots,
1690
+ };
1691
+ if (rec.anchor)
1692
+ result.anchor = rec.anchor;
1693
+ if (rec.title)
1694
+ result.title = rec.title;
1695
+ try {
1696
+ opts.onResult(result);
1697
+ }
1698
+ catch (e) {
1699
+ void e;
1571
1700
  }
1572
1701
  }
1573
1702
  function clearFeedback() {
@@ -1577,11 +1706,21 @@ function exerciseRuntime() {
1577
1706
  scoreEl.textContent = '';
1578
1707
  }
1579
1708
  function persistNow() {
1580
- save(key, persist, { v: state.v, checked: state.checked });
1709
+ const kept = { v: state.v, checked: state.checked };
1710
+ if (state.n)
1711
+ kept.n = state.n;
1712
+ if (state.r)
1713
+ kept.r = state.r;
1714
+ if (state.all)
1715
+ kept.all = true;
1716
+ if (state.ended)
1717
+ kept.e = true;
1718
+ save(key, persist, kept, store);
1581
1719
  }
1582
1720
  function afterChange(slot) {
1583
1721
  if (instant) {
1584
1722
  feedback();
1723
+ report('check');
1585
1724
  return;
1586
1725
  }
1587
1726
  if (state.checked) {
@@ -1600,6 +1739,12 @@ function exerciseRuntime() {
1600
1739
  function changed(slot, value) {
1601
1740
  if (state.revealed)
1602
1741
  return;
1742
+ if (locked()) {
1743
+ for (let i = 0; i < widgets.length; i++)
1744
+ if (widgets[i].slot === slot)
1745
+ widgets[i].paint(valueOf(widgets[i]));
1746
+ return;
1747
+ }
1603
1748
  if (slot.kind !== 'word') {
1604
1749
  if (value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0))
1605
1750
  delete state.v[slot.key];
@@ -1616,6 +1761,11 @@ function exerciseRuntime() {
1616
1761
  }
1617
1762
  function reveal(on) {
1618
1763
  state.revealed = on;
1764
+ // With limited checks, the solution ends the attempt.
1765
+ if (on && maxChecks > 0 && !state.ended) {
1766
+ state.ended = true;
1767
+ persistNow();
1768
+ }
1619
1769
  if (solutionBtn)
1620
1770
  solutionBtn.textContent = on ? words.hideSolution || 'Hide solution' : words.showSolution || 'Show solution';
1621
1771
  if (solutionBtn)
@@ -1630,11 +1780,24 @@ function exerciseRuntime() {
1630
1780
  if (state.checked || instant)
1631
1781
  feedback();
1632
1782
  }
1783
+ if (on)
1784
+ report('solution');
1633
1785
  }
1634
1786
  function reset() {
1787
+ if (!allowReset || (maxResets > 0 && state.r >= maxResets))
1788
+ return;
1635
1789
  state.v = {};
1636
1790
  state.checked = false;
1637
- forget(key, persist);
1791
+ state.n = 0;
1792
+ state.all = false;
1793
+ state.ended = false;
1794
+ // The fresh starts used are kept while they are limited.
1795
+ if (limits) {
1796
+ state.r++;
1797
+ persistNow();
1798
+ }
1799
+ else
1800
+ forget(key, persist, store);
1638
1801
  if (state.revealed)
1639
1802
  reveal(false);
1640
1803
  paintAll();
@@ -1642,15 +1805,17 @@ function exerciseRuntime() {
1642
1805
  showPanel(null);
1643
1806
  if (instant)
1644
1807
  feedback();
1808
+ report('reset');
1645
1809
  }
1646
1810
  paintAll();
1647
1811
  if (state.checked || instant)
1648
1812
  feedback();
1649
1813
  if (solutionBtn)
1650
1814
  solutionBtn.setAttribute('aria-expanded', 'false');
1815
+ report('open');
1651
1816
  return { record: rec, state, reveal, reset, feedback };
1652
1817
  }
1653
- return { normalizeAnswer, isGraded, checkSlot, solutionValue, actionsOf, attach };
1818
+ return { normalizeAnswer, isGraded, checkSlot, solutionValue, actionsOf, pointsOf, attach };
1654
1819
  }
1655
1820
  /* eslint-enable @typescript-eslint/no-explicit-any */
1656
1821
  const runtime = exerciseRuntime();
@@ -1664,6 +1829,9 @@ export const isGradedSlot = runtime.isGraded;
1664
1829
  export const checkExerciseSlot = runtime.checkSlot;
1665
1830
  /** The buttons an exercise's bar offers: Check, Show solution, Try again. */
1666
1831
  export const exerciseActions = runtime.actionsOf;
1832
+ /** What an exercise's score counts out of with nothing answered (#1267):
1833
+ * its graded slots. */
1834
+ export const exercisePoints = runtime.pointsOf;
1667
1835
  /** What a slot shows when the solution is shown. */
1668
1836
  export const exerciseSolutionValue = runtime.solutionValue;
1669
1837
  /** Makes the exercises of the HTML output under `root` answerable (see the
@@ -1674,6 +1842,11 @@ export const exerciseSolutionValue = runtime.solutionValue;
1674
1842
  export function attachExercises(root, options) {
1675
1843
  return runtime.attach(root, options);
1676
1844
  }
1845
+ /** The runtime's source, a function expression that returns it: what a
1846
+ * host script calls to attach with options of its own (a SCORM package's
1847
+ * LMS store and results, #1267), as `var runtime = (${EXERCISE_RUNTIME})();`.
1848
+ * It closes over nothing. */
1849
+ export const EXERCISE_RUNTIME = exerciseRuntime.toString();
1677
1850
  /** The exercise script as a standalone script, for an output with no host
1678
1851
  * page to call `attachExercises`: the HTML file of the command line, an
1679
1852
  * EPUB content document. It attaches to the document once it is parsed;