react-cheminfo 0.29.0 → 0.31.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 (126) hide show
  1. package/README.md +62 -2
  2. package/lib/chrome/ui/AccountButton.d.ts +76 -0
  3. package/lib/chrome/ui/AccountButton.d.ts.map +1 -0
  4. package/lib/chrome/ui/AccountButton.js +66 -0
  5. package/lib/chrome/ui/AccountButton.js.map +1 -0
  6. package/lib/chrome/ui/index.d.ts +2 -0
  7. package/lib/chrome/ui/index.d.ts.map +1 -1
  8. package/lib/chrome/ui/index.js +1 -0
  9. package/lib/chrome/ui/index.js.map +1 -1
  10. package/lib/clipboard/ui/ClickToCopy.d.ts +6 -0
  11. package/lib/clipboard/ui/ClickToCopy.d.ts.map +1 -1
  12. package/lib/clipboard/ui/ClickToCopy.js +3 -3
  13. package/lib/clipboard/ui/ClickToCopy.js.map +1 -1
  14. package/lib/download/core/downloadFigure.d.ts +7 -5
  15. package/lib/download/core/downloadFigure.d.ts.map +1 -1
  16. package/lib/download/core/downloadFigure.js +7 -6
  17. package/lib/download/core/downloadFigure.js.map +1 -1
  18. package/lib/download/core/figureLayout.d.ts +55 -0
  19. package/lib/download/core/figureLayout.d.ts.map +1 -0
  20. package/lib/download/core/figureLayout.js +89 -0
  21. package/lib/download/core/figureLayout.js.map +1 -0
  22. package/lib/download/core/figureScale.d.ts +9 -0
  23. package/lib/download/core/figureScale.d.ts.map +1 -1
  24. package/lib/download/core/figureScale.js +10 -0
  25. package/lib/download/core/figureScale.js.map +1 -1
  26. package/lib/download/core/figureSettle.d.ts +51 -0
  27. package/lib/download/core/figureSettle.d.ts.map +1 -0
  28. package/lib/download/core/figureSettle.js +81 -0
  29. package/lib/download/core/figureSettle.js.map +1 -0
  30. package/lib/download/core/figureSvg.d.ts +7 -1
  31. package/lib/download/core/figureSvg.d.ts.map +1 -1
  32. package/lib/download/core/figureSvg.js +1 -0
  33. package/lib/download/core/figureSvg.js.map +1 -1
  34. package/lib/download/core/figureSvgDocument.d.ts +7 -0
  35. package/lib/download/core/figureSvgDocument.d.ts.map +1 -1
  36. package/lib/download/core/figureSvgDocument.js +11 -2
  37. package/lib/download/core/figureSvgDocument.js.map +1 -1
  38. package/lib/download/core/index.d.ts +5 -1
  39. package/lib/download/core/index.d.ts.map +1 -1
  40. package/lib/download/core/index.js +3 -1
  41. package/lib/download/core/index.js.map +1 -1
  42. package/lib/download/ui/FigureDownload.d.ts +18 -4
  43. package/lib/download/ui/FigureDownload.d.ts.map +1 -1
  44. package/lib/download/ui/FigureDownload.js +35 -9
  45. package/lib/download/ui/FigureDownload.js.map +1 -1
  46. package/lib/download/ui/FigureDownloadPanel.d.ts +17 -7
  47. package/lib/download/ui/FigureDownloadPanel.d.ts.map +1 -1
  48. package/lib/download/ui/FigureDownloadPanel.js +30 -22
  49. package/lib/download/ui/FigureDownloadPanel.js.map +1 -1
  50. package/lib/download/ui/FigureSizeField.d.ts +27 -0
  51. package/lib/download/ui/FigureSizeField.d.ts.map +1 -0
  52. package/lib/download/ui/FigureSizeField.js +87 -0
  53. package/lib/download/ui/FigureSizeField.js.map +1 -0
  54. package/lib/download/ui/FigureSizeRows.d.ts +34 -0
  55. package/lib/download/ui/FigureSizeRows.d.ts.map +1 -0
  56. package/lib/download/ui/FigureSizeRows.js +54 -0
  57. package/lib/download/ui/FigureSizeRows.js.map +1 -0
  58. package/lib/download/ui/index.d.ts +2 -0
  59. package/lib/download/ui/index.d.ts.map +1 -1
  60. package/lib/download/ui/index.js.map +1 -1
  61. package/lib/download/ui/useFigureRedraw.d.ts +27 -0
  62. package/lib/download/ui/useFigureRedraw.d.ts.map +1 -0
  63. package/lib/download/ui/useFigureRedraw.js +78 -0
  64. package/lib/download/ui/useFigureRedraw.js.map +1 -0
  65. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  66. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  67. package/lib/locales/de.json +13 -2
  68. package/lib/locales/en.json +13 -2
  69. package/lib/locales/es.json +13 -2
  70. package/lib/locales/fr.json +13 -2
  71. package/lib/overlay/ui/OverlayValueMenu.d.ts.map +1 -1
  72. package/lib/overlay/ui/OverlayValueMenu.js +7 -1
  73. package/lib/overlay/ui/OverlayValueMenu.js.map +1 -1
  74. package/lib/structure/core/conformerBoltzmann.d.ts +56 -0
  75. package/lib/structure/core/conformerBoltzmann.d.ts.map +1 -0
  76. package/lib/structure/core/conformerBoltzmann.js +96 -0
  77. package/lib/structure/core/conformerBoltzmann.js.map +1 -0
  78. package/lib/structure/core/conformerEnergy.d.ts +69 -0
  79. package/lib/structure/core/conformerEnergy.d.ts.map +1 -0
  80. package/lib/structure/core/conformerEnergy.js +47 -0
  81. package/lib/structure/core/conformerEnergy.js.map +1 -0
  82. package/lib/structure/core/index.d.ts +3 -0
  83. package/lib/structure/core/index.d.ts.map +1 -1
  84. package/lib/structure/core/index.js +2 -0
  85. package/lib/structure/core/index.js.map +1 -1
  86. package/lib/structure/ui/ConformerTable.d.ts +73 -0
  87. package/lib/structure/ui/ConformerTable.d.ts.map +1 -0
  88. package/lib/structure/ui/ConformerTable.js +259 -0
  89. package/lib/structure/ui/ConformerTable.js.map +1 -0
  90. package/lib/structure/ui/conformerColumns.d.ts +30 -0
  91. package/lib/structure/ui/conformerColumns.d.ts.map +1 -0
  92. package/lib/structure/ui/conformerColumns.js +37 -0
  93. package/lib/structure/ui/conformerColumns.js.map +1 -0
  94. package/lib/ui.d.ts +4 -0
  95. package/lib/ui.d.ts.map +1 -1
  96. package/lib/ui.js +2 -0
  97. package/lib/ui.js.map +1 -1
  98. package/package.json +1 -1
  99. package/src/chrome/ui/AccountButton.tsx +185 -0
  100. package/src/chrome/ui/index.ts +2 -0
  101. package/src/clipboard/ui/ClickToCopy.tsx +9 -1
  102. package/src/download/core/downloadFigure.ts +12 -9
  103. package/src/download/core/figureLayout.ts +114 -0
  104. package/src/download/core/figureScale.ts +13 -0
  105. package/src/download/core/figureSettle.ts +112 -0
  106. package/src/download/core/figureSvg.ts +8 -1
  107. package/src/download/core/figureSvgDocument.ts +19 -2
  108. package/src/download/core/index.ts +13 -0
  109. package/src/download/ui/FigureDownload.tsx +88 -35
  110. package/src/download/ui/FigureDownloadPanel.tsx +45 -22
  111. package/src/download/ui/FigureSizeField.tsx +153 -0
  112. package/src/download/ui/FigureSizeRows.tsx +108 -0
  113. package/src/download/ui/index.ts +2 -0
  114. package/src/download/ui/useFigureRedraw.tsx +115 -0
  115. package/src/locales/de.json +13 -2
  116. package/src/locales/en.json +13 -2
  117. package/src/locales/es.json +13 -2
  118. package/src/locales/fr.json +13 -2
  119. package/src/overlay/ui/OverlayValueMenu.tsx +7 -1
  120. package/src/structure/core/conformerBoltzmann.ts +102 -0
  121. package/src/structure/core/conformerEnergy.ts +97 -0
  122. package/src/structure/core/index.ts +15 -0
  123. package/src/structure/ui/ConformerTable.tsx +429 -0
  124. package/src/structure/ui/conformerColumns.ts +55 -0
  125. package/src/ui.ts +12 -0
  126. package/styles/chrome.css +55 -0
