react-cheminfo 0.135.0 → 0.137.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 (311) hide show
  1. package/README.md +1 -1
  2. package/lib/crystal/core/cellAtoms.d.ts +43 -0
  3. package/lib/crystal/core/cellAtoms.d.ts.map +1 -0
  4. package/lib/crystal/core/cellAtoms.js +70 -0
  5. package/lib/crystal/core/cellAtoms.js.map +1 -0
  6. package/lib/crystal/core/index.d.ts +2 -0
  7. package/lib/crystal/core/index.d.ts.map +1 -1
  8. package/lib/crystal/core/index.js +1 -0
  9. package/lib/crystal/core/index.js.map +1 -1
  10. package/lib/crystal/core/molecules.d.ts.map +1 -1
  11. package/lib/crystal/core/molecules.js +6 -40
  12. package/lib/crystal/core/molecules.js.map +1 -1
  13. package/lib/crystal3d/core/cellClip.d.ts +46 -0
  14. package/lib/crystal3d/core/cellClip.d.ts.map +1 -0
  15. package/lib/crystal3d/core/cellClip.js +198 -0
  16. package/lib/crystal3d/core/cellClip.js.map +1 -0
  17. package/lib/crystal3d/core/cellGeometry.d.ts +63 -0
  18. package/lib/crystal3d/core/cellGeometry.d.ts.map +1 -0
  19. package/lib/crystal3d/core/cellGeometry.js +153 -0
  20. package/lib/crystal3d/core/cellGeometry.js.map +1 -0
  21. package/lib/crystal3d/core/cellStack.d.ts +51 -0
  22. package/lib/crystal3d/core/cellStack.d.ts.map +1 -0
  23. package/lib/crystal3d/core/cellStack.js +79 -0
  24. package/lib/crystal3d/core/cellStack.js.map +1 -0
  25. package/lib/crystal3d/core/crystalView.d.ts +81 -0
  26. package/lib/crystal3d/core/crystalView.d.ts.map +1 -0
  27. package/lib/crystal3d/core/crystalView.js +55 -0
  28. package/lib/crystal3d/core/crystalView.js.map +1 -0
  29. package/lib/crystal3d/core/drawingSet.d.ts +31 -0
  30. package/lib/crystal3d/core/drawingSet.d.ts.map +1 -0
  31. package/lib/crystal3d/core/drawingSet.js +63 -0
  32. package/lib/crystal3d/core/drawingSet.js.map +1 -0
  33. package/lib/crystal3d/core/elementDrawing.d.ts +50 -0
  34. package/lib/crystal3d/core/elementDrawing.d.ts.map +1 -0
  35. package/lib/crystal3d/core/elementDrawing.js +100 -0
  36. package/lib/crystal3d/core/elementDrawing.js.map +1 -0
  37. package/lib/crystal3d/core/elementExtent.d.ts +42 -0
  38. package/lib/crystal3d/core/elementExtent.d.ts.map +1 -0
  39. package/lib/crystal3d/core/elementExtent.js +56 -0
  40. package/lib/crystal3d/core/elementExtent.js.map +1 -0
  41. package/lib/crystal3d/core/elementLabels.d.ts +31 -0
  42. package/lib/crystal3d/core/elementLabels.d.ts.map +1 -0
  43. package/lib/crystal3d/core/elementLabels.js +99 -0
  44. package/lib/crystal3d/core/elementLabels.js.map +1 -0
  45. package/lib/crystal3d/core/elementShapes.d.ts +43 -0
  46. package/lib/crystal3d/core/elementShapes.d.ts.map +1 -0
  47. package/lib/crystal3d/core/elementShapes.js +115 -0
  48. package/lib/crystal3d/core/elementShapes.js.map +1 -0
  49. package/lib/crystal3d/core/frame.d.ts +36 -0
  50. package/lib/crystal3d/core/frame.d.ts.map +1 -0
  51. package/lib/crystal3d/core/frame.js +35 -0
  52. package/lib/crystal3d/core/frame.js.map +1 -0
  53. package/lib/crystal3d/core/framing.d.ts +41 -0
  54. package/lib/crystal3d/core/framing.d.ts.map +1 -0
  55. package/lib/crystal3d/core/framing.js +79 -0
  56. package/lib/crystal3d/core/framing.js.map +1 -0
  57. package/lib/crystal3d/core/index.d.ts +42 -0
  58. package/lib/crystal3d/core/index.d.ts.map +1 -0
  59. package/lib/crystal3d/core/index.js +29 -0
  60. package/lib/crystal3d/core/index.js.map +1 -0
  61. package/lib/crystal3d/core/labelText.d.ts +19 -0
  62. package/lib/crystal3d/core/labelText.d.ts.map +1 -0
  63. package/lib/crystal3d/core/labelText.js +42 -0
  64. package/lib/crystal3d/core/labelText.js.map +1 -0
  65. package/lib/crystal3d/core/latticeBasis.d.ts +49 -0
  66. package/lib/crystal3d/core/latticeBasis.d.ts.map +1 -0
  67. package/lib/crystal3d/core/latticeBasis.js +89 -0
  68. package/lib/crystal3d/core/latticeBasis.js.map +1 -0
  69. package/lib/crystal3d/core/mat3.d.ts +68 -0
  70. package/lib/crystal3d/core/mat3.d.ts.map +1 -0
  71. package/lib/crystal3d/core/mat3.js +146 -0
  72. package/lib/crystal3d/core/mat3.js.map +1 -0
  73. package/lib/crystal3d/core/operationMatrix.d.ts +44 -0
  74. package/lib/crystal3d/core/operationMatrix.d.ts.map +1 -0
  75. package/lib/crystal3d/core/operationMatrix.js +172 -0
  76. package/lib/crystal3d/core/operationMatrix.js.map +1 -0
  77. package/lib/crystal3d/core/orientation.d.ts +62 -0
  78. package/lib/crystal3d/core/orientation.d.ts.map +1 -0
  79. package/lib/crystal3d/core/orientation.js +166 -0
  80. package/lib/crystal3d/core/orientation.js.map +1 -0
  81. package/lib/crystal3d/core/palette.d.ts +24 -0
  82. package/lib/crystal3d/core/palette.d.ts.map +1 -0
  83. package/lib/crystal3d/core/palette.js +34 -0
  84. package/lib/crystal3d/core/palette.js.map +1 -0
  85. package/lib/crystal3d/core/primitives.d.ts +133 -0
  86. package/lib/crystal3d/core/primitives.d.ts.map +1 -0
  87. package/lib/crystal3d/core/primitives.js +80 -0
  88. package/lib/crystal3d/core/primitives.js.map +1 -0
  89. package/lib/crystal3d/core/structureText.d.ts +20 -0
  90. package/lib/crystal3d/core/structureText.d.ts.map +1 -0
  91. package/lib/crystal3d/core/structureText.js +37 -0
  92. package/lib/crystal3d/core/structureText.js.map +1 -0
  93. package/lib/crystal3d/core/supercell.d.ts +20 -0
  94. package/lib/crystal3d/core/supercell.d.ts.map +1 -0
  95. package/lib/crystal3d/core/supercell.js +35 -0
  96. package/lib/crystal3d/core/supercell.js.map +1 -0
  97. package/lib/crystal3d/core/types.d.ts +166 -0
  98. package/lib/crystal3d/core/types.d.ts.map +1 -0
  99. package/lib/crystal3d/core/types.js +10 -0
  100. package/lib/crystal3d/core/types.js.map +1 -0
  101. package/lib/crystal3d/core/vec3.d.ts +75 -0
  102. package/lib/crystal3d/core/vec3.d.ts.map +1 -0
  103. package/lib/crystal3d/core/vec3.js +119 -0
  104. package/lib/crystal3d/core/vec3.js.map +1 -0
  105. package/lib/crystal3d/ui/CrystalCanvas3D.d.ts +52 -0
  106. package/lib/crystal3d/ui/CrystalCanvas3D.d.ts.map +1 -0
  107. package/lib/crystal3d/ui/CrystalCanvas3D.js +250 -0
  108. package/lib/crystal3d/ui/CrystalCanvas3D.js.map +1 -0
  109. package/lib/crystal3d/ui/CrystalStructureView.d.ts +17 -0
  110. package/lib/crystal3d/ui/CrystalStructureView.d.ts.map +1 -0
  111. package/lib/crystal3d/ui/CrystalStructureView.js +79 -0
  112. package/lib/crystal3d/ui/CrystalStructureView.js.map +1 -0
  113. package/lib/crystal3d/ui/CrystalStructureViewer.d.ts +22 -0
  114. package/lib/crystal3d/ui/CrystalStructureViewer.d.ts.map +1 -0
  115. package/lib/crystal3d/ui/CrystalStructureViewer.js +29 -0
  116. package/lib/crystal3d/ui/CrystalStructureViewer.js.map +1 -0
  117. package/lib/crystal3d/ui/CrystalViewControls.d.ts +29 -0
  118. package/lib/crystal3d/ui/CrystalViewControls.d.ts.map +1 -0
  119. package/lib/crystal3d/ui/CrystalViewControls.js +24 -0
  120. package/lib/crystal3d/ui/CrystalViewControls.js.map +1 -0
  121. package/lib/crystal3d/ui/CrystalViewer3D.d.ts +72 -0
  122. package/lib/crystal3d/ui/CrystalViewer3D.d.ts.map +1 -0
  123. package/lib/crystal3d/ui/CrystalViewer3D.js +70 -0
  124. package/lib/crystal3d/ui/CrystalViewer3D.js.map +1 -0
  125. package/lib/crystal3d/ui/crystalScene.d.ts +97 -0
  126. package/lib/crystal3d/ui/crystalScene.d.ts.map +1 -0
  127. package/lib/crystal3d/ui/crystalScene.js +10 -0
  128. package/lib/crystal3d/ui/crystalScene.js.map +1 -0
  129. package/lib/crystal3d/ui/crystalStructureViewerProps.d.ts +52 -0
  130. package/lib/crystal3d/ui/crystalStructureViewerProps.d.ts.map +1 -0
  131. package/lib/crystal3d/ui/crystalStructureViewerProps.js +2 -0
  132. package/lib/crystal3d/ui/crystalStructureViewerProps.js.map +1 -0
  133. package/lib/crystal3d/ui/index.d.ts +16 -0
  134. package/lib/crystal3d/ui/index.d.ts.map +1 -0
  135. package/lib/crystal3d/ui/index.js +12 -0
  136. package/lib/crystal3d/ui/index.js.map +1 -0
  137. package/lib/crystal3d/ui/useCrystalCaption.d.ts +14 -0
  138. package/lib/crystal3d/ui/useCrystalCaption.d.ts.map +1 -0
  139. package/lib/crystal3d/ui/useCrystalCaption.js +32 -0
  140. package/lib/crystal3d/ui/useCrystalCaption.js.map +1 -0
  141. package/lib/crystal3d/viewer/animate.d.ts +49 -0
  142. package/lib/crystal3d/viewer/animate.d.ts.map +1 -0
  143. package/lib/crystal3d/viewer/animate.js +83 -0
  144. package/lib/crystal3d/viewer/animate.js.map +1 -0
  145. package/lib/crystal3d/viewer/camera.d.ts +44 -0
  146. package/lib/crystal3d/viewer/camera.d.ts.map +1 -0
  147. package/lib/crystal3d/viewer/camera.js +103 -0
  148. package/lib/crystal3d/viewer/camera.js.map +1 -0
  149. package/lib/crystal3d/viewer/index.d.ts +13 -0
  150. package/lib/crystal3d/viewer/index.d.ts.map +1 -0
  151. package/lib/crystal3d/viewer/index.js +8 -0
  152. package/lib/crystal3d/viewer/index.js.map +1 -0
  153. package/lib/crystal3d/viewer/layerRegistry.d.ts +24 -0
  154. package/lib/crystal3d/viewer/layerRegistry.d.ts.map +1 -0
  155. package/lib/crystal3d/viewer/layerRegistry.js +39 -0
  156. package/lib/crystal3d/viewer/layerRegistry.js.map +1 -0
  157. package/lib/crystal3d/viewer/meshPrimitives.d.ts +13 -0
  158. package/lib/crystal3d/viewer/meshPrimitives.d.ts.map +1 -0
  159. package/lib/crystal3d/viewer/meshPrimitives.js +76 -0
  160. package/lib/crystal3d/viewer/meshPrimitives.js.map +1 -0
  161. package/lib/crystal3d/viewer/plugin.d.ts +133 -0
  162. package/lib/crystal3d/viewer/plugin.d.ts.map +1 -0
  163. package/lib/crystal3d/viewer/plugin.js +181 -0
  164. package/lib/crystal3d/viewer/plugin.js.map +1 -0
  165. package/lib/crystal3d/viewer/renderCell.d.ts +43 -0
  166. package/lib/crystal3d/viewer/renderCell.d.ts.map +1 -0
  167. package/lib/crystal3d/viewer/renderCell.js +53 -0
  168. package/lib/crystal3d/viewer/renderCell.js.map +1 -0
  169. package/lib/crystal3d/viewer/renderElements.d.ts +54 -0
  170. package/lib/crystal3d/viewer/renderElements.d.ts.map +1 -0
  171. package/lib/crystal3d/viewer/renderElements.js +77 -0
  172. package/lib/crystal3d/viewer/renderElements.js.map +1 -0
  173. package/lib/crystal3d/viewer/renderStructure.d.ts +80 -0
  174. package/lib/crystal3d/viewer/renderStructure.d.ts.map +1 -0
  175. package/lib/crystal3d/viewer/renderStructure.js +143 -0
  176. package/lib/crystal3d/viewer/renderStructure.js.map +1 -0
  177. package/lib/crystal3d/viewer/sceneParts.d.ts +32 -0
  178. package/lib/crystal3d/viewer/sceneParts.d.ts.map +1 -0
  179. package/lib/crystal3d/viewer/sceneParts.js +77 -0
  180. package/lib/crystal3d/viewer/sceneParts.js.map +1 -0
  181. package/lib/crystal3d/viewer/shapeLayer.d.ts +41 -0
  182. package/lib/crystal3d/viewer/shapeLayer.d.ts.map +1 -0
  183. package/lib/crystal3d/viewer/shapeLayer.js +57 -0
  184. package/lib/crystal3d/viewer/shapeLayer.js.map +1 -0
  185. package/lib/crystal3d/viewer/textLayer.d.ts +21 -0
  186. package/lib/crystal3d/viewer/textLayer.d.ts.map +1 -0
  187. package/lib/crystal3d/viewer/textLayer.js +59 -0
  188. package/lib/crystal3d/viewer/textLayer.js.map +1 -0
  189. package/lib/crystal3d/viewer/viewer.d.ts +119 -0
  190. package/lib/crystal3d/viewer/viewer.d.ts.map +1 -0
  191. package/lib/crystal3d/viewer/viewer.js +172 -0
  192. package/lib/crystal3d/viewer/viewer.js.map +1 -0
  193. package/lib/crystal3d.d.ts +12 -0
  194. package/lib/crystal3d.d.ts.map +1 -0
  195. package/lib/crystal3d.js +12 -0
  196. package/lib/crystal3d.js.map +1 -0
  197. package/lib/download/core/croppedMarkup.d.ts.map +1 -1
  198. package/lib/download/core/croppedMarkup.js +16 -40
  199. package/lib/download/core/croppedMarkup.js.map +1 -1
  200. package/lib/download/core/dataUrl.d.ts +27 -0
  201. package/lib/download/core/dataUrl.d.ts.map +1 -0
  202. package/lib/download/core/dataUrl.js +54 -0
  203. package/lib/download/core/dataUrl.js.map +1 -0
  204. package/lib/download/core/index.d.ts +3 -0
  205. package/lib/download/core/index.d.ts.map +1 -1
  206. package/lib/download/core/index.js +2 -0
  207. package/lib/download/core/index.js.map +1 -1
  208. package/lib/download/core/svgMarkup.d.ts +39 -0
  209. package/lib/download/core/svgMarkup.d.ts.map +1 -0
  210. package/lib/download/core/svgMarkup.js +100 -0
  211. package/lib/download/core/svgMarkup.js.map +1 -0
  212. package/lib/drawing/ui/movingFigure.d.ts.map +1 -1
  213. package/lib/drawing/ui/movingFigure.js +4 -30
  214. package/lib/drawing/ui/movingFigure.js.map +1 -1
  215. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  216. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  217. package/lib/locales/de.json +26 -1
  218. package/lib/locales/en.json +26 -1
  219. package/lib/locales/es.json +26 -1
  220. package/lib/locales/fr.json +26 -1
  221. package/lib/locales/it.json +26 -1
  222. package/lib/molecule3d/ui/MoleculeCanvas3D.d.ts.map +1 -1
  223. package/lib/molecule3d/ui/MoleculeCanvas3D.js +16 -27
  224. package/lib/molecule3d/ui/MoleculeCanvas3D.js.map +1 -1
  225. package/lib/molstar/bonds/bondRule.d.ts +16 -0
  226. package/lib/molstar/bonds/bondRule.d.ts.map +1 -0
  227. package/lib/molstar/bonds/bondRule.js +23 -0
  228. package/lib/molstar/bonds/bondRule.js.map +1 -0
  229. package/lib/molstar/bonds/index.d.ts +9 -0
  230. package/lib/molstar/bonds/index.d.ts.map +1 -0
  231. package/lib/molstar/bonds/index.js +9 -0
  232. package/lib/molstar/bonds/index.js.map +1 -0
  233. package/lib/molstar/ui/MolstarToolbar.d.ts +47 -0
  234. package/lib/molstar/ui/MolstarToolbar.d.ts.map +1 -0
  235. package/lib/molstar/ui/MolstarToolbar.js +28 -0
  236. package/lib/molstar/ui/MolstarToolbar.js.map +1 -0
  237. package/lib/molstar/ui/index.d.ts +10 -0
  238. package/lib/molstar/ui/index.d.ts.map +1 -0
  239. package/lib/molstar/ui/index.js +8 -0
  240. package/lib/molstar/ui/index.js.map +1 -0
  241. package/lib/molstar/ui/useMolstarTools.d.ts +63 -0
  242. package/lib/molstar/ui/useMolstarTools.d.ts.map +1 -0
  243. package/lib/molstar/ui/useMolstarTools.js +36 -0
  244. package/lib/molstar/ui/useMolstarTools.js.map +1 -0
  245. package/package.json +2 -1
  246. package/src/crystal/core/cellAtoms.ts +106 -0
  247. package/src/crystal/core/index.ts +2 -0
  248. package/src/crystal/core/molecules.ts +7 -44
  249. package/src/crystal3d/core/cellClip.ts +229 -0
  250. package/src/crystal3d/core/cellGeometry.ts +193 -0
  251. package/src/crystal3d/core/cellStack.ts +87 -0
  252. package/src/crystal3d/core/crystalView.ts +131 -0
  253. package/src/crystal3d/core/drawingSet.ts +71 -0
  254. package/src/crystal3d/core/elementDrawing.ts +147 -0
  255. package/src/crystal3d/core/elementExtent.ts +81 -0
  256. package/src/crystal3d/core/elementLabels.ts +130 -0
  257. package/src/crystal3d/core/elementShapes.ts +141 -0
  258. package/src/crystal3d/core/frame.ts +56 -0
  259. package/src/crystal3d/core/framing.ts +101 -0
  260. package/src/crystal3d/core/index.ts +134 -0
  261. package/src/crystal3d/core/labelText.ts +44 -0
  262. package/src/crystal3d/core/latticeBasis.ts +110 -0
  263. package/src/crystal3d/core/mat3.ts +163 -0
  264. package/src/crystal3d/core/operationMatrix.ts +255 -0
  265. package/src/crystal3d/core/orientation.ts +229 -0
  266. package/src/crystal3d/core/palette.ts +43 -0
  267. package/src/crystal3d/core/primitives.ts +183 -0
  268. package/src/crystal3d/core/structureText.ts +43 -0
  269. package/src/crystal3d/core/supercell.ts +40 -0
  270. package/src/crystal3d/core/types.ts +186 -0
  271. package/src/crystal3d/core/vec3.ts +127 -0
  272. package/src/crystal3d/ui/CrystalCanvas3D.tsx +393 -0
  273. package/src/crystal3d/ui/CrystalStructureView.tsx +153 -0
  274. package/src/crystal3d/ui/CrystalStructureViewer.tsx +39 -0
  275. package/src/crystal3d/ui/CrystalViewControls.tsx +129 -0
  276. package/src/crystal3d/ui/CrystalViewer3D.tsx +183 -0
  277. package/src/crystal3d/ui/crystalScene.ts +106 -0
  278. package/src/crystal3d/ui/crystalStructureViewerProps.ts +54 -0
  279. package/src/crystal3d/ui/index.ts +20 -0
  280. package/src/crystal3d/ui/useCrystalCaption.ts +38 -0
  281. package/src/crystal3d/viewer/animate.ts +113 -0
  282. package/src/crystal3d/viewer/camera.ts +139 -0
  283. package/src/crystal3d/viewer/index.ts +13 -0
  284. package/src/crystal3d/viewer/layerRegistry.ts +47 -0
  285. package/src/crystal3d/viewer/meshPrimitives.ts +102 -0
  286. package/src/crystal3d/viewer/plugin.ts +258 -0
  287. package/src/crystal3d/viewer/renderCell.ts +98 -0
  288. package/src/crystal3d/viewer/renderElements.ts +121 -0
  289. package/src/crystal3d/viewer/renderStructure.ts +209 -0
  290. package/src/crystal3d/viewer/sceneParts.ts +119 -0
  291. package/src/crystal3d/viewer/shapeLayer.ts +108 -0
  292. package/src/crystal3d/viewer/textLayer.ts +85 -0
  293. package/src/crystal3d/viewer/viewer.ts +223 -0
  294. package/src/crystal3d.ts +12 -0
  295. package/src/download/core/croppedMarkup.ts +16 -42
  296. package/src/download/core/dataUrl.ts +59 -0
  297. package/src/download/core/index.ts +3 -0
  298. package/src/download/core/svgMarkup.ts +113 -0
  299. package/src/drawing/ui/movingFigure.ts +4 -28
  300. package/src/locales/de.json +26 -1
  301. package/src/locales/en.json +26 -1
  302. package/src/locales/es.json +26 -1
  303. package/src/locales/fr.json +26 -1
  304. package/src/locales/it.json +26 -1
  305. package/src/molecule3d/ui/MoleculeCanvas3D.tsx +45 -71
  306. package/src/molstar/bonds/bondRule.ts +34 -0
  307. package/src/molstar/bonds/index.ts +9 -0
  308. package/src/molstar/ui/MolstarToolbar.tsx +103 -0
  309. package/src/molstar/ui/index.ts +14 -0
  310. package/src/molstar/ui/useMolstarTools.ts +98 -0
  311. package/styles/chrome.css +90 -0
