react-cheminfo 0.17.0 → 0.19.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 (187) hide show
  1. package/lib/about/core/about.d.ts +9 -0
  2. package/lib/about/core/about.d.ts.map +1 -1
  3. package/lib/about/core/about.js +1 -0
  4. package/lib/about/core/about.js.map +1 -1
  5. package/lib/about/core/index.d.ts +1 -0
  6. package/lib/about/core/index.d.ts.map +1 -1
  7. package/lib/about/core/index.js +1 -0
  8. package/lib/about/core/index.js.map +1 -1
  9. package/lib/about/core/repository.d.ts +14 -0
  10. package/lib/about/core/repository.d.ts.map +1 -0
  11. package/lib/about/core/repository.js +21 -0
  12. package/lib/about/core/repository.js.map +1 -0
  13. package/lib/about/ui/AboutBuild.d.ts +5 -4
  14. package/lib/about/ui/AboutBuild.d.ts.map +1 -1
  15. package/lib/about/ui/AboutBuild.js +10 -10
  16. package/lib/about/ui/AboutBuild.js.map +1 -1
  17. package/lib/about/ui/AboutPage.d.ts.map +1 -1
  18. package/lib/about/ui/AboutPage.js +7 -8
  19. package/lib/about/ui/AboutPage.js.map +1 -1
  20. package/lib/about/ui/AboutVersion.d.ts +25 -0
  21. package/lib/about/ui/AboutVersion.d.ts.map +1 -0
  22. package/lib/about/ui/AboutVersion.js +31 -0
  23. package/lib/about/ui/AboutVersion.js.map +1 -0
  24. package/lib/about/ui/index.d.ts +2 -0
  25. package/lib/about/ui/index.d.ts.map +1 -1
  26. package/lib/about/ui/index.js +1 -0
  27. package/lib/about/ui/index.js.map +1 -1
  28. package/lib/build/core/buildInfo.d.ts +16 -0
  29. package/lib/build/core/buildInfo.d.ts.map +1 -1
  30. package/lib/build/core/buildInfo.js +21 -0
  31. package/lib/build/core/buildInfo.js.map +1 -1
  32. package/lib/build/core/index.d.ts +1 -1
  33. package/lib/build/core/index.d.ts.map +1 -1
  34. package/lib/build/core/index.js +1 -1
  35. package/lib/build/core/index.js.map +1 -1
  36. package/lib/build/vite/buildInfo.d.ts.map +1 -1
  37. package/lib/build/vite/buildInfo.js +2 -2
  38. package/lib/build/vite/buildInfo.js.map +1 -1
  39. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  40. package/lib/ecosystem/core/sites.js +12 -0
  41. package/lib/ecosystem/core/sites.js.map +1 -1
  42. package/lib/ecosystem/core/types.d.ts +8 -0
  43. package/lib/ecosystem/core/types.d.ts.map +1 -1
  44. package/lib/molecule3d/core/camera.d.ts +89 -0
  45. package/lib/molecule3d/core/camera.d.ts.map +1 -0
  46. package/lib/molecule3d/core/camera.js +172 -0
  47. package/lib/molecule3d/core/camera.js.map +1 -0
  48. package/lib/molecule3d/core/index.d.ts +2 -0
  49. package/lib/molecule3d/core/index.d.ts.map +1 -1
  50. package/lib/molecule3d/core/index.js +1 -0
  51. package/lib/molecule3d/core/index.js.map +1 -1
  52. package/lib/molecule3d/ui/MoleculeCanvas3D.d.ts.map +1 -1
  53. package/lib/molecule3d/ui/MoleculeCanvas3D.js +14 -37
  54. package/lib/molecule3d/ui/MoleculeCanvas3D.js.map +1 -1
  55. package/lib/molecule3d/ui/camera.d.ts +35 -2
  56. package/lib/molecule3d/ui/camera.d.ts.map +1 -1
  57. package/lib/molecule3d/ui/camera.js +113 -4
  58. package/lib/molecule3d/ui/camera.js.map +1 -1
  59. package/lib/molecule3d/ui/captureScene.d.ts +14 -0
  60. package/lib/molecule3d/ui/captureScene.d.ts.map +1 -0
  61. package/lib/molecule3d/ui/captureScene.js +24 -0
  62. package/lib/molecule3d/ui/captureScene.js.map +1 -0
  63. package/lib/molecule3d/ui/createViewer.d.ts +18 -0
  64. package/lib/molecule3d/ui/createViewer.d.ts.map +1 -0
  65. package/lib/molecule3d/ui/createViewer.js +19 -0
  66. package/lib/molecule3d/ui/createViewer.js.map +1 -0
  67. package/lib/molecule3d/ui/moleculeViewer3DProps.d.ts +21 -0
  68. package/lib/molecule3d/ui/moleculeViewer3DProps.d.ts.map +1 -1
  69. package/lib/molecule3d/ui/useMoleculeScene.d.ts +47 -0
  70. package/lib/molecule3d/ui/useMoleculeScene.d.ts.map +1 -0
  71. package/lib/molecule3d/ui/useMoleculeScene.js +82 -0
  72. package/lib/molecule3d/ui/useMoleculeScene.js.map +1 -0
  73. package/lib/molecule3d/ui/useSharedCamera.d.ts +52 -0
  74. package/lib/molecule3d/ui/useSharedCamera.d.ts.map +1 -0
  75. package/lib/molecule3d/ui/useSharedCamera.js +82 -0
  76. package/lib/molecule3d/ui/useSharedCamera.js.map +1 -0
  77. package/lib/molecule3d/ui/viewer.d.ts +21 -15
  78. package/lib/molecule3d/ui/viewer.d.ts.map +1 -1
  79. package/lib/molecule3d/ui/viewer.js +41 -47
  80. package/lib/molecule3d/ui/viewer.js.map +1 -1
  81. package/lib/molecule3d/ui/viewerSpec.d.ts +17 -0
  82. package/lib/molecule3d/ui/viewerSpec.d.ts.map +1 -0
  83. package/lib/molecule3d/ui/viewerSpec.js +34 -0
  84. package/lib/molecule3d/ui/viewerSpec.js.map +1 -0
  85. package/lib/structure/core/editorGuide.d.ts +69 -0
  86. package/lib/structure/core/editorGuide.d.ts.map +1 -0
  87. package/lib/structure/core/editorGuide.js +162 -0
  88. package/lib/structure/core/editorGuide.js.map +1 -0
  89. package/lib/structure/core/editorToolbar.d.ts +45 -0
  90. package/lib/structure/core/editorToolbar.d.ts.map +1 -0
  91. package/lib/structure/core/editorToolbar.js +132 -0
  92. package/lib/structure/core/editorToolbar.js.map +1 -0
  93. package/lib/structure/core/editorToolbarGeometry.d.ts +36 -0
  94. package/lib/structure/core/editorToolbarGeometry.d.ts.map +1 -0
  95. package/lib/structure/core/editorToolbarGeometry.js +61 -0
  96. package/lib/structure/core/editorToolbarGeometry.js.map +1 -0
  97. package/lib/structure/core/index.d.ts +6 -0
  98. package/lib/structure/core/index.d.ts.map +1 -1
  99. package/lib/structure/core/index.js +3 -0
  100. package/lib/structure/core/index.js.map +1 -1
  101. package/lib/structure/core/pendingCall.d.ts +21 -0
  102. package/lib/structure/core/pendingCall.d.ts.map +1 -0
  103. package/lib/structure/core/pendingCall.js +37 -0
  104. package/lib/structure/core/pendingCall.js.map +1 -0
  105. package/lib/structure/ui/EditorCanvas.d.ts +1 -16
  106. package/lib/structure/ui/EditorCanvas.d.ts.map +1 -1
  107. package/lib/structure/ui/EditorCanvas.js +5 -18
  108. package/lib/structure/ui/EditorCanvas.js.map +1 -1
  109. package/lib/structure/ui/EditorHelpButton.d.ts +22 -0
  110. package/lib/structure/ui/EditorHelpButton.d.ts.map +1 -0
  111. package/lib/structure/ui/EditorHelpButton.js +38 -0
  112. package/lib/structure/ui/EditorHelpButton.js.map +1 -0
  113. package/lib/structure/ui/KeyCaps.d.ts +12 -0
  114. package/lib/structure/ui/KeyCaps.d.ts.map +1 -0
  115. package/lib/structure/ui/KeyCaps.js +29 -0
  116. package/lib/structure/ui/KeyCaps.js.map +1 -0
  117. package/lib/structure/ui/StructureEditor.d.ts +9 -2
  118. package/lib/structure/ui/StructureEditor.d.ts.map +1 -1
  119. package/lib/structure/ui/StructureEditor.js +17 -24
  120. package/lib/structure/ui/StructureEditor.js.map +1 -1
  121. package/lib/structure/ui/StructureEditorHelp.d.ts +18 -0
  122. package/lib/structure/ui/StructureEditorHelp.d.ts.map +1 -0
  123. package/lib/structure/ui/StructureEditorHelp.js +68 -0
  124. package/lib/structure/ui/StructureEditorHelp.js.map +1 -0
  125. package/lib/structure/ui/ToolbarTooltip.d.ts +20 -0
  126. package/lib/structure/ui/ToolbarTooltip.d.ts.map +1 -0
  127. package/lib/structure/ui/ToolbarTooltip.js +49 -0
  128. package/lib/structure/ui/ToolbarTooltip.js.map +1 -0
  129. package/lib/structure/ui/editorChange.d.ts +52 -0
  130. package/lib/structure/ui/editorChange.d.ts.map +1 -0
  131. package/lib/structure/ui/editorChange.js +34 -0
  132. package/lib/structure/ui/editorChange.js.map +1 -0
  133. package/lib/structure/ui/index.d.ts +3 -1
  134. package/lib/structure/ui/index.d.ts.map +1 -1
  135. package/lib/structure/ui/index.js +1 -0
  136. package/lib/structure/ui/index.js.map +1 -1
  137. package/lib/structure/ui/useToolbarFloor.d.ts.map +1 -1
  138. package/lib/structure/ui/useToolbarFloor.js +6 -45
  139. package/lib/structure/ui/useToolbarFloor.js.map +1 -1
  140. package/lib/structure/ui/useToolbarHover.d.ts +28 -0
  141. package/lib/structure/ui/useToolbarHover.d.ts.map +1 -0
  142. package/lib/structure/ui/useToolbarHover.js +96 -0
  143. package/lib/structure/ui/useToolbarHover.js.map +1 -0
  144. package/lib/structure/ui/watchToolbar.d.ts +17 -0
  145. package/lib/structure/ui/watchToolbar.d.ts.map +1 -0
  146. package/lib/structure/ui/watchToolbar.js +56 -0
  147. package/lib/structure/ui/watchToolbar.js.map +1 -0
  148. package/package.json +1 -1
  149. package/src/about/core/about.ts +11 -0
  150. package/src/about/core/index.ts +1 -0
  151. package/src/about/core/repository.ts +22 -0
  152. package/src/about/ui/AboutBuild.tsx +13 -20
  153. package/src/about/ui/AboutPage.tsx +30 -22
  154. package/src/about/ui/AboutVersion.tsx +67 -0
  155. package/src/about/ui/index.ts +2 -0
  156. package/src/build/core/buildInfo.ts +24 -0
  157. package/src/build/core/index.ts +6 -1
  158. package/src/build/vite/buildInfo.ts +2 -2
  159. package/src/ecosystem/core/sites.ts +12 -0
  160. package/src/ecosystem/core/types.ts +8 -0
  161. package/src/molecule3d/core/camera.ts +212 -0
  162. package/src/molecule3d/core/index.ts +14 -0
  163. package/src/molecule3d/ui/MoleculeCanvas3D.tsx +14 -43
  164. package/src/molecule3d/ui/camera.ts +128 -7
  165. package/src/molecule3d/ui/captureScene.ts +31 -0
  166. package/src/molecule3d/ui/createViewer.ts +24 -0
  167. package/src/molecule3d/ui/moleculeViewer3DProps.ts +21 -0
  168. package/src/molecule3d/ui/useMoleculeScene.ts +141 -0
  169. package/src/molecule3d/ui/useSharedCamera.ts +121 -0
  170. package/src/molecule3d/ui/viewer.ts +45 -50
  171. package/src/molecule3d/ui/viewerSpec.ts +38 -0
  172. package/src/structure/core/editorGuide.ts +221 -0
  173. package/src/structure/core/editorToolbar.ts +175 -0
  174. package/src/structure/core/editorToolbarGeometry.ts +80 -0
  175. package/src/structure/core/index.ts +29 -0
  176. package/src/structure/core/pendingCall.ts +59 -0
  177. package/src/structure/ui/EditorCanvas.tsx +9 -35
  178. package/src/structure/ui/EditorHelpButton.tsx +70 -0
  179. package/src/structure/ui/KeyCaps.tsx +40 -0
  180. package/src/structure/ui/StructureEditor.tsx +39 -21
  181. package/src/structure/ui/StructureEditorHelp.tsx +136 -0
  182. package/src/structure/ui/ToolbarTooltip.tsx +95 -0
  183. package/src/structure/ui/editorChange.ts +81 -0
  184. package/src/structure/ui/index.ts +5 -1
  185. package/src/structure/ui/useToolbarFloor.ts +7 -46
  186. package/src/structure/ui/useToolbarHover.ts +134 -0
  187. package/src/structure/ui/watchToolbar.ts +59 -0
