postext 1.61.1 → 1.63.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 (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 +202 -23
  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;
@@ -285,6 +321,8 @@ function exerciseRuntime() {
285
321
  '.pt-ex-drag{touch-action:none;user-select:none;-webkit-user-select:none;}' +
286
322
  // The word is painted under its slot: a veil fades it.
287
323
  '.pt-ex-sorted{background:rgba(255,255,255,.62);border-radius:.25em;}' +
324
+ // A chip set in a flow holds its own text, which a veil would not cover.
325
+ '.pt-ex-faded{opacity:.42;}' +
288
326
  '.pt-ex-col{position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;padding:0 .2em;color:var(--pt-ex-text,#1d1d1f);border-radius:.2em;}' +
289
327
  '.pt-ex-placed{display:table;cursor:grab;white-space:nowrap;}' +
290
328
  '.pt-ex-placed.pt-ex-right{color:var(--pt-ex-right,#1e8e3e);}' +
@@ -393,8 +431,12 @@ function exerciseRuntime() {
393
431
  const text = String((line || el).textContent || '').replace(/\s+/g, ' ').trim();
394
432
  return text.length > 80 ? text.slice(0, 79) + '\u2026' : text;
395
433
  }
396
- /** The text a chip shows: the runs right after its slot, inside its box. */
434
+ /** The text a chip shows: the runs right after its slot, inside its box;
435
+ * in a flow (a reflowable book), the chip's own text (#1263). */
397
436
  function chipText(el) {
437
+ const own = String(el.textContent || '').replace(/\s+/g, ' ').trim();
438
+ if (own)
439
+ return own;
398
440
  const right = parseFloat(el.style.left || '0') + parseFloat(el.style.width || '0');
399
441
  let text = '';
400
442
  let n = el.nextSibling;
@@ -428,15 +470,28 @@ function exerciseRuntime() {
428
470
  }
429
471
  if (outers.length === 0 && found.length === 0)
430
472
  return null;
431
- 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;
432
475
  const instant = (opts.check || data.check) === 'instant';
433
476
  const words = data.words || {};
434
477
  const look = (data.styles && data.styles[rec.style]) || { correct: '#1e8e3e', wrong: '#c5221f', answer: '#d6246e', ink: 'currentColor', marker: 'none', trueFalse: words.trueFalse || ['T', 'F'] };
435
478
  const tf = look.trueFalse;
436
479
  const exName = (words.exercise || 'Exercise') + ' ' + rec.number;
437
480
  const key = base + ':' + (rec.anchor || '#' + rec.ordinal + '.' + rec.id);
438
- const saved = load(key, persist) || {};
439
- 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 = [];
440
495
  const ppt = data.pxPerPt || 1;
441
496
  const body = data.bodyPx || 16;
442
497
  const flow = data.flow === true;
@@ -572,6 +627,7 @@ function exerciseRuntime() {
572
627
  const input = given || own(el('input', 'pt-ex-input pt-ex-ui', target));
573
628
  if (given)
574
629
  addClass(given, 'pt-ex-ui');
630
+ fields.push(input);
575
631
  input.setAttribute('type', 'text');
576
632
  input.setAttribute('autocomplete', 'off');
577
633
  input.setAttribute('spellcheck', 'false');
@@ -914,7 +970,7 @@ function exerciseRuntime() {
914
970
  paint(v) {
915
971
  const at = typeof v === 'string' ? categories.indexOf(v) : -1;
916
972
  // Sorted: the word stands in its column, and fades in the bank.
917
- t.classList.toggle('pt-ex-sorted', at >= 0 && columnWidgets.length > 0);
973
+ t.classList.toggle(flow ? 'pt-ex-faded' : 'pt-ex-sorted', at >= 0 && columnWidgets.length > 0);
918
974
  t.setAttribute('aria-label', (word || lineText(t)) + (at >= 0 ? ', ' + categories[at] : ''));
919
975
  refreshColumns();
920
976
  },
@@ -969,6 +1025,7 @@ function exerciseRuntime() {
969
1025
  * order. */
970
1026
  function area(slot, box) {
971
1027
  const ta = own(el('textarea', 'pt-ex-input pt-ex-ui', box));
1028
+ fields.push(ta);
972
1029
  ta.setAttribute('aria-label', (words.blank || 'blank') + ' ' + (++blankN) + ', ' + exName);
973
1030
  ta.setAttribute('spellcheck', 'false');
974
1031
  const line = (slot.linePt || 0) * ppt || body * 1.35;
@@ -1020,6 +1077,7 @@ function exerciseRuntime() {
1020
1077
  f.style.fontFamily = textFont;
1021
1078
  colours(f);
1022
1079
  const input = el('input', 'pt-ex-input' + (grid.kind === 'crossword' ? ' pt-ex-cell' : ''), f);
1080
+ fields.push(input);
1023
1081
  input.setAttribute('type', 'text');
1024
1082
  input.setAttribute('autocomplete', 'off');
1025
1083
  input.setAttribute('spellcheck', 'false');
@@ -1437,6 +1495,8 @@ function exerciseRuntime() {
1437
1495
  let panel = null;
1438
1496
  let solutionBtn = null;
1439
1497
  let hintBtn = null;
1498
+ let checkBtn = null;
1499
+ let resetBtn = null;
1440
1500
  if (outer) {
1441
1501
  const bar = own(el('div', 'pt-ex-bar pt-ex-ui' + (flow ? ' pt-ex-flow' : ''), outer));
1442
1502
  bar.setAttribute('role', 'group');
@@ -1457,11 +1517,11 @@ function exerciseRuntime() {
1457
1517
  if (hasHints)
1458
1518
  hintBtn = button(words.hint || 'Hint', () => showPanel(panel && panel.getAttribute('data-show') === 'hint' ? null : 'hint'));
1459
1519
  if (!instant && checkable)
1460
- button(words.check || 'Check', () => { state.checked = true; persistNow(); feedback(); });
1461
- if (solvable)
1520
+ checkBtn = button(words.check || 'Check', check);
1521
+ if (solvable && showWhen !== 'never')
1462
1522
  solutionBtn = button(words.showSolution || 'Show solution', () => reveal(!state.revealed));
1463
- if (resettable)
1464
- button(words.reset || 'Try again', reset);
1523
+ if (resettable && allowReset)
1524
+ resetBtn = button(words.reset || 'Try again', reset);
1465
1525
  panel = el('div', 'pt-ex-panel pt-ex-ui' + (flow ? ' pt-ex-flow' : ''), outer);
1466
1526
  own(panel);
1467
1527
  panel.hidden = true;
@@ -1532,7 +1592,48 @@ function exerciseRuntime() {
1532
1592
  widgets[i].paint(valueOf(widgets[i]));
1533
1593
  widgets[i].lock(state.revealed);
1534
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;
1535
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');
1536
1637
  }
1537
1638
  function verdict(slot) {
1538
1639
  const v = slot.kind === 'word' ? state.v._found : state.v[slot.key];
@@ -1540,6 +1641,17 @@ function exerciseRuntime() {
1540
1641
  }
1541
1642
  /** Right and wrong on every slot, and the score. */
1542
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() {
1543
1655
  let right = 0;
1544
1656
  let total = 0;
1545
1657
  for (let i = 0; i < rec.slots.length; i++) {
@@ -1557,11 +1669,34 @@ function exerciseRuntime() {
1557
1669
  if (verdict(s) === true)
1558
1670
  right++;
1559
1671
  }
1560
- for (let i = 0; i < widgets.length; i++)
1561
- widgets[i].mark(state.revealed ? null : verdict(widgets[i].slot), !state.revealed);
1562
- if (scoreEl) {
1563
- const score = String(words.score || '{right} of {total} right').replace('{right}', String(right)).replace('{total}', String(total));
1564
- 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;
1565
1700
  }
1566
1701
  }
1567
1702
  function clearFeedback() {
@@ -1571,11 +1706,21 @@ function exerciseRuntime() {
1571
1706
  scoreEl.textContent = '';
1572
1707
  }
1573
1708
  function persistNow() {
1574
- 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);
1575
1719
  }
1576
1720
  function afterChange(slot) {
1577
1721
  if (instant) {
1578
1722
  feedback();
1723
+ report('check');
1579
1724
  return;
1580
1725
  }
1581
1726
  if (state.checked) {
@@ -1594,6 +1739,12 @@ function exerciseRuntime() {
1594
1739
  function changed(slot, value) {
1595
1740
  if (state.revealed)
1596
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
+ }
1597
1748
  if (slot.kind !== 'word') {
1598
1749
  if (value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0))
1599
1750
  delete state.v[slot.key];
@@ -1610,6 +1761,11 @@ function exerciseRuntime() {
1610
1761
  }
1611
1762
  function reveal(on) {
1612
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
+ }
1613
1769
  if (solutionBtn)
1614
1770
  solutionBtn.textContent = on ? words.hideSolution || 'Hide solution' : words.showSolution || 'Show solution';
1615
1771
  if (solutionBtn)
@@ -1624,11 +1780,24 @@ function exerciseRuntime() {
1624
1780
  if (state.checked || instant)
1625
1781
  feedback();
1626
1782
  }
1783
+ if (on)
1784
+ report('solution');
1627
1785
  }
1628
1786
  function reset() {
1787
+ if (!allowReset || (maxResets > 0 && state.r >= maxResets))
1788
+ return;
1629
1789
  state.v = {};
1630
1790
  state.checked = false;
1631
- 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);
1632
1801
  if (state.revealed)
1633
1802
  reveal(false);
1634
1803
  paintAll();
@@ -1636,15 +1805,17 @@ function exerciseRuntime() {
1636
1805
  showPanel(null);
1637
1806
  if (instant)
1638
1807
  feedback();
1808
+ report('reset');
1639
1809
  }
1640
1810
  paintAll();
1641
1811
  if (state.checked || instant)
1642
1812
  feedback();
1643
1813
  if (solutionBtn)
1644
1814
  solutionBtn.setAttribute('aria-expanded', 'false');
1815
+ report('open');
1645
1816
  return { record: rec, state, reveal, reset, feedback };
1646
1817
  }
1647
- return { normalizeAnswer, isGraded, checkSlot, solutionValue, actionsOf, attach };
1818
+ return { normalizeAnswer, isGraded, checkSlot, solutionValue, actionsOf, pointsOf, attach };
1648
1819
  }
1649
1820
  /* eslint-enable @typescript-eslint/no-explicit-any */
1650
1821
  const runtime = exerciseRuntime();
@@ -1658,6 +1829,9 @@ export const isGradedSlot = runtime.isGraded;
1658
1829
  export const checkExerciseSlot = runtime.checkSlot;
1659
1830
  /** The buttons an exercise's bar offers: Check, Show solution, Try again. */
1660
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;
1661
1835
  /** What a slot shows when the solution is shown. */
1662
1836
  export const exerciseSolutionValue = runtime.solutionValue;
1663
1837
  /** Makes the exercises of the HTML output under `root` answerable (see the
@@ -1668,6 +1842,11 @@ export const exerciseSolutionValue = runtime.solutionValue;
1668
1842
  export function attachExercises(root, options) {
1669
1843
  return runtime.attach(root, options);
1670
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();
1671
1850
  /** The exercise script as a standalone script, for an output with no host
1672
1851
  * page to call `attachExercises`: the HTML file of the command line, an
1673
1852
  * EPUB content document. It attaches to the document once it is parsed;