@@ -4,13 +4,13 @@
4
4
  * Which of the two formats a reader wants is not a question the figure can
5
5
  * answer for them: an SVG is the one to put in a paper, where it stays sharp
6
6
  * at any size and can still be edited, and a PNG is the one every chat window,
7
- * slide deck and issue tracker actually accepts. So both are offered, and the
8
- * resolution goes with the second.
7
+ * slide deck and issue tracker actually accepts. So both are offered. The
8
+ * resolution paints a PNG with more pixels, and makes an SVG open larger.
9
9
  */
10
10
 
11
11
  import { downloadBlob } from './downloadBlob.ts';
12
12
  import { figurePng } from './figurePng.ts';
13
- import { DEFAULT_FIGURE_SCALE } from './figureScale.ts';
13
+ import { defaultFigureScale } from './figureScale.ts';
14
14
  import type { FigureSvgOptions } from './figureSvg.ts';
15
15
  import { FIGURE_SVG_TYPE, figureSvg } from './figureSvg.ts';
16
16
  import { sanitizeFileName } from './sanitizeFileName.ts';
@@ -26,9 +26,11 @@ export interface DownloadFigureOptions extends FigureSvgOptions {
26
26
  */
27
27
  format?: FigureFormat;
28
28
  /**
29
- * How many pixels are painted per pixel on screen. An SVG ignores it: it
30
- * carries no resolution of its own.
31
- * @default 2
29
+ * How many pixels are painted per pixel on screen. An SVG is drawn the same
30
+ * and opens this many times larger, which is the size a slide or a word
31
+ * processor places it at.
32
+ * @default 2 for a PNG, 1 for an SVG — which then opens at the size it has
33
+ * on screen
32
34
  */
33
35
  scale?: number;
34
36
  /**
@@ -51,16 +53,17 @@ export async function downloadFigure(
51
53
  target: string | Element,
52
54
  options: DownloadFigureOptions = {},
53
55
  ): Promise<void> {
54
- const { format = 'png', scale = DEFAULT_FIGURE_SCALE } = options;
55
- const { fileName = DEFAULT_FILE_NAME, background } = options;
56
+ const { format = 'png', fileName = DEFAULT_FILE_NAME, background } = options;
57
+ const scale = options.scale ?? defaultFigureScale(format);
56
58
 
57
- const figure = figureSvg(target, { background });
58
59
  const name = sanitizeFileName(fileName, DEFAULT_FILE_NAME);
59
60
 
60
61
  if (format === 'svg') {
62
+ const figure = figureSvg(target, { background, scale });
61
63
  const blob = new Blob([figure.markup], { type: FIGURE_SVG_TYPE });
62
64
  downloadBlob(blob, `${name}.svg`);
63
65
  return;
64
66
  }
67
+ const figure = figureSvg(target, { background });
65
68
  downloadBlob(await figurePng(figure, scale), `${name}.png`);
66
69
  }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The shape a figure is drawn at before it is saved.
3
+ *
4
+ * A saved figure is the drawing the reader is looking at, so its proportions
5
+ * are the proportions of the box it sits in. The resolution scales that
6
+ * drawing — more pixels for a PNG, a larger page for an SVG — but leaves its
7
+ * shape, and the size of its words against its axes, exactly as they were. A
8
+ * figure for a slide or for a journal column wants another shape, and the only
9
+ * way to give it one is to draw it again at that size, so its axes and labels
10
+ * are laid out for the new box rather than stretched into it.
11
+ */
12
+
13
+ import type { FigurePixels } from './figureScale.ts';
14
+
15
+ /** The shapes a figure can be drawn at for a file. */
16
+ export type FigureLayout = 'screen' | 'standard' | 'wide' | 'column' | 'custom';
17
+
18
+ /** The shapes, in the order they are offered. */
19
+ export const FIGURE_LAYOUTS = [
20
+ 'screen',
21
+ 'standard',
22
+ 'wide',
23
+ 'column',
24
+ 'custom',
25
+ ] as const satisfies readonly FigureLayout[];
26
+
27
+ /**
28
+ * How wide a journal's single column is, in CSS pixels: 8.5 cm at 96 pixels
29
+ * per inch. A figure laid out at that width prints its text at the size it is
30
+ * read at on screen, a 12 px label becoming a 9 pt one.
31
+ */
32
+ export const FIGURE_COLUMN_WIDTH = 321;
33
+
34
+ /** The narrowest side a figure is drawn at, below which no chart has room for its axes. */
35
+ export const FIGURE_LAYOUT_MIN = 120;
36
+
37
+ /** The widest side a figure is drawn at: past it the page would be laying out a poster. */
38
+ export const FIGURE_LAYOUT_MAX = 4096;
39
+
40
+ /**
41
+ * The size a figure is drawn at, in the shape the reader picked.
42
+ *
43
+ * The two slide shapes keep the width of the figure on screen and change its
44
+ * height, since the width is what a page already gave it. The journal column
45
+ * does the reverse: its width is fixed by the journal, and the figure keeps
46
+ * the proportions it has on screen.
47
+ * @param layout - The shape picked.
48
+ * @param screen - The figure as it is drawn on the page.
49
+ * @param custom - The size typed in, read only for `custom`. Defaults to the
50
+ * figure on screen.
51
+ * @returns The size to draw it at, in whole pixels.
52
+ */
53
+ export function figureLayoutSize(
54
+ layout: FigureLayout,
55
+ screen: FigurePixels,
56
+ custom: FigurePixels = screen,
57
+ ): FigurePixels {
58
+ switch (layout) {
59
+ case 'standard':
60
+ return sized(screen.width, (screen.width * 3) / 4);
61
+ case 'wide':
62
+ return sized(screen.width, (screen.width * 9) / 16);
63
+ case 'column':
64
+ return sized(
65
+ FIGURE_COLUMN_WIDTH,
66
+ (FIGURE_COLUMN_WIDTH * screen.height) / Math.max(screen.width, 1),
67
+ );
68
+ case 'custom':
69
+ return sized(custom.width, custom.height);
70
+ case 'screen':
71
+ return { width: screen.width, height: screen.height };
72
+ default:
73
+ throw new Error(`unknown figure layout: ${String(layout)}`);
74
+ }
75
+ }
76
+
77
+ /**
78
+ * One side, held to the range a figure can be drawn at.
79
+ * @param value - What was asked for, typed or computed.
80
+ * @returns A whole number of pixels, inside the range.
81
+ */
82
+ export function figureLayoutSide(value: number): number {
83
+ if (!Number.isFinite(value)) return FIGURE_LAYOUT_MIN;
84
+ return Math.min(
85
+ FIGURE_LAYOUT_MAX,
86
+ Math.max(FIGURE_LAYOUT_MIN, Math.round(value)),
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Whether a figure is drawn again for the file, rather than copied as it is.
92
+ * @param layout - The shape picked.
93
+ * @param screen - The figure as it is drawn on the page.
94
+ * @param custom - The size typed in.
95
+ * @returns Whether the size differs from the one on screen.
96
+ */
97
+ export function figureLayoutRedraws(
98
+ layout: FigureLayout,
99
+ screen: FigurePixels,
100
+ custom?: FigurePixels,
101
+ ): boolean {
102
+ const drawn = figureLayoutSize(layout, screen, custom);
103
+ return drawn.width !== screen.width || drawn.height !== screen.height;
104
+ }
105
+
106
+ /**
107
+ * A size, both sides held to the range.
108
+ * @param width - The width asked for.
109
+ * @param height - The height asked for.
110
+ * @returns The size.
111
+ */
112
+ function sized(width: number, height: number): FigurePixels {
113
+ return { width: figureLayoutSide(width), height: figureLayoutSide(height) };
114
+ }
@@ -8,6 +8,8 @@
8
8
  * to work it out.
9
9
  */
10
10
 
11
+ import type { FigureFormat } from './downloadFigure.ts';
12
+
11
13
  /** The size of a saved figure, in pixels. */
12
14
  export interface FigurePixels {
13
15
  /** Its width. */
@@ -28,6 +30,17 @@ export const FIGURE_SCALES: readonly number[] = [1, 2, 3, 4];
28
30
  */
29
31
  export const DEFAULT_FIGURE_SCALE = 2;
30
32
 
33
+ /**
34
+ * The multiple a figure is saved at in this format unless the reader says
35
+ * otherwise. An SVG stays sharp however large it is shown, so it opens at the
36
+ * size it has on screen; a PNG is painted twice over, for a slide.
37
+ * @param format - The file being written.
38
+ * @returns The multiple.
39
+ */
40
+ export function defaultFigureScale(format: FigureFormat): number {
41
+ return format === 'svg' ? 1 : DEFAULT_FIGURE_SCALE;
42
+ }
43
+
31
44
  /**
32
45
  * The longest side a browser will paint into one canvas.
33
46
  *
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Knowing when a figure drawn for a file has finished drawing.
3
+ *
4
+ * A chart mounted off the page to be saved at another size does not arrive in
5
+ * one piece: it may render once at a guessed size, measure its box, and render
6
+ * again, and a chart behind a lazy import arrives whenever its chunk does. So
7
+ * nothing here waits a fixed time. The figure is watched frame by frame, and
8
+ * it is taken the moment it has stood still for two frames running.
9
+ */
10
+
11
+ import { figureSize } from './figureTarget.ts';
12
+
13
+ /** How the wait is run. */
14
+ export interface FigureSettleOptions {
15
+ /**
16
+ * Stops the wait, for a panel closed or a page left while the figure is
17
+ * still drawing.
18
+ * @default undefined — the wait runs to its end
19
+ */
20
+ signal?: AbortSignal;
21
+ /**
22
+ * How many frames the figure is given to settle before the save is given
23
+ * up, about ten seconds at sixty frames a second. It is how a figure that
24
+ * never draws is reported, not how long a slow one is expected to take.
25
+ * @default 600
26
+ */
27
+ maxFrames?: number;
28
+ /**
29
+ * Resolves at the next frame.
30
+ * @default the browser's `requestAnimationFrame`
31
+ */
32
+ nextFrame?: () => Promise<void>;
33
+ }
34
+
35
+ /** How many frames in a row the figure has to hold still. */
36
+ const STILL_FRAMES = 2;
37
+
38
+ /** What {@link FigureSettleOptions.maxFrames} is unless the caller says. */
39
+ const DEFAULT_MAX_FRAMES = 600;
40
+
41
+ /**
42
+ * Wait until the figure mounted in a box has finished drawing.
43
+ * @param element - The box the figure is being drawn in.
44
+ * @param options - See {@link FigureSettleOptions}.
45
+ * @returns Resolves once the figure has stood still.
46
+ * @throws {Error} When the wait is stopped, or the figure never settles.
47
+ */
48
+ export async function whenFigureSettles(
49
+ element: Element,
50
+ options: FigureSettleOptions = {},
51
+ ): Promise<void> {
52
+ let changes = 0;
53
+ const observer = new MutationObserver(() => {
54
+ changes++;
55
+ });
56
+ observer.observe(element, {
57
+ subtree: true,
58
+ childList: true,
59
+ attributes: true,
60
+ characterData: true,
61
+ });
62
+ try {
63
+ await settle(() => {
64
+ const size = figureSize(element);
65
+ return size === null ? null : `${changes}:${size.width}×${size.height}`;
66
+ }, options);
67
+ } finally {
68
+ observer.disconnect();
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Wait until a reading of the figure comes back the same frames in a row.
74
+ *
75
+ * The reading is whatever says the figure has changed — for a drawing on the
76
+ * page, how many times it was touched and the size it came out at — and
77
+ * `null` while there is nothing drawn yet.
78
+ * @param read - Takes the reading.
79
+ * @param options - See {@link FigureSettleOptions}.
80
+ * @returns Resolves once the reading has held.
81
+ * @throws {Error} When the wait is stopped, or the reading never holds.
82
+ */
83
+ export async function settle(
84
+ read: () => string | null,
85
+ options: FigureSettleOptions = {},
86
+ ): Promise<void> {
87
+ const { signal, maxFrames = DEFAULT_MAX_FRAMES } = options;
88
+ const { nextFrame = animationFrame } = options;
89
+
90
+ let last: string | null = null;
91
+ let still = 0;
92
+ for (let frame = 0; frame < maxFrames; frame++) {
93
+ // eslint-disable-next-line no-await-in-loop -- each frame is compared with the one before it
94
+ await nextFrame();
95
+ if (signal?.aborted) throw new Error('The figure was not saved.');
96
+ const reading = read();
97
+ still = reading !== null && reading === last ? still + 1 : 0;
98
+ if (still >= STILL_FRAMES) return;
99
+ last = reading;
100
+ }
101
+ throw new Error('The figure did not finish drawing, so nothing was saved.');
102
+ }
103
+
104
+ /**
105
+ * The next frame the browser paints.
106
+ * @returns Resolves at that frame.
107
+ */
108
+ function animationFrame(): Promise<void> {
109
+ return new Promise((resolve) => {
110
+ requestAnimationFrame(() => resolve());
111
+ });
112
+ }
@@ -27,13 +27,19 @@ export interface FigureSvgOptions {
27
27
  * @default the surface the figure is drawn on, which is the page's own
28
28
  */
29
29
  background?: string;
30
+ /**
31
+ * How many times the figure on screen the document says it is. The drawing
32
+ * is unchanged; only the size the file opens at grows.
33
+ * @default 1
34
+ */
35
+ scale?: number;
30
36
  }
31
37
 
32
38
  /** A figure, as the file that leaves the page. */
33
39
  export interface FigureSvg {
34
40
  /** The whole SVG document, tokens resolved. */
35
41
  markup: string;
36
- /** Its width in pixels, at the size the figure was drawn. */
42
+ /** Its width in pixels, at the size the figure was drawn on screen. */
37
43
  width: number;
38
44
  /** Its height. */
39
45
  height: number;
@@ -94,6 +100,7 @@ export function figureSvg(
94
100
  background: options.background ?? surfaceOf(styles),
95
101
  fontFamily: styles.fontFamily,
96
102
  fontSize: Number.parseFloat(styles.fontSize),
103
+ scale: options.scale,
97
104
  });
98
105
 
99
106
  return {
@@ -53,6 +53,13 @@ export interface FigureSvgDocumentOptions {
53
53
  * @default undefined — the reader's own default
54
54
  */
55
55
  fontSize?: number;
56
+ /**
57
+ * How many times the figure on screen the document says it is. Only the
58
+ * size it opens at changes — the drawing keeps its own coordinates, so every
59
+ * line and every word grows together and stays as sharp as before.
60
+ * @default 1
61
+ */
62
+ scale?: number;
56
63
  }
57
64
 
58
65
  /** Where an SVG document says it belongs. */
@@ -69,7 +76,8 @@ export function figureSvgDocument(
69
76
  pieces: readonly FigurePiece[],
70
77
  options: FigureSvgDocumentOptions,
71
78
  ): string {
72
- const { width, height, background, fontFamily, fontSize } = options;
79
+ const { width, height, background, fontFamily, fontSize, scale } = options;
80
+ const factor = positiveOrOne(scale);
73
81
  const box = `0 0 ${round(width)} ${round(height)}`;
74
82
  const style = documentStyle(fontFamily, fontSize);
75
83
 
@@ -86,7 +94,7 @@ export function figureSvgDocument(
86
94
 
87
95
  return (
88
96
  `<?xml version="1.0" encoding="UTF-8"?>` +
89
- `<svg xmlns="${SVG_NAMESPACE}" width="${round(width)}" height="${round(height)}" viewBox="${box}"${style}>` +
97
+ `<svg xmlns="${SVG_NAMESPACE}" width="${round(width * factor)}" height="${round(height * factor)}" viewBox="${box}"${style}>` +
90
98
  `${inside}</svg>`
91
99
  );
92
100
  }
@@ -113,6 +121,15 @@ function documentStyle(
113
121
  : ` style="${escapeAttribute(rules.join(';'))}"`;
114
122
  }
115
123
 
124
+ /**
125
+ * A multiple that makes sense, or one.
126
+ * @param value - The multiple asked for.
127
+ * @returns It, where it is a positive number; one otherwise.
128
+ */
129
+ function positiveOrOne(value: number | undefined): number {
130
+ return value !== undefined && Number.isFinite(value) && value > 0 ? value : 1;
131
+ }
132
+
116
133
  /**
117
134
  * A number as an attribute reads it: whole pixels where it is one, and two
118
135
  * decimals otherwise, because a serialized figure carrying sixteen digits per
@@ -3,12 +3,25 @@ export { downloadBlob } from './downloadBlob.ts';
3
3
  export type { DownloadFigureOptions, FigureFormat } from './downloadFigure.ts';
4
4
  export { downloadFigure } from './downloadFigure.ts';
5
5
  export { downloadText } from './downloadText.ts';
6
+ export type { FigureLayout } from './figureLayout.ts';
7
+ export {
8
+ FIGURE_COLUMN_WIDTH,
9
+ FIGURE_LAYOUTS,
10
+ FIGURE_LAYOUT_MAX,
11
+ FIGURE_LAYOUT_MIN,
12
+ figureLayoutRedraws,
13
+ figureLayoutSide,
14
+ figureLayoutSize,
15
+ } from './figureLayout.ts';
6
16
  export { FIGURE_PNG_TYPE, figurePng } from './figurePng.ts';
17
+ export type { FigureSettleOptions } from './figureSettle.ts';
18
+ export { whenFigureSettles } from './figureSettle.ts';
7
19
  export type { FigurePixels } from './figureScale.ts';
8
20
  export {
9
21
  DEFAULT_FIGURE_SCALE,
10
22
  FIGURE_MAX_PIXELS,
11
23
  FIGURE_SCALES,
24
+ defaultFigureScale,
12
25
  figurePixels,
13
26
  figureScaleFits,
14
27
  figureScaleLabel,
@@ -7,11 +7,19 @@ import { useChromeT } from '../../i18n/ui/useT.ts';
7
7
  import { OverlayIconButton } from '../../overlay/ui/OverlayIconButton.tsx';
8
8
  import type { FigureFormat } from '../core/downloadFigure.ts';
9
9
  import { downloadFigure } from '../core/downloadFigure.ts';
10
+ import type { FigureLayout } from '../core/figureLayout.ts';
11
+ import { figureLayoutRedraws, figureLayoutSize } from '../core/figureLayout.ts';
10
12
  import type { FigurePixels } from '../core/figureScale.ts';
11
- import { DEFAULT_FIGURE_SCALE, FIGURE_SCALES } from '../core/figureScale.ts';
13
+ import {
14
+ DEFAULT_FIGURE_SCALE,
15
+ FIGURE_SCALES,
16
+ defaultFigureScale,
17
+ } from '../core/figureScale.ts';
12
18
  import { figureSize } from '../core/figureTarget.ts';
13
19
 
14
20
  import { FigureDownloadPanel } from './FigureDownloadPanel.tsx';
21
+ import type { FigureRenderer } from './useFigureRedraw.tsx';
22
+ import { useFigureRedraw } from './useFigureRedraw.tsx';
15
23
 
16
24
  /** What {@link FigureDownload} needs. */
17
25
  export interface FigureDownloadProps {
@@ -34,7 +42,9 @@ export interface FigureDownloadProps {
34
42
  */
35
43
  defaultFormat?: FigureFormat;
36
44
  /**
37
- * The resolution it opens on, as a multiple of the figure on screen.
45
+ * The resolution a PNG opens on, as a multiple of the figure on screen. An
46
+ * SVG opens at 1×, the size it has on screen, and each format keeps the
47
+ * multiple the reader last picked for it.
38
48
  * @default 2
39
49
  */
40
50
  defaultScale?: number;
@@ -69,6 +79,16 @@ export interface FigureDownloadProps {
69
79
  * @default undefined
70
80
  */
71
81
  testId?: string;
82
+ /**
83
+ * Draws the figure at a size, for a file. When it is given the panel offers
84
+ * a size — as shown, 4:3, 16:9, a journal column, or typed — and a figure
85
+ * saved at a size other than its own is drawn again at that size, off the
86
+ * page, so its axes and labels are laid out for the new shape rather than
87
+ * stretched into it. Draw exactly what is on screen, with the same data,
88
+ * zoom and colours; only the box changes.
89
+ * @default undefined — the figure is saved at the size it has on screen
90
+ */
91
+ renderFigure?: FigureRenderer;
72
92
  }
73
93
 
74
94
  /**
@@ -84,9 +104,10 @@ export interface FigureDownloadProps {
84
104
  * Both formats are offered because they answer different questions. An SVG is
85
105
  * the figure itself, sharp at any size and still editable, which is what a
86
106
  * paper wants; a PNG is a picture of it, which is what every chat window and
87
- * slide deck accepts. The resolution belongs to the second alone, and the
88
- * panel writes out the pixels it is about to produce so nobody has to guess
89
- * what `3×` means for the figure in front of them.
107
+ * slide deck accepts. The resolution paints a PNG with more pixels and makes
108
+ * an SVG open larger, and the panel writes out the size it is about to
109
+ * produce so nobody has to guess what `3×` means for the figure in front of
110
+ * them.
90
111
  * @param props - See {@link FigureDownloadProps}.
91
112
  * @returns The glyph and its panel.
92
113
  */
@@ -94,15 +115,22 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
94
115
  const { targetId, fileName, background, scales = FIGURE_SCALES } = props;
95
116
  const { defaultFormat = 'png', defaultScale = DEFAULT_FIGURE_SCALE } = props;
96
117
  const { label, title } = props;
97
- const { icon = 'download', testId } = props;
118
+ const { icon = 'download', testId, renderFigure } = props;
98
119
 
99
120
  const t = useChromeT();
100
121
  const [open, setOpen] = useState(false);
101
122
  const [format, setFormat] = useState<FigureFormat>(defaultFormat);
102
- const [scale, setScale] = useState(defaultScale);
123
+ const [picked, setPicked] = useState<Record<FigureFormat, number>>(() => ({
124
+ png: defaultScale,
125
+ svg: defaultFigureScale('svg'),
126
+ }));
127
+ const scale = picked[format];
103
128
  const [size, setSize] = useState<FigurePixels | null>(null);
104
129
  const [failure, setFailure] = useState<string | null>(null);
105
130
  const [saving, setSaving] = useState(false);
131
+ const [layout, setLayout] = useState<FigureLayout>('screen');
132
+ const [custom, setCustom] = useState<FigurePixels | null>(null);
133
+ const { redraw, portal } = useFigureRedraw(renderFigure);
106
134
 
107
135
  // The panel is held open from here rather than left to itself, because the
108
136
  // figure has to be measured at the moment it opens: its size is what the
@@ -118,7 +146,17 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
118
146
  async function save(): Promise<void> {
119
147
  setSaving(true);
120
148
  try {
121
- await downloadFigure(targetId, { format, scale, fileName, background });
149
+ const options = { format, scale, fileName, background };
150
+ if (
151
+ renderFigure !== undefined &&
152
+ size !== null &&
153
+ figureLayoutRedraws(layout, size, custom ?? size)
154
+ ) {
155
+ const drawn = figureLayoutSize(layout, size, custom ?? size);
156
+ await redraw(targetId, drawn, (box) => downloadFigure(box, options));
157
+ } else {
158
+ await downloadFigure(targetId, options);
159
+ }
122
160
  setFailure(null);
123
161
  } catch (error) {
124
162
  setFailure(error instanceof Error ? error.message : String(error));
@@ -128,33 +166,48 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
128
166
  }
129
167
 
130
168
  return (
131
- <PopoverNext
132
- isOpen={open}
133
- placement="bottom-end"
134
- onInteraction={interact}
135
- content={
136
- <FigureDownloadPanel
137
- title={title ?? t('download.saveFigure')}
138
- format={format}
139
- scale={scale}
140
- scales={scales}
141
- size={size}
142
- failure={failure}
143
- saving={saving}
144
- onFormatChange={setFormat}
145
- onScaleChange={setScale}
146
- onSave={() => void save()}
169
+ <>
170
+ <PopoverNext
171
+ isOpen={open}
172
+ placement="bottom-end"
173
+ onInteraction={interact}
174
+ content={
175
+ <FigureDownloadPanel
176
+ title={title ?? t('download.saveFigure')}
177
+ format={format}
178
+ scale={scale}
179
+ scales={scales}
180
+ size={size}
181
+ failure={failure}
182
+ saving={saving}
183
+ onFormatChange={setFormat}
184
+ onScaleChange={(next) =>
185
+ setPicked((current) => ({ ...current, [format]: next }))
186
+ }
187
+ onSave={() => void save()}
188
+ sizing={
189
+ renderFigure === undefined
190
+ ? undefined
191
+ : {
192
+ layout,
193
+ custom,
194
+ onLayoutChange: setLayout,
195
+ onCustomChange: setCustom,
196
+ }
197
+ }
198
+ />
199
+ }
200
+ >
201
+ <OverlayIconButton
202
+ icon={icon}
203
+ label={label ?? t('download.saveThisFigure')}
204
+ value={format.toUpperCase()}
205
+ active={open}
206
+ testId={testId}
207
+ opensMenu
147
208
  />
148
- }
149
- >
150
- <OverlayIconButton
151
- icon={icon}
152
- label={label ?? t('download.saveThisFigure')}
153
- value={format.toUpperCase()}
154
- active={open}
155
- testId={testId}
156
- opensMenu
157
- />
158
- </PopoverNext>
209
+ </PopoverNext>
210
+ {portal}
211
+ </>
159
212
  );
160
213
  }