@@ -0,0 +1,136 @@
1
+ /**
2
+ * What the keyboard and the mouse do in the structure editor, shown in the
3
+ * editor's help popover, and usable by a site on a page of its own.
4
+ */
5
+
6
+ import { Classes } from '@blueprintjs/core';
7
+ import type { CSSProperties, ReactElement } from 'react';
8
+
9
+ import { TOKEN } from '../../tokens/core/familyTokens.ts';
10
+ import type { EditorGesture, EditorGuideOptions } from '../core/editorGuide.ts';
11
+ import {
12
+ STRUCTURE_EDITOR_DOCS,
13
+ editorGuideSections,
14
+ } from '../core/editorGuide.ts';
15
+
16
+ import { KeyCaps } from './KeyCaps.tsx';
17
+
18
+ /** What {@link StructureEditorHelp} is written for. */
19
+ export type StructureEditorHelpProps = EditorGuideOptions;
20
+
21
+ /**
22
+ * The guide to the structure editor: the keys that change the atom or bond
23
+ * under the pointer, the selection gestures, the stereo conventions, and links
24
+ * to the full documentation. Gestures that do nothing in the given editor are
25
+ * left out.
26
+ * @param props - See {@link StructureEditorHelpProps}.
27
+ * @returns The guide.
28
+ */
29
+ export function StructureEditorHelp(
30
+ props: StructureEditorHelpProps,
31
+ ): ReactElement {
32
+ const sections = editorGuideSections(props);
33
+
34
+ return (
35
+ <div style={PANEL_STYLE} data-testid="structure-editor-help">
36
+ <p className={Classes.TEXT_MUTED} style={PARAGRAPH_STYLE}>
37
+ Hover a toolbar button to see what it does and its key.
38
+ </p>
39
+ {/* One grid for every section, so all the actions start on one edge. */}
40
+ <div style={GRID_STYLE}>
41
+ {sections.map((section) => (
42
+ <section key={section.title} style={ROW_STYLE}>
43
+ <h4 style={HEADING_STYLE}>{section.title}</h4>
44
+ {section.gestures.map((gesture) => (
45
+ <div key={gestureKey(gesture)} style={ROW_STYLE}>
46
+ <GestureInput input={gesture.input} />
47
+ <span>{gesture.action}</span>
48
+ </div>
49
+ ))}
50
+ {section.note === undefined ? null : (
51
+ <p className={Classes.TEXT_MUTED} style={NOTE_STYLE}>
52
+ {section.note}
53
+ </p>
54
+ )}
55
+ </section>
56
+ ))}
57
+ </div>
58
+ <p style={PARAGRAPH_STYLE}>
59
+ Learn more:{' '}
60
+ {STRUCTURE_EDITOR_DOCS.map((link, index) => (
61
+ <span key={link.url}>
62
+ {index === 0 ? null : ' · '}
63
+ <a href={link.url} target="_blank" rel="noopener noreferrer">
64
+ {link.title}
65
+ </a>
66
+ </span>
67
+ ))}
68
+ </p>
69
+ </div>
70
+ );
71
+ }
72
+
73
+ function GestureInput(props: { input: EditorGesture['input'] }): ReactElement {
74
+ return (
75
+ <span style={INPUT_STYLE}>
76
+ {props.input.map((part) =>
77
+ typeof part === 'string' ? (
78
+ <span key={part}>{part}</span>
79
+ ) : (
80
+ <KeyCaps key={part.key} keys={[part.key]} />
81
+ ),
82
+ )}
83
+ </span>
84
+ );
85
+ }
86
+
87
+ function gestureKey(gesture: EditorGesture): string {
88
+ const input = gesture.input
89
+ .map((part) => (typeof part === 'string' ? part : part.key))
90
+ .join(' ');
91
+ return `${input} ${gesture.action}`;
92
+ }
93
+
94
+ const PANEL_STYLE: CSSProperties = {
95
+ display: 'flex',
96
+ flexDirection: 'column',
97
+ gap: 10,
98
+ width: 400,
99
+ maxHeight: 'min(70vh, 36rem)',
100
+ overflowY: 'auto',
101
+ padding: 12,
102
+ fontSize: 12,
103
+ };
104
+
105
+ const GRID_STYLE: CSSProperties = {
106
+ display: 'grid',
107
+ gridTemplateColumns: 'max-content minmax(0, 1fr)',
108
+ alignItems: 'center',
109
+ columnGap: 12,
110
+ rowGap: 6,
111
+ };
112
+
113
+ const ROW_STYLE: CSSProperties = { display: 'contents' };
114
+
115
+ const HEADING_STYLE: CSSProperties = {
116
+ gridColumn: '1 / -1',
117
+ margin: '8px 0 0',
118
+ paddingBottom: 3,
119
+ borderBottom: `1px solid ${TOKEN.border}`,
120
+ color: TOKEN.textMuted,
121
+ fontSize: 11,
122
+ fontWeight: 700,
123
+ letterSpacing: '0.04em',
124
+ textTransform: 'uppercase',
125
+ };
126
+
127
+ const INPUT_STYLE: CSSProperties = {
128
+ display: 'flex',
129
+ alignItems: 'center',
130
+ gap: 4,
131
+ whiteSpace: 'nowrap',
132
+ };
133
+
134
+ const PARAGRAPH_STYLE: CSSProperties = { margin: 0, lineHeight: 1.45 };
135
+
136
+ const NOTE_STYLE: CSSProperties = { ...PARAGRAPH_STYLE, gridColumn: '1 / -1' };
@@ -0,0 +1,95 @@
1
+ import { Classes, Tooltip } from '@blueprintjs/core';
2
+ import type { CSSProperties, ReactElement, RefObject } from 'react';
3
+
4
+ import type { EditorToolbarButton } from '../core/editorToolbar.ts';
5
+ import {
6
+ EDITOR_TOOLBAR_BUTTONS,
7
+ isEditorToolbarButtonAvailable,
8
+ } from '../core/editorToolbar.ts';
9
+
10
+ import { KeyCaps } from './KeyCaps.tsx';
11
+ import type { StructureEditorMode } from './editorChange.ts';
12
+ import { useToolbarHover } from './useToolbarHover.ts';
13
+
14
+ /** What {@link ToolbarTooltip} needs. */
15
+ export interface ToolbarTooltipProps {
16
+ /** The positioned element wrapping the editor. */
17
+ containerRef: RefObject<HTMLElement | null>;
18
+ /** Which editor the toolbar belongs to, since some buttons are greyed. */
19
+ mode: StructureEditorMode;
20
+ }
21
+
22
+ /**
23
+ * The name, key and purpose of the toolbar button under the pointer.
24
+ *
25
+ * The toolbar is a canvas, so the tooltip is anchored to an invisible box laid
26
+ * over the button rather than to the button itself; the box lets the pointer
27
+ * through, so the editor still sees every hover and click.
28
+ * @param props - See {@link ToolbarTooltipProps}.
29
+ * @returns The tooltip, or nothing before a button has been hovered.
30
+ */
31
+ export function ToolbarTooltip(
32
+ props: ToolbarTooltipProps,
33
+ ): ReactElement | null {
34
+ const { containerRef, mode } = props;
35
+ const hover = useToolbarHover(containerRef);
36
+ if (hover === null) return null;
37
+ const button = EDITOR_TOOLBAR_BUTTONS[hover.button];
38
+ if (button === undefined) return null;
39
+
40
+ return (
41
+ <Tooltip
42
+ isOpen={hover.open}
43
+ placement="right"
44
+ content={<ButtonTip button={button} mode={mode} />}
45
+ renderTarget={({ ref }) => (
46
+ <span ref={ref} aria-hidden style={{ ...TARGET_STYLE, ...hover.box }} />
47
+ )}
48
+ />
49
+ );
50
+ }
51
+
52
+ function ButtonTip(props: {
53
+ button: EditorToolbarButton;
54
+ mode: StructureEditorMode;
55
+ }): ReactElement {
56
+ const { button, mode } = props;
57
+ const available = isEditorToolbarButtonAvailable(button, mode);
58
+
59
+ return (
60
+ <div style={TIP_STYLE} data-testid="structure-editor-tooltip">
61
+ <div style={HEAD_STYLE}>
62
+ <strong>{button.name}</strong>
63
+ {button.keys === undefined ? null : <KeyCaps keys={button.keys} />}
64
+ </div>
65
+ <div>{button.description}</div>
66
+ {available ? null : (
67
+ <div className={Classes.TEXT_MUTED}>
68
+ {button.availability === 'reaction'
69
+ ? 'Only when drawing a reaction.'
70
+ : 'Not available in this editor.'}
71
+ </div>
72
+ )}
73
+ </div>
74
+ );
75
+ }
76
+
77
+ const TARGET_STYLE: CSSProperties = {
78
+ position: 'absolute',
79
+ pointerEvents: 'none',
80
+ };
81
+
82
+ const TIP_STYLE: CSSProperties = {
83
+ display: 'flex',
84
+ flexDirection: 'column',
85
+ gap: 4,
86
+ maxWidth: 260,
87
+ lineHeight: 1.4,
88
+ };
89
+
90
+ const HEAD_STYLE: CSSProperties = {
91
+ display: 'flex',
92
+ alignItems: 'center',
93
+ justifyContent: 'space-between',
94
+ gap: 12,
95
+ };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * What the structure editor reports after an edit.
3
+ *
4
+ * Only types are imported from react-ocl and openchemlib, so this module can be
5
+ * reached from the `react-cheminfo/structure` barrel without pulling either in.
6
+ */
7
+
8
+ import type { Molecule } from 'openchemlib';
9
+ import type {
10
+ CanvasEditorOnChangeMolecule,
11
+ CanvasEditorOnChangeReaction,
12
+ } from 'react-ocl';
13
+
14
+ /** What the editor draws: one structure, or a reaction with its arrow. */
15
+ export type StructureEditorMode = 'molecule' | 'reaction';
16
+
17
+ /** The notations every change carries, whichever the editor. */
18
+ interface StructureEditorNotations {
19
+ /**
20
+ * The idCode, with the coordinates the editor laid out written after a
21
+ * space. `splitIdCode` takes the two apart.
22
+ */
23
+ idCode: string;
24
+ /** The V2000 molfile of a molecule, or the RXN file of a reaction. */
25
+ molfile: string;
26
+ /** The SMILES of a molecule, or the reaction SMILES. */
27
+ smiles: string;
28
+ }
29
+
30
+ /** What a molecule editor holds at the moment it changed. */
31
+ export interface StructureEditorMoleculeChange extends StructureEditorNotations {
32
+ mode: 'molecule';
33
+ /**
34
+ * A copy of the drawn molecule, with its coordinates. It is taken when the
35
+ * edit happens, so it still describes that edit when it arrives after the
36
+ * debounce, and the caller may modify it without touching the canvas.
37
+ */
38
+ molecule: Molecule;
39
+ }
40
+
41
+ /** What a reaction editor holds at the moment it changed. */
42
+ export interface StructureEditorReactionChange extends StructureEditorNotations {
43
+ mode: 'reaction';
44
+ }
45
+
46
+ /** Everything the editor holds at the moment it changed. */
47
+ export type StructureEditorChange =
48
+ StructureEditorMoleculeChange | StructureEditorReactionChange;
49
+
50
+ /**
51
+ * Read a molecule editor's event out, while it still describes the edit.
52
+ * @param event - The event react-ocl passes to its callback.
53
+ * @returns The change, independent of the editor from then on.
54
+ */
55
+ export function moleculeChange(
56
+ event: CanvasEditorOnChangeMolecule,
57
+ ): StructureEditorMoleculeChange {
58
+ return {
59
+ mode: 'molecule',
60
+ idCode: event.getIdcode(),
61
+ molfile: event.getMolfile(),
62
+ smiles: event.getSmiles(),
63
+ molecule: event.getMolecule(),
64
+ };
65
+ }
66
+
67
+ /**
68
+ * Read a reaction editor's event out, while it still describes the edit.
69
+ * @param event - The event react-ocl passes to its callback.
70
+ * @returns The change, independent of the editor from then on.
71
+ */
72
+ export function reactionChange(
73
+ event: CanvasEditorOnChangeReaction,
74
+ ): StructureEditorReactionChange {
75
+ return {
76
+ mode: 'reaction',
77
+ idCode: event.getIdcode(),
78
+ molfile: event.getRxn(),
79
+ smiles: event.getSmiles(),
80
+ };
81
+ }
@@ -13,10 +13,14 @@
13
13
  export type {
14
14
  StructureEditorChange,
15
15
  StructureEditorMode,
16
- } from './EditorCanvas.tsx';
16
+ StructureEditorMoleculeChange,
17
+ StructureEditorReactionChange,
18
+ } from './editorChange.ts';
17
19
  export type { StructureLabels, StructureProps } from './Structure.tsx';
