react-cheminfo 0.128.0 → 0.130.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -43
- package/lib/i18n/core/chromeCatalog.d.ts +1 -1
- package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
- package/lib/locales/de.json +9 -0
- package/lib/locales/en.json +9 -0
- package/lib/locales/es.json +9 -0
- package/lib/locales/fr.json +9 -0
- package/lib/locales/it.json +9 -0
- package/lib/mass/ui/MassChart.d.ts.map +1 -1
- package/lib/mass/ui/MassChart.js +4 -2
- package/lib/mass/ui/MassChart.js.map +1 -1
- package/lib/mass/ui/PeakLabels.d.ts +9 -3
- package/lib/mass/ui/PeakLabels.d.ts.map +1 -1
- package/lib/mass/ui/PeakLabels.js +38 -15
- package/lib/mass/ui/PeakLabels.js.map +1 -1
- package/lib/mass/ui/massChartProps.d.ts +8 -0
- package/lib/mass/ui/massChartProps.d.ts.map +1 -1
- package/lib/share/core/exampleMark.d.ts +31 -0
- package/lib/share/core/exampleMark.d.ts.map +1 -0
- package/lib/share/core/exampleMark.js +31 -0
- package/lib/share/core/exampleMark.js.map +1 -0
- package/lib/share/core/index.d.ts +5 -0
- package/lib/share/core/index.d.ts.map +1 -1
- package/lib/share/core/index.js +3 -0
- package/lib/share/core/index.js.map +1 -1
- package/lib/share/core/linkBytes.d.ts +29 -0
- package/lib/share/core/linkBytes.d.ts.map +1 -0
- package/lib/share/core/linkBytes.js +70 -0
- package/lib/share/core/linkBytes.js.map +1 -0
- package/lib/share/core/linkValue.d.ts +34 -0
- package/lib/share/core/linkValue.d.ts.map +1 -0
- package/lib/share/core/linkValue.js +58 -0
- package/lib/share/core/linkValue.js.map +1 -0
- package/lib/share/core/opensOn.d.ts +133 -0
- package/lib/share/core/opensOn.d.ts.map +1 -0
- package/lib/share/core/opensOn.js +82 -0
- package/lib/share/core/opensOn.js.map +1 -0
- package/lib/share/core/seal.d.ts.map +1 -1
- package/lib/share/core/seal.js +3 -32
- package/lib/share/core/seal.js.map +1 -1
- package/lib/share/ui/ShareOpensOn.d.ts +32 -0
- package/lib/share/ui/ShareOpensOn.d.ts.map +1 -0
- package/lib/share/ui/ShareOpensOn.js +49 -0
- package/lib/share/ui/ShareOpensOn.js.map +1 -0
- package/lib/share/ui/index.d.ts +4 -0
- package/lib/share/ui/index.d.ts.map +1 -1
- package/lib/share/ui/index.js +2 -0
- package/lib/share/ui/index.js.map +1 -1
- package/lib/share/ui/useOpensOnShare.d.ts +65 -0
- package/lib/share/ui/useOpensOnShare.d.ts.map +1 -0
- package/lib/share/ui/useOpensOnShare.js +60 -0
- package/lib/share/ui/useOpensOnShare.js.map +1 -0
- package/package.json +1 -1
- package/src/locales/de.json +9 -0
- package/src/locales/en.json +9 -0
- package/src/locales/es.json +9 -0
- package/src/locales/fr.json +9 -0
- package/src/locales/it.json +9 -0
- package/src/mass/ui/MassChart.tsx +4 -0
- package/src/mass/ui/PeakLabels.tsx +50 -18
- package/src/mass/ui/massChartProps.ts +8 -0
- package/src/share/core/exampleMark.ts +52 -0
- package/src/share/core/index.ts +25 -0
- package/src/share/core/linkBytes.ts +75 -0
- package/src/share/core/linkValue.ts +66 -0
- package/src/share/core/opensOn.ts +203 -0
- package/src/share/core/seal.ts +3 -36
- package/src/share/ui/ShareOpensOn.tsx +169 -0
- package/src/share/ui/index.ts +7 -0
- package/src/share/ui/useOpensOnShare.tsx +152 -0
- package/styles/chrome.css +18 -0
package/src/locales/en.json
CHANGED
|
@@ -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",
|
package/src/locales/es.json
CHANGED
|
@@ -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",
|
package/src/locales/fr.json
CHANGED
|
@@ -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",
|
package/src/locales/it.json
CHANGED
|
@@ -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",
|
|
@@ -50,6 +50,7 @@ export function MassChart(props: MassChartProps): ReactElement {
|
|
|
50
50
|
domain = null,
|
|
51
51
|
onDomainChange,
|
|
52
52
|
onPlotClick,
|
|
53
|
+
onPeakLabelClick,
|
|
53
54
|
drag = 'xAxis',
|
|
54
55
|
answersKeys = true,
|
|
55
56
|
emphasisedId = null,
|
|
@@ -196,6 +197,9 @@ export function MassChart(props: MassChartProps): ReactElement {
|
|
|
196
197
|
showCharges={showCharges}
|
|
197
198
|
mzDecimals={labelDecimals}
|
|
198
199
|
{...(annotationLabels === undefined ? {} : { annotationLabels })}
|
|
200
|
+
{...(onPeakLabelClick === undefined
|
|
201
|
+
? {}
|
|
202
|
+
: { onClick: onPeakLabelClick })}
|
|
199
203
|
color={spectra.length > 1 ? labelled.color : undefined}
|
|
200
204
|
/>
|
|
201
205
|
) : null}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CSSProperties, ReactElement } from 'react';
|
|
1
|
+
import type { CSSProperties, PointerEvent, ReactElement } from 'react';
|
|
2
2
|
import { useMemo } from 'react';
|
|
3
3
|
|
|
4
4
|
import type { PlotRect } from '../../chart/core/chartGeometry.ts';
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
import type { HoveredPeak } from '../core/labelStackBox.ts';
|
|
14
14
|
import type { MassSpectrum } from '../core/massSpectrum.ts';
|
|
15
15
|
import type { PeakLabelLineKind } from '../core/peakLabelLines.ts';
|
|
16
|
+
import type { PeakLabelPlan } from '../core/peakLabelPlan.ts';
|
|
16
17
|
import { planPeakLabels } from '../core/peakLabelPlan.ts';
|
|
17
18
|
|
|
18
19
|
export interface PeakLabelsProps {
|
|
@@ -76,6 +77,12 @@ export interface PeakLabelsProps {
|
|
|
76
77
|
* @default undefined
|
|
77
78
|
*/
|
|
78
79
|
onHover?: (peak: HoveredPeak | null) => void;
|
|
80
|
+
/**
|
|
81
|
+
* Called with the peak whose stack was clicked. The stack then shows a hand,
|
|
82
|
+
* and a press on it is its own: it starts no drag and is no click on the plot.
|
|
83
|
+
* @default undefined
|
|
84
|
+
*/
|
|
85
|
+
onClick?: (peak: HoveredPeak) => void;
|
|
79
86
|
}
|
|
80
87
|
|
|
81
88
|
/**
|
|
@@ -96,9 +103,9 @@ export interface PeakLabelsProps {
|
|
|
96
103
|
* around each glyph, where a filled box would hide the very data it is about.
|
|
97
104
|
*
|
|
98
105
|
* The numbers take no pointer event; the room they occupy does, as one
|
|
99
|
-
* transparent rectangle per stack, and only when somebody asked for it —
|
|
100
|
-
* stack
|
|
101
|
-
* underneath goes on following the pointer.
|
|
106
|
+
* transparent rectangle per stack, and only when somebody asked for it — to be
|
|
107
|
+
* told which stack the pointer rests on, or which one was clicked — while the
|
|
108
|
+
* crosshair underneath goes on following the pointer.
|
|
102
109
|
* @param props - Component props.
|
|
103
110
|
* @returns The marks and the labels, as SVG.
|
|
104
111
|
*/
|
|
@@ -116,6 +123,7 @@ export function PeakLabels(props: PeakLabelsProps): ReactElement {
|
|
|
116
123
|
color = CHART_COLORS.labelText,
|
|
117
124
|
intensityScale = 1,
|
|
118
125
|
onHover,
|
|
126
|
+
onClick,
|
|
119
127
|
} = props;
|
|
120
128
|
|
|
121
129
|
const plans = useMemo(
|
|
@@ -195,7 +203,7 @@ export function PeakLabels(props: PeakLabelsProps): ReactElement {
|
|
|
195
203
|
</text>
|
|
196
204
|
))}
|
|
197
205
|
</g>
|
|
198
|
-
{onHover === undefined
|
|
206
|
+
{onHover === undefined && onClick === undefined
|
|
199
207
|
? null
|
|
200
208
|
: written.map((plan) => (
|
|
201
209
|
<rect
|
|
@@ -205,25 +213,49 @@ export function PeakLabels(props: PeakLabelsProps): ReactElement {
|
|
|
205
213
|
width={plan.box.width}
|
|
206
214
|
height={plan.box.height}
|
|
207
215
|
fill="transparent"
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
216
|
+
{...(onHover === undefined
|
|
217
|
+
? {}
|
|
218
|
+
: {
|
|
219
|
+
onPointerEnter: () => onHover(hoveredPeak(plan, below)),
|
|
220
|
+
onPointerLeave: () => onHover(null),
|
|
221
|
+
})}
|
|
222
|
+
{...(onClick === undefined
|
|
223
|
+
? {}
|
|
224
|
+
: {
|
|
225
|
+
style: CLICKABLE_STACK,
|
|
226
|
+
// Kept from the chart, which would capture the pointer for a
|
|
227
|
+
// drag and take the click with it.
|
|
228
|
+
onPointerDown: (event: PointerEvent) =>
|
|
229
|
+
event.stopPropagation(),
|
|
230
|
+
onClick: () => onClick(hoveredPeak(plan, below)),
|
|
231
|
+
})}
|
|
221
232
|
/>
|
|
222
233
|
))}
|
|
223
234
|
</g>
|
|
224
235
|
);
|
|
225
236
|
}
|
|
226
237
|
|
|
238
|
+
/**
|
|
239
|
+
* The peak a stack is about, as the pointer reports it.
|
|
240
|
+
* @param plan - The stack.
|
|
241
|
+
* @param below - Whether the stacks hang under the sticks.
|
|
242
|
+
* @returns The peak, anchored at the edge of the stack facing away from the
|
|
243
|
+
* stick, so a card hanging off it never covers the peak it is about.
|
|
244
|
+
*/
|
|
245
|
+
function hoveredPeak(plan: PeakLabelPlan, below: boolean): HoveredPeak {
|
|
246
|
+
return {
|
|
247
|
+
key: plan.key,
|
|
248
|
+
mz: plan.peak.x,
|
|
249
|
+
intensity: plan.peak.y,
|
|
250
|
+
charge: plan.peak.charge,
|
|
251
|
+
x: plan.labelX,
|
|
252
|
+
y: below ? plan.box.y + plan.box.height : plan.box.y,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A stack that can be clicked says so. */
|
|
257
|
+
const CLICKABLE_STACK: CSSProperties = { cursor: 'pointer' };
|
|
258
|
+
|
|
227
259
|
/**
|
|
228
260
|
* How each line of a stack is set.
|
|
229
261
|
*
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import type { ReactNode } from 'react';
|
|
15
15
|
|
|
16
16
|
import type { ZoomDragMode } from '../../chart/core/chartDomain.ts';
|
|
17
|
+
import type { HoveredPeak } from '../core/labelStackBox.ts';
|
|
17
18
|
import type { MassDomain, MassSpectrum } from '../core/massSpectrum.ts';
|
|
18
19
|
import type { NearestPeak } from '../core/nearestPeak.ts';
|
|
19
20
|
|
|
@@ -93,6 +94,13 @@ export interface MassChartProps {
|
|
|
93
94
|
* @default undefined
|
|
94
95
|
*/
|
|
95
96
|
onPlotClick?: (mz: number, peak: NearestPeak | null) => void;
|
|
97
|
+
/**
|
|
98
|
+
* Told which labelled peak was clicked, by its label. The labels then show a
|
|
99
|
+
* hand, and a press on one is never a click on the plot: a host that wants the
|
|
100
|
+
* m/z a reader can see written passes this rather than `onPlotClick`.
|
|
101
|
+
* @default undefined
|
|
102
|
+
*/
|
|
103
|
+
onPeakLabelClick?: (peak: HoveredPeak) => void;
|
|
96
104
|
/**
|
|
97
105
|
* What a drag across the plot asks for: the m/z axis alone, or exactly the
|
|
98
106
|
* rectangle it sweeps out.
|
|
@@ -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
|
+
}
|
package/src/share/core/index.ts
CHANGED
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a link to a page holding a document opens on: what is on screen, one
|
|
3
|
+
* of the examples the site comes with, or the page alone. A site whose folder
|
|
4
|
+
* panel lists examples lets its Share dialog hand out any of them, so a link
|
|
5
|
+
* to an example can always be made, whatever is on screen.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { QueryEntry } from '../../router/core/query.ts';
|
|
9
|
+
import {
|
|
10
|
+
formatQueryEntries,
|
|
11
|
+
parseQueryEntries,
|
|
12
|
+
} from '../../router/core/query.ts';
|
|
13
|
+
|
|
14
|
+
/** The parameter naming the example a link opens on, on every site. */
|
|
15
|
+
export const EXAMPLE_PARAM = 'example';
|
|
16
|
+
|
|
17
|
+
/** What a link opens the page on. */
|
|
18
|
+
export type OpensOnTarget = 'screen' | 'example' | 'none';
|
|
19
|
+
|
|
20
|
+
/** An example a link can open on, as the folder panel lists it. */
|
|
21
|
+
export interface ShareExample {
|
|
22
|
+
/** Its identifier. */
|
|
23
|
+
id: string;
|
|
24
|
+
/** Its name. */
|
|
25
|
+
name: string;
|
|
26
|
+
/**
|
|
27
|
+
* What it shows, in a few words.
|
|
28
|
+
* @default undefined — its name alone
|
|
29
|
+
*/
|
|
30
|
+
description?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The group the picker lists it under: its field, its family.
|
|
33
|
+
* @default undefined — no group
|
|
34
|
+
*/
|
|
35
|
+
group?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Where a link goes, beyond the parameters the page keeps. */
|
|
39
|
+
export interface ShareAddress {
|
|
40
|
+
/** The parameters it adds. */
|
|
41
|
+
entries: readonly QueryEntry[];
|
|
42
|
+
/**
|
|
43
|
+
* The page it opens, as a path from the origin.
|
|
44
|
+
* @default undefined — the page on screen
|
|
45
|
+
*/
|
|
46
|
+
path?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What is on screen, as a link carries it, or why it cannot. */
|
|
50
|
+
export type ScreenLink = ShareAddress | 'too-large';
|
|
51
|
+
|
|
52
|
+
/** How a site writes a link to an example, and to the page alone. */
|
|
53
|
+
export interface OpensOnAddresses {
|
|
54
|
+
/**
|
|
55
|
+
* The link to an example.
|
|
56
|
+
* @default `?example=<id>` on the page on screen
|
|
57
|
+
*/
|
|
58
|
+
example?: (id: string) => ShareAddress;
|
|
59
|
+
/**
|
|
60
|
+
* The link to the page alone.
|
|
61
|
+
* @default the page on screen, with nothing added
|
|
62
|
+
*/
|
|
63
|
+
alone?: ShareAddress;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** What is on screen as the Share dialog opens. */
|
|
67
|
+
export interface ShareOnScreen {
|
|
68
|
+
/**
|
|
69
|
+
* The example the page opened on, changed since or not.
|
|
70
|
+
* @default null — none
|
|
71
|
+
*/
|
|
72
|
+
example?: string | null;
|
|
73
|
+
/**
|
|
74
|
+
* Whether what is on screen has changed since that example opened.
|
|
75
|
+
* @default false
|
|
76
|
+
*/
|
|
77
|
+
changed?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* What is on screen, as a link carries it.
|
|
80
|
+
* @default undefined — nothing is on screen, or the site writes no
|
|
81
|
+
* document into its links
|
|
82
|
+
*/
|
|
83
|
+
screen?: ScreenLink;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** What the Share dialog can hand out for the page on screen. */
|
|
87
|
+
export interface OpensOnLinks {
|
|
88
|
+
/** The page's own query string, what it opened on left out. */
|
|
89
|
+
search: string;
|
|
90
|
+
/** The example the page opened on, or `null`. */
|
|
91
|
+
example: string | null;
|
|
92
|
+
/** Whether what is on screen has changed since that example opened. */
|
|
93
|
+
changed: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* What is on screen, as a link carries it: `undefined` when nothing is,
|
|
96
|
+
* and for an example still as it opened, which the link names instead.
|
|
97
|
+
*/
|
|
98
|
+
screen: ScreenLink | undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** What the link opens on, and which example. */
|
|
102
|
+
export interface OpensOnChoice {
|
|
103
|
+
/** What the link opens the page on. */
|
|
104
|
+
opensOn: OpensOnTarget;
|
|
105
|
+
/** The example the link names when it opens on one. */
|
|
106
|
+
example: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* What the Share dialog can hand out for the page on screen.
|
|
111
|
+
* @param search - The query string of the page.
|
|
112
|
+
* @param openedOn - The parameters naming what a page opens on, which a link
|
|
113
|
+
* replaces: the example, and whatever carries a document.
|
|
114
|
+
* @param onScreen - What is on screen.
|
|
115
|
+
* @returns The links.
|
|
116
|
+
*/
|
|
117
|
+
export function opensOnLinks(
|
|
118
|
+
search: string,
|
|
119
|
+
openedOn: readonly string[],
|
|
120
|
+
onScreen: ShareOnScreen,
|
|
121
|
+
): OpensOnLinks {
|
|
122
|
+
const kept = parseQueryEntries(search).filter(
|
|
123
|
+
([key]) => !openedOn.includes(key),
|
|
124
|
+
);
|
|
125
|
+
const example = onScreen.example ?? null;
|
|
126
|
+
const changed = example !== null && (onScreen.changed ?? false);
|
|
127
|
+
return {
|
|
128
|
+
search: formatQueryEntries(kept),
|
|
129
|
+
example,
|
|
130
|
+
changed,
|
|
131
|
+
screen: example !== null && !changed ? undefined : onScreen.screen,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* What the dialog opens on: the example on screen while it is as it opened,
|
|
137
|
+
* else what is on screen when a link can carry it, else the example it was
|
|
138
|
+
* changed from, else the page alone.
|
|
139
|
+
* @param links - What the dialog can hand out.
|
|
140
|
+
* @param examples - The examples a link can open on, in the order they are
|
|
141
|
+
* listed.
|
|
142
|
+
* @returns The choice.
|
|
143
|
+
*/
|
|
144
|
+
export function defaultOpensOn(
|
|
145
|
+
links: OpensOnLinks,
|
|
146
|
+
examples: readonly ShareExample[],
|
|
147
|
+
): OpensOnChoice {
|
|
148
|
+
const listed =
|
|
149
|
+
links.example !== null &&
|
|
150
|
+
examples.some((example) => example.id === links.example);
|
|
151
|
+
const example = listed ? (links.example ?? '') : (examples[0]?.id ?? '');
|
|
152
|
+
if (listed && !links.changed) return { opensOn: 'example', example };
|
|
153
|
+
if (carriedScreen(links.screen)) return { opensOn: 'screen', example };
|
|
154
|
+
return { opensOn: listed ? 'example' : 'none', example };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The address of a link: the query string, and the page when it is not the
|
|
159
|
+
* page on screen.
|
|
160
|
+
* @param links - What the dialog can hand out.
|
|
161
|
+
* @param choice - What the link opens on.
|
|
162
|
+
* @param addresses - How the site writes a link to an example, and to the
|
|
163
|
+
* page alone.
|
|
164
|
+
* @default {} — `?example=<id>` and the page on screen
|
|
165
|
+
* @returns The query string, and the path or `undefined`.
|
|
166
|
+
*/
|
|
167
|
+
export function opensOnAddress(
|
|
168
|
+
links: OpensOnLinks,
|
|
169
|
+
choice: OpensOnChoice,
|
|
170
|
+
addresses: OpensOnAddresses = {},
|
|
171
|
+
): { search: string; path: string | undefined } {
|
|
172
|
+
const added = addressOf(links, choice, addresses);
|
|
173
|
+
const entries = [...parseQueryEntries(links.search), ...added.entries];
|
|
174
|
+
return { search: formatQueryEntries(entries), path: added.path };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* What is on screen, when a link can carry it.
|
|
179
|
+
* @param screen - What is on screen, as a link carries it.
|
|
180
|
+
* @returns The address, or `undefined` when there is nothing a link carries.
|
|
181
|
+
*/
|
|
182
|
+
export function carriedScreen(
|
|
183
|
+
screen: ScreenLink | undefined,
|
|
184
|
+
): ShareAddress | undefined {
|
|
185
|
+
return screen === undefined || screen === 'too-large' ? undefined : screen;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function addressOf(
|
|
189
|
+
links: OpensOnLinks,
|
|
190
|
+
choice: OpensOnChoice,
|
|
191
|
+
addresses: OpensOnAddresses,
|
|
192
|
+
): ShareAddress {
|
|
193
|
+
const alone = addresses.alone ?? { entries: [] };
|
|
194
|
+
if (choice.opensOn === 'screen') return carriedScreen(links.screen) ?? alone;
|
|
195
|
+
if (choice.opensOn === 'example' && choice.example !== '') {
|
|
196
|
+
return (
|
|
197
|
+
addresses.example?.(choice.example) ?? {
|
|
198
|
+
entries: [[EXAMPLE_PARAM, choice.example]],
|
|
199
|
+
}
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return alone;
|
|
203
|
+
}
|