react-cheminfo 0.34.0 → 0.35.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 (171) hide show
  1. package/README.md +28 -28
  2. package/lib/about/core/about.d.ts +1 -30
  3. package/lib/about/core/about.d.ts.map +1 -1
  4. package/lib/about/core/about.js +0 -16
  5. package/lib/about/core/about.js.map +1 -1
  6. package/lib/about/core/index.d.ts +0 -1
  7. package/lib/about/core/index.d.ts.map +1 -1
  8. package/lib/about/core/index.js +0 -1
  9. package/lib/about/core/index.js.map +1 -1
  10. package/lib/about/ui/AboutBuild.d.ts +2 -5
  11. package/lib/about/ui/AboutBuild.d.ts.map +1 -1
  12. package/lib/about/ui/AboutBuild.js +7 -19
  13. package/lib/about/ui/AboutBuild.js.map +1 -1
  14. package/lib/about/ui/AboutPage.d.ts +2 -2
  15. package/lib/about/ui/AboutPage.d.ts.map +1 -1
  16. package/lib/about/ui/AboutPage.js +3 -14
  17. package/lib/about/ui/AboutPage.js.map +1 -1
  18. package/lib/about/ui/AboutVersion.d.ts +0 -4
  19. package/lib/about/ui/AboutVersion.d.ts.map +1 -1
  20. package/lib/about/ui/AboutVersion.js +3 -31
  21. package/lib/about/ui/AboutVersion.js.map +1 -1
  22. package/lib/download/core/index.d.ts +0 -5
  23. package/lib/download/core/index.d.ts.map +1 -1
  24. package/lib/download/core/index.js +0 -3
  25. package/lib/download/core/index.js.map +1 -1
  26. package/lib/download/ui/FigureDownload.d.ts +4 -55
  27. package/lib/download/ui/FigureDownload.d.ts.map +1 -1
  28. package/lib/download/ui/FigureDownload.js +6 -66
  29. package/lib/download/ui/FigureDownload.js.map +1 -1
  30. package/lib/download/ui/FigureDownloadPanel.d.ts +16 -2
  31. package/lib/download/ui/FigureDownloadPanel.d.ts.map +1 -1
  32. package/lib/download/ui/FigureDownloadPanel.js +11 -30
  33. package/lib/download/ui/FigureDownloadPanel.js.map +1 -1
  34. package/lib/download/ui/figureDownloadHint.d.ts +30 -0
  35. package/lib/download/ui/figureDownloadHint.d.ts.map +1 -0
  36. package/lib/download/ui/figureDownloadHint.js +33 -0
  37. package/lib/download/ui/figureDownloadHint.js.map +1 -0
  38. package/lib/download/ui/index.d.ts +3 -10
  39. package/lib/download/ui/index.d.ts.map +1 -1
  40. package/lib/download/ui/index.js +1 -5
  41. package/lib/download/ui/index.js.map +1 -1
  42. package/lib/download/ui/useFigureActions.d.ts +50 -0
  43. package/lib/download/ui/useFigureActions.d.ts.map +1 -0
  44. package/lib/download/ui/useFigureActions.js +58 -0
  45. package/lib/download/ui/useFigureActions.js.map +1 -0
  46. package/lib/download/ui/useFigureDownload.d.ts +85 -0
  47. package/lib/download/ui/useFigureDownload.d.ts.map +1 -0
  48. package/lib/download/ui/useFigureDownload.js +75 -0
  49. package/lib/download/ui/useFigureDownload.js.map +1 -0
  50. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  51. package/lib/ecosystem/core/sites.js +0 -40
  52. package/lib/ecosystem/core/sites.js.map +1 -1
  53. package/lib/ecosystem/core/types.d.ts +0 -10
  54. package/lib/ecosystem/core/types.d.ts.map +1 -1
  55. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  56. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  57. package/lib/ir/core/irCommands.d.ts +2 -2
  58. package/lib/ir/core/irCommands.js +2 -2
  59. package/lib/ir/core/irCommands.js.map +1 -1
  60. package/lib/ir/ui/IrAboutDialog.d.ts.map +1 -1
  61. package/lib/ir/ui/IrAboutDialog.js +1 -1
  62. package/lib/ir/ui/IrAboutDialog.js.map +1 -1
  63. package/lib/ir/ui/IrCanvas.d.ts +6 -0
  64. package/lib/ir/ui/IrCanvas.d.ts.map +1 -1
  65. package/lib/ir/ui/IrCanvas.js +2 -2
  66. package/lib/ir/ui/IrCanvas.js.map +1 -1
  67. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  68. package/lib/ir/ui/IrShell.js +16 -11
  69. package/lib/ir/ui/IrShell.js.map +1 -1
  70. package/lib/ir/ui/IrToolbar.d.ts +11 -1
  71. package/lib/ir/ui/IrToolbar.d.ts.map +1 -1
  72. package/lib/ir/ui/IrToolbar.js +12 -2
  73. package/lib/ir/ui/IrToolbar.js.map +1 -1
  74. package/lib/ir/ui/index.d.ts +1 -0
  75. package/lib/ir/ui/index.d.ts.map +1 -1
  76. package/lib/ir/ui/index.js.map +1 -1
  77. package/lib/locales/de.json +2 -4
  78. package/lib/locales/en.json +2 -4
  79. package/lib/locales/es.json +2 -4
  80. package/lib/locales/fr.json +2 -4
  81. package/lib/locales/it.json +2 -4
  82. package/lib/overlay/ui/OverlayPanel.d.ts +6 -0
  83. package/lib/overlay/ui/OverlayPanel.d.ts.map +1 -1
  84. package/lib/overlay/ui/OverlayPanel.js +2 -2
  85. package/lib/overlay/ui/OverlayPanel.js.map +1 -1
  86. package/lib/overlay/ui/overlayPanelStyles.d.ts +2 -1
  87. package/lib/overlay/ui/overlayPanelStyles.d.ts.map +1 -1
  88. package/lib/overlay/ui/overlayPanelStyles.js +3 -1
  89. package/lib/overlay/ui/overlayPanelStyles.js.map +1 -1
  90. package/lib/periodic/ui/ElementCell.d.ts +0 -6
  91. package/lib/periodic/ui/ElementCell.d.ts.map +1 -1
  92. package/lib/periodic/ui/ElementCell.js +34 -30
  93. package/lib/periodic/ui/ElementCell.js.map +1 -1
  94. package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -1
  95. package/lib/periodic/ui/PeriodicTable.js +12 -15
  96. package/lib/periodic/ui/PeriodicTable.js.map +1 -1
  97. package/package.json +1 -1
  98. package/src/about/core/about.ts +1 -51
  99. package/src/about/core/index.ts +0 -1
  100. package/src/about/ui/AboutBuild.tsx +8 -37
  101. package/src/about/ui/AboutPage.tsx +8 -64
  102. package/src/about/ui/AboutVersion.tsx +5 -45
  103. package/src/download/core/index.ts +0 -25
  104. package/src/download/ui/FigureDownload.tsx +11 -148
  105. package/src/download/ui/FigureDownloadPanel.tsx +46 -45
  106. package/src/download/ui/figureDownloadHint.ts +52 -0
  107. package/src/download/ui/index.ts +6 -10
  108. package/src/download/ui/useFigureActions.ts +113 -0
  109. package/src/download/ui/useFigureDownload.tsx +191 -0
  110. package/src/ecosystem/core/sites.ts +0 -40
  111. package/src/ecosystem/core/types.ts +0 -10
  112. package/src/ir/core/irCommands.ts +2 -2
  113. package/src/ir/ui/IrAboutDialog.tsx +0 -13
  114. package/src/ir/ui/IrCanvas.tsx +8 -2
  115. package/src/ir/ui/IrShell.tsx +23 -25
  116. package/src/ir/ui/IrToolbar.tsx +36 -6
  117. package/src/ir/ui/index.ts +1 -0
  118. package/src/locales/de.json +2 -4
  119. package/src/locales/en.json +2 -4
  120. package/src/locales/es.json +2 -4
  121. package/src/locales/fr.json +2 -4
  122. package/src/locales/it.json +2 -4
  123. package/src/overlay/ui/OverlayPanel.tsx +8 -2
  124. package/src/overlay/ui/overlayPanelStyles.ts +3 -0
  125. package/src/periodic/ui/ElementCell.tsx +34 -37
  126. package/src/periodic/ui/PeriodicTable.tsx +12 -17
  127. package/lib/about/core/repository.d.ts +0 -14
  128. package/lib/about/core/repository.d.ts.map +0 -1
  129. package/lib/about/core/repository.js +0 -21
  130. package/lib/about/core/repository.js.map +0 -1
  131. package/lib/download/core/exportStyles.d.ts +0 -22
  132. package/lib/download/core/exportStyles.d.ts.map +0 -1
  133. package/lib/download/core/exportStyles.js +0 -19
  134. package/lib/download/core/exportStyles.js.map +0 -1
  135. package/lib/download/core/imageResolution.d.ts +0 -88
  136. package/lib/download/core/imageResolution.d.ts.map +0 -1
  137. package/lib/download/core/imageResolution.js +0 -77
  138. package/lib/download/core/imageResolution.js.map +0 -1
  139. package/lib/download/core/pictureExport.d.ts +0 -93
  140. package/lib/download/core/pictureExport.d.ts.map +0 -1
  141. package/lib/download/core/pictureExport.js +0 -116
  142. package/lib/download/core/pictureExport.js.map +0 -1
  143. package/lib/download/ui/ExportDialogFrame.d.ts +0 -46
  144. package/lib/download/ui/ExportDialogFrame.d.ts.map +0 -1
  145. package/lib/download/ui/ExportDialogFrame.js +0 -24
  146. package/lib/download/ui/ExportDialogFrame.js.map +0 -1
  147. package/lib/download/ui/ExportImageDialog.d.ts +0 -47
  148. package/lib/download/ui/ExportImageDialog.d.ts.map +0 -1
  149. package/lib/download/ui/ExportImageDialog.js +0 -25
  150. package/lib/download/ui/ExportImageDialog.js.map +0 -1
  151. package/lib/download/ui/ExportNameField.d.ts +0 -29
  152. package/lib/download/ui/ExportNameField.d.ts.map +0 -1
  153. package/lib/download/ui/ExportNameField.js +0 -99
  154. package/lib/download/ui/ExportNameField.js.map +0 -1
  155. package/lib/download/ui/ExportPictureSection.d.ts +0 -38
  156. package/lib/download/ui/ExportPictureSection.d.ts.map +0 -1
  157. package/lib/download/ui/ExportPictureSection.js +0 -48
  158. package/lib/download/ui/ExportPictureSection.js.map +0 -1
  159. package/lib/download/ui/useDrawingBox.d.ts +0 -23
  160. package/lib/download/ui/useDrawingBox.d.ts.map +0 -1
  161. package/lib/download/ui/useDrawingBox.js +0 -21
  162. package/lib/download/ui/useDrawingBox.js.map +0 -1
  163. package/src/about/core/repository.ts +0 -22
  164. package/src/download/core/exportStyles.ts +0 -20
  165. package/src/download/core/imageResolution.ts +0 -118
  166. package/src/download/core/pictureExport.ts +0 -207
  167. package/src/download/ui/ExportDialogFrame.tsx +0 -86
  168. package/src/download/ui/ExportImageDialog.tsx +0 -91
  169. package/src/download/ui/ExportNameField.tsx +0 -148
  170. package/src/download/ui/ExportPictureSection.tsx +0 -142
  171. package/src/download/ui/useDrawingBox.ts +0 -37