18
20
  export { Structure } from './Structure.tsx';
19
21
  export type { StructureEditorProps } from './StructureEditor.tsx';
20
22
  export { StructureEditor } from './StructureEditor.tsx';
23
+ export type { StructureEditorHelpProps } from './StructureEditorHelp.tsx';
24
+ export { StructureEditorHelp } from './StructureEditorHelp.tsx';
21
25
  export type { ToolbarFloorOptions } from './useToolbarFloor.ts';
22
26
  export { useToolbarFloor } from './useToolbarFloor.ts';
@@ -3,6 +3,8 @@ import { useLayoutEffect, useRef } from 'react';
3
3
 
4
4
  import { toolbarFloorHeight } from '../core/toolbarFloor.ts';
5
5
 
6
+ import { watchToolbar } from './watchToolbar.ts';
7
+
6
8
  /** What {@link useToolbarFloor} needs. */
7
9
  export interface ToolbarFloorOptions {
8
10
  /**
@@ -38,62 +40,21 @@ export function useToolbarFloor(
38
40
  const container = containerRef.current;
39
41
  if (container === null) return;
40
42
 
41
- let toolbar: HTMLCanvasElement | null = null;
42
- let size: ResizeObserver | null = null;
43
-
44
- const attach = (): void => {
45
- const found = findToolbar(container);
46
- if (found === null) return;
47
- size?.disconnect();
48
- toolbar = found;
49
- size = new ResizeObserver(() => {
43
+ return watchToolbar(container, (toolbar) => {
44
+ const size = new ResizeObserver(() => {
50
45
  // offsetHeight, because a dialog opens under a scaling transform and a
51
46
  // measured rectangle would be that of the half drawn toolbar.
52
- const height = found.offsetHeight;
47
+ const height = toolbar.offsetHeight;
53
48
  // Zero is a toolbar being torn down or not yet laid out, never a
54
49
  // toolbar that needs no room: writing the fallback for it would undo a
55
50
  // correct floor and leave the palette cut off.
56
51
  if (height === 0) return;
57
52
  container.style.minHeight = `${toolbarFloorHeight(height, minHeight)}px`;
58
53
  });
59
- size.observe(found);
60
- };
61
-
62
- // The editor is imported lazily and builds itself asynchronously, so it is
63
- // almost never there on the first look, and how long it takes is a cold
64
- // module graph and a network away from anything this code can predict. So
65
- // the arrival is watched for rather than waited out: a deadline that
66
- // expires leaves the toolbar cut off for the life of the page, with
67
- // nothing on screen to say why.
68
- const arrivals = new MutationObserver(() => {
69
- if (toolbar?.isConnected === true) return;
70
- attach();
54
+ size.observe(toolbar);
55
+ return () => size.disconnect();
71
56
  });
72
- arrivals.observe(container, { childList: true, subtree: true });
73
- attach();
74
-
75
- return () => {
76
- arrivals.disconnect();
77
- size?.disconnect();
78
- };
79
57
  }, [minHeight, revision]);
80
58
 
81
59
  return containerRef;
82
60
  }
83
-
84
- /**
85
- * Find the toolbar canvas of the editor.
86
- *
87
- * The editor builds itself inside a shadow root and puts the toolbar there as
88
- * a direct child, the drawing canvas being nested deeper.
89
- * @param container - The element wrapping the editor.
90
- * @returns The toolbar canvas, or null while the editor has not drawn one.
91
- */
92
- function findToolbar(container: HTMLElement): HTMLCanvasElement | null {
93
- for (const element of container.querySelectorAll('*')) {
94
- for (const child of element.shadowRoot?.children ?? []) {
95
- if (child instanceof HTMLCanvasElement) return child;
96
- }
97
- }
98
- return null;
99
- }
@@ -0,0 +1,134 @@
1
+ import type { RefObject } from 'react';
2
+ import { useEffect, useState } from 'react';
3
+
4
+ import type { EditorToolbarButtonBox } from '../core/editorToolbarGeometry.ts';
5
+ import {
6
+ editorToolbarButtonAt,
7
+ editorToolbarButtonBox,
8
+ } from '../core/editorToolbarGeometry.ts';
9
+
10
+ import { watchToolbar } from './watchToolbar.ts';
11
+
12
+ /** The toolbar button the pointer rests on, or last rested on. */
13
+ export interface ToolbarHover {
14
+ /** The button's index in `EDITOR_TOOLBAR_BUTTONS`. */
15
+ button: number;
16
+ /** Where the button is drawn, relative to the container's padding box. */
17
+ box: EditorToolbarButtonBox;
18
+ /**
19
+ * Whether its tooltip is showing. The last button is kept after the pointer
20
+ * leaves, so a closing tooltip fades out with its text rather than empty.
21
+ */
22
+ open: boolean;
23
+ }
24
+
25
+ /**
26
+ * Follow the pointer over the editor's toolbar.
27
+ *
28
+ * The first button waits for the pointer to rest, so crossing the toolbar on
29
+ * the way to the drawing opens nothing; once one is showing, the next follows
30
+ * at once. Pressing a button hides its tooltip until the pointer moves on,
31
+ * since whoever pressed it no longer needs telling. Touch is ignored: a finger
32
+ * has no hover, and a tooltip opened by a tap would cover the canvas.
33
+ * @param containerRef - The element wrapping the editor, positioned so the
34
+ * returned box can be laid over the button.
35
+ * @returns The button under the pointer, or null before any has been hovered.
36
+ */
37
+ export function useToolbarHover(
38
+ containerRef: RefObject<HTMLElement | null>,
39
+ ): ToolbarHover | null {
40
+ const [hover, setHover] = useState<ToolbarHover | null>(null);
41
+
42
+ useEffect(() => {
43
+ const container = containerRef.current;
44
+ if (container === null) return;
45
+
46
+ let timer: ReturnType<typeof setTimeout> | undefined;
47
+ let current = -1;
48
+ let pressed = -1;
49
+ let open = false;
50
+
51
+ const show = (toolbar: HTMLCanvasElement, button: number): void => {
52
+ open = true;
53
+ setHover({ button, box: boxIn(container, toolbar, button), open });
54
+ };
55
+ const hide = (): void => {
56
+ clearTimeout(timer);
57
+ if (!open) return;
58
+ open = false;
59
+ setHover((previous) =>
60
+ previous === null ? null : { ...previous, open: false },
61
+ );
62
+ };
63
+
64
+ const onLeave = (): void => {
65
+ current = -1;
66
+ pressed = -1;
67
+ hide();
68
+ };
69
+ const onDown = (): void => {
70
+ pressed = current;
71
+ hide();
72
+ };
73
+
74
+ const stop = watchToolbar(container, (toolbar) => {
75
+ const onMove = (event: PointerEvent): void => {
76
+ if (event.pointerType === 'touch') return;
77
+ const button = editorToolbarButtonAt(
78
+ event.offsetX,
79
+ event.offsetY,
80
+ toolbar.offsetHeight,
81
+ );
82
+ if (button === current) return;
83
+ current = button;
84
+ if (button !== pressed) pressed = -1;
85
+ if (button === -1 || button === pressed) {
86
+ hide();
87
+ return;
88
+ }
89
+ clearTimeout(timer);
90
+ if (open) {
91
+ show(toolbar, button);
92
+ return;
93
+ }
94
+ timer = setTimeout(() => show(toolbar, button), OPEN_DELAY);
95
+ };
96
+
97
+ toolbar.addEventListener('pointermove', onMove);
98
+ toolbar.addEventListener('pointerleave', onLeave);
99
+ toolbar.addEventListener('pointerdown', onDown);
100
+ return () => {
101
+ toolbar.removeEventListener('pointermove', onMove);
102
+ toolbar.removeEventListener('pointerleave', onLeave);
103
+ toolbar.removeEventListener('pointerdown', onDown);
104
+ onLeave();
105
+ };
106
+ });
107
+
108
+ return () => {
109
+ stop();
110
+ clearTimeout(timer);
111
+ };
112
+ }, [containerRef]);
113
+
114
+ return hover;
115
+ }
116
+
117
+ // Long enough that sweeping the pointer across the toolbar opens nothing.
118
+ const OPEN_DELAY = 250;
119
+
120
+ function boxIn(
121
+ container: HTMLElement,
122
+ toolbar: HTMLCanvasElement,
123
+ button: number,
124
+ ): EditorToolbarButtonBox {
125
+ const box = editorToolbarButtonBox(button, toolbar.offsetHeight);
126
+ const toolbarRect = toolbar.getBoundingClientRect();
127
+ const containerRect = container.getBoundingClientRect();
128
+ return {
129
+ ...box,
130
+ left:
131
+ toolbarRect.left - containerRect.left - container.clientLeft + box.left,
132
+ top: toolbarRect.top - containerRect.top - container.clientTop + box.top,
133
+ };
134
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Run something against the editor's toolbar canvas as soon as it exists, and
3
+ * again against each new one the editor builds.
4
+ *
5
+ * The editor is imported lazily and builds itself asynchronously, so the
6
+ * toolbar is almost never there on the first look, and how long it takes is a
7
+ * cold module graph and a network away from anything this code can predict.
8
+ * So its arrival is watched for rather than waited out: a deadline that
9
+ * expires leaves whatever depends on the toolbar missing for the life of the
10
+ * page, with nothing on screen to say why.
11
+ * @param container - The element wrapping the editor.
12
+ * @param onToolbar - Called with each toolbar found; what it returns is called
13
+ * when that toolbar is replaced or the watch ends.
14
+ * @returns A function ending the watch.
15
+ */
16
+ export function watchToolbar(
17
+ container: HTMLElement,
18
+ onToolbar: (toolbar: HTMLCanvasElement) => () => void,
19
+ ): () => void {
20
+ let toolbar: HTMLCanvasElement | null = null;
21
+ let release: (() => void) | null = null;
22
+
23
+ const attach = (): void => {
24
+ const found = findToolbar(container);
25
+ if (found === null) return;
26
+ release?.();
27
+ toolbar = found;
28
+ release = onToolbar(found);
29
+ };
30
+
31
+ const arrivals = new MutationObserver(() => {
32
+ if (toolbar?.isConnected === true) return;
33
+ attach();
34
+ });
35
+ arrivals.observe(container, { childList: true, subtree: true });
36
+ attach();
37
+
38
+ return () => {
39
+ arrivals.disconnect();
40
+ release?.();
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Find the toolbar canvas of the editor.
46
+ *
47
+ * The editor builds itself inside a shadow root and puts the toolbar there as
48
+ * a direct child, the drawing canvas being nested deeper.
49
+ * @param container - The element wrapping the editor.
50
+ * @returns The toolbar canvas, or null while the editor has not drawn one.
51
+ */
52
+ function findToolbar(container: HTMLElement): HTMLCanvasElement | null {
53
+ for (const element of container.querySelectorAll('*')) {
54
+ for (const child of element.shadowRoot?.children ?? []) {
55
+ if (child instanceof HTMLCanvasElement) return child;
56
+ }
57
+ }
58
+ return null;
59
+ }