react-cheminfo 0.70.0 → 0.72.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 +57 -2
- package/lib/about/core/providers.d.ts +6 -0
- package/lib/about/core/providers.d.ts.map +1 -1
- package/lib/about/core/providers.js +7 -0
- package/lib/about/core/providers.js.map +1 -1
- package/lib/about/ui/AboutProvidedBy.js +34 -0
- package/lib/about/ui/AboutProvidedBy.js.map +1 -1
- package/lib/credits/core/credits.d.ts +6 -0
- package/lib/credits/core/credits.d.ts.map +1 -1
- package/lib/credits/core/credits.js +8 -0
- package/lib/credits/core/credits.js.map +1 -1
- package/lib/download/core/figurePng.d.ts +14 -0
- package/lib/download/core/figurePng.d.ts.map +1 -1
- package/lib/download/core/figurePng.js +21 -6
- package/lib/download/core/figurePng.js.map +1 -1
- package/lib/download/core/svgToPng.d.ts.map +1 -1
- package/lib/download/core/svgToPng.js +4 -2
- package/lib/download/core/svgToPng.js.map +1 -1
- package/lib/format/core/relativeTime.d.ts +8 -3
- package/lib/format/core/relativeTime.d.ts.map +1 -1
- package/lib/format/core/relativeTime.js +19 -7
- package/lib/format/core/relativeTime.js.map +1 -1
- package/lib/host/core/connectHost.d.ts +25 -34
- package/lib/host/core/connectHost.d.ts.map +1 -1
- package/lib/host/core/connectHost.js +43 -89
- package/lib/host/core/connectHost.js.map +1 -1
- package/lib/host/core/hostOutputs.d.ts +58 -0
- package/lib/host/core/hostOutputs.d.ts.map +1 -0
- package/lib/host/core/hostOutputs.js +151 -0
- package/lib/host/core/hostOutputs.js.map +1 -0
- package/lib/host/core/index.d.ts +9 -3
- package/lib/host/core/index.d.ts.map +1 -1
- package/lib/host/core/index.js +4 -1
- package/lib/host/core/index.js.map +1 -1
- package/lib/host/core/irKinds.d.ts +124 -0
- package/lib/host/core/irKinds.d.ts.map +1 -0
- package/lib/host/core/irKinds.js +46 -0
- package/lib/host/core/irKinds.js.map +1 -0
- package/lib/host/core/kinds.d.ts +13 -5
- package/lib/host/core/kinds.d.ts.map +1 -1
- package/lib/host/core/kinds.js +11 -5
- package/lib/host/core/kinds.js.map +1 -1
- package/lib/host/core/protocol.d.ts +50 -7
- package/lib/host/core/protocol.d.ts.map +1 -1
- package/lib/host/core/protocol.js +10 -6
- package/lib/host/core/protocol.js.map +1 -1
- package/lib/host/core/selectInputs.d.ts +37 -0
- package/lib/host/core/selectInputs.d.ts.map +1 -0
- package/lib/host/core/selectInputs.js +110 -0
- package/lib/host/core/selectInputs.js.map +1 -0
- package/lib/host/testing/sciPeaks.d.ts +31 -0
- package/lib/host/testing/sciPeaks.d.ts.map +1 -1
- package/lib/host/testing/sciPeaks.js +73 -0
- package/lib/host/testing/sciPeaks.js.map +1 -1
- package/lib/host/testing/standInHost.d.ts +9 -1
- package/lib/host/testing/standInHost.d.ts.map +1 -1
- package/lib/host/testing/standInHost.js +19 -1
- package/lib/host/testing/standInHost.js.map +1 -1
- package/lib/host/ui/useHostConnection.d.ts +13 -5
- package/lib/host/ui/useHostConnection.d.ts.map +1 -1
- package/lib/host/ui/useHostConnection.js +4 -3
- package/lib/host/ui/useHostConnection.js.map +1 -1
- package/lib/host-testing.d.ts +2 -2
- package/lib/host-testing.d.ts.map +1 -1
- package/lib/host-testing.js +2 -2
- package/lib/host-testing.js.map +1 -1
- package/lib/i18n/core/chromeCatalog.d.ts +1 -1
- package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
- package/lib/ir/core/handPicking.d.ts +58 -0
- package/lib/ir/core/handPicking.d.ts.map +1 -0
- package/lib/ir/core/handPicking.js +130 -0
- package/lib/ir/core/handPicking.js.map +1 -0
- package/lib/ir/core/index.d.ts +3 -0
- package/lib/ir/core/index.d.ts.map +1 -1
- package/lib/ir/core/index.js +3 -0
- package/lib/ir/core/index.js.map +1 -1
- package/lib/ir/core/irBandState.d.ts +57 -0
- package/lib/ir/core/irBandState.d.ts.map +1 -0
- package/lib/ir/core/irBandState.js +119 -0
- package/lib/ir/core/irBandState.js.map +1 -0
- package/lib/ir/core/irCommands.d.ts +11 -0
- package/lib/ir/core/irCommands.d.ts.map +1 -1
- package/lib/ir/core/irCommands.js +6 -0
- package/lib/ir/core/irCommands.js.map +1 -1
- package/lib/ir/core/irPeaks.d.ts +28 -0
- package/lib/ir/core/irPeaks.d.ts.map +1 -0
- package/lib/ir/core/irPeaks.js +66 -0
- package/lib/ir/core/irPeaks.js.map +1 -0
- package/lib/ir/core/irReducer.d.ts +8 -0
- package/lib/ir/core/irReducer.d.ts.map +1 -1
- package/lib/ir/core/irReducer.js +15 -32
- package/lib/ir/core/irReducer.js.map +1 -1
- package/lib/ir/core/irSpectrum.d.ts +15 -0
- package/lib/ir/core/irSpectrum.d.ts.map +1 -1
- package/lib/ir/core/irSpectrum.js.map +1 -1
- package/lib/ir/core/irState.d.ts +9 -6
- package/lib/ir/core/irState.d.ts.map +1 -1
- package/lib/ir/core/irState.js +10 -6
- package/lib/ir/core/irState.js.map +1 -1
- package/lib/ir/core/pickBands.d.ts +14 -0
- package/lib/ir/core/pickBands.d.ts.map +1 -1
- package/lib/ir/core/pickBands.js +39 -33
- package/lib/ir/core/pickBands.js.map +1 -1
- package/lib/ir/core/ramanBands.d.ts +9 -1
- package/lib/ir/core/ramanBands.d.ts.map +1 -1
- package/lib/ir/core/ramanBands.js +2 -2
- package/lib/ir/core/ramanBands.js.map +1 -1
- package/lib/ir/core/spectrumBands.d.ts +46 -0
- package/lib/ir/core/spectrumBands.d.ts.map +1 -0
- package/lib/ir/core/spectrumBands.js +98 -0
- package/lib/ir/core/spectrumBands.js.map +1 -0
- package/lib/ir/ui/BandRow.d.ts +5 -0
- package/lib/ir/ui/BandRow.d.ts.map +1 -1
- package/lib/ir/ui/BandRow.js +4 -3
- package/lib/ir/ui/BandRow.js.map +1 -1
- package/lib/ir/ui/BandsPanel.d.ts +5 -0
- package/lib/ir/ui/BandsPanel.d.ts.map +1 -1
- package/lib/ir/ui/BandsPanel.js +36 -16
- package/lib/ir/ui/BandsPanel.js.map +1 -1
- package/lib/ir/ui/BandsToolbar.d.ts +9 -0
- package/lib/ir/ui/BandsToolbar.d.ts.map +1 -0
- package/lib/ir/ui/BandsToolbar.js +61 -0
- package/lib/ir/ui/BandsToolbar.js.map +1 -0
- package/lib/ir/ui/IrCanvas.d.ts +2 -1
- package/lib/ir/ui/IrCanvas.d.ts.map +1 -1
- package/lib/ir/ui/IrCanvas.js +19 -5
- package/lib/ir/ui/IrCanvas.js.map +1 -1
- package/lib/ir/ui/IrChart.d.ts.map +1 -1
- package/lib/ir/ui/IrChart.js +8 -1
- package/lib/ir/ui/IrChart.js.map +1 -1
- package/lib/ir/ui/IrDocumentationDialog.js +1 -1
- package/lib/ir/ui/IrDocumentationDialog.js.map +1 -1
- package/lib/ir/ui/IrEditor.d.ts +4 -2
- package/lib/ir/ui/IrEditor.d.ts.map +1 -1
- package/lib/ir/ui/IrEditor.js.map +1 -1
- package/lib/ir/ui/IrShell.d.ts.map +1 -1
- package/lib/ir/ui/IrShell.js +6 -0
- package/lib/ir/ui/IrShell.js.map +1 -1
- package/lib/ir/ui/IrStatusBar.d.ts.map +1 -1
- package/lib/ir/ui/IrStatusBar.js +1 -1
- package/lib/ir/ui/IrStatusBar.js.map +1 -1
- package/lib/ir/ui/IrToolbar.d.ts +4 -3
- package/lib/ir/ui/IrToolbar.d.ts.map +1 -1
- package/lib/ir/ui/IrToolbar.js +5 -4
- package/lib/ir/ui/IrToolbar.js.map +1 -1
- package/lib/ir/ui/VibrationalEditor.d.ts +2 -1
- package/lib/ir/ui/VibrationalEditor.d.ts.map +1 -1
- package/lib/ir/ui/VibrationalEditor.js.map +1 -1
- package/lib/ir/ui/irActions.d.ts +5 -0
- package/lib/ir/ui/irActions.d.ts.map +1 -1
- package/lib/ir/ui/irActions.js +6 -0
- package/lib/ir/ui/irActions.js.map +1 -1
- package/lib/ir/ui/irChartProps.d.ts +4 -1
- package/lib/ir/ui/irChartProps.d.ts.map +1 -1
- package/lib/ir/ui/useIrEditor.d.ts +5 -11
- package/lib/ir/ui/useIrEditor.d.ts.map +1 -1
- package/lib/ir/ui/useIrEditor.js +17 -18
- package/lib/ir/ui/useIrEditor.js.map +1 -1
- package/lib/locales/de.json +63 -0
- package/lib/locales/en.json +63 -0
- package/lib/locales/es.json +63 -0
- package/lib/locales/fr.json +63 -0
- package/lib/locales/it.json +63 -0
- package/lib/slides/ui/slideSanitizeSchema.d.ts +3 -2
- package/lib/slides/ui/slideSanitizeSchema.d.ts.map +1 -1
- package/lib/slides/ui/slideSanitizeSchema.js +82 -2
- package/lib/slides/ui/slideSanitizeSchema.js.map +1 -1
- package/package.json +1 -1
- package/src/about/core/providers.ts +7 -0
- package/src/about/ui/AboutProvidedBy.tsx +49 -0
- package/src/credits/core/credits.ts +9 -0
- package/src/download/core/figurePng.ts +21 -8
- package/src/download/core/svgToPng.ts +3 -4
- package/src/format/core/relativeTime.ts +29 -7
- package/src/host/core/connectHost.ts +77 -120
- package/src/host/core/hostOutputs.ts +197 -0
- package/src/host/core/index.ts +13 -7
- package/src/host/core/irKinds.ts +92 -0
- package/src/host/core/kinds.ts +16 -6
- package/src/host/core/protocol.ts +50 -8
- package/src/host/core/selectInputs.ts +145 -0
- package/src/host/testing/sciPeaks.ts +140 -0
- package/src/host/testing/standInHost.ts +30 -5
- package/src/host/ui/useHostConnection.ts +17 -8
- package/src/host-testing.ts +4 -0
- package/src/ir/core/handPicking.ts +154 -0
- package/src/ir/core/index.ts +14 -0
- package/src/ir/core/irBandState.ts +155 -0
- package/src/ir/core/irCommands.ts +7 -0
- package/src/ir/core/irPeaks.ts +79 -0
- package/src/ir/core/irReducer.ts +27 -34
- package/src/ir/core/irSpectrum.ts +16 -0
- package/src/ir/core/irState.ts +15 -8
- package/src/ir/core/pickBands.ts +46 -39
- package/src/ir/core/ramanBands.ts +2 -2
- package/src/ir/core/spectrumBands.ts +113 -0
- package/src/ir/ui/BandRow.tsx +20 -2
- package/src/ir/ui/BandsPanel.tsx +46 -88
- package/src/ir/ui/BandsToolbar.tsx +152 -0
- package/src/ir/ui/IrCanvas.tsx +22 -11
- package/src/ir/ui/IrChart.tsx +10 -1
- package/src/ir/ui/IrDocumentationDialog.tsx +7 -0
- package/src/ir/ui/IrEditor.tsx +4 -2
- package/src/ir/ui/IrShell.tsx +5 -0
- package/src/ir/ui/IrStatusBar.tsx +4 -1
- package/src/ir/ui/IrToolbar.tsx +14 -5
- package/src/ir/ui/VibrationalEditor.tsx +2 -1
- package/src/ir/ui/irActions.ts +11 -0
- package/src/ir/ui/irChartProps.ts +4 -1
- package/src/ir/ui/useIrEditor.ts +21 -21
- package/src/locales/de.json +63 -0
- package/src/locales/en.json +63 -0
- package/src/locales/es.json +63 -0
- package/src/locales/fr.json +63 -0
- package/src/locales/it.json +63 -0
- package/src/slides/ui/slideSanitizeSchema.ts +86 -2
- package/styles/chrome.css +7 -0
- package/styles/slides.css +47 -0
|
@@ -42,18 +42,31 @@ export async function figurePng(
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Where the browser reads an SVG document back from, to paint it.
|
|
46
46
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* An object URL rather than a `data:` one, because a figure of a few thousand
|
|
48
|
+
* dots runs to hundreds of kilobytes and a `data:` URL that long is refused
|
|
49
|
+
* outright by some browsers — except for a document holding a
|
|
50
|
+
* `<foreignObject>`, which is how native MathML sits in a chart. Chromium
|
|
51
|
+
* taints any canvas that paints such a document read from an object URL, and
|
|
52
|
+
* the figure then cannot be saved at all; read from a `data:` URL it paints
|
|
53
|
+
* clean. Such a figure carries a handful of labels, far short of that length.
|
|
54
|
+
* @param markup - The SVG document.
|
|
55
|
+
* @returns The URL; one starting with `blob:` is revoked once read.
|
|
56
|
+
*/
|
|
57
|
+
export function svgImageUrl(markup: string): string {
|
|
58
|
+
return markup.includes('<foreignObject')
|
|
59
|
+
? `data:${FIGURE_SVG_TYPE},${encodeURIComponent(markup)}`
|
|
60
|
+
: URL.createObjectURL(new Blob([markup], { type: FIGURE_SVG_TYPE }));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The SVG document, decoded and ready to be painted.
|
|
50
65
|
* @param figure - The figure, already copied off the page.
|
|
51
66
|
* @returns The decoded image.
|
|
52
67
|
*/
|
|
53
68
|
async function loadFigure(figure: FigureSvg): Promise<HTMLImageElement> {
|
|
54
|
-
const url =
|
|
55
|
-
new Blob([figure.markup], { type: FIGURE_SVG_TYPE }),
|
|
56
|
-
);
|
|
69
|
+
const url = svgImageUrl(figure.markup);
|
|
57
70
|
try {
|
|
58
71
|
const image = new Image(figure.width, figure.height);
|
|
59
72
|
await new Promise<void>((resolve, reject) => {
|
|
@@ -65,7 +78,7 @@ async function loadFigure(figure: FigureSvg): Promise<HTMLImageElement> {
|
|
|
65
78
|
});
|
|
66
79
|
return image;
|
|
67
80
|
} finally {
|
|
68
|
-
URL.revokeObjectURL(url);
|
|
81
|
+
if (url.startsWith('blob:')) URL.revokeObjectURL(url);
|
|
69
82
|
}
|
|
70
83
|
}
|
|
71
84
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { svgImageUrl } from './figurePng.ts';
|
|
1
2
|
import type { SvgBox } from './svgBounds.ts';
|
|
2
3
|
import { svgToString } from './svgToString.ts';
|
|
3
4
|
|
|
@@ -92,9 +93,7 @@ function side(value: number): number {
|
|
|
92
93
|
* @returns The image, already decoded, so it can be drawn straight away.
|
|
93
94
|
*/
|
|
94
95
|
async function loadImage(source: string): Promise<HTMLImageElement> {
|
|
95
|
-
const url =
|
|
96
|
-
new Blob([source], { type: 'image/svg+xml' }),
|
|
97
|
-
);
|
|
96
|
+
const url = svgImageUrl(source);
|
|
98
97
|
try {
|
|
99
98
|
const image = new Image();
|
|
100
99
|
image.src = url;
|
|
@@ -102,7 +101,7 @@ async function loadImage(source: string): Promise<HTMLImageElement> {
|
|
|
102
101
|
return image;
|
|
103
102
|
} finally {
|
|
104
103
|
// The decoded image no longer needs the URL it came from.
|
|
105
|
-
URL.revokeObjectURL(url);
|
|
104
|
+
if (url.startsWith('blob:')) URL.revokeObjectURL(url);
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
|
|
@@ -16,7 +16,8 @@ const UNITS: ReadonlyArray<{
|
|
|
16
16
|
{ unit: 'minute', milliseconds: MINUTE },
|
|
17
17
|
];
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
/** One formatter per locale, made the first time that locale is asked for. */
|
|
20
|
+
const FORMATTERS = new Map<string, Intl.RelativeTimeFormat>();
|
|
20
21
|
|
|
21
22
|
/** How {@link formatRelativeTime} measures and what it writes for nothing. */
|
|
22
23
|
export interface FormatRelativeTimeOptions {
|
|
@@ -32,16 +33,21 @@ export interface FormatRelativeTimeOptions {
|
|
|
32
33
|
* @default MISSING_VALUE
|
|
33
34
|
*/
|
|
34
35
|
missing?: string;
|
|
36
|
+
/**
|
|
37
|
+
* The language the phrase is written in: the page's, from `useLanguage()`.
|
|
38
|
+
* @default 'en-US'
|
|
39
|
+
*/
|
|
40
|
+
locale?: string;
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
38
44
|
* How far an instant is from now, in words: `just now`, `5 minutes ago`,
|
|
39
|
-
* `yesterday`, `in 3 weeks
|
|
45
|
+
* `yesterday`, `in 3 weeks` — or `il y a 5 minutes` in French.
|
|
40
46
|
*
|
|
41
47
|
* The largest unit that fits is used and the count is rounded towards zero, so
|
|
42
48
|
* ninety minutes read `1 hour ago` rather than claiming two. Under a minute
|
|
43
|
-
* either way is `just now
|
|
44
|
-
*
|
|
49
|
+
* either way is `just now` (the language's own word for now, outside
|
|
50
|
+
* English), which also absorbs a clock that is a few seconds ahead.
|
|
45
51
|
* @param instant - Epoch milliseconds, an ISO timestamp or a date.
|
|
46
52
|
* @param options - See {@link FormatRelativeTimeOptions}.
|
|
47
53
|
* @returns The phrase, or the missing text when the instant is absent or unreadable.
|
|
@@ -50,20 +56,36 @@ export function formatRelativeTime(
|
|
|
50
56
|
instant: number | string | Date | undefined,
|
|
51
57
|
options: FormatRelativeTimeOptions = {},
|
|
52
58
|
): string {
|
|
53
|
-
const {
|
|
59
|
+
const {
|
|
60
|
+
now = Date.now(),
|
|
61
|
+
missing = MISSING_VALUE,
|
|
62
|
+
locale = 'en-US',
|
|
63
|
+
} = options;
|
|
54
64
|
const time = toTime(instant);
|
|
55
65
|
const reference = toTime(now);
|
|
56
66
|
if (time === undefined || reference === undefined) return missing;
|
|
57
67
|
|
|
68
|
+
const formatter = formatterOf(locale);
|
|
58
69
|
const elapsed = time - reference;
|
|
59
70
|
const distance = Math.abs(elapsed);
|
|
60
71
|
for (const { unit, milliseconds } of UNITS) {
|
|
61
72
|
if (distance >= milliseconds) {
|
|
62
73
|
const count = Math.floor(distance / milliseconds);
|
|
63
|
-
return
|
|
74
|
+
return formatter.format(elapsed < 0 ? -count : count, unit);
|
|
64
75
|
}
|
|
65
76
|
}
|
|
66
|
-
return '
|
|
77
|
+
return locale.toLowerCase().startsWith('en')
|
|
78
|
+
? 'just now'
|
|
79
|
+
: formatter.format(0, 'second');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatterOf(locale: string): Intl.RelativeTimeFormat {
|
|
83
|
+
let formatter = FORMATTERS.get(locale);
|
|
84
|
+
if (formatter === undefined) {
|
|
85
|
+
formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });
|
|
86
|
+
FORMATTERS.set(locale, formatter);
|
|
87
|
+
}
|
|
88
|
+
return formatter;
|
|
67
89
|
}
|
|
68
90
|
|
|
69
91
|
function toTime(
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A tool's side of the host protocol: introduce itself to the page that
|
|
3
|
-
* frames it, take the private port that page answers with,
|
|
4
|
-
*
|
|
3
|
+
* frames it, take the private port that page answers with, serve what arrives
|
|
4
|
+
* through it, and send back what the host takes back.
|
|
5
5
|
*
|
|
6
6
|
* Free of React and of the DOM — the window is handed in — so a test drives the
|
|
7
7
|
* whole exchange with a fake host and a real `MessageChannel`.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
10
|
+
import type { HostOutputs } from './hostOutputs.ts';
|
|
11
|
+
import {
|
|
12
|
+
OUTPUT_DELAY,
|
|
13
|
+
answerGet,
|
|
14
|
+
createOutputPublisher,
|
|
15
|
+
} from './hostOutputs.ts';
|
|
12
16
|
import type {
|
|
13
|
-
GetRequest,
|
|
14
17
|
HelloMessage,
|
|
15
|
-
HostError,
|
|
16
|
-
HostItem,
|
|
17
18
|
LoadRequest,
|
|
18
19
|
ResultMessage,
|
|
19
|
-
ToolInput,
|
|
20
20
|
ToolInterface,
|
|
21
21
|
UsedItem,
|
|
22
22
|
} from './protocol.ts';
|
|
@@ -26,13 +26,11 @@ import {
|
|
|
26
26
|
isConnect,
|
|
27
27
|
parseRequest,
|
|
28
28
|
} from './protocol.ts';
|
|
29
|
+
import type { LoadedInput } from './selectInputs.ts';
|
|
30
|
+
import { selectInputs } from './selectInputs.ts';
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
export
|
|
32
|
-
key: string;
|
|
33
|
-
item: HostItem;
|
|
34
|
-
checked: CheckedValue;
|
|
35
|
-
}
|
|
32
|
+
export type { InputSelection, LoadedInput } from './selectInputs.ts';
|
|
33
|
+
export { selectInputs } from './selectInputs.ts';
|
|
36
34
|
|
|
37
35
|
/** What a window `message` event carries, as far as the protocol reads it. */
|
|
38
36
|
export interface HostMessageEvent {
|
|
@@ -54,17 +52,25 @@ export interface HostConnectionOptions {
|
|
|
54
52
|
/** The build that is running, as the About states it, or `null`. */
|
|
55
53
|
build: unknown;
|
|
56
54
|
/**
|
|
57
|
-
* Show the inputs of a new document. Throwing means they
|
|
58
|
-
* the error's message goes back to the host, and the
|
|
59
|
-
* as applied.
|
|
55
|
+
* Show the inputs of a new document. Throwing, or rejecting, means they
|
|
56
|
+
* could not be read: the error's message goes back to the host, and the
|
|
57
|
+
* document is not marked as applied.
|
|
58
|
+
*/
|
|
59
|
+
load: (inputs: readonly LoadedInput[]) => void | Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Where the values of the tool's outputs are read, for `get` and for the
|
|
62
|
+
* `output` sent when one the host accepted changes.
|
|
63
|
+
* @default undefined — nothing goes back
|
|
60
64
|
*/
|
|
61
|
-
|
|
65
|
+
outputs?: HostOutputs;
|
|
66
|
+
/**
|
|
67
|
+
* How long the outputs must stay still before a change is sent, in
|
|
68
|
+
* milliseconds.
|
|
69
|
+
* @default OUTPUT_DELAY
|
|
70
|
+
*/
|
|
71
|
+
outputDelay?: number;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
|
-
/** What choosing the items of a request gave. */
|
|
65
|
-
export type InputSelection =
|
|
66
|
-
{ ok: true; inputs: LoadedInput[] } | { ok: false; error: HostError };
|
|
67
|
-
|
|
68
74
|
/**
|
|
69
75
|
* Say `hello` to the host, and serve its requests once it connects.
|
|
70
76
|
*
|
|
@@ -72,24 +78,50 @@ export type InputSelection =
|
|
|
72
78
|
* one replaces the first — a host that reloads its own script connects again.
|
|
73
79
|
* A document is applied once: the same one again is answered as before and
|
|
74
80
|
* changes nothing, so a host re-sending it never overwrites the user's work.
|
|
81
|
+
* Requests are served one at a time, in order, so a `get` sent after a `load`
|
|
82
|
+
* reads what that load showed, however long it took.
|
|
75
83
|
* @param options - The window, the tool, and its load function.
|
|
76
84
|
* @returns The function that stops listening and closes the port.
|
|
77
85
|
*/
|
|
78
86
|
export function connectHost(options: HostConnectionOptions): () => void {
|
|
79
|
-
const { host, listen, tool, origin, build, load } = options;
|
|
87
|
+
const { host, listen, tool, origin, build, load, outputs } = options;
|
|
88
|
+
const { outputDelay = OUTPUT_DELAY } = options;
|
|
80
89
|
let port: MessagePort | null = null;
|
|
81
90
|
let appliedDocument: string | null = null;
|
|
82
91
|
let appliedUsed: UsedItem[] = [];
|
|
92
|
+
let served: Promise<void> = Promise.resolve();
|
|
93
|
+
|
|
94
|
+
const publisher =
|
|
95
|
+
outputs === undefined
|
|
96
|
+
? null
|
|
97
|
+
: createOutputPublisher(
|
|
98
|
+
outputs,
|
|
99
|
+
(message) => port?.postMessage(message),
|
|
100
|
+
outputDelay,
|
|
101
|
+
);
|
|
102
|
+
const stopHearing = outputs?.subscribe(() => publisher?.changed());
|
|
83
103
|
|
|
84
104
|
function serve(data: unknown): void {
|
|
85
105
|
const request = parseRequest(data);
|
|
86
106
|
if (request === null) return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
107
|
+
served = served
|
|
108
|
+
.then(async () => {
|
|
109
|
+
const result =
|
|
110
|
+
request.type === 'load'
|
|
111
|
+
? await applyLoad(request)
|
|
112
|
+
: answerGet(request, tool, outputs);
|
|
113
|
+
port?.postMessage(result);
|
|
114
|
+
})
|
|
115
|
+
// A request that failed must not stop the ones after it being served,
|
|
116
|
+
// and must still be seen: it is thrown again, on its own.
|
|
117
|
+
.catch((error: unknown) => {
|
|
118
|
+
queueMicrotask(() => {
|
|
119
|
+
throw error;
|
|
120
|
+
});
|
|
121
|
+
});
|
|
90
122
|
}
|
|
91
123
|
|
|
92
|
-
function applyLoad(request: LoadRequest): ResultMessage {
|
|
124
|
+
async function applyLoad(request: LoadRequest): Promise<ResultMessage> {
|
|
93
125
|
const { id, document, items } = request;
|
|
94
126
|
if (document === appliedDocument) {
|
|
95
127
|
return { type: 'result', id, ok: true, used: appliedUsed };
|
|
@@ -98,8 +130,11 @@ export function connectHost(options: HostConnectionOptions): () => void {
|
|
|
98
130
|
if (!selection.ok) {
|
|
99
131
|
return { type: 'result', id, ok: false, error: selection.error };
|
|
100
132
|
}
|
|
133
|
+
// An edit still settling belongs to the document on screen, which the
|
|
134
|
+
// outputs describe only until this one replaces it.
|
|
135
|
+
publisher?.flush();
|
|
101
136
|
try {
|
|
102
|
-
load(selection.inputs);
|
|
137
|
+
await load(selection.inputs);
|
|
103
138
|
} catch (error) {
|
|
104
139
|
const message = error instanceof Error ? error.message : String(error);
|
|
105
140
|
return {
|
|
@@ -109,11 +144,19 @@ export function connectHost(options: HostConnectionOptions): () => void {
|
|
|
109
144
|
error: { code: 'unreadable', message },
|
|
110
145
|
};
|
|
111
146
|
}
|
|
147
|
+
return applied(request, selection.inputs);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Nothing else runs between a load's check and this: requests are served
|
|
151
|
+
// one at a time.
|
|
152
|
+
function applied(
|
|
153
|
+
request: LoadRequest,
|
|
154
|
+
inputs: readonly LoadedInput[],
|
|
155
|
+
): ResultMessage {
|
|
156
|
+
const { id, document, accept = [] } = request;
|
|
112
157
|
appliedDocument = document;
|
|
113
|
-
appliedUsed =
|
|
114
|
-
|
|
115
|
-
kind: item.kind,
|
|
116
|
-
}));
|
|
158
|
+
appliedUsed = inputs.map(({ key, item }) => ({ key, kind: item.kind }));
|
|
159
|
+
publisher?.baseline(document, accept);
|
|
117
160
|
return { type: 'result', id, ok: true, used: appliedUsed };
|
|
118
161
|
}
|
|
119
162
|
|
|
@@ -146,95 +189,9 @@ export function connectHost(options: HostConnectionOptions): () => void {
|
|
|
146
189
|
|
|
147
190
|
return () => {
|
|
148
191
|
stopListening();
|
|
192
|
+
stopHearing?.();
|
|
193
|
+
publisher?.stop();
|
|
149
194
|
port?.close();
|
|
150
195
|
port = null;
|
|
151
196
|
};
|
|
152
197
|
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Give each input of the tool one item of a request.
|
|
156
|
-
*
|
|
157
|
-
* Each input tries its kinds in its own order of preference, and takes the
|
|
158
|
-
* first item that falls under one and passes that kind's schema: the tool
|
|
159
|
-
* knows which notation serves it best, whatever order the host listed them in.
|
|
160
|
-
* An item naming another input's `key` is left to that input.
|
|
161
|
-
* @param tool - What the tool takes.
|
|
162
|
-
* @param items - What the host sent.
|
|
163
|
-
* @returns One item per input, or why an input got none.
|
|
164
|
-
*/
|
|
165
|
-
export function selectInputs(
|
|
166
|
-
tool: ToolInterface,
|
|
167
|
-
items: readonly HostItem[],
|
|
168
|
-
): InputSelection {
|
|
169
|
-
const inputs: LoadedInput[] = [];
|
|
170
|
-
for (const input of tool.inputs) {
|
|
171
|
-
const errors: string[] = [];
|
|
172
|
-
const loaded = selectInput(input, items, errors);
|
|
173
|
-
if (loaded !== null) {
|
|
174
|
-
inputs.push(loaded);
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
if (errors.length > 0) {
|
|
178
|
-
return {
|
|
179
|
-
ok: false,
|
|
180
|
-
error: {
|
|
181
|
-
code: 'invalid',
|
|
182
|
-
message: `No item for ${input.key} has the shape its kind requires, so ${tool.name} loaded nothing.`,
|
|
183
|
-
errors,
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
ok: false,
|
|
189
|
-
error: {
|
|
190
|
-
code: 'no-input',
|
|
191
|
-
message: `No item is a ${KIND_LIST.format(input.kinds)}, so ${tool.name} loaded nothing.`,
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
return { ok: true, inputs };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function selectInput(
|
|
199
|
-
input: ToolInput,
|
|
200
|
-
items: readonly HostItem[],
|
|
201
|
-
errors: string[],
|
|
202
|
-
): LoadedInput | null {
|
|
203
|
-
for (const accepted of input.kinds) {
|
|
204
|
-
if (!isKnownKind(accepted)) continue;
|
|
205
|
-
for (const item of items) {
|
|
206
|
-
if (item.key !== undefined && item.key !== input.key) continue;
|
|
207
|
-
if (!kindAccepts(accepted, item.kind)) continue;
|
|
208
|
-
const loaded = checkItem(input.key, accepted, item, errors);
|
|
209
|
-
if (loaded !== null) return loaded;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function checkItem(
|
|
216
|
-
key: string,
|
|
217
|
-
accepted: KnownKind,
|
|
218
|
-
item: HostItem,
|
|
219
|
-
errors: string[],
|
|
220
|
-
): LoadedInput | null {
|
|
221
|
-
const outcome = checkValue(accepted, item.value);
|
|
222
|
-
if (outcome.valid) return { key, item, checked: outcome.checked };
|
|
223
|
-
for (const error of outcome.errors) errors.push(`${item.kind} ${error}`);
|
|
224
|
-
return null;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function answerGet(request: GetRequest, tool: ToolInterface): ResultMessage {
|
|
228
|
-
// Nothing goes back yet: no tool declares an output.
|
|
229
|
-
return {
|
|
230
|
-
type: 'result',
|
|
231
|
-
id: request.id,
|
|
232
|
-
ok: false,
|
|
233
|
-
error: {
|
|
234
|
-
code: 'no-output',
|
|
235
|
-
message: `${tool.name} has nothing to give back yet.`,
|
|
236
|
-
},
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const KIND_LIST = new Intl.ListFormat('en', { type: 'disjunction' });
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a tool hands back: the values a `get` asks for, and the `output` it
|
|
3
|
+
* sends when a value the host accepted changes.
|
|
4
|
+
*
|
|
5
|
+
* An `output` carries only what changed, item by item, against what the host
|
|
6
|
+
* already has: the values as they stood once its document was applied, then
|
|
7
|
+
* every value sent since. Opening a document therefore never writes it back,
|
|
8
|
+
* and a band picked on one spectrum of a sample leaves the others alone.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { kindAccepts } from './kinds.ts';
|
|
12
|
+
import type {
|
|
13
|
+
GetRequest,
|
|
14
|
+
HostItem,
|
|
15
|
+
OutputMessage,
|
|
16
|
+
ResultMessage,
|
|
17
|
+
ToolInterface,
|
|
18
|
+
} from './protocol.ts';
|
|
19
|
+
|
|
20
|
+
/** Where a tool's outputs are read, and how their changes are heard. */
|
|
21
|
+
export interface HostOutputs {
|
|
22
|
+
/**
|
|
23
|
+
* The value of every output now, one item each, carrying its `key` and the
|
|
24
|
+
* `id` of the item it was made from. It must already show a document once
|
|
25
|
+
* `load` has returned, or the host is sent back what it just loaded.
|
|
26
|
+
*/
|
|
27
|
+
read: () => readonly HostItem[];
|
|
28
|
+
/** Be told whenever they may have changed; the returned function stops it. */
|
|
29
|
+
subscribe: (listener: () => void) => () => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* How long the outputs must stay still before a change is sent, in
|
|
34
|
+
* milliseconds: a burst of edits is one message.
|
|
35
|
+
*/
|
|
36
|
+
export const OUTPUT_DELAY = 300;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Answer a `get` with the values it asks for — every output when it names
|
|
40
|
+
* none.
|
|
41
|
+
* @param request - The request.
|
|
42
|
+
* @param tool - What the tool hands back.
|
|
43
|
+
* @param outputs - Where the values are read, if anywhere.
|
|
44
|
+
* @returns The values, or `no-output` when the tool has none of those asked.
|
|
45
|
+
*/
|
|
46
|
+
export function answerGet(
|
|
47
|
+
request: GetRequest,
|
|
48
|
+
tool: ToolInterface,
|
|
49
|
+
outputs: HostOutputs | undefined,
|
|
50
|
+
): ResultMessage {
|
|
51
|
+
const { id, keys } = request;
|
|
52
|
+
const declared = new Set<string>();
|
|
53
|
+
for (const output of tool.outputs) declared.add(output.key);
|
|
54
|
+
if (outputs === undefined || declared.size === 0) {
|
|
55
|
+
return noOutput(id, `${tool.name} has nothing to give back yet.`);
|
|
56
|
+
}
|
|
57
|
+
const wanted = keys.length === 0 ? declared : new Set<string>();
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
if (declared.has(key)) wanted.add(key);
|
|
60
|
+
}
|
|
61
|
+
if (wanted.size === 0) {
|
|
62
|
+
return noOutput(
|
|
63
|
+
id,
|
|
64
|
+
`${tool.name} has no output called ${LIST.format(keys)}.`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const items: HostItem[] = [];
|
|
68
|
+
for (const item of outputs.read()) {
|
|
69
|
+
if (item.key !== undefined && wanted.has(item.key)) items.push(item);
|
|
70
|
+
}
|
|
71
|
+
return { type: 'result', id, ok: true, items };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What the connection tells the publisher, and the means to stop it. */
|
|
75
|
+
export interface OutputPublisher {
|
|
76
|
+
/** A document was applied: what the host has is what the tool now shows. */
|
|
77
|
+
baseline: (document: string, accept: readonly string[]) => void;
|
|
78
|
+
/** The outputs may have changed. */
|
|
79
|
+
changed: () => void;
|
|
80
|
+
/**
|
|
81
|
+
* Send what changed now rather than once it settles: another document is
|
|
82
|
+
* about to replace what the outputs are read from.
|
|
83
|
+
*/
|
|
84
|
+
flush: () => void;
|
|
85
|
+
/** Send nothing more. */
|
|
86
|
+
stop: () => void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Send the host what changed of the outputs it accepted, once they settle.
|
|
91
|
+
* @param outputs - Where the values are read.
|
|
92
|
+
* @param send - Posts a message to the host, when there is one.
|
|
93
|
+
* @param delay - How long the values must stay still, in milliseconds.
|
|
94
|
+
* @returns The publisher.
|
|
95
|
+
*/
|
|
96
|
+
export function createOutputPublisher(
|
|
97
|
+
outputs: HostOutputs,
|
|
98
|
+
send: (message: OutputMessage) => void,
|
|
99
|
+
delay: number,
|
|
100
|
+
): OutputPublisher {
|
|
101
|
+
let document: string | null = null;
|
|
102
|
+
let accepted = new Set<string>();
|
|
103
|
+
let known = new Map<string, string>();
|
|
104
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
105
|
+
|
|
106
|
+
function flush(): void {
|
|
107
|
+
timer = undefined;
|
|
108
|
+
if (document === null) return;
|
|
109
|
+
const items: HostItem[] = [];
|
|
110
|
+
for (const [identity, entry] of readAccepted(outputs, accepted)) {
|
|
111
|
+
if (known.get(identity) === entry.text) continue;
|
|
112
|
+
known.set(identity, entry.text);
|
|
113
|
+
items.push(entry.item);
|
|
114
|
+
}
|
|
115
|
+
if (items.length > 0) send({ type: 'output', document, items });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function stop(): void {
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
timer = undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
baseline(next, accept) {
|
|
125
|
+
stop();
|
|
126
|
+
document = next;
|
|
127
|
+
accepted = new Set(accept);
|
|
128
|
+
known = new Map();
|
|
129
|
+
for (const [identity, entry] of readAccepted(outputs, accepted)) {
|
|
130
|
+
known.set(identity, entry.text);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
changed() {
|
|
134
|
+
if (document === null || accepted.size === 0) return;
|
|
135
|
+
stop();
|
|
136
|
+
timer = setTimeout(flush, delay);
|
|
137
|
+
},
|
|
138
|
+
flush() {
|
|
139
|
+
stop();
|
|
140
|
+
flush();
|
|
141
|
+
},
|
|
142
|
+
stop,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The accepted items, each under an identity that survives a re-read: its
|
|
148
|
+
* key, the id of the item it was made from, and its rank among items sharing
|
|
149
|
+
* both.
|
|
150
|
+
* @param outputs - Where the values are read.
|
|
151
|
+
* @param accepted - The keys and kinds the host takes back.
|
|
152
|
+
* @returns Each item with its value as text, by identity.
|
|
153
|
+
*/
|
|
154
|
+
function readAccepted(
|
|
155
|
+
outputs: HostOutputs,
|
|
156
|
+
accepted: ReadonlySet<string>,
|
|
157
|
+
): Map<string, { item: HostItem; text: string }> {
|
|
158
|
+
const entries = new Map<string, { item: HostItem; text: string }>();
|
|
159
|
+
if (accepted.size === 0) return entries;
|
|
160
|
+
for (const item of outputs.read()) {
|
|
161
|
+
if (!isAccepted(item, accepted)) continue;
|
|
162
|
+
const base = `${item.key ?? item.kind}\n${item.id ?? ''}`;
|
|
163
|
+
let rank = 0;
|
|
164
|
+
while (entries.has(`${base}\n${rank}`)) rank++;
|
|
165
|
+
entries.set(`${base}\n${rank}`, {
|
|
166
|
+
item,
|
|
167
|
+
text: JSON.stringify(item.value),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return entries;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Whether the host takes an item back: `accept` names its output's key, or
|
|
175
|
+
* its kind — a kind taking every kind under it, as an input's does.
|
|
176
|
+
* @param item - The item.
|
|
177
|
+
* @param accepted - What the host takes back.
|
|
178
|
+
* @returns Whether it goes back.
|
|
179
|
+
*/
|
|
180
|
+
function isAccepted(item: HostItem, accepted: ReadonlySet<string>): boolean {
|
|
181
|
+
if (item.key !== undefined && accepted.has(item.key)) return true;
|
|
182
|
+
for (const name of accepted) {
|
|
183
|
+
if (kindAccepts(name, item.kind)) return true;
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function noOutput(id: string, message: string): ResultMessage {
|
|
189
|
+
return {
|
|
190
|
+
type: 'result',
|
|
191
|
+
id,
|
|
192
|
+
ok: false,
|
|
193
|
+
error: { code: 'no-output', message },
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const LIST = new Intl.ListFormat('en', { type: 'disjunction' });
|
package/src/host/core/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export type { HostConnectionOptions, HostMessageEvent } from './connectHost.ts';
|
|
2
|
+
export { connectHost } from './connectHost.ts';
|
|
3
|
+
export type { HostOutputs, OutputPublisher } from './hostOutputs.ts';
|
|
4
|
+
export {
|
|
5
|
+
OUTPUT_DELAY,
|
|
6
|
+
answerGet,
|
|
7
|
+
createOutputPublisher,
|
|
8
|
+
} from './hostOutputs.ts';
|
|
8
9
|
export type { StructureValue } from './hostStructure.ts';
|
|
9
10
|
export {
|
|
10
11
|
STRUCTURE_KINDS,
|
|
@@ -12,6 +13,8 @@ export {
|
|
|
12
13
|
hostStructureSource,
|
|
13
14
|
readHostMolecule,
|
|
14
15
|
} from './hostStructure.ts';
|
|
16
|
+
export type { IrJdxValue, IrPeakValue, IrPeaksValue } from './irKinds.ts';
|
|
17
|
+
export { IR_JDX_SCHEMA, IR_PEAKS_SCHEMA } from './irKinds.ts';
|
|
15
18
|
export type {
|
|
16
19
|
CheckOutcome,
|
|
17
20
|
CheckedValue,
|
|
@@ -29,6 +32,7 @@ export type {
|
|
|
29
32
|
HostItem,
|
|
30
33
|
HostRequest,
|
|
31
34
|
LoadRequest,
|
|
35
|
+
OutputMessage,
|
|
32
36
|
ResultMessage,
|
|
33
37
|
ToolInput,
|
|
34
38
|
ToolInterface,
|
|
@@ -44,3 +48,5 @@ export {
|
|
|
44
48
|
isConnect,
|
|
45
49
|
parseRequest,
|
|
46
50
|
} from './protocol.ts';
|
|
51
|
+
export type { InputSelection, LoadedInput } from './selectInputs.ts';
|
|
52
|
+
export { selectInputs } from './selectInputs.ts';
|