@@ -51,31 +51,6 @@ export {
51
51
  } from './figureTarget.ts';
52
52
  export { sanitizeFileName } from './sanitizeFileName.ts';
53
53
 
54
- export {
55
- exportButtonsStyle,
56
- exportNoteStyle,
57
- exportSectionStyle,
58
- } from './exportStyles.ts';
59
- export type {
60
- ImageResolution,
61
- ImageResolutionId,
62
- ImageSize,
63
- } from './imageResolution.ts';
64
- export {
65
- DEFAULT_IMAGE_RESOLUTION,
66
- IMAGE_RESOLUTIONS,
67
- formatImageSize,
68
- imageResolution,
69
- imageSize,
70
- } from './imageResolution.ts';
71
- export type { PictureFrame, PictureOptions } from './pictureExport.ts';
72
- export {
73
- PICTURE_PADDING,
74
- copyPicture,
75
- pictureSize,
76
- savePngPicture,
77
- saveSvgPicture,
78
- } from './pictureExport.ts';
79
54
  export { drawingIn, stackDrawings } from './stackedDrawing.ts';
80
55
  export type { SvgBox } from './svgBounds.ts';
81
56
  export { CONTENT_ATTRIBUTE, contentBox, elementBox } from './svgBounds.ts';
@@ -1,74 +1,20 @@
1
1
  import type { IconName } from '@blueprintjs/core';