@@ -0,0 +1,393 @@
1
+ /**
2
+ * The molstar canvas of the crystal view, with the family's toolbars over it,
3
+ * in a box that can fill the screen.
4
+ *
5
+ * `CrystalViewer3D` reaches this module through `React.lazy`, so a page that
6
+ * never shows a crystal never downloads molstar. Every effect below lists
7
+ * exactly what it reads, so a page that keeps its scene stable redraws only the
8
+ * part that changed: toggling one mirror does not rebuild the structure, and
9
+ * moving the pointer does not redraw anything.
10
+ */
11
+
12
+ import type { CSSProperties, ReactElement } from 'react';
13
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
14
+
15
+ import type { Molecule3DCamera } from '../../molecule3d/core/camera.ts';
16
+ import type { Measurement } from '../../molecule3d/core/measurement.ts';
17
+ import type { Molecule3DTools } from '../../molecule3d/core/settings.ts';
18
+ import { Molecule3DStage } from '../../molecule3d/ui/Molecule3DStage.tsx';
19
+ import { useImageExport } from '../../molecule3d/ui/useImageExport.ts';
20
+ import { MolstarToolbar } from '../../molstar/ui/MolstarToolbar.tsx';
21
+ import { useMolstarTools } from '../../molstar/ui/useMolstarTools.ts';
22
+ import { TOKEN } from '../../tokens/core/familyTokens.ts';
23
+ import { sceneOrientation } from '../core/orientation.ts';
24
+ import type { ViewerAtom } from '../core/types.ts';
25
+ import type { CrystalViewer } from '../viewer/index.ts';
26
+ import {
27
+ DEFAULT_CAMERA_DURATION,
28
+ createCrystalViewer,
29
+ } from '../viewer/index.ts';
30
+
31
+ import type { CrystalScene, CrystalViewHandle } from './crystalScene.ts';
32
+
33
+ /** Props of {@link CrystalCanvas3D}. */
34
+ export interface CrystalCanvas3DProps {
35
+ /** What to show. */
36
+ scene: CrystalScene;
37
+ /**
38
+ * Scene background, `#rrggbb`.
39
+ * @default '#ffffff'
40
+ */
41
+ background?: string;
42
+ /**
43
+ * Which tools the toolbars offer; every one it does not name is on.
44
+ * @default every tool but the display options and the surface
45
+ */
46
+ tools?: Partial<Molecule3DTools>;
47
+ /**
48
+ * Base name of a picture saved from the toolbar, extension excluded.
49
+ * @default 'crystal'
50
+ */
51
+ fileName?: string;
52
+ /**
53
+ * Called whenever drawing fails, with one sentence, or `null` once it works
54
+ * again.
55
+ * @default undefined
56
+ */
57
+ onFailure?: (message: string | null) => void;
58
+ /**
59
+ * Called with the means to picture the scene once the canvas exists, and with
60
+ * `null` once it is gone.
61
+ * @default undefined
62
+ */
63
+ onHandle?: (handle: CrystalViewHandle | null) => void;
64
+ }
65
+
66
+ /**
67
+ * Draw the scene, say what the pointer rests on, and float the toolbars.
68
+ * @param props - See {@link CrystalCanvas3DProps}.
69
+ * @returns The stage holding the canvas, its toolbars and its readout.
70
+ */
71
+ export function CrystalCanvas3D(props: CrystalCanvas3DProps): ReactElement {
72
+ const {
73
+ scene,
74
+ background,
75
+ tools,
76
+ fileName = 'crystal',
77
+ onFailure,
78
+ onHandle,
79
+ } = props;
80
+ const {
81
+ atoms,
82
+ camera = null,
83
+ cell = null,
84
+ repeat,
85
+ representation,
86
+ bonds = 'all',
87
+ elements,
88
+ labels,
89
+ elementStyle,
90
+ play = null,
91
+ } = scene;
92
+ const [container, setContainer] = useState<HTMLDivElement | null>(null);
93
+ const [hovered, setHovered] = useState<string | null>(null);
94
+
95
+ // The viewer is not React state: it is an external resource the effects below
96
+ // reach through this handle. Putting it in state would set state inside an
97
+ // effect, and every draw would then cost an extra render.
98
+ const held = useRef<CrystalViewer | null>(null);
99
+
100
+ // Which way the scene is opened on: measured from its own principal axis, so
101
+ // a molecule built about z and one built about x both open on a view where a
102
+ // plane reads as a plane and an axis reads as a rod.
103
+ const orientation = useMemo(
104
+ () => sceneOrientation({ cell, elements }),
105
+ [cell, elements],
106
+ );
107
+ // Held in a ref rather than read as a dependency, because only a new
108
+ // structure reframes: switching a layer off changes which elements are drawn,
109
+ // and a camera that swung round at that moment would look like the chip had
110
+ // moved the molecule.
111
+ const framing = useRef(orientation);
112
+ useEffect(() => {
113
+ framing.current = orientation;
114
+ }, [orientation]);
115
+
116
+ // A camera a figure carried is put back once, on the structure it came
117
+ // with; every later redraw frames the scene as any other does.
118
+ const opening = useRef<Molecule3DCamera | null>(camera);
119
+ const applied = useRef<Molecule3DCamera | null>(null);
120
+ useEffect(() => {
121
+ opening.current = camera;
122
+ }, [camera]);
123
+
124
+ // Kept in a ref as well, so a page passing an inline callback does not redraw
125
+ // the whole scene on every render.
126
+ const failure = useRef(onFailure);
127
+ useEffect(() => {
128
+ failure.current = onFailure;
129
+ }, [onFailure]);
130
+
131
+ // The draw in flight, so a picture or a measurement asked for meanwhile
132
+ // waits for it.
133
+ const drawing = useRef<Promise<void> | null>(null);
134
+
135
+ const { measurements, addMeasurement, clearMeasurements } =
136
+ useMeasurementsOf(atoms);
137
+
138
+ const imageExport = useImageExport(container, held, fileName);
139
+ const { capture } = imageExport;
140
+ const handing = useRef(onHandle);
141
+ useEffect(() => {
142
+ handing.current = onHandle;
143
+ }, [onHandle]);
144
+ useEffect(() => {
145
+ const listener = handing.current;
146
+ if (listener === undefined || container === null) return;
147
+ listener({
148
+ capture,
149
+ readCamera: () => held.current?.readCamera() ?? Promise.resolve(null),
150
+ whenDrawn: () => drawing.current ?? Promise.resolve(),
151
+ });
152
+ return () => {
153
+ listener(null);
154
+ };
155
+ }, [capture, container]);
156
+
157
+ // A callback ref, so the viewer is created by the element arriving; the
158
+ // effects that draw are declared after this one and so run after it, and
159
+ // every one of them lists `container` so a remount redraws.
160
+ useEffect(() => {
161
+ if (container === null) return;
162
+ const created = createCrystalViewer(container, {
163
+ background,
164
+ onMeasure: addMeasurement,
165
+ });
166
+ held.current = created;
167
+ const observer = new ResizeObserver(() => {
168
+ created.handleResize();
169
+ });
170
+ observer.observe(container);
171
+ return () => {
172
+ held.current = null;
173
+ observer.disconnect();
174
+ created.dispose();
175
+ };
176
+ }, [container, background, addMeasurement]);
177
+
178
+ const controls = useMolstarTools(held, container);
179
+
180
+ useEffect(() => {
181
+ const viewer = held.current;
182
+ if (viewer === null) return;
183
+ return viewer.onHover(setHovered);
184
+ }, [container, background]);
185
+
186
+ useEffect(() => {
187
+ const viewer = held.current;
188
+ if (viewer === null) return;
189
+ container?.setAttribute('data-drawn', 'false');
190
+ const work = (async () => {
191
+ await viewer.showStructure(atoms, { representation });
192
+ await viewer.showCell(cell, repeat);
193
+ const wanted = opening.current;
194
+ if (wanted !== null && wanted !== applied.current) {
195
+ applied.current = wanted;
196
+ await viewer.applyCamera(wanted);
197
+ } else {
198
+ await viewer.resetCamera(0, framing.current);
199
+ }
200
+ container?.setAttribute('data-drawn', 'true');
201
+ })();
202
+ drawing.current = work.catch(() => undefined);
203
+ void report(failure, work);
204
+ }, [container, background, atoms, cell, repeat, representation]);
205
+
206
+ // Declared after the structure, so a new viewer draws its first structure
207
+ // with these bonds; a later change redraws the bonds alone.
208
+ useEffect(() => {
209
+ const viewer = held.current;
210
+ if (viewer === null) return;
211
+ void report(failure, viewer.setBonds(bonds));
212
+ }, [container, background, bonds]);
213
+
214
+ // Once the structure above is on screen, so they are drawn on the atoms they
215
+ // name.
216
+ useEffect(() => {
217
+ const viewer = held.current;
218
+ if (viewer === null) return;
219
+ let cancelled = false;
220
+ void (drawing.current ?? Promise.resolve()).then(() => {
221
+ if (!cancelled) void viewer.showMeasurements(measurements);
222
+ });
223
+ return () => {
224
+ cancelled = true;
225
+ };
226
+ }, [container, background, measurements]);
227
+
228
+ useEffect(() => {
229
+ const viewer = held.current;
230
+ if (viewer === null) return;
231
+ void report(
232
+ failure,
233
+ viewer.showElements(elements ?? [], { labels, style: elementStyle }),
234
+ );
235
+ }, [container, background, elements, labels, elementStyle]);
236
+
237
+ // The nonce is the cue that replays the same operation; the operation itself
238
+ // is in the list so that changing it applies the new one.
239
+ const operation = play?.operation ?? null;
240
+ const duration = play?.durationMs;
241
+ const nonce = play?.nonce ?? null;
242
+ useEffect(() => {
243
+ const viewer = held.current;
244
+ if (viewer === null) return;
245
+ if (operation === null || nonce === null) {
246
+ void report(failure, viewer.resetOperation());
247
+ return;
248
+ }
249
+ void report(
250
+ failure,
251
+ viewer.playOperation(operation, { durationMs: duration }),
252
+ );
253
+ }, [container, background, operation, duration, nonce]);
254
+
255
+ return (
256
+ <Molecule3DStage style={STAGE_STYLE}>
257
+ {(fullscreen) => (
258
+ <>
259
+ <div
260
+ ref={setContainer}
261
+ style={CANVAS_STYLE}
262
+ data-testid="crystal-3d"
263
+ />
264
+ {atoms.length > 0 && (
265
+ <MolstarToolbar
266
+ controls={controls}
267
+ tools={{ ...DEFAULT_TOOLS, ...tools }}
268
+ fullscreen={fullscreen}
269
+ measurementCount={measurements.length}
270
+ onClearMeasurements={clearMeasurements}
271
+ onResetView={() => {
272
+ void held.current?.resetCamera(
273
+ DEFAULT_CAMERA_DURATION,
274
+ framing.current,
275
+ );
276
+ }}
277
+ imageExport={imageExport}
278
+ fileName={fileName}
279
+ />
280
+ )}
281
+ {hovered !== null && (
282
+ <div style={READOUT_STYLE} data-testid="crystal-3d-readout">
283
+ {hovered}
284
+ </div>
285
+ )}
286
+ </>
287
+ )}
288
+ </Molecule3DStage>
289
+ );
290
+ }
291
+
292
+ /** What was measured, and on which atoms. */
293
+ interface TakenMeasurements {
294
+ readonly atoms: readonly ViewerAtom[];
295
+ readonly list: readonly Measurement[];
296
+ }
297
+
298
+ /**
299
+ * The measurements taken on the atoms on screen. A new set of atoms is a new
300
+ * structure, whose atoms no measurement names, so the list starts empty again.
301
+ * @param atoms - The atoms on screen.
302
+ * @returns The list, and stable callbacks that change it.
303
+ */
304
+ function useMeasurementsOf(atoms: readonly ViewerAtom[]): {
305
+ measurements: readonly Measurement[];
306
+ addMeasurement: (measurement: Measurement) => void;
307
+ clearMeasurements: () => void;
308
+ } {
309
+ const latest = useRef(atoms);
310
+ useEffect(() => {
311
+ latest.current = atoms;
312
+ }, [atoms]);
313
+ const [taken, setTaken] = useState<TakenMeasurements>(() => ({
314
+ atoms,
315
+ list: NO_MEASUREMENTS,
316
+ }));
317
+ const addMeasurement = useCallback((measurement: Measurement) => {
318
+ setTaken((previous) => ({
319
+ atoms: latest.current,
320
+ list:
321
+ previous.atoms === latest.current
322
+ ? [...previous.list, measurement]
323
+ : [measurement],
324
+ }));
325
+ }, []);
326
+ const clearMeasurements = useCallback(() => {
327
+ setTaken({ atoms: latest.current, list: NO_MEASUREMENTS });
328
+ }, []);
329
+ const measurements = taken.atoms === atoms ? taken.list : NO_MEASUREMENTS;
330
+ return { measurements, addMeasurement, clearMeasurements };
331
+ }
332
+
333
+ /**
334
+ * Report a failure as one sentence, and clear it once the next draw works.
335
+ * @param listener - The ref holding the page's failure callback.
336
+ * @param listener.current - The callback, given the message or `null`.
337
+ * @param work - The draw whose outcome is reported.
338
+ */
339
+ async function report(
340
+ listener: { current?: (message: string | null) => void },
341
+ work: Promise<void>,
342
+ ): Promise<void> {
343
+ try {
344
+ await work;
345
+ listener.current?.(null);
346
+ } catch (error) {
347
+ listener.current?.(
348
+ error instanceof Error ? error.message : 'The scene could not be drawn.',
349
+ );
350
+ }
351
+ }
352
+
353
+ const NO_MEASUREMENTS: readonly Measurement[] = [];
354
+
355
+ /**
356
+ * A crystal is not a molecule: its look is the page's to set — the elements,
357
+ * the labels, the representation — so the display options and the molecular
358
+ * surface are not offered.
359
+ */
360
+ const DEFAULT_TOOLS: Partial<Molecule3DTools> = {
361
+ options: false,
362
+ surface: false,
363
+ };
364
+
365
+ const STAGE_STYLE: CSSProperties = {
366
+ position: 'relative',
367
+ width: '100%',
368
+ height: '100%',
369
+ minHeight: 0,
370
+ background: TOKEN.surface,
371
+ };
372
+
373
+ /** molstar inserts its canvas here, which therefore has a size of its own. */
374
+ const CANVAS_STYLE: CSSProperties = { position: 'absolute', inset: 0 };
375
+
376
+ /*
377
+ * The readout sits over the canvas rather than beside it, so naming an axis does
378
+ * not reflow the page while the pointer moves.
379
+ */
380
+ const READOUT_STYLE: CSSProperties = {
381
+ position: 'absolute',
382
+ right: 8,
383
+ bottom: 8,
384
+ left: 8,
385
+ padding: '3px 8px',
386
+ borderRadius: 6,
387
+ background: TOKEN.surface,
388
+ color: TOKEN.text,
389
+ fontSize: 12,
390
+ opacity: 0.92,
391
+ pointerEvents: 'none',
392
+ textAlign: 'right',
393
+ };
@@ -0,0 +1,153 @@
1
+ /**
2
+ * A crystal from its CIF: read, its cell filled by the space group, drawn in
3
+ * 3D with the chips that say how, and a line saying what is on screen.
4
+ *
5
+ * Reached through `React.lazy` from `CrystalStructureViewer`: it bonds
6
+ * atoms by the rule molstar draws bonds with, so what the packing completes is
7
+ * what the viewer joins, and that rule is a molstar module.
8
+ */
9
+
10
+ import { Callout } from '@blueprintjs/core';
11
+ import type { CSSProperties, ReactElement } from 'react';
12
+ import { useMemo } from 'react';
13
+
14
+ import type { CellAtoms } from '../../crystal/core/cellAtoms.ts';
15
+ import { crystalCellAtoms } from '../../crystal/core/cellAtoms.ts';
16
+ import { readCif } from '../../crystal/core/cif/read.ts';
17
+ import { useChromeT } from '../../i18n/ui/useT.ts';
18
+ import { useControlledState } from '../../molecule3d/ui/useControlledState.ts';
19
+ import { bondLimit } from '../../molstar/bonds/index.ts';
20
+ import { crystalView } from '../core/crystalView.ts';
21
+
22
+ import { CrystalViewControls } from './CrystalViewControls.tsx';
23
+ import { CrystalViewer3D } from './CrystalViewer3D.tsx';
24
+ import type { CrystalScene } from './crystalScene.ts';
25
+ import type { CrystalStructureViewerProps } from './crystalStructureViewerProps.ts';
26
+ import { DEFAULT_SETTINGS } from './crystalStructureViewerProps.ts';
27
+ import { useCrystalCaption } from './useCrystalCaption.ts';
28
+
29
+ /**
30
+ * The crystal, or why its file could not be drawn.
31
+ * @param props - See {@link CrystalStructureViewerProps}.
32
+ * @returns The view, its chips and its caption.
33
+ */
34
+ export function CrystalStructureView(
35
+ props: CrystalStructureViewerProps,
36
+ ): ReactElement {
37
+ const {
38
+ cif,
39
+ name = '',
40
+ settings: settingsProp,
41
+ defaultSettings = DEFAULT_SETTINGS,
42
+ onSettingsChange,
43
+ height,
44
+ tools,
45
+ fileName,
46
+ onHandle,
47
+ } = props;
48
+ const t = useChromeT();
49
+ const [settings, setSettings] = useControlledState(
50
+ settingsProp,
51
+ defaultSettings,
52
+ onSettingsChange,
53
+ );
54
+ const cell = useMemo(() => readCell(cif), [cif]);
55
+
56
+ if (typeof cell === 'string') {
57
+ return (
58
+ <Callout intent="danger" title={t('crystal3d.unreadable')} compact>
59
+ {cell}
60
+ </Callout>
61
+ );
62
+ }
63
+ if (cell.tooLarge) {
64
+ return (
65
+ <Callout intent="warning" compact>
66
+ {t('crystal3d.tooLarge', { count: cell.count })}
67
+ </Callout>
68
+ );
69
+ }
70
+ return (
71
+ <CrystalDrawing
72
+ cell={cell}
73
+ name={name}
74
+ settings={settings}
75
+ onSettingsChange={setSettings}
76
+ height={height}
77
+ tools={tools}
78
+ fileName={fileName}
79
+ onHandle={onHandle}
80
+ />
81
+ );
82
+ }
83
+
84
+ type CrystalDrawingProps = Required<
85
+ Pick<CrystalStructureViewerProps, 'name' | 'settings' | 'onSettingsChange'>
86
+ > &
87
+ Pick<
88
+ CrystalStructureViewerProps,
89
+ 'height' | 'tools' | 'fileName' | 'onHandle'
90
+ > & {
91
+ cell: CellAtoms;
92
+ };
93
+
94
+ function CrystalDrawing(props: CrystalDrawingProps): ReactElement {
95
+ const { cell, name, settings, onSettingsChange, ...viewer } = props;
96
+ const { packing, cells, unitCell, bonds } = settings;
97
+ const view = useMemo(
98
+ () => crystalView(cell.atoms, cell.lattice, { packing, cells, bondLimit }),
99
+ [cell, packing, cells],
100
+ );
101
+ const scene = useMemo(
102
+ (): CrystalScene => ({
103
+ atoms: view.atoms,
104
+ cell: unitCell ? cell.lattice.cell : null,
105
+ repeat: [view.cells, view.cells, view.cells],
106
+ bonds,
107
+ }),
108
+ [view, unitCell, cell, bonds],
109
+ );
110
+ const caption = useCrystalCaption(name, view, cells);
111
+ return (
112
+ <div style={COLUMN_STYLE}>
113
+ <CrystalViewer3D {...viewer} scene={scene} caption={caption} />
114
+ <CrystalViewControls
115
+ packing={packing}
116
+ onPackingChange={(next) => {
117
+ onSettingsChange({ ...settings, packing: next });
118
+ }}
119
+ unitCell={unitCell}
120
+ onUnitCellChange={(next) => {
121
+ onSettingsChange({ ...settings, unitCell: next });
122
+ }}
123
+ bonds={bonds}
124
+ onBondsChange={(next) => {
125
+ onSettingsChange({ ...settings, bonds: next });
126
+ }}
127
+ cells={cells}
128
+ onCellsChange={(next) => {
129
+ onSettingsChange({ ...settings, cells: next });
130
+ }}
131
+ />
132
+ </div>
133
+ );
134
+ }
135
+
136
+ /**
137
+ * The atoms of one cell, or the sentence saying why the file gives none.
138
+ * @param cif - The file's text.
139
+ * @returns The cell, or the sentence.
140
+ */
141
+ function readCell(cif: string): CellAtoms | string {
142
+ try {
143
+ return crystalCellAtoms(readCif(cif));
144
+ } catch (error) {
145
+ return error instanceof Error ? error.message : String(error);
146
+ }
147
+ }
148
+
149
+ const COLUMN_STYLE: CSSProperties = {
150
+ display: 'flex',
151
+ flexDirection: 'column',
152
+ gap: 10,
153
+ };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * A crystal from its CIF, in 3D, with the chips that say how it is drawn.
3
+ *
4
+ * ```tsx
5
+ * import { CrystalStructureViewer } from 'react-cheminfo/crystal3d';
6
+ *
7
+ * <CrystalStructureViewer cif={text} name="Aspirin" />;
8
+ * ```
9
+ *
10
+ * Reading the file, filling the cell and drawing it all happen behind a
11
+ * `React.lazy` boundary, so a page that never shows a crystal downloads
12
+ * neither the space groups nor molstar.
13
+ */
14
+
15
+ import { Spinner, SpinnerSize } from '@blueprintjs/core';
16
+ import type { ReactElement } from 'react';
17
+ import { Suspense, lazy } from 'react';
18
+
19
+ import type { CrystalStructureViewerProps } from './crystalStructureViewerProps.ts';
20
+
21
+ const CrystalStructureView = lazy(async () => {
22
+ const module = await import('./CrystalStructureView.tsx');
23
+ return { default: module.CrystalStructureView };
24
+ });
25
+
26
+ /**
27
+ * The crystal, its chips and its caption.
28
+ * @param props - See {@link CrystalStructureViewerProps}.
29
+ * @returns The view, once its code has arrived.
30
+ */
31
+ export function CrystalStructureViewer(
32
+ props: CrystalStructureViewerProps,
33
+ ): ReactElement {
34
+ return (
35
+ <Suspense fallback={<Spinner size={SpinnerSize.STANDARD} />}>
36
+ <CrystalStructureView {...props} />
37
+ </Suspense>
38
+ );
39
+ }