react-cheminfo 0.127.0 → 0.129.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 (151) hide show
  1. package/README.md +79 -43
  2. package/lib/drawing/core/camera.d.ts +22 -0
  3. package/lib/drawing/core/camera.d.ts.map +1 -1
  4. package/lib/drawing/core/camera.js +37 -0
  5. package/lib/drawing/core/camera.js.map +1 -1
  6. package/lib/drawing/core/graphics.d.ts.map +1 -1
  7. package/lib/drawing/core/graphics.js +2 -10
  8. package/lib/drawing/core/graphics.js.map +1 -1
  9. package/lib/drawing/core/groupFiles.d.ts +22 -0
  10. package/lib/drawing/core/groupFiles.d.ts.map +1 -0
  11. package/lib/drawing/core/groupFiles.js +51 -0
  12. package/lib/drawing/core/groupFiles.js.map +1 -0
  13. package/lib/drawing/core/groups.d.ts +76 -0
  14. package/lib/drawing/core/groups.d.ts.map +1 -0
  15. package/lib/drawing/core/groups.js +190 -0
  16. package/lib/drawing/core/groups.js.map +1 -0
  17. package/lib/drawing/core/index.d.ts +7 -2
  18. package/lib/drawing/core/index.d.ts.map +1 -1
  19. package/lib/drawing/core/index.js +5 -2
  20. package/lib/drawing/core/index.js.map +1 -1
  21. package/lib/drawing/core/selectionFrame.d.ts +70 -0
  22. package/lib/drawing/core/selectionFrame.d.ts.map +1 -0
  23. package/lib/drawing/core/selectionFrame.js +156 -0
  24. package/lib/drawing/core/selectionFrame.js.map +1 -0
  25. package/lib/drawing/core/transform.d.ts +30 -0
  26. package/lib/drawing/core/transform.d.ts.map +1 -1
  27. package/lib/drawing/core/transform.js +59 -0
  28. package/lib/drawing/core/transform.js.map +1 -1
  29. package/lib/drawing/ui/FrameMarks.d.ts +30 -0
  30. package/lib/drawing/ui/FrameMarks.d.ts.map +1 -0
  31. package/lib/drawing/ui/FrameMarks.js +19 -0
  32. package/lib/drawing/ui/FrameMarks.js.map +1 -0
  33. package/lib/drawing/ui/curveHandleMarks.d.ts +10 -0
  34. package/lib/drawing/ui/curveHandleMarks.d.ts.map +1 -1
  35. package/lib/drawing/ui/curveHandleMarks.js +20 -2
  36. package/lib/drawing/ui/curveHandleMarks.js.map +1 -1
  37. package/lib/drawing/ui/index.d.ts +5 -1
  38. package/lib/drawing/ui/index.d.ts.map +1 -1
  39. package/lib/drawing/ui/index.js +3 -1
  40. package/lib/drawing/ui/index.js.map +1 -1
  41. package/lib/drawing/ui/useCanvasNavigation.d.ts +35 -0
  42. package/lib/drawing/ui/useCanvasNavigation.d.ts.map +1 -0
  43. package/lib/drawing/ui/useCanvasNavigation.js +95 -0
  44. package/lib/drawing/ui/useCanvasNavigation.js.map +1 -0
  45. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  46. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  47. package/lib/ir/ui/BandsPanel.d.ts +3 -3
  48. package/lib/ir/ui/BandsPanel.d.ts.map +1 -1
  49. package/lib/ir/ui/BandsPanel.js +7 -7
  50. package/lib/ir/ui/BandsPanel.js.map +1 -1
  51. package/lib/ir/ui/BandsToolbar.d.ts.map +1 -1
  52. package/lib/ir/ui/BandsToolbar.js +2 -4
  53. package/lib/ir/ui/BandsToolbar.js.map +1 -1
  54. package/lib/ir/ui/IrEditor.d.ts +7 -0
  55. package/lib/ir/ui/IrEditor.d.ts.map +1 -1
  56. package/lib/ir/ui/IrEditor.js.map +1 -1
  57. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  58. package/lib/ir/ui/IrShell.js +3 -2
  59. package/lib/ir/ui/IrShell.js.map +1 -1
  60. package/lib/ir/ui/IrStatusBar.js +2 -2
  61. package/lib/ir/ui/IrStatusBar.js.map +1 -1
  62. package/lib/ir/ui/IrToolbar.js +2 -2
  63. package/lib/ir/ui/IrToolbar.js.map +1 -1
  64. package/lib/ir/ui/VibrationalEditor.d.ts +7 -0
  65. package/lib/ir/ui/VibrationalEditor.d.ts.map +1 -1
  66. package/lib/ir/ui/VibrationalEditor.js +2 -1
  67. package/lib/ir/ui/VibrationalEditor.js.map +1 -1
  68. package/lib/ir/ui/useIrEditor.d.ts +12 -0
  69. package/lib/ir/ui/useIrEditor.d.ts.map +1 -1
  70. package/lib/ir/ui/useIrEditor.js +10 -3
  71. package/lib/ir/ui/useIrEditor.js.map +1 -1
  72. package/lib/locales/de.json +9 -0
  73. package/lib/locales/en.json +9 -0
  74. package/lib/locales/es.json +9 -0
  75. package/lib/locales/fr.json +9 -0
  76. package/lib/locales/it.json +9 -0
  77. package/lib/pedagogy/ui/ExerciseActions.d.ts +10 -2
  78. package/lib/pedagogy/ui/ExerciseActions.d.ts.map +1 -1
  79. package/lib/pedagogy/ui/ExerciseActions.js +7 -5
  80. package/lib/pedagogy/ui/ExerciseActions.js.map +1 -1
  81. package/lib/share/core/exampleMark.d.ts +31 -0
  82. package/lib/share/core/exampleMark.d.ts.map +1 -0
  83. package/lib/share/core/exampleMark.js +31 -0
  84. package/lib/share/core/exampleMark.js.map +1 -0
  85. package/lib/share/core/index.d.ts +5 -0
  86. package/lib/share/core/index.d.ts.map +1 -1
  87. package/lib/share/core/index.js +3 -0
  88. package/lib/share/core/index.js.map +1 -1
  89. package/lib/share/core/linkBytes.d.ts +29 -0
  90. package/lib/share/core/linkBytes.d.ts.map +1 -0
  91. package/lib/share/core/linkBytes.js +70 -0
  92. package/lib/share/core/linkBytes.js.map +1 -0
  93. package/lib/share/core/linkValue.d.ts +34 -0
  94. package/lib/share/core/linkValue.d.ts.map +1 -0
  95. package/lib/share/core/linkValue.js +58 -0
  96. package/lib/share/core/linkValue.js.map +1 -0
  97. package/lib/share/core/opensOn.d.ts +133 -0
  98. package/lib/share/core/opensOn.d.ts.map +1 -0
  99. package/lib/share/core/opensOn.js +82 -0
  100. package/lib/share/core/opensOn.js.map +1 -0
  101. package/lib/share/core/seal.d.ts.map +1 -1
  102. package/lib/share/core/seal.js +3 -32
  103. package/lib/share/core/seal.js.map +1 -1
  104. package/lib/share/ui/ShareOpensOn.d.ts +32 -0
  105. package/lib/share/ui/ShareOpensOn.d.ts.map +1 -0
  106. package/lib/share/ui/ShareOpensOn.js +49 -0
  107. package/lib/share/ui/ShareOpensOn.js.map +1 -0
  108. package/lib/share/ui/index.d.ts +4 -0
  109. package/lib/share/ui/index.d.ts.map +1 -1
  110. package/lib/share/ui/index.js +2 -0
  111. package/lib/share/ui/index.js.map +1 -1
  112. package/lib/share/ui/useOpensOnShare.d.ts +65 -0
  113. package/lib/share/ui/useOpensOnShare.d.ts.map +1 -0
  114. package/lib/share/ui/useOpensOnShare.js +60 -0
  115. package/lib/share/ui/useOpensOnShare.js.map +1 -0
  116. package/package.json +1 -1
  117. package/src/drawing/core/camera.ts +45 -0
  118. package/src/drawing/core/graphics.ts +2 -12
  119. package/src/drawing/core/groupFiles.ts +55 -0
  120. package/src/drawing/core/groups.ts +221 -0
  121. package/src/drawing/core/index.ts +29 -1
  122. package/src/drawing/core/selectionFrame.ts +192 -0
  123. package/src/drawing/core/transform.ts +80 -0
  124. package/src/drawing/ui/FrameMarks.tsx +57 -0
  125. package/src/drawing/ui/curveHandleMarks.tsx +43 -10
  126. package/src/drawing/ui/index.ts +8 -1
  127. package/src/drawing/ui/useCanvasNavigation.ts +120 -0
  128. package/src/ir/ui/BandsPanel.tsx +15 -10
  129. package/src/ir/ui/BandsToolbar.tsx +2 -4
  130. package/src/ir/ui/IrEditor.tsx +7 -0
  131. package/src/ir/ui/IrShell.tsx +4 -2
  132. package/src/ir/ui/IrStatusBar.tsx +2 -2
  133. package/src/ir/ui/IrToolbar.tsx +2 -2
  134. package/src/ir/ui/VibrationalEditor.tsx +9 -0
  135. package/src/ir/ui/useIrEditor.ts +23 -4
  136. package/src/locales/de.json +9 -0
  137. package/src/locales/en.json +9 -0
  138. package/src/locales/es.json +9 -0
  139. package/src/locales/fr.json +9 -0
  140. package/src/locales/it.json +9 -0
  141. package/src/pedagogy/ui/ExerciseActions.tsx +22 -7
  142. package/src/share/core/exampleMark.ts +52 -0
  143. package/src/share/core/index.ts +25 -0
  144. package/src/share/core/linkBytes.ts +75 -0
  145. package/src/share/core/linkValue.ts +66 -0
  146. package/src/share/core/opensOn.ts +203 -0
  147. package/src/share/core/seal.ts +3 -36
  148. package/src/share/ui/ShareOpensOn.tsx +169 -0
  149. package/src/share/ui/index.ts +7 -0
  150. package/src/share/ui/useOpensOnShare.tsx +152 -0
  151. package/styles/chrome.css +18 -0
