react-cheminfo 0.34.0 → 0.36.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 (225) hide show
  1. package/README.md +28 -28
  2. package/bin/check-seo.mjs +333 -0
  3. package/lib/about/core/about.d.ts +1 -30
  4. package/lib/about/core/about.d.ts.map +1 -1
  5. package/lib/about/core/about.js +0 -16
  6. package/lib/about/core/about.js.map +1 -1
  7. package/lib/about/core/index.d.ts +0 -1
  8. package/lib/about/core/index.d.ts.map +1 -1
  9. package/lib/about/core/index.js +0 -1
  10. package/lib/about/core/index.js.map +1 -1
  11. package/lib/about/ui/AboutBuild.d.ts +2 -5
  12. package/lib/about/ui/AboutBuild.d.ts.map +1 -1
  13. package/lib/about/ui/AboutBuild.js +7 -19
  14. package/lib/about/ui/AboutBuild.js.map +1 -1
  15. package/lib/about/ui/AboutPage.d.ts +2 -2
  16. package/lib/about/ui/AboutPage.d.ts.map +1 -1
  17. package/lib/about/ui/AboutPage.js +3 -14
  18. package/lib/about/ui/AboutPage.js.map +1 -1
  19. package/lib/about/ui/AboutVersion.d.ts +0 -4
  20. package/lib/about/ui/AboutVersion.d.ts.map +1 -1
  21. package/lib/about/ui/AboutVersion.js +3 -31
  22. package/lib/about/ui/AboutVersion.js.map +1 -1
  23. package/lib/download/core/index.d.ts +0 -5
  24. package/lib/download/core/index.d.ts.map +1 -1
  25. package/lib/download/core/index.js +0 -3
  26. package/lib/download/core/index.js.map +1 -1
  27. package/lib/download/ui/FigureDownload.d.ts +4 -55
  28. package/lib/download/ui/FigureDownload.d.ts.map +1 -1
  29. package/lib/download/ui/FigureDownload.js +6 -66
  30. package/lib/download/ui/FigureDownload.js.map +1 -1
  31. package/lib/download/ui/FigureDownloadPanel.d.ts +16 -2
  32. package/lib/download/ui/FigureDownloadPanel.d.ts.map +1 -1
  33. package/lib/download/ui/FigureDownloadPanel.js +11 -30
  34. package/lib/download/ui/FigureDownloadPanel.js.map +1 -1
  35. package/lib/download/ui/figureDownloadHint.d.ts +30 -0
  36. package/lib/download/ui/figureDownloadHint.d.ts.map +1 -0
  37. package/lib/download/ui/figureDownloadHint.js +33 -0
  38. package/lib/download/ui/figureDownloadHint.js.map +1 -0
  39. package/lib/download/ui/index.d.ts +3 -10
  40. package/lib/download/ui/index.d.ts.map +1 -1
  41. package/lib/download/ui/index.js +1 -5
  42. package/lib/download/ui/index.js.map +1 -1
  43. package/lib/download/ui/useFigureActions.d.ts +50 -0
  44. package/lib/download/ui/useFigureActions.d.ts.map +1 -0
  45. package/lib/download/ui/useFigureActions.js +58 -0
  46. package/lib/download/ui/useFigureActions.js.map +1 -0
  47. package/lib/download/ui/useFigureDownload.d.ts +85 -0
  48. package/lib/download/ui/useFigureDownload.d.ts.map +1 -0
  49. package/lib/download/ui/useFigureDownload.js +75 -0
  50. package/lib/download/ui/useFigureDownload.js.map +1 -0
  51. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  52. package/lib/ecosystem/core/sites.js +0 -40
  53. package/lib/ecosystem/core/sites.js.map +1 -1
  54. package/lib/ecosystem/core/types.d.ts +0 -10
  55. package/lib/ecosystem/core/types.d.ts.map +1 -1
  56. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  57. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  58. package/lib/ir/core/irCommands.d.ts +2 -2
  59. package/lib/ir/core/irCommands.js +2 -2
  60. package/lib/ir/core/irCommands.js.map +1 -1
  61. package/lib/ir/ui/IrAboutDialog.d.ts.map +1 -1
  62. package/lib/ir/ui/IrAboutDialog.js +1 -1
  63. package/lib/ir/ui/IrAboutDialog.js.map +1 -1
  64. package/lib/ir/ui/IrCanvas.d.ts +6 -0
  65. package/lib/ir/ui/IrCanvas.d.ts.map +1 -1
  66. package/lib/ir/ui/IrCanvas.js +2 -2
  67. package/lib/ir/ui/IrCanvas.js.map +1 -1
  68. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  69. package/lib/ir/ui/IrShell.js +16 -11
  70. package/lib/ir/ui/IrShell.js.map +1 -1
  71. package/lib/ir/ui/IrToolbar.d.ts +11 -1
  72. package/lib/ir/ui/IrToolbar.d.ts.map +1 -1
  73. package/lib/ir/ui/IrToolbar.js +12 -2
  74. package/lib/ir/ui/IrToolbar.js.map +1 -1
  75. package/lib/ir/ui/index.d.ts +1 -0
  76. package/lib/ir/ui/index.d.ts.map +1 -1
  77. package/lib/ir/ui/index.js.map +1 -1
  78. package/lib/language/core/index.d.ts +2 -0
  79. package/lib/language/core/index.d.ts.map +1 -1
  80. package/lib/language/core/index.js +1 -0
  81. package/lib/language/core/index.js.map +1 -1
  82. package/lib/language/core/languagePath.d.ts +52 -0
  83. package/lib/language/core/languagePath.d.ts.map +1 -0
  84. package/lib/language/core/languagePath.js +65 -0
  85. package/lib/language/core/languagePath.js.map +1 -0
  86. package/lib/locales/de.json +2 -4
  87. package/lib/locales/en.json +2 -4
  88. package/lib/locales/es.json +2 -4
  89. package/lib/locales/fr.json +2 -4
  90. package/lib/locales/it.json +2 -4
  91. package/lib/overlay/ui/OverlayPanel.d.ts +6 -0
  92. package/lib/overlay/ui/OverlayPanel.d.ts.map +1 -1
  93. package/lib/overlay/ui/OverlayPanel.js +2 -2
  94. package/lib/overlay/ui/OverlayPanel.js.map +1 -1
  95. package/lib/overlay/ui/overlayPanelStyles.d.ts +2 -1
  96. package/lib/overlay/ui/overlayPanelStyles.d.ts.map +1 -1
  97. package/lib/overlay/ui/overlayPanelStyles.js +3 -1
  98. package/lib/overlay/ui/overlayPanelStyles.js.map +1 -1
  99. package/lib/periodic/ui/ElementCell.d.ts +0 -6
  100. package/lib/periodic/ui/ElementCell.d.ts.map +1 -1
  101. package/lib/periodic/ui/ElementCell.js +34 -30
  102. package/lib/periodic/ui/ElementCell.js.map +1 -1
  103. package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -1
  104. package/lib/periodic/ui/PeriodicTable.js +12 -15
  105. package/lib/periodic/ui/PeriodicTable.js.map +1 -1
  106. package/lib/seo/core/alternates.d.ts +40 -0
  107. package/lib/seo/core/alternates.d.ts.map +1 -0
  108. package/lib/seo/core/alternates.js +42 -0
  109. package/lib/seo/core/alternates.js.map +1 -0
  110. package/lib/seo/core/index.d.ts +3 -0
  111. package/lib/seo/core/index.d.ts.map +1 -1
  112. package/lib/seo/core/index.js +2 -0
  113. package/lib/seo/core/index.js.map +1 -1
  114. package/lib/seo/core/noscript.d.ts +9 -0
  115. package/lib/seo/core/noscript.d.ts.map +1 -1
  116. package/lib/seo/core/noscript.js +7 -2
  117. package/lib/seo/core/noscript.js.map +1 -1
  118. package/lib/seo/core/pageMeta.d.ts +23 -0
  119. package/lib/seo/core/pageMeta.d.ts.map +1 -1
  120. package/lib/seo/core/pageMeta.js +52 -3
  121. package/lib/seo/core/pageMeta.js.map +1 -1
  122. package/lib/seo/core/pageProse.d.ts +76 -0
  123. package/lib/seo/core/pageProse.d.ts.map +1 -0
  124. package/lib/seo/core/pageProse.js +92 -0
  125. package/lib/seo/core/pageProse.js.map +1 -0
  126. package/lib/seo/core/routeProblems.d.ts +33 -0
  127. package/lib/seo/core/routeProblems.d.ts.map +1 -0
  128. package/lib/seo/core/routeProblems.js +89 -0
  129. package/lib/seo/core/routeProblems.js.map +1 -0
  130. package/lib/seo/core/routes.d.ts +14 -0
  131. package/lib/seo/core/routes.d.ts.map +1 -1
  132. package/lib/seo/core/routes.js.map +1 -1
  133. package/lib/seo/core/siteFiles.d.ts.map +1 -1
  134. package/lib/seo/core/siteFiles.js +2 -1
  135. package/lib/seo/core/siteFiles.js.map +1 -1
  136. package/lib/seo/vite/prerender.d.ts +13 -0
  137. package/lib/seo/vite/prerender.d.ts.map +1 -1
  138. package/lib/seo/vite/prerender.js +17 -10
  139. package/lib/seo/vite/prerender.js.map +1 -1
  140. package/package.json +2 -1
  141. package/src/about/core/about.ts +1 -51
  142. package/src/about/core/index.ts +0 -1
  143. package/src/about/ui/AboutBuild.tsx +8 -37
  144. package/src/about/ui/AboutPage.tsx +8 -64
  145. package/src/about/ui/AboutVersion.tsx +5 -45
  146. package/src/download/core/index.ts +0 -25
  147. package/src/download/ui/FigureDownload.tsx +11 -148
  148. package/src/download/ui/FigureDownloadPanel.tsx +46 -45
  149. package/src/download/ui/figureDownloadHint.ts +52 -0
  150. package/src/download/ui/index.ts +6 -10
  151. package/src/download/ui/useFigureActions.ts +113 -0
  152. package/src/download/ui/useFigureDownload.tsx +191 -0
  153. package/src/ecosystem/core/sites.ts +0 -40
  154. package/src/ecosystem/core/types.ts +0 -10
  155. package/src/ir/core/irCommands.ts +2 -2
  156. package/src/ir/ui/IrAboutDialog.tsx +0 -13
  157. package/src/ir/ui/IrCanvas.tsx +8 -2
  158. package/src/ir/ui/IrShell.tsx +23 -25
  159. package/src/ir/ui/IrToolbar.tsx +36 -6
  160. package/src/ir/ui/index.ts +1 -0
  161. package/src/language/core/index.ts +2 -0
  162. package/src/language/core/languagePath.ts +78 -0
  163. package/src/locales/de.json +2 -4
  164. package/src/locales/en.json +2 -4
  165. package/src/locales/es.json +2 -4
  166. package/src/locales/fr.json +2 -4
  167. package/src/locales/it.json +2 -4
  168. package/src/overlay/ui/OverlayPanel.tsx +8 -2
  169. package/src/overlay/ui/overlayPanelStyles.ts +3 -0
  170. package/src/periodic/ui/ElementCell.tsx +34 -37
  171. package/src/periodic/ui/PeriodicTable.tsx +12 -17
  172. package/src/seo/core/alternates.ts +65 -0
  173. package/src/seo/core/index.ts +3 -0
  174. package/src/seo/core/noscript.ts +18 -2
  175. package/src/seo/core/pageMeta.ts +74 -3
  176. package/src/seo/core/pageProse.ts +146 -0
  177. package/src/seo/core/routeProblems.ts +111 -0
  178. package/src/seo/core/routes.ts +14 -0
  179. package/src/seo/core/siteFiles.ts +2 -1
  180. package/src/seo/vite/prerender.ts +30 -11
  181. package/lib/about/core/repository.d.ts +0 -14
  182. package/lib/about/core/repository.d.ts.map +0 -1
  183. package/lib/about/core/repository.js +0 -21
  184. package/lib/about/core/repository.js.map +0 -1
  185. package/lib/download/core/exportStyles.d.ts +0 -22
  186. package/lib/download/core/exportStyles.d.ts.map +0 -1
  187. package/lib/download/core/exportStyles.js +0 -19
  188. package/lib/download/core/exportStyles.js.map +0 -1
  189. package/lib/download/core/imageResolution.d.ts +0 -88
  190. package/lib/download/core/imageResolution.d.ts.map +0 -1
  191. package/lib/download/core/imageResolution.js +0 -77
  192. package/lib/download/core/imageResolution.js.map +0 -1
  193. package/lib/download/core/pictureExport.d.ts +0 -93
  194. package/lib/download/core/pictureExport.d.ts.map +0 -1
  195. package/lib/download/core/pictureExport.js +0 -116
  196. package/lib/download/core/pictureExport.js.map +0 -1
  197. package/lib/download/ui/ExportDialogFrame.d.ts +0 -46
  198. package/lib/download/ui/ExportDialogFrame.d.ts.map +0 -1
  199. package/lib/download/ui/ExportDialogFrame.js +0 -24
  200. package/lib/download/ui/ExportDialogFrame.js.map +0 -1
  201. package/lib/download/ui/ExportImageDialog.d.ts +0 -47
  202. package/lib/download/ui/ExportImageDialog.d.ts.map +0 -1
  203. package/lib/download/ui/ExportImageDialog.js +0 -25
  204. package/lib/download/ui/ExportImageDialog.js.map +0 -1
  205. package/lib/download/ui/ExportNameField.d.ts +0 -29
  206. package/lib/download/ui/ExportNameField.d.ts.map +0 -1
  207. package/lib/download/ui/ExportNameField.js +0 -99
  208. package/lib/download/ui/ExportNameField.js.map +0 -1
  209. package/lib/download/ui/ExportPictureSection.d.ts +0 -38
  210. package/lib/download/ui/ExportPictureSection.d.ts.map +0 -1
  211. package/lib/download/ui/ExportPictureSection.js +0 -48
  212. package/lib/download/ui/ExportPictureSection.js.map +0 -1
  213. package/lib/download/ui/useDrawingBox.d.ts +0 -23
  214. package/lib/download/ui/useDrawingBox.d.ts.map +0 -1
  215. package/lib/download/ui/useDrawingBox.js +0 -21
  216. package/lib/download/ui/useDrawingBox.js.map +0 -1
  217. package/src/about/core/repository.ts +0 -22
  218. package/src/download/core/exportStyles.ts +0 -20
  219. package/src/download/core/imageResolution.ts +0 -118
  220. package/src/download/core/pictureExport.ts +0 -207
  221. package/src/download/ui/ExportDialogFrame.tsx +0 -86
  222. package/src/download/ui/ExportImageDialog.tsx +0 -91
  223. package/src/download/ui/ExportNameField.tsx +0 -148
  224. package/src/download/ui/ExportPictureSection.tsx +0 -142
  225. package/src/download/ui/useDrawingBox.ts +0 -37