2
2
  import { PopoverNext } from '@blueprintjs/core';
3
3
  import type { ReactElement } from 'react';
4
- import { useState } from 'react';
5
4
 
6
5
  import { useChromeT } from '../../i18n/ui/useT.ts';
7
6
  import { OverlayIconButton } from '../../overlay/ui/OverlayIconButton.tsx';
8
- import type { FigureFormat } from '../core/downloadFigure.ts';
9
- import { downloadFigure } from '../core/downloadFigure.ts';
10
- import type { FigureLayout } from '../core/figureLayout.ts';
11
- import { figureLayoutRedraws, figureLayoutSize } from '../core/figureLayout.ts';
12
- import type { FigurePixels } from '../core/figureScale.ts';
13
- import {
14
- DEFAULT_FIGURE_SCALE,
15
- FIGURE_SCALES,
16
- defaultFigureScale,
17
- } from '../core/figureScale.ts';
18
- import { figureSize } from '../core/figureTarget.ts';
19
7
 
20
- import { FigureDownloadPanel } from './FigureDownloadPanel.tsx';
21
- import type { FigureRenderer } from './useFigureRedraw.tsx';
22
- import { useFigureRedraw } from './useFigureRedraw.tsx';
8
+ import type { FigureDownloadOptions } from './useFigureDownload.tsx';
9
+ import { useFigureDownload } from './useFigureDownload.tsx';
23
10
 
24
11
  /** What {@link FigureDownload} needs. */
25
- export interface FigureDownloadProps {
26
- /**
27
- * The `id` of the box the figure is mounted in. Everything drawn inside it
28
- * is saved — sixteen charts of a pair grid as readily as one scatter plot —
29
- * and the controls floating over it are left behind.
30
- */
31
- targetId: string;
32
- /**
33
- * What the saved file is called, without its extension. Name it after the
34
- * data rather than after the tool: a reader with four of these in a
35
- * downloads folder cannot tell four `figure.png` apart.
36
- * @default 'figure'
37
- */
38
- fileName?: string;
39
- /**
40
- * The format it opens on.
41
- * @default 'png'
42
- */
43
- defaultFormat?: FigureFormat;
44
- /**
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.
48
- * @default 2
49
- */
50
- defaultScale?: number;
51
- /**
52
- * The resolutions offered.
53
- * @default [1, 2, 3, 4]
54
- */
55
- scales?: readonly number[];
56
- /**
57
- * What is painted under the figure. `transparent` leaves it unpainted, for a
58
- * figure going onto a coloured slide.
59
- * @default the surface the figure is drawn on
60
- */
61
- background?: string;
12
+ export interface FigureDownloadProps extends FigureDownloadOptions {
62
13
  /**
63
14
  * What the glyph is called, for the pointer and for a screen reader.
64
15
  * @default the chrome's own line, in the language of the page
65
16
  */
66
17
  label?: string;
67
- /**
68
- * What the panel behind it is called.
69
- * @default the chrome's own line, in the language of the page
70
- */
71
- title?: string;
72
18
  /**
73
19
  * Its glyph.
74
20
  * @default 'download'
@@ -79,20 +25,11 @@ export interface FigureDownloadProps {
79
25
  * @default undefined
80
26
  */
81
27
  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;
92
28
  }
93
29
 
94
30
  /**
95
- * The glyph that takes the figure off the page as a file.
31
+ * The glyph that takes the figure off the page, as a file or onto the
32
+ * clipboard.
96
33
  *
97
34
  * It works from the `id` of the box the figure is mounted in rather than from
98
35
  * the figure itself, so the control is free to sit in the bar above the
@@ -112,97 +49,23 @@ export interface FigureDownloadProps {
112
49
  * @returns The glyph and its panel.
113
50
  */
114
51
  export function FigureDownload(props: FigureDownloadProps): ReactElement {
115
- const { targetId, fileName, background, scales = FIGURE_SCALES } = props;
116
- const { defaultFormat = 'png', defaultScale = DEFAULT_FIGURE_SCALE } = props;
117
- const { label, title } = props;
118
- const { icon = 'download', testId, renderFigure } = props;
119
-
52
+ const { label, icon = 'download', testId, ...options } = props;
120
53
  const t = useChromeT();
121
- const [open, setOpen] = useState(false);
122
- const [format, setFormat] = useState<FigureFormat>(defaultFormat);
123
- const [picked, setPicked] = useState<Record<FigureFormat, number>>(() => ({
124
- png: defaultScale,
125
- svg: defaultFigureScale('svg'),
126
- }));
127
- const scale = picked[format];
128
- const [size, setSize] = useState<FigurePixels | null>(null);
129
- const [failure, setFailure] = useState<string | null>(null);
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);
134
-
135
- // The panel is held open from here rather than left to itself, because the
136
- // figure has to be measured at the moment it opens: its size is what the
137
- // panel writes out, and reading a bounding box on every render would cost a
138
- // layout pass on a bar that is re-rendered on every pointer move.
139
- function interact(next: boolean): void {
140
- setOpen(next);
141
- if (!next) return;
142
- setSize(figureSize(targetId));
143
- setFailure(null);
144
- }
145
-
146
- async function save(): Promise<void> {
147
- setSaving(true);
148
- try {
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
- }
160
- setFailure(null);
161
- } catch (error) {
162
- setFailure(error instanceof Error ? error.message : String(error));
163
- } finally {
164
- setSaving(false);
165
- }
166
- }
54
+ const { isOpen, setOpen, panel, format, portal } = useFigureDownload(options);
167
55
 