@@ -27,6 +27,13 @@ export interface IrEditorProps {
27
27
  * @default undefined
28
28
  */
29
29
  onChange?: (spectra: IrSpectrum[]) => void;
30
+ /**
31
+ * Whether the Bands panel says what each band could be. An exercise switches
32
+ * it off, so the reading of the bands is the student's; the table's ranges
33
+ * can still be offered to look up, in a panel of the host's.
34
+ * @default true
35
+ */
36
+ assignments?: boolean;
30
37
  /**
31
38
  * Which value axis to open in: transmittance or absorbance. Intensity, which
32
39
  * infrared does not offer, opens transmittance instead.
@@ -66,7 +66,8 @@ export interface IrShellProps {
66
66
  export function IrShell(props: IrShellProps) {
67
67
  const { handle, panels, onPlotClick, actions: hostActions } = props;
68
68
 
69
- const { state, visibleSpectra, selectedSpectrum } = useIrEditorState();
69
+ const { state, visibleSpectra, selectedSpectrum, assignments } =
70
+ useIrEditorState();
70
71
  const actions = useIrActions();
71
72
  const { toggle: toggleFullScreen } = useFullscreen();
72
73
  const { tool } = state.view;
@@ -114,7 +115,7 @@ export function IrShell(props: IrShellProps) {
114
115
  ? () => actions.setSettings({ mode: otherIrMode(mode, technique) })
115
116
  : undefined,
116
117
  togglePicking: () => actions.setSettings({ pickBands: !pickBands }),
117
- toggleAssignments: irCommandOffered(technique, 'toggleAssignments')
118
+ toggleAssignments: assignments
118
119
  ? () => actions.setSettings({ showAssignments: !showAssignments })
119
120
  : undefined,
120
121
  exportImage: () => setFigureOpen(!isFigureOpen),
@@ -127,6 +128,7 @@ export function IrShell(props: IrShellProps) {
127
128
  technique,
128
129
  pickBands,
129
130
  showAssignments,
131
+ assignments,
130
132
  zoomIn,
131
133
  zoomOut,
132
134
  toggleFullScreen,
@@ -29,12 +29,12 @@ import { useIrText } from './useIrText.ts';
29
29
  * @returns The status bar.
30
30
  */
31
31
  export function IrStatusBar() {
32
- const { state, visibleSpectra, selectedSpectrum, assigned } =
32
+ const { state, visibleSpectra, selectedSpectrum, assigned, assignments } =
33
33
  useIrEditorState();
34
34
  const { spectra } = state.data;
35
35
  const { error, tool } = state.view;
36
36
  const { mode, pickBands, technique } = state.settings;
37
- const { reverseX, assignments, unit } = IR_TECHNIQUE_RULES[technique];
37
+ const { reverseX, unit } = IR_TECHNIQUE_RULES[technique];
38
38
  const text = useIrText(technique);
39
39
 
40
40
  const range =
@@ -41,7 +41,7 @@ export interface IrToolbarProps {
41
41
  */
42
42
  export function IrToolbar(props: IrToolbarProps) {
43
43
  const { figure } = props;
44
- const { state, selectedSpectrum } = useIrEditorState();
44
+ const { state, selectedSpectrum, assignments } = useIrEditorState();
45
45
  const command = useIrCommandProps();
46
46
  const { spectra } = state.data;
47
47
  const { domain, tool } = state.view;
@@ -113,7 +113,7 @@ export function IrToolbar(props: IrToolbarProps) {
113
113
  {...command('togglePicking')}
114
114
  disabled={selectedSpectrum === null}
115
115
  />
116
- {irCommandOffered(technique, 'toggleAssignments') ? (
116
+ {assignments ? (
117
117
  <Toolbar.Item
118
118
  icon="label"
119
119
  active={showAssignments}
@@ -28,6 +28,13 @@ export interface VibrationalEditorProps {
28
28
  * @default undefined
29
29
  */
30
30
  onChange?: (spectra: IrSpectrum[]) => void;
31
+ /**
32
+ * Whether the Bands panel says what each band could be. An exercise switches
33
+ * it off, so the reading of the bands is the student's; the table's ranges
34
+ * can still be offered to look up, in a panel of the host's.
35
+ * @default true
36
+ */
37
+ assignments?: boolean;
31
38
  /**
32
39
  * Which value axis to open in; one the technique does not offer opens its
33
40
  * first instead.
@@ -79,6 +86,7 @@ export function VibrationalEditor(props: VibrationalEditorProps) {
79
86
  technique,
80
87
  data = NO_SPECTRA,
81
88
  onChange,
89
+ assignments,
82
90
  mode,
83
91
  defaultPanelIds,
84
92
  panels = NO_PANELS,
@@ -92,6 +100,7 @@ export function VibrationalEditor(props: VibrationalEditorProps) {
92
100
  settings: { technique, ...(mode === undefined ? {} : { mode }) },
93
101
  panelIds: defaultPanelIds,
94
102
  onChange,
103
+ assignments,
95
104
  });
96
105
 
97
106
  return (
@@ -31,6 +31,13 @@ export interface UseIrEditorOptions extends CreateIrStateOptions {
31
31
  * @default undefined
32
32
  */
33
33
  onChange?: (spectra: IrSpectrum[]) => void;
34
+ /**
35
+ * Whether the correlation table is asked what each band could be. An
36
+ * exercise switches it off, so the reading of the bands is the student's.
37
+ * A technique the table does not cover is never asked, whatever this says.
38
+ * @default true
39
+ */
40
+ assignments?: boolean;
34
41
  }
35
42
 
36
43
  /** What a component drives the viewer through. */
@@ -47,6 +54,11 @@ export interface IrEditorApi {
47
54
  * without a candidate for a technique the correlation table does not cover.
48
55
  */
49
56
  assigned: AssignedBand[];
57
+ /**
58
+ * Whether the bands are offered what they could be: the technique is one the
59
+ * correlation table covers, and the host has not switched it off.
60
+ */
61
+ assignments: boolean;
50
62
  /** Everything to do. */
51
63
  actions: IrEditorActions;
52
64
  }
@@ -57,7 +69,7 @@ export interface IrEditorApi {
57
69
  * @returns The state, what follows from it, and everything to do.
58
70
  */
59
71
  export function useIrEditor(options: UseIrEditorOptions = {}): IrEditorApi {
60
- const { onChange, ...initial } = options;
72
+ const { onChange, assignments: allowed = true, ...initial } = options;
61
73
 
62
74
  const [state, dispatch] = useReducer(irReducer, initial, createIrState);
63
75
  const actions = useMemo(() => createIrActions(dispatch), [dispatch]);
@@ -93,7 +105,7 @@ export function useIrEditor(options: UseIrEditorOptions = {}): IrEditorApi {
93
105
  // Assigning is a walk of thirty ranges per band, which is nothing beside the
94
106
  // picking — so it is a memo of its own, and moving the tolerance does not
95
107
  // re-pick. A technique the table does not cover is matched against no table.
96
- const { assignments } = IR_TECHNIQUE_RULES[technique];
108
+ const assignments = allowed && IR_TECHNIQUE_RULES[technique].assignments;
97
109
  const assigned = useMemo(
98
110
  () =>
99
111
  assignBands(bands, {
@@ -115,8 +127,15 @@ export function useIrEditor(options: UseIrEditorOptions = {}): IrEditorApi {
115
127
  }, [spectra, onChange]);
116
128
 
117
129
  return useMemo(
118
- () => ({ state, visibleSpectra, selectedSpectrum, assigned, actions }),
119
- [state, visibleSpectra, selectedSpectrum, assigned, actions],
130
+ () => ({
131
+ state,
132
+ visibleSpectra,
133
+ selectedSpectrum,
134
+ assigned,
135
+ assignments,
136
+ actions,
137
+ }),
138
+ [state, visibleSpectra, selectedSpectrum, assigned, assignments, actions],
120
139
  );
121
140
  }
122
141
 
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Als SVG kopieren",
180
180
  "share.qrCodeHint": "Das PNG lässt sich überall einfügen; das SVG bleibt in jeder Größe scharf.",
181
181
  "share.qrCodeTooLong": "Dieser Link ist zu lang für einen QR-Code. Kopieren Sie stattdessen den Link.",
182
+ "share.opensOn": "Der Link öffnet",
183
+ "share.opensOnScreen": "Was auf dem Bildschirm ist",
184
+ "share.opensOnScreenNote": "Der Link enthält es vollständig, es ist also keine Datei zu senden.",
185
+ "share.opensOnTooLarge": "Zu groß für einen Link: Speichern Sie es als Datei und senden Sie die Datei.",
186
+ "share.opensOnExample": "Ein Beispiel",
187
+ "share.opensOnExampleLabel": "Beispiel",
188
+ "share.opensOnChanged": "Das Beispiel im Original: Die hier gemachten Änderungen bleiben zurück.",
189
+ "share.opensOnNone": "Nur die Seite",
190
+ "share.opensOnNoneNote": "Jeder Besucher findet dort, was er zurückgelassen hat, oder eine leere Seite.",
182
191
  "seal.refusedTitle": "Dieser Link lässt sich nicht öffnen",
183
192
  "seal.refusedBody": "Er wurde nach dem Erstellen verändert oder ist unvollständig, daher wurde nichts geöffnet. Bitten Sie erneut um den Link.",
184
193
  "seal.openWithout": "Das Werkzeug ohne den Link öffnen",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copy as SVG",
180
180
  "share.qrCodeHint": "The PNG pastes anywhere; the SVG stays sharp at any size.",
181
181
  "share.qrCodeTooLong": "This link is too long for a QR code. Copy the link instead.",
182
+ "share.opensOn": "Opens on",
183
+ "share.opensOnScreen": "What is on screen",
184
+ "share.opensOnScreenNote": "The link carries it whole, so there is no file to send.",
185
+ "share.opensOnTooLarge": "Too large for a link: save it as a file and send the file.",
186
+ "share.opensOnExample": "An example",
187
+ "share.opensOnExampleLabel": "Example",
188
+ "share.opensOnChanged": "The example as it comes: the changes made here stay behind.",
189
+ "share.opensOnNone": "The page alone",
190
+ "share.opensOnNoneNote": "Each visitor finds what they left there, or an empty page.",
182
191
  "seal.refusedTitle": "This link cannot be opened",
183
192
  "seal.refusedBody": "It was changed after it was made, or it is incomplete, so nothing was opened. Ask for the link again.",
184
193
  "seal.openWithout": "Open the tool without it",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copiar como SVG",
180
180
  "share.qrCodeHint": "El PNG se pega en cualquier sitio; el SVG se ve nítido a cualquier tamaño.",
181
181
  "share.qrCodeTooLong": "Este enlace es demasiado largo para un código QR. Copia el enlace en su lugar.",
182
+ "share.opensOn": "El enlace abre",
183
+ "share.opensOnScreen": "Lo que hay en pantalla",
184
+ "share.opensOnScreenNote": "El enlace lo lleva entero, así que no hay ningún archivo que enviar.",
185
+ "share.opensOnTooLarge": "Demasiado grande para un enlace: guárdelo en un archivo y envíe el archivo.",
186
+ "share.opensOnExample": "Un ejemplo",
187
+ "share.opensOnExampleLabel": "Ejemplo",
188
+ "share.opensOnChanged": "El ejemplo tal como viene: los cambios hechos aquí no se incluyen.",
189
+ "share.opensOnNone": "Solo la página",
190
+ "share.opensOnNoneNote": "Cada visitante encuentra lo que dejó allí, o una página vacía.",
182
191
  "seal.refusedTitle": "Este enlace no se puede abrir",
183
192
  "seal.refusedBody": "Se modificó después de crearse, o está incompleto, así que no se abrió nada. Pide el enlace de nuevo.",
184
193
  "seal.openWithout": "Abrir la herramienta sin el enlace",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copier en SVG",
180
180
  "share.qrCodeHint": "Le PNG se colle partout ; le SVG reste net à toute taille.",
181
181
  "share.qrCodeTooLong": "Ce lien est trop long pour un code QR. Copiez plutôt le lien.",
182
+ "share.opensOn": "Le lien ouvre",
183
+ "share.opensOnScreen": "Ce qui est à l’écran",
184
+ "share.opensOnScreenNote": "Le lien le contient en entier : aucun fichier à envoyer.",
185
+ "share.opensOnTooLarge": "Trop grand pour un lien : enregistrez-le dans un fichier et envoyez le fichier.",
186
+ "share.opensOnExample": "Un exemple",
187
+ "share.opensOnExampleLabel": "Exemple",
188
+ "share.opensOnChanged": "L’exemple tel qu’il est fourni : les modifications faites ici n’en font pas partie.",
189
+ "share.opensOnNone": "La page seule",
190
+ "share.opensOnNoneNote": "Chaque visiteur y retrouve ce qu’il avait laissé, ou une page vide.",
182
191
  "seal.refusedTitle": "Ce lien ne peut pas être ouvert",
183
192
  "seal.refusedBody": "Il a été modifié après sa création, ou il est incomplet : rien n'a été ouvert. Demandez à nouveau le lien.",
184
193
  "seal.openWithout": "Ouvrir l'outil sans ce lien",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copia come SVG",
180
180
  "share.qrCodeHint": "Il PNG si incolla ovunque; l'SVG resta nitido a ogni dimensione.",
181
181
  "share.qrCodeTooLong": "Questo link è troppo lungo per un codice QR. Copia invece il link.",
182
+ "share.opensOn": "Il link apre",
183
+ "share.opensOnScreen": "Ciò che è sullo schermo",
184
+ "share.opensOnScreenNote": "Il link lo contiene per intero, quindi non c’è nessun file da inviare.",
185
+ "share.opensOnTooLarge": "Troppo grande per un link: salvalo in un file e invia il file.",
186
+ "share.opensOnExample": "Un esempio",
187
+ "share.opensOnExampleLabel": "Esempio",
188
+ "share.opensOnChanged": "L’esempio così come viene fornito: le modifiche fatte qui non sono incluse.",
189
+ "share.opensOnNone": "Solo la pagina",
190
+ "share.opensOnNoneNote": "Ogni visitatore ritrova ciò che vi aveva lasciato, o una pagina vuota.",
182
191
  "seal.refusedTitle": "Questo link non può essere aperto",
183
192
  "seal.refusedBody": "È stato modificato dopo la creazione, o è incompleto, quindi non si è aperto nulla. Chiedi di nuovo il link.",
184
193
  "seal.openWithout": "Apri lo strumento senza il link",
@@ -32,10 +32,18 @@ export interface ExerciseActionsProps {
32
32
  */
33
33
  hintsRevealed?: number;
34
34
  /**
35
- * How many hints the exercise has, for the button's count.
35
+ * How many hints the exercise has, for the button's count. `null` for a
36
+ * ladder that has no fixed length — each hint chosen from what the answer
37
+ * holds when it is asked for — so the button carries no count and
38
+ * `hintDisabled` alone says when there is nothing more to say.
36
39
  * @default 0
37
40
  */
38
- hintCount?: number;
41
+ hintCount?: number | null;
42
+ /**
43
+ * Whether no further hint can be given now, beyond the count being reached.
44
+ * @default false
45
+ */
46
+ hintDisabled?: boolean;
39
47
  /**
40
48
  * Shows or hides the sample answer. Never gated behind anything: getting
41
49
  * stuck and reading the answer is part of how the intuition is built.
@@ -84,6 +92,7 @@ export function ExerciseActions(props: ExerciseActionsProps): ReactElement {
84
92
  onRevealHint,
85
93
  hintsRevealed = 0,
86
94
  hintCount = 0,
95
+ hintDisabled = false,
87
96
  onToggleSolution,
88
97
  showSolution = false,
89
98
  onReset,
@@ -106,11 +115,17 @@ export function ExerciseActions(props: ExerciseActionsProps): ReactElement {
106
115
  {onRevealHint !== undefined && (
107
116
  <Button
108
117
  icon="lightbulb"
109
- text={t('pedagogy.revealHintCount', {
110
- revealed: Math.min(hintsRevealed, hintCount),
111
- total: hintCount,
112
- })}
113
- disabled={hintsRevealed >= hintCount}
118
+ text={
119
+ hintCount === null
120
+ ? t('pedagogy.revealHint')
121
+ : t('pedagogy.revealHintCount', {
122
+ revealed: Math.min(hintsRevealed, hintCount),
123
+ total: hintCount,
124
+ })
125
+ }
126
+ disabled={
127
+ hintDisabled || (hintCount !== null && hintsRevealed >= hintCount)
128
+ }
114
129
  onClick={onRevealHint}
115
130
  />
116
131
  )}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The examples a page opened, and the documents they opened as, so a link can
3
+ * tell an example still as it opened from one changed since.
4
+ */
5
+
6
+ import type { ShareOnScreen } from './opensOn.ts';
7
+
8
+ /** The examples a page opened, as {@link createExampleMark} keeps them. */
9
+ export interface ExampleMark<TDocument extends object> {
10
+ /**
11
+ * Say the document on screen is an example, as it opened.
12
+ * @param id - The example.
13
+ * @param document - The document it opened as.
14
+ */
15
+ mark: (id: string, document: TDocument) => void;
16
+ /** Say what is on screen came from no example: a file, a draft, a clear page. */
17
+ clear: () => void;
18
+ /**
19
+ * What the Share dialog is told of the document on screen.
20
+ * @param document - The document on screen.
21
+ * @returns The example it opened on, and whether it has changed since.
22
+ */
23
+ read: (document: TDocument) => Pick<ShareOnScreen, 'example' | 'changed'>;
24
+ }
25
+
26
+ /**
27
+ * Keep the examples a page opened. Documents are compared by identity: every
28
+ * change replaces the document, so the one an example opened as is the one on
29
+ * screen until something is changed, and an undo back to it — even past a file
30
+ * opened since — makes it that example again.
31
+ * @returns The mark.
32
+ */
33
+ export function createExampleMark<
34
+ TDocument extends object,
35
+ >(): ExampleMark<TDocument> {
36
+ const opened = new WeakMap<TDocument, string>();
37
+ let last: string | null = null;
38
+ return {
39
+ mark: (id, document) => {
40
+ opened.set(document, id);
41
+ last = id;
42
+ },
43
+ clear: () => {
44
+ last = null;
45
+ },
46
+ read: (document) => {
47
+ const example = opened.get(document);
48
+ if (example !== undefined) return { example, changed: false };
49
+ return { example: last, changed: last !== null };
50
+ },
51
+ };
52
+ }
@@ -19,8 +19,33 @@ export {
19
19
  sharePreviewSize,
20
20
  } from './devices.ts';
21
21
  export { escapeAttribute, escapeText } from './escape.ts';
22
+ export type { ExampleMark } from './exampleMark.ts';
23
+ export { createExampleMark } from './exampleMark.ts';
22
24
  export type { KeySetParamOptions } from './keySetParam.ts';
23
25
  export { keySetParam } from './keySetParam.ts';
26
+ export {
27
+ MAX_LINK_TOKEN,
28
+ packLinkValue,
29
+ packLinkValueIfFits,
30
+ unpackLinkValue,
31
+ } from './linkValue.ts';
32
+ export type {
33
+ OpensOnAddresses,
34
+ OpensOnChoice,
35
+ OpensOnLinks,
36
+ OpensOnTarget,
37
+ ScreenLink,
38
+ ShareAddress,
39
+ ShareExample,
40
+ ShareOnScreen,
41
+ } from './opensOn.ts';
42
+ export {
43
+ EXAMPLE_PARAM,
44
+ carriedScreen,
45
+ defaultOpensOn,
46
+ opensOnAddress,
47
+ opensOnLinks,
48
+ } from './opensOn.ts';
24
49
  export type { PageShareConfig } from './pageShareConfig.ts';
25
50
  export { createPageShareConfig } from './pageShareConfig.ts';
26
51
  export type { ShareRegion } from './regions.ts';
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Bytes written into a link: in the URL-safe base64 alphabet, and deflated or
3
+ * inflated through the browser's own streams.
4
+ */
5
+
6
+ /**
7
+ * Write bytes in the URL-safe base64 alphabet, without padding.
8
+ * @param bytes - The bytes.
9
+ * @returns The text.
10
+ */
11
+ export function toBase64Url(bytes: Uint8Array): string {
12
+ let binary = '';
13
+ for (const byte of bytes) binary += String.fromCodePoint(byte);
14
+ return btoa(binary)
15
+ .replaceAll('+', '-')
16
+ .replaceAll('/', '_')
17
+ .replace(/[=]+$/, '');
18
+ }
19
+
20
+ /**
21
+ * Read bytes written by {@link toBase64Url}.
22
+ * @param text - The text, without padding.
23
+ * @returns The bytes, or `null` when the text is not URL-safe base64.
24
+ */
25
+ export function fromBase64Url(text: string): Uint8Array<ArrayBuffer> | null {
26
+ if (!/^[\w-]*$/.test(text) || text.length % 4 === 1) return null;
27
+ const base64 = text.replaceAll('-', '+').replaceAll('_', '/');
28
+ const binary = atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '='));
29
+ const bytes = new Uint8Array(binary.length);
30
+ for (let index = 0; index < binary.length; index++) {
31
+ bytes[index] = binary.codePointAt(index) ?? 0;
32
+ }
33
+ return bytes;
34
+ }
35
+
36
+ /**
37
+ * Pass bytes through a compression or a decompression stream.
38
+ * @param bytes - The bytes in.
39
+ * @param stream - The stream.
40
+ * @param limit - The most the bytes out may weigh, so a link made by hand
41
+ * cannot make the page inflate without end.
42
+ * @default Infinity
43
+ * @returns The bytes out.
44
+ * @throws {Error} When the bytes out weigh more than `limit`, or the stream
45
+ * cannot read the bytes in.
46
+ */
47
+ export async function pipeBytes(
48
+ bytes: Uint8Array<ArrayBuffer>,
49
+ stream: CompressionStream | DecompressionStream,
50
+ limit = Infinity,
51
+ ): Promise<Uint8Array<ArrayBuffer>> {
52
+ // A stream of its own, not a Blob's: jsdom's Blob has no `stream()`.
53
+ const source = new ReadableStream<Uint8Array<ArrayBuffer>>({
54
+ start(controller) {
55
+ controller.enqueue(bytes);
56
+ controller.close();
57
+ },
58
+ });
59
+ const output = source.pipeThrough(stream).pipeThrough(capped(limit));
60
+ return new Uint8Array(await new Response(output).arrayBuffer());
61
+ }
62
+
63
+ function capped(limit: number): TransformStream<Uint8Array, Uint8Array> {
64
+ let length = 0;
65
+ return new TransformStream({
66
+ transform(chunk, controller) {
67
+ length += chunk.byteLength;
68
+ if (length > limit) {
69
+ controller.error(new Error('The value unpacks larger than allowed.'));
70
+ return;
71
+ }
72
+ controller.enqueue(chunk);
73
+ },
74
+ });
75
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * A value written into a link: its JSON, deflated, in the URL-safe base64
3
+ * alphabet, so the link opens what was on screen with no file beside it and
4
+ * no server behind it. A circuit of twenty parts takes about a thousand
5
+ * characters.
6
+ */
7
+
8
+ import { fromBase64Url, pipeBytes, toBase64Url } from './linkBytes.ts';
9
+
10
+ /**
11
+ * The longest token a link is given: with the rest of the address it stays
12
+ * under the 8 KB request line nginx and Apache accept by default. A page of
13
+ * text fits; a pasted picture rarely does.
14
+ */
15
+ export const MAX_LINK_TOKEN = 7000;
16
+
17
+ // Bound what a link made by hand can make the page read and inflate.
18
+ const MAX_READ_TOKEN = 32 * 1024;
19
+ const MAX_UNPACKED_BYTES = 4 * 1024 * 1024;
20
+
21
+ /**
22
+ * Write a value as the token a link carries.
23
+ * @param value - The value, as JSON writes it.
24
+ * @returns The token, in the URL-safe base64 alphabet without padding.
25
+ */
26
+ export async function packLinkValue(value: unknown): Promise<string> {
27
+ const json = new TextEncoder().encode(JSON.stringify(value));
28
+ const packed = await pipeBytes(json, new CompressionStream('deflate-raw'));
29
+ return toBase64Url(packed);
30
+ }
31
+
32
+ /**
33
+ * Read the value a link carries. The link may come from anywhere, so what it
34
+ * holds is trusted for nothing: the caller checks it as it would a file.
35
+ * @param token - The token, as {@link packLinkValue} writes it.
36
+ * @returns The value, or `undefined` when the token holds none.
37
+ */
38
+ export async function unpackLinkValue(token: string): Promise<unknown> {
39
+ if (token.length > MAX_READ_TOKEN) return undefined;
40
+ const bytes = fromBase64Url(token);
41
+ if (!bytes) return undefined;
42
+ try {
43
+ const json = await pipeBytes(
44
+ bytes,
45
+ new DecompressionStream('deflate-raw'),
46
+ MAX_UNPACKED_BYTES,
47
+ );
48
+ return JSON.parse(new TextDecoder().decode(json));
49
+ } catch {
50
+ return undefined;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Write a value as the token a link carries, unless the token would be too
56
+ * long for one.
57
+ * @param value - The value, as JSON writes it.
58
+ * @returns The token, or `undefined` when it is longer than
59
+ * {@link MAX_LINK_TOKEN}.
60
+ */
61
+ export async function packLinkValueIfFits(
62
+ value: unknown,
63
+ ): Promise<string | undefined> {
64
+ const token = await packLinkValue(value);
65
+ return token.length > MAX_LINK_TOKEN ? undefined : token;
66
+ }