@@ -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
+ }
@@ -0,0 +1,191 @@
1
+ import type { ReactElement, ReactNode } from 'react';
2
+ import { useCallback, useState } from 'react';
3
+
4
+ import { useChromeT } from '../../i18n/ui/useT.ts';
5
+ import type { FigureFormat } from '../core/downloadFigure.ts';
6
+ import type { FigureLayout } from '../core/figureLayout.ts';
7
+ import { figureLayoutRedraws, figureLayoutSize } from '../core/figureLayout.ts';
8
+ import type { FigurePixels } from '../core/figureScale.ts';
9
+ import {
10
+ DEFAULT_FIGURE_SCALE,
11
+ FIGURE_SCALES,
12
+ defaultFigureScale,
13
+ } from '../core/figureScale.ts';
14
+ import { figureSize } from '../core/figureTarget.ts';
15
+
16
+ import { FigureDownloadPanel } from './FigureDownloadPanel.tsx';
17
+ import { useFigureActions } from './useFigureActions.ts';
18
+ import type { FigureRenderer } from './useFigureRedraw.tsx';
19
+ import { useFigureRedraw } from './useFigureRedraw.tsx';
20
+
21
+ /** What the "Save figure" panel is set up with. */
22
+ export interface FigureDownloadOptions {
23
+ /**
24
+ * The `id` of the box the figure is mounted in. Everything drawn inside it
25
+ * is saved — sixteen charts of a pair grid as readily as one scatter plot —
26
+ * and the controls floating over it are left behind.
27
+ */
28
+ targetId: string;
29
+ /**
30
+ * What the saved file is called, without its extension. Name it after the
31
+ * data rather than after the tool: a reader with four of these in a
32
+ * downloads folder cannot tell four `figure.png` apart.
33
+ * @default 'figure'
34
+ */
35
+ fileName?: string;
36
+ /**
37
+ * The format it opens on.
38
+ * @default 'png'
39
+ */
40
+ defaultFormat?: FigureFormat;
41
+ /**
42
+ * The resolution a PNG opens on, as a multiple of the figure on screen. An
43
+ * SVG opens at 1×, the size it has on screen, and each format keeps the
44
+ * multiple the reader last picked for it.
45
+ * @default 2
46
+ */
47
+ defaultScale?: number;
48
+ /**
49
+ * The resolutions offered.
50
+ * @default [1, 2, 3, 4]
51
+ */
52
+ scales?: readonly number[];
53
+ /**
54
+ * What is painted under the figure. `transparent` leaves it unpainted, for a
55
+ * figure going onto a coloured slide.
56
+ * @default the surface the figure is drawn on
57
+ */
58
+ background?: string;
59
+ /**
60
+ * What the panel is called.
61
+ * @default the chrome's own line, in the language of the page
62
+ */
63
+ title?: string;
64
+ /**
65
+ * Draws the figure at a size, for a file. When it is given the panel offers
66
+ * a size — as shown, 4:3, 16:9, a journal column, or typed — and a figure
67
+ * saved at a size other than its own is drawn again at that size, off the
68
+ * page, so its axes and labels are laid out for the new shape rather than
69
+ * stretched into it. Draw exactly what is on screen, with the same data,
70
+ * zoom and colours; only the box changes.
71
+ * @default undefined — the figure is saved at the size it has on screen
72
+ */
73
+ renderFigure?: FigureRenderer;
74
+ }
75
+
76
+ /** What {@link useFigureDownload} hands back. */
77
+ export interface FigureDownloadState {
78
+ /** Whether the panel is showing. */
79
+ isOpen: boolean;
80
+ /**
81
+ * Show or hide the panel. Showing it measures the figure, so hand this to
82
+ * the popover's `onInteraction` rather than a setter of your own.
83
+ */
84
+ setOpen: (open: boolean) => void;
85
+ /** The panel, for the popover's `content`. */
86
+ panel: ReactElement;
87
+ /** The format picked, for a trigger that shows it. */
88
+ format: FigureFormat;
89
+ /**
90
+ * The copy drawn off the page while a figure is saved at another size, to
91
+ * be rendered anywhere in the caller's tree.
92
+ */
93
+ portal: ReactNode;
94
+ }
95
+
96
+ /**
97
+ * The "Save figure" panel — save as PNG or SVG, or copy as PNG — for a caller
98
+ * that brings its own trigger, such as a button in an editor's toolbar.
99
+ * `FigureDownload` is the same panel behind the family's own glyph.
100
+ * @param options - See {@link FigureDownloadOptions}.
101
+ * @returns See {@link FigureDownloadState}.
102
+ */
103
+ export function useFigureDownload(
104
+ options: FigureDownloadOptions,
105
+ ): FigureDownloadState {
106
+ const { targetId, fileName, background, scales = FIGURE_SCALES } = options;
107
+ const { defaultFormat = 'png', defaultScale = DEFAULT_FIGURE_SCALE } =
108
+ options;
109
+ const { title, renderFigure } = options;
110
+
111
+ const t = useChromeT();
112
+ const [isOpen, setIsOpen] = useState(false);
113
+ const [format, setFormat] = useState<FigureFormat>(defaultFormat);
114
+ const [picked, setPicked] = useState<Record<FigureFormat, number>>(() => ({
115
+ png: defaultScale,
116
+ svg: defaultFigureScale('svg'),
117
+ }));
118
+ const scale = picked[format];
119
+ const [size, setSize] = useState<FigurePixels | null>(null);
120
+ const [layout, setLayout] = useState<FigureLayout>('screen');
121
+ const [custom, setCustom] = useState<FigurePixels | null>(null);
122
+ const { redraw, portal } = useFigureRedraw(renderFigure);
123
+ const redrawAt =
124
+ renderFigure !== undefined &&
125
+ size !== null &&
126
+ figureLayoutRedraws(layout, size, custom ?? size)
127
+ ? figureLayoutSize(layout, size, custom ?? size)
128
+ : null;
129
+ const { save, copy, busy, failure, notice, clear } = useFigureActions({
130
+ targetId,
131
+ format,
132
+ scale,
133
+ fileName,
134
+ background,
135
+ redrawAt,
136
+ redraw,
137
+ });
138
+
139
+ // The panel is held open from here rather than left to itself, because the
140
+ // figure has to be measured at the moment it opens: its size is what the
141
+ // panel writes out, and reading a bounding box on every render would cost a
142
+ // layout pass on a bar that is re-rendered on every pointer move.
143
+ const setOpen = useCallback(
144
+ (next: boolean) => {
145
+ setIsOpen(next);
146
+ if (!next) return;
147
+ setSize(figureSize(targetId));
148
+ clear();
149
+ },
150
+ [targetId, clear],
151
+ );
152
+
153
+ // What the last copy came to only describes the choices it was made with.
154
+ function changed<T>(apply: (value: T) => void): (value: T) => void {
155
+ return (value) => {
156
+ clear();
157
+ apply(value);
158
+ };
159
+ }
160
+
161
+ const panel = (
162
+ <FigureDownloadPanel
163
+ title={title ?? t('download.saveFigure')}
164
+ format={format}
165
+ scale={scale}
166
+ scales={scales}
167
+ size={size}
168
+ failure={failure}
169
+ notice={notice}
170
+ saving={busy}
171
+ onFormatChange={changed(setFormat)}
172
+ onScaleChange={changed((next: number) =>
173
+ setPicked((current) => ({ ...current, [format]: next })),
174
+ )}
175
+ onSave={save}
176
+ onCopy={copy}
177
+ sizing={
178
+ renderFigure === undefined
179
+ ? undefined
180
+ : {
181
+ layout,
182
+ custom,
183
+ onLayoutChange: changed(setLayout),
184
+ onCustomChange: changed(setCustom),
185
+ }
186
+ }
187
+ />
188
+ );
189
+
190
+ return { isOpen, setOpen, panel, format, portal };
191
+ }