168
56
  return (
169
57
  <>
170
58
  <PopoverNext
171
- isOpen={open}
59
+ isOpen={isOpen}
172
60
  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
- }
61
+ onInteraction={setOpen}
62
+ content={panel}
200
63
  >
201
64
  <OverlayIconButton
202
65
  icon={icon}
203
66
  label={label ?? t('download.saveThisFigure')}
204
67
  value={format.toUpperCase()}
205
- active={open}
68
+ active={isOpen}
206
69
  testId={testId}
207
70
  opensMenu
208
71
  />
@@ -1,7 +1,5 @@
1
1
  import type { ReactElement } from 'react';
2
2
 
3
- import type { ChromeKey } from '../../i18n/core/chromeCatalog.ts';
4
- import type { Translate } from '../../i18n/ui/useT.ts';
5
3
  import { useChromeT } from '../../i18n/ui/useT.ts';
6
4
  import { OverlayAction } from '../../overlay/ui/OverlayAction.tsx';
7
5
  import { OverlayPanel } from '../../overlay/ui/OverlayPanel.tsx';
@@ -19,6 +17,8 @@ import {
19
17
 
20
18
  import type { FigureSizing } from './FigureSizeRows.tsx';
21
19
  import { FigureSizeRows } from './FigureSizeRows.tsx';
20
+ import { hintOf } from './figureDownloadHint.ts';
21
+ import type { FigureNotice } from './useFigureActions.ts';
22
22
 
23
23
  /** What {@link FigureDownloadPanel} is drawn from. */
24
24
  export interface FigureDownloadPanelProps {
@@ -34,7 +34,13 @@ export interface FigureDownloadPanelProps {
34
34
  size: FigurePixels | null;
35
35
  /** What went wrong the last time save was pressed, if anything. */
36
36
  failure: string | null;
37
- /** Whether a file is being written right now. */
37
+ /**
38
+ * What the last copy came to, while the choices it was made with still
39
+ * stand.
40
+ * @default null
41
+ */
42
+ notice?: FigureNotice | null;
43
+ /** Whether a file or a copy is being written right now. */
38
44
  saving: boolean;
39
45
  /** Called with the format the reader picked. */
40
46
  onFormatChange: (format: FigureFormat) => void;
@@ -42,6 +48,12 @@ export interface FigureDownloadPanelProps {
42
48
  onScaleChange: (scale: number) => void;
43
49
  /** Called when they press save. */
44
50
  onSave: () => void;
51
+ /**
52
+ * Called when they press copy, which puts a PNG at the resolution picked on
53
+ * the clipboard.
54
+ * @default undefined — no copy is offered
55
+ */
56
+ onCopy?: () => void;
45
57
  /**
46
58
  * Whether the SVG holds a rendered picture rather than vector drawings, as
47
59
  * for a WebGL scene: its resolution is then the pixels of that picture, it
@@ -59,7 +71,8 @@ export interface FigureDownloadPanelProps {
59
71
  }
60
72
 
61
73
  /**
62
- * The choices behind the save glyph, and the button that writes the file.
74
+ * The choices behind the save glyph, and the buttons that write the file and
75
+ * copy the picture.
63
76
  *
64
77
  * The resolution applies to both formats, and means what the reader needs it
65
78
  * to: a PNG is painted with that many more pixels, and an SVG, drawn the same,
@@ -81,7 +94,7 @@ export function FigureDownloadPanel(
81
94
  ): ReactElement {
82
95
  const { title, format, scale, scales, size, failure, saving } = props;
83
96
  const { onFormatChange, onScaleChange, onSave, rasterSvg = false } = props;
84
- const { sizing } = props;
97
+ const { sizing, onCopy, notice = null } = props;
85
98
  const t = useChromeT();
86
99
 
87
100
  // What the file is drawn at: the figure on screen, or the shape picked.
@@ -95,15 +108,29 @@ export function FigureDownloadPanel(
95
108
  return (
96
109
  <OverlayPanel
97
110
  title={title}
98
- hint={hintOf(format, drawn, scale, failure, rasterSvg, t)}
111
+ width={PANEL_WIDTH}
112
+ hint={hintOf(
113
+ { format, size: drawn, scale, failure, notice, rasterSvg },
114
+ t,
115
+ )}
99
116
  actions={
100
- <OverlayAction
101
- text={t('download.save')}
102
- icon="download"
103
- intent="primary"
104
- disabled={saving || size === null}
105
- onClick={onSave}
106
- />
117
+ <>
118
+ <OverlayAction
119
+ text={t('download.save')}
120
+ icon="download"
121
+ intent="primary"
122
+ disabled={saving || size === null}
123
+ onClick={onSave}
124
+ />
125
+ {onCopy === undefined ? null : (
126
+ <OverlayAction
127
+ text={t('clipboard.copy')}
128
+ icon="clipboard"
129
+ disabled={saving || size === null}
130
+ onClick={onCopy}
131
+ />
132
+ )}
133
+ </>
107
134
  }
108
135
  >
109
136
  <OverlaySegmented<FigureFormat>
@@ -137,6 +164,12 @@ export function FigureDownloadPanel(
137
164
  );
138
165
  }
139
166
 
167
+ /**
168
+ * Wide enough for the longest hint on one line in English, so switching
169
+ * between PNG and SVG — whose hints differ in length — never resizes the panel.
170
+ */
171
+ const PANEL_WIDTH = '27em';
172
+
140
173
  /** The two files a figure can leave the page as. */
141
174
  const FORMAT_CHOICES: ReadonlyArray<OverlayOption<FigureFormat>> = [
142
175
  { value: 'png', label: 'PNG' },
@@ -171,35 +204,3 @@ function scaleChoices(
171
204
  }
172
205
  return choices;
173
206
  }
174
-
175
- /**
176
- * The line at the foot of the panel: what pressing save is about to do.
177
- * @param format - Which file the reader is about to write.
178
- * @param size - How big the figure is drawn for the file.
179
- * @param scale - The multiple it is saved at.
180
- * @param failure - What went wrong last time, if anything.
181
- * @param rasterSvg - Whether the SVG embeds a rendered picture.
182
- * @param t - The chrome's formatter, so the sentence is in the language of
183
- * the page.
184
- * @returns The sentence.
185
- */
186
- function hintOf(
187
- format: FigureFormat,
188
- size: FigurePixels | null,
189
- scale: number,
190
- failure: string | null,
191
- rasterSvg: boolean,
192
- t: Translate<ChromeKey>,
193
- ): string {
194
- if (failure !== null) return failure;
195
- if (size === null) return t('download.noFigure');
196
- if (format === 'svg' && rasterSvg) {
197
- return t('download.hintRasterSvg', {
198
- pixels: formatFigurePixels(figurePixels(size, scale)),
199
- });
200
- }
201
- const pixels = formatFigurePixels(figurePixels(size, scale));
202
- return format === 'svg'
203
- ? t('download.hintSvg', { pixels })
204
- : t('download.hintPng', { pixels });
205
- }
@@ -0,0 +1,52 @@
1
+ import type { ChromeKey } from '../../i18n/core/chromeCatalog.ts';
2
+ import type { Translate } from '../../i18n/ui/useT.ts';
3
+ import type { FigureFormat } from '../core/downloadFigure.ts';
4
+ import type { FigurePixels } from '../core/figureScale.ts';
5
+ import { figurePixels, formatFigurePixels } from '../core/figureScale.ts';
6
+
7
+ import type { FigureNotice } from './useFigureActions.ts';
8
+
9
+ /** What the line at the foot of the panel is written from. */
10
+ export interface HintState {
11
+ /** Which file the reader is about to write. */
12
+ format: FigureFormat;
13
+ /** How big the figure is drawn for the file. */
14
+ size: FigurePixels | null;
15
+ /** The multiple it is saved at. */
16
+ scale: number;
17
+ /** What went wrong last time, if anything. */
18
+ failure: string | null;
19
+ /** What the last copy came to, if anything. */
20
+ notice: FigureNotice | null;
21
+ /** Whether the SVG embeds a rendered picture. */
22
+ rasterSvg: boolean;
23
+ }
24
+
25
+ /**
26
+ * The line at the foot of the panel: what pressing save is about to do, or
27
+ * what the last copy did.
28
+ * @param state - See {@link HintState}.
29
+ * @param t - The chrome's formatter, so the sentence is in the language of
30
+ * the page.
31
+ * @returns The sentence.
32
+ */
33
+ export function hintOf(state: HintState, t: Translate<ChromeKey>): string {
34
+ const { format, size, scale, failure, notice, rasterSvg } = state;
35
+ if (failure !== null) return failure;
36
+ if (size === null) return t('download.noFigure');
37
+ if (notice === 'copyUnsupported') return t('download.copyUnsupported');
38
+ if (notice === 'copied') {
39
+ return t('download.hintCopied', {
40
+ pixels: formatFigurePixels(figurePixels(size, scale)),
41
+ });
42
+ }
43
+ if (format === 'svg' && rasterSvg) {
44
+ return t('download.hintRasterSvg', {
45
+ pixels: formatFigurePixels(figurePixels(size, scale)),
46
+ });
47
+ }
48
+ const pixels = formatFigurePixels(figurePixels(size, scale));
49
+ return format === 'svg'
50
+ ? t('download.hintSvg', { pixels })
51
+ : t('download.hintPng', { pixels });
52
+ }
@@ -14,13 +14,9 @@ export { FigureDownloadPanel } from './FigureDownloadPanel.tsx';
14
14
  export type { FigureSizing } from './FigureSizeRows.tsx';
15
15
  export type { FigureRenderer } from './useFigureRedraw.tsx';
16
16
 
17
- export type { ExportDialogFrameProps } from './ExportDialogFrame.tsx';
18
- export { ExportDialogFrame } from './ExportDialogFrame.tsx';
19
- export type { ExportImageDialogProps } from './ExportImageDialog.tsx';
20
- export { ExportImageDialog } from './ExportImageDialog.tsx';
21
- export type { ExportNameFieldProps } from './ExportNameField.tsx';
22
- export { ExportNameField } from './ExportNameField.tsx';
23
- export type { ExportPictureSectionProps } from './ExportPictureSection.tsx';
24
- export { ExportPictureSection } from './ExportPictureSection.tsx';
25
- export type { DrawingBox } from './useDrawingBox.ts';
26
- export { useDrawingBox } from './useDrawingBox.ts';
17
+ export type {
18
+ FigureDownloadOptions,
19
+ FigureDownloadState,
20
+ } from './useFigureDownload.tsx';
21
+ export { useFigureDownload } from './useFigureDownload.tsx';
22
+ export type { FigureNotice } from './useFigureActions.ts';
@@ -0,0 +1,113 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ import { writeImageToClipboard } from '../../clipboard/core/copyPng.ts';
4
+ import type { FigureFormat } from '../core/downloadFigure.ts';
5
+ import { downloadFigure } from '../core/downloadFigure.ts';
6
+ import { figurePng } from '../core/figurePng.ts';
7
+ import type { FigurePixels } from '../core/figureScale.ts';
8
+ import { figureSvg } from '../core/figureSvg.ts';
9
+
10
+ import type { FigureRedraw } from './useFigureRedraw.tsx';
11
+
12
+ /** What the last copy came to, when it is worth a line of its own. */
13
+ export type FigureNotice = 'copied' | 'copyUnsupported';
14
+
15
+ /** What {@link useFigureActions} works from. */
16
+ export interface FigureActionsOptions {
17
+ /** The `id` of the box the figure is mounted in. */
18
+ targetId: string;
19
+ /** Which file save writes. */
20
+ format: FigureFormat;
21
+ /** The multiple of the figure on screen it is painted at. */
22
+ scale: number;
23
+ /** What the saved file is called, without its extension. */
24
+ fileName?: string;
25
+ /** What is painted under the figure. */
26
+ background?: string;
27
+ /**
28
+ * The size the figure is drawn again at, off the page, or `null` to take
29
+ * the figure as it is on screen.
30
+ */
31
+ redrawAt: FigurePixels | null;
32
+ /** Draws the figure again, from {@link FigureRedraw}. */
33
+ redraw: FigureRedraw['redraw'];
34
+ }
35
+
36
+ /** What {@link useFigureActions} hands back. */
37
+ export interface FigureActions {
38
+ /** Write the figure to a file, in the format picked. */
39
+ save: () => void;
40
+ /** Put the figure on the clipboard, as a PNG at the resolution picked. */
41
+ copy: () => void;
42
+ /** Whether a file or a copy is being written right now. */
43
+ busy: boolean;
44
+ /** What went wrong the last time, if anything. */
45
+ failure: string | null;
46
+ /** What the last copy came to, if it is still what the panel describes. */
47
+ notice: FigureNotice | null;
48
+ /** Forget the last outcome, once the choices it answered have changed. */
49
+ clear: () => void;
50
+ }
51
+
52
+ /**
53
+ * The two things the figure panel does: save the figure, and copy it.
54
+ *
55
+ * A copy is always a PNG, whatever format save is set to: a clipboard carries
56
+ * a picture, and the one every slide, chat window and issue accepts is a PNG.
57
+ * @param options - See {@link FigureActionsOptions}.
58
+ * @returns See {@link FigureActions}.
59
+ */
60
+ export function useFigureActions(options: FigureActionsOptions): FigureActions {
61
+ const { targetId, format, scale, fileName, background } = options;
62
+ const { redrawAt, redraw } = options;
63
+ const [busy, setBusy] = useState(false);
64
+ const [failure, setFailure] = useState<string | null>(null);
65
+ const [notice, setNotice] = useState<FigureNotice | null>(null);
66
+
67
+ function withFigure(write: (box: string | Element) => Promise<void>) {
68
+ return redrawAt === null
69
+ ? write(targetId)
70
+ : redraw(targetId, redrawAt, write);
71
+ }
72
+
73
+ function run(task: Promise<FigureNotice | null>): void {
74
+ setBusy(true);
75
+ setFailure(null);
76
+ setNotice(null);
77
+ void task
78
+ .then(setNotice, (error: unknown) => {
79
+ setFailure(error instanceof Error ? error.message : String(error));
80
+ })
81
+ .finally(() => setBusy(false));
82
+ }
83
+
84
+ function save(): void {
85
+ const options = { format, scale, fileName, background };
86
+ run(withFigure((box) => downloadFigure(box, options)).then(() => null));
87
+ }
88
+
89
+ function copy(): void {
90
+ // The clipboard is written before anything is awaited, with the picture
91
+ // still being painted: Safari only allows a write the click itself started.
92
+ const png = new Promise<Blob>((resolve, reject) => {
93
+ withFigure(async (box) => {
94
+ resolve(await figurePng(figureSvg(box, { background }), scale));
95
+ }).catch(reject);
96
+ });
97
+ // A browser whose clipboard takes text only never reads the picture, so
98
+ // its failure would otherwise go unhandled.
99
+ png.catch(() => undefined);
100
+ run(
101
+ writeImageToClipboard(png).then((copied) =>
102
+ copied ? 'copied' : 'copyUnsupported',
103
+ ),
104
+ );
105
+ }
106
+
107
+ const clear = useCallback(() => {
108
+ setFailure(null);
109
+ setNotice(null);
110
+ }, []);
111
+
112
+ return { save, copy, busy, failure, notice, clear };
113
+ }