react-cheminfo 0.63.1 → 0.64.1
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.
- package/lib/molecule3d/core/highlight.d.ts +53 -0
- package/lib/molecule3d/core/highlight.d.ts.map +1 -0
- package/lib/molecule3d/core/highlight.js +96 -0
- package/lib/molecule3d/core/highlight.js.map +1 -0
- package/lib/molecule3d/core/index.d.ts +3 -1
- package/lib/molecule3d/core/index.d.ts.map +1 -1
- package/lib/molecule3d/core/index.js +2 -1
- package/lib/molecule3d/core/index.js.map +1 -1
- package/lib/molecule3d/core/measurement.d.ts +17 -0
- package/lib/molecule3d/core/measurement.d.ts.map +1 -1
- package/lib/molecule3d/core/measurement.js +43 -0
- package/lib/molecule3d/core/measurement.js.map +1 -1
- package/lib/molecule3d/ui/MoleculeCanvas3D.d.ts.map +1 -1
- package/lib/molecule3d/ui/MoleculeCanvas3D.js +7 -1
- package/lib/molecule3d/ui/MoleculeCanvas3D.js.map +1 -1
- package/lib/molecule3d/ui/drawScene.d.ts +4 -2
- package/lib/molecule3d/ui/drawScene.d.ts.map +1 -1
- package/lib/molecule3d/ui/drawScene.js +1 -1
- package/lib/molecule3d/ui/drawScene.js.map +1 -1
- package/lib/molecule3d/ui/highlightColor.d.ts +18 -0
- package/lib/molecule3d/ui/highlightColor.d.ts.map +1 -0
- package/lib/molecule3d/ui/highlightColor.js +31 -0
- package/lib/molecule3d/ui/highlightColor.js.map +1 -0
- package/lib/molecule3d/ui/moleculeViewer3DProps.d.ts +16 -1
- package/lib/molecule3d/ui/moleculeViewer3DProps.d.ts.map +1 -1
- package/lib/molecule3d/ui/renderMolecule.d.ts +2 -0
- package/lib/molecule3d/ui/renderMolecule.d.ts.map +1 -1
- package/lib/molecule3d/ui/renderMolecule.js +1 -1
- package/lib/molecule3d/ui/renderMolecule.js.map +1 -1
- package/lib/molecule3d/ui/sceneDrawer.d.ts +47 -0
- package/lib/molecule3d/ui/sceneDrawer.d.ts.map +1 -0
- package/lib/molecule3d/ui/sceneDrawer.js +103 -0
- package/lib/molecule3d/ui/sceneDrawer.js.map +1 -0
- package/lib/molecule3d/ui/useMoleculeScene.d.ts +2 -1
- package/lib/molecule3d/ui/useMoleculeScene.d.ts.map +1 -1
- package/lib/molecule3d/ui/useMoleculeScene.js +13 -22
- package/lib/molecule3d/ui/useMoleculeScene.js.map +1 -1
- package/lib/molecule3d/ui/useSharedCamera.d.ts +1 -3
- package/lib/molecule3d/ui/useSharedCamera.d.ts.map +1 -1
- package/lib/molecule3d/ui/useSharedCamera.js.map +1 -1
- package/lib/molecule3d/ui/viewer.d.ts +9 -0
- package/lib/molecule3d/ui/viewer.d.ts.map +1 -1
- package/lib/molecule3d/ui/viewer.js +18 -2
- package/lib/molecule3d/ui/viewer.js.map +1 -1
- package/lib/molstar/core/highlights.d.ts +57 -0
- package/lib/molstar/core/highlights.d.ts.map +1 -0
- package/lib/molstar/core/highlights.js +178 -0
- package/lib/molstar/core/highlights.js.map +1 -0
- package/lib/molstar/core/index.d.ts +2 -1
- package/lib/molstar/core/index.d.ts.map +1 -1
- package/lib/molstar/core/index.js +2 -1
- package/lib/molstar/core/index.js.map +1 -1
- package/package.json +1 -1
- package/src/molecule3d/core/highlight.ts +127 -0
- package/src/molecule3d/core/index.ts +7 -0
- package/src/molecule3d/core/measurement.ts +47 -0
- package/src/molecule3d/ui/MoleculeCanvas3D.tsx +13 -0
- package/src/molecule3d/ui/drawScene.ts +13 -2
- package/src/molecule3d/ui/highlightColor.ts +34 -0
- package/src/molecule3d/ui/moleculeViewer3DProps.ts +16 -1
- package/src/molecule3d/ui/renderMolecule.ts +1 -1
- package/src/molecule3d/ui/sceneDrawer.ts +132 -0
- package/src/molecule3d/ui/useMoleculeScene.ts +16 -26
- package/src/molecule3d/ui/useSharedCamera.ts +1 -3
- package/src/molecule3d/ui/viewer.ts +23 -0
- package/src/molstar/core/highlights.ts +236 -0
- package/src/molstar/core/index.ts +2 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The molstar layer under the 3D views: the plugin lifecycle, the camera, the
|
|
3
|
-
* hover labels, the measurements and the picture of a scene.
|
|
3
|
+
* hover labels, the measurements, the highlights and the picture of a scene.
|
|
4
4
|
*
|
|
5
5
|
* Three of our sites mount a headless molstar canvas — the molecule viewer
|
|
6
6
|
* here, lcao's orbitals and symmetry's crystals — and each of them had its own
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
export { cameraToPose, poseToCamera } from "./cameraPose.js";
|
|
18
18
|
export { DEFAULT_CAMERA_DURATION, DEFAULT_SPIN_SPEED, FRAMING_MARGIN, MINIMUM_FRAMING_RADIUS, framedRadius, setSpin, } from "./camera.js";
|
|
19
|
+
export { HighlightPainter, highlightOverpaint } from "./highlights.js";
|
|
19
20
|
export { atomName, atomText, lociText, subscribeHover } from "./hover.js";
|
|
20
21
|
export { MolstarPlugin } from "./plugin.js";
|
|
21
22
|
export { captureScene } from "./captureScene.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/molstar/core/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EAClB,cAAc,EACd,sBAAsB,EACtB,YAAY,EACZ,OAAO,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/molstar/core/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EAClB,cAAc,EACd,sBAAsB,EACtB,YAAY,EACZ,OAAO,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,mBAAmB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-cheminfo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.64.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React components, hooks and framework-free helpers shared by the cheminfo sites: site chrome, citations, sharing, routing, pedagogy, charts, and the structure, orbital and 3D molecule viewers",
|
|
6
6
|
"author": "Luc Patiny",
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a highlight on a 3D molecule is, without React and without molstar: a
|
|
3
|
+
* bonded path of atoms a site points at — the two atoms of a bond, the three of
|
|
4
|
+
* an angle, the four of a dihedral — for the model to emphasise.
|
|
5
|
+
*
|
|
6
|
+
* Atoms are named by position, as measurements name them, so a row of a table
|
|
7
|
+
* read off one conformer lands on the same atoms of every other conformer of
|
|
8
|
+
* the same molecule.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { AtomReference } from './measurement.ts';
|
|
12
|
+
import { sameAtoms } from './measurement.ts';
|
|
13
|
+
|
|
14
|
+
/** One bonded path of atoms to emphasise on the model. */
|
|
15
|
+
export interface Molecule3DHighlight {
|
|
16
|
+
/**
|
|
17
|
+
* The atoms in the order they are bonded: two for a bond, three for the two
|
|
18
|
+
* bonds of an angle, four for the three bonds of a dihedral. One atom
|
|
19
|
+
* emphasises that atom alone.
|
|
20
|
+
*/
|
|
21
|
+
atoms: readonly AtomReference[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Two consecutive atoms of a path: the bond a highlight emphasises. */
|
|
25
|
+
export type HighlightedBond = readonly [AtomReference, AtomReference];
|
|
26
|
+
|
|
27
|
+
/** Everything a list of highlights emphasises, each atom and bond once. */
|
|
28
|
+
export interface HighlightedParts {
|
|
29
|
+
/** Every atom of every path, in the order the paths first name them. */
|
|
30
|
+
atoms: readonly AtomReference[];
|
|
31
|
+
/** Every pair of consecutive atoms, in the order the paths first name them. */
|
|
32
|
+
bonds: readonly HighlightedBond[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The atoms and the bonds a list of highlights emphasises.
|
|
37
|
+
*
|
|
38
|
+
* A bond is a pair of consecutive atoms of one path, and nothing else: two
|
|
39
|
+
* atoms that are both highlighted, but by different paths or not next to each
|
|
40
|
+
* other in one, are not a bond of the highlight — so six C–H paths never
|
|
41
|
+
* emphasise the C–C bond between two of their carbons. An atom the structure
|
|
42
|
+
* lacks is left out, with the two pairs it stands in; the atoms on either side
|
|
43
|
+
* of it are not joined in its place.
|
|
44
|
+
* @param highlights - The paths, as a site passes them.
|
|
45
|
+
* @param hasAtom - Whether the structure on screen has an atom; asked only of
|
|
46
|
+
* references whose two numbers are whole and not negative.
|
|
47
|
+
* @default () => true
|
|
48
|
+
* @returns The atoms and the bonds, each once, in the order first named.
|
|
49
|
+
*/
|
|
50
|
+
export function highlightedParts(
|
|
51
|
+
highlights: readonly Molecule3DHighlight[],
|
|
52
|
+
hasAtom: (atom: AtomReference) => boolean = isAnyAtom,
|
|
53
|
+
): HighlightedParts {
|
|
54
|
+
const atoms: AtomReference[] = [];
|
|
55
|
+
const bonds: HighlightedBond[] = [];
|
|
56
|
+
const atomKeys = new Set<string>();
|
|
57
|
+
const bondKeys = new Set<string>();
|
|
58
|
+
for (const highlight of highlights) {
|
|
59
|
+
let previous: AtomReference | undefined;
|
|
60
|
+
for (const atom of highlight.atoms) {
|
|
61
|
+
const present = isAtomReference(atom) && hasAtom(atom);
|
|
62
|
+
if (present && !atomKeys.has(atomKey(atom))) {
|
|
63
|
+
atomKeys.add(atomKey(atom));
|
|
64
|
+
atoms.push({ unit: atom.unit, element: atom.element });
|
|
65
|
+
}
|
|
66
|
+
if (present && previous !== undefined) {
|
|
67
|
+
const key = bondKey(previous, atom);
|
|
68
|
+
if (key !== null && !bondKeys.has(key)) {
|
|
69
|
+
bondKeys.add(key);
|
|
70
|
+
bonds.push([
|
|
71
|
+
{ unit: previous.unit, element: previous.element },
|
|
72
|
+
{ unit: atom.unit, element: atom.element },
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
previous = present ? atom : undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { atoms, bonds };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Whether two lists of highlights name the same atoms, in the same paths and
|
|
84
|
+
* the same order — what decides that the model needs no repainting.
|
|
85
|
+
* @param first - One list.
|
|
86
|
+
* @param second - The other.
|
|
87
|
+
* @returns True when every path of one is the path of the other at its place.
|
|
88
|
+
*/
|
|
89
|
+
export function sameHighlights(
|
|
90
|
+
first: readonly Molecule3DHighlight[],
|
|
91
|
+
second: readonly Molecule3DHighlight[],
|
|
92
|
+
): boolean {
|
|
93
|
+
if (first === second) return true;
|
|
94
|
+
if (first.length !== second.length) return false;
|
|
95
|
+
for (let index = 0; index < first.length; index++) {
|
|
96
|
+
const one = first[index]?.atoms;
|
|
97
|
+
const other = second[index]?.atoms;
|
|
98
|
+
if (one === undefined || other === undefined) return false;
|
|
99
|
+
if (!sameAtoms(one, other)) return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isAnyAtom(): boolean {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isAtomReference(atom: AtomReference): boolean {
|
|
109
|
+
return (
|
|
110
|
+
Number.isInteger(atom.unit) &&
|
|
111
|
+
atom.unit >= 0 &&
|
|
112
|
+
Number.isInteger(atom.element) &&
|
|
113
|
+
atom.element >= 0
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function atomKey(atom: AtomReference): string {
|
|
118
|
+
return `${atom.unit}:${atom.element}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// One key for a pair whichever way round, and none for an atom with itself.
|
|
122
|
+
function bondKey(first: AtomReference, second: AtomReference): string | null {
|
|
123
|
+
const one = atomKey(first);
|
|
124
|
+
const other = atomKey(second);
|
|
125
|
+
if (one === other) return null;
|
|
126
|
+
return one < other ? `${one}-${other}` : `${other}-${one}`;
|
|
127
|
+
}
|
|
@@ -16,6 +16,12 @@ export type { ImageSize } from './exportImage.ts';
|
|
|
16
16
|
export { dataUriBytes, rasterSvgMarkup } from './exportImage.ts';
|
|
17
17
|
export type { Molecule3DGesture, Molecule3DGestureKey } from './gestures.ts';
|
|
18
18
|
export { MOLECULE_3D_GESTURES } from './gestures.ts';
|
|
19
|
+
export type {
|
|
20
|
+
HighlightedBond,
|
|
21
|
+
HighlightedParts,
|
|
22
|
+
Molecule3DHighlight,
|
|
23
|
+
} from './highlight.ts';
|
|
24
|
+
export { highlightedParts, sameHighlights } from './highlight.ts';
|
|
19
25
|
export type {
|
|
20
26
|
AtomReference,
|
|
21
27
|
Measurement,
|
|
@@ -25,6 +31,7 @@ export {
|
|
|
25
31
|
MEASUREMENT_ATOM_COUNTS,
|
|
26
32
|
MEASUREMENT_KINDS,
|
|
27
33
|
MEASUREMENT_LABELS,
|
|
34
|
+
sameMeasurements,
|
|
28
35
|
} from './measurement.ts';
|
|
29
36
|
export type {
|
|
30
37
|
Molecule3DFile,
|
|
@@ -41,3 +41,50 @@ export interface Measurement {
|
|
|
41
41
|
kind: MeasurementKind;
|
|
42
42
|
atoms: readonly AtomReference[];
|
|
43
43
|
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether two lists name the same measurements, of the same kinds over the
|
|
47
|
+
* same atoms, in the same order — what decides that nothing needs drawing
|
|
48
|
+
* again.
|
|
49
|
+
* @param first - One list.
|
|
50
|
+
* @param second - The other.
|
|
51
|
+
* @returns True when every measurement of one is the measurement of the other
|
|
52
|
+
* at its place.
|
|
53
|
+
*/
|
|
54
|
+
export function sameMeasurements(
|
|
55
|
+
first: readonly Measurement[],
|
|
56
|
+
second: readonly Measurement[],
|
|
57
|
+
): boolean {
|
|
58
|
+
if (first === second) return true;
|
|
59
|
+
if (first.length !== second.length) return false;
|
|
60
|
+
for (let index = 0; index < first.length; index++) {
|
|
61
|
+
const one = first[index];
|
|
62
|
+
const other = second[index];
|
|
63
|
+
if (one === undefined || other === undefined) return false;
|
|
64
|
+
if (one.kind !== other.kind || !sameAtoms(one.atoms, other.atoms)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether two lists name the same atoms in the same order.
|
|
73
|
+
* @param first - One list.
|
|
74
|
+
* @param second - The other.
|
|
75
|
+
* @returns True when every atom of one is the atom of the other at its place.
|
|
76
|
+
*/
|
|
77
|
+
export function sameAtoms(
|
|
78
|
+
first: readonly AtomReference[],
|
|
79
|
+
second: readonly AtomReference[],
|
|
80
|
+
): boolean {
|
|
81
|
+
if (first.length !== second.length) return false;
|
|
82
|
+
for (let position = 0; position < first.length; position++) {
|
|
83
|
+
const atom = first[position];
|
|
84
|
+
const match = second[position];
|
|
85
|
+
if (atom?.unit !== match?.unit || atom?.element !== match?.element) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
@@ -10,6 +10,7 @@ import type { CSSProperties, ReactElement } from 'react';
|
|
|
10
10
|
import { useEffect, useRef, useState } from 'react';
|
|
11
11
|
|
|
12
12
|
import { DEFAULT_SPIN_SPEED } from '../../molstar/core/index.ts';
|
|
13
|
+
import type { Molecule3DHighlight } from '../core/highlight.ts';
|
|
13
14
|
import type { Measurement, MeasurementKind } from '../core/measurement.ts';
|
|
14
15
|
import {
|
|
15
16
|
normalizeMolecule3DSettings,
|
|
@@ -21,6 +22,7 @@ import { Molecule3DOptions } from './Molecule3DOptions.tsx';
|
|
|
21
22
|
import { Molecule3DStage } from './Molecule3DStage.tsx';
|
|
22
23
|
import { Molecule3DToolbar } from './Molecule3DToolbar.tsx';
|
|
23
24
|
import { createMolecule3DViewer } from './createViewer.ts';
|
|
25
|
+
import { highlightColorOf } from './highlightColor.ts';
|
|
24
26
|
import type { MoleculeViewer3DProps } from './moleculeViewer3DProps.ts';
|
|
25
27
|
import { useControlledState } from './useControlledState.ts';
|
|
26
28
|
import { useImageExport } from './useImageExport.ts';
|
|
@@ -56,6 +58,7 @@ export function MoleculeCanvas3D(props: MoleculeCanvas3DProps): ReactElement {
|
|
|
56
58
|
spinSpeed = DEFAULT_SPIN_SPEED,
|
|
57
59
|
measurements: measurementsProp,
|
|
58
60
|
onMeasurementsChange,
|
|
61
|
+
highlights = NO_HIGHLIGHTS,
|
|
59
62
|
fileName = 'molecule',
|
|
60
63
|
background,
|
|
61
64
|
minHeight = 320,
|
|
@@ -133,6 +136,14 @@ export function MoleculeCanvas3D(props: MoleculeCanvas3DProps): ReactElement {
|
|
|
133
136
|
void viewerRef.current?.setMeasureKind(measureKind);
|
|
134
137
|
}, [container, measureKind]);
|
|
135
138
|
|
|
139
|
+
// Kept out of the scene: a new highlight repaints the model, never redraws it.
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
void viewerRef.current?.setHighlights(
|
|
142
|
+
highlights,
|
|
143
|
+
highlightColorOf(container),
|
|
144
|
+
);
|
|
145
|
+
}, [container, highlights]);
|
|
146
|
+
|
|
136
147
|
const { resetView } = useMoleculeScene({
|
|
137
148
|
container,
|
|
138
149
|
viewerRef,
|
|
@@ -204,6 +215,8 @@ export function MoleculeCanvas3D(props: MoleculeCanvas3DProps): ReactElement {
|
|
|
204
215
|
|
|
205
216
|
const NO_MEASUREMENTS: readonly Measurement[] = [];
|
|
206
217
|
|
|
218
|
+
const NO_HIGHLIGHTS: readonly Molecule3DHighlight[] = [];
|
|
219
|
+
|
|
207
220
|
const STAGE_STYLE: CSSProperties = {
|
|
208
221
|
position: 'relative',
|
|
209
222
|
flex: '1 0 auto',
|
|
@@ -4,8 +4,19 @@ import { normalizeMolecule3DSettings } from '../core/settings.ts';
|
|
|
4
4
|
|
|
5
5
|
import type { Molecule3DViewer } from './viewer.ts';
|
|
6
6
|
|
|
7
|
+
/** The operations of the viewer a scene is drawn with. */
|
|
8
|
+
export type SceneViewer = Pick<
|
|
9
|
+
Molecule3DViewer,
|
|
10
|
+
| 'showMolecule'
|
|
11
|
+
| 'hideMolecule'
|
|
12
|
+
| 'showSurface'
|
|
13
|
+
| 'showMeasurements'
|
|
14
|
+
| 'resetCamera'
|
|
15
|
+
| 'setCamera'
|
|
16
|
+
>;
|
|
17
|
+
|
|
7
18
|
/**
|
|
8
|
-
* Draw the scene. `showMolecule` empties it, so the surface and the
|
|
19
|
+
* Draw the whole scene. `showMolecule` empties it, so the surface and the
|
|
9
20
|
* measurements are added after the model, and the camera is framed last.
|
|
10
21
|
* @param viewer - The viewer holding the canvas.
|
|
11
22
|
* @param molfile - What to draw, or `null` to empty the scene.
|
|
@@ -17,7 +28,7 @@ import type { Molecule3DViewer } from './viewer.ts';
|
|
|
17
28
|
* @returns Nothing; resolves once the scene is complete.
|
|
18
29
|
*/
|
|
19
30
|
export async function drawScene(
|
|
20
|
-
viewer:
|
|
31
|
+
viewer: SceneViewer,
|
|
21
32
|
molfile: Molecule3DFile | null,
|
|
22
33
|
settings: Molecule3DSettings,
|
|
23
34
|
measurements: readonly Measurement[],
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The colour the 3D view emphasises in, read off the page it sits on.
|
|
3
|
+
*
|
|
4
|
+
* A WebGL colour cannot be a custom property, and the family's accent is the
|
|
5
|
+
* one colour a site owns, so it cannot be written into a shared library either:
|
|
6
|
+
* the view asks the browser what `--accent` resolved to where it is drawn, as
|
|
7
|
+
* the parallel coordinates do, and hands molstar the answer.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { normalizeColor } from '../../tokens/core/colors.ts';
|
|
11
|
+
import { FAMILY_TOKEN_VALUES } from '../../tokens/core/familyTokens.ts';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What `--accent` resolved to on an element of the view.
|
|
15
|
+
* @param element - An element of the view, once it is on the page, or `null`
|
|
16
|
+
* before it is.
|
|
17
|
+
* @returns The accent as `#rrggbb`. A site that declares none emphasises in its
|
|
18
|
+
* text colour, and a page that declares neither, or no page yet, in the
|
|
19
|
+
* family's.
|
|
20
|
+
*/
|
|
21
|
+
export function highlightColorOf(element: Element | null): string {
|
|
22
|
+
return (
|
|
23
|
+
tokenColor(element, '--accent') ??
|
|
24
|
+
tokenColor(element, '--text') ??
|
|
25
|
+
FAMILY_TOKEN_VALUES['--text']
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function tokenColor(element: Element | null, name: string): string | undefined {
|
|
30
|
+
const resolve = globalThis.getComputedStyle;
|
|
31
|
+
if (element === null || typeof resolve !== 'function') return undefined;
|
|
32
|
+
const color = normalizeColor(resolve(element).getPropertyValue(name));
|
|
33
|
+
return color === null ? undefined : `#${color}`;
|
|
34
|
+
}
|
|
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import type { ViewerCapability } from '../../orbital/ui/capability.ts';
|
|
4
4
|
import type { Molecule3DCamera } from '../core/camera.ts';
|
|
5
|
+
import type { Molecule3DHighlight } from '../core/highlight.ts';
|
|
5
6
|
import type { Measurement } from '../core/measurement.ts';
|
|
6
7
|
import type {
|
|
7
8
|
Molecule3DFile,
|
|
@@ -71,7 +72,9 @@ export interface MoleculeViewer3DProps {
|
|
|
71
72
|
/**
|
|
72
73
|
* The measurements drawn over the model, when the site owns them. They name
|
|
73
74
|
* atoms by position, so they carry over to another conformer of the same
|
|
74
|
-
* molecule; a site showing an unrelated molecule should clear them.
|
|
75
|
+
* molecule; a site showing an unrelated molecule should clear them. Changing
|
|
76
|
+
* them redraws the measurements and nothing else: the model, its highlights
|
|
77
|
+
* and the camera are left as they are.
|
|
75
78
|
* @default undefined
|
|
76
79
|
*/
|
|
77
80
|
measurements?: readonly Measurement[];
|
|
@@ -80,6 +83,18 @@ export interface MoleculeViewer3DProps {
|
|
|
80
83
|
* @default undefined
|
|
81
84
|
*/
|
|
82
85
|
onMeasurementsChange?: (measurements: readonly Measurement[]) => void;
|
|
86
|
+
/**
|
|
87
|
+
* The parts of the model to emphasise in the site's accent colour, each a
|
|
88
|
+
* bonded path of atoms named as measurements name them: two atoms are that
|
|
89
|
+
* bond, three the two bonds of an angle, four the three bonds of a dihedral,
|
|
90
|
+
* and one atom is that atom alone. Only the bonds between consecutive atoms
|
|
91
|
+
* of one path are emphasised, never another bond between two highlighted
|
|
92
|
+
* atoms, and an atom the structure lacks is skipped. Changing them repaints
|
|
93
|
+
* the model and nothing else: the camera, the measurements and the hover are
|
|
94
|
+
* left as they are.
|
|
95
|
+
* @default [] — nothing is emphasised
|
|
96
|
+
*/
|
|
97
|
+
highlights?: readonly Molecule3DHighlight[];
|
|
83
98
|
/**
|
|
84
99
|
* Where the camera stands when the first molecule is framed — what a shared
|
|
85
100
|
* link carries. The camera belongs to whoever is dragging it afterwards, so
|
|
@@ -26,7 +26,7 @@ export const MOLECULE_STRUCTURE_REF = 'molecule3d-structure';
|
|
|
26
26
|
const MOLECULE_DATA_REF = 'molecule3d-data';
|
|
27
27
|
|
|
28
28
|
/** The drawn model, replaced in place whenever the style changes. */
|
|
29
|
-
const MODEL_REPRESENTATION_REF = 'molecule3d-model';
|
|
29
|
+
export const MODEL_REPRESENTATION_REF = 'molecule3d-model';
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* molstar's ball-and-stick radius factor that a size factor of 1 stands for:
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one canvas shows, what it is asked to show next, and the least work
|
|
3
|
+
* between the two.
|
|
4
|
+
*
|
|
5
|
+
* The measurements are drawn over the model, not into it: a change of them
|
|
6
|
+
* alone replaces their own representations and leaves the model, its surface,
|
|
7
|
+
* its highlights and the camera as they are. Only another molecule, or another
|
|
8
|
+
* style for it, draws the scene again, and only another molecule frames the
|
|
9
|
+
* camera. Draws run one after another, so a measurement never lands on a model
|
|
10
|
+
* that is half built, and the last request asked is the one drawn.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Molecule3DCamera } from '../core/camera.ts';
|
|
14
|
+
import type { Measurement } from '../core/measurement.ts';
|
|
15
|
+
import { sameMeasurements } from '../core/measurement.ts';
|
|
16
|
+
import type { Molecule3DFile, Molecule3DSettings } from '../core/settings.ts';
|
|
17
|
+
import { sameMolecule3DSettings } from '../core/settings.ts';
|
|
18
|
+
|
|
19
|
+
import type { SceneViewer } from './drawScene.ts';
|
|
20
|
+
import { drawScene } from './drawScene.ts';
|
|
21
|
+
|
|
22
|
+
/** What the canvas is asked to show. */
|
|
23
|
+
export interface SceneRequest {
|
|
24
|
+
/** What to draw, or `null` to empty the scene. */
|
|
25
|
+
molfile: Molecule3DFile | null;
|
|
26
|
+
/** How the model is drawn. */
|
|
27
|
+
settings: Molecule3DSettings;
|
|
28
|
+
/** What is drawn over it. */
|
|
29
|
+
measurements: readonly Measurement[];
|
|
30
|
+
/** How the camera meets a molecule it has not shown before. */
|
|
31
|
+
frameNewMolecule: 'front' | 'keep';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Keeps one viewer showing the last scene it was asked for. */
|
|
35
|
+
export class SceneDrawer {
|
|
36
|
+
/** The viewer drawn on. */
|
|
37
|
+
readonly viewer: SceneViewer;
|
|
38
|
+
readonly #takeLinkedCamera: () => Molecule3DCamera | null;
|
|
39
|
+
#asked: SceneRequest | null = null;
|
|
40
|
+
// The model on screen, or `null` when the next request must draw it again.
|
|
41
|
+
#drawn: Pick<SceneRequest, 'molfile' | 'settings'> | null = null;
|
|
42
|
+
// The measurements on screen, or `null` when the next request must draw them.
|
|
43
|
+
#measured: readonly Measurement[] | null = [];
|
|
44
|
+
// The molecule the camera was last framed on, so another one is told apart.
|
|
45
|
+
#framed: Molecule3DFile | null = null;
|
|
46
|
+
#queue: Promise<void> = Promise.resolve();
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Start drawing on `viewer`, whose scene is empty.
|
|
50
|
+
* @param viewer - The viewer to draw on.
|
|
51
|
+
* @param takeLinkedCamera - Asked when a molecule is new: a camera to stand
|
|
52
|
+
* at instead of framing it, or `null`.
|
|
53
|
+
*/
|
|
54
|
+
constructor(
|
|
55
|
+
viewer: SceneViewer,
|
|
56
|
+
takeLinkedCamera: () => Molecule3DCamera | null,
|
|
57
|
+
) {
|
|
58
|
+
this.viewer = viewer;
|
|
59
|
+
this.#takeLinkedCamera = takeLinkedCamera;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Bring the canvas to `request`, once whatever was asked before is drawn.
|
|
64
|
+
* @param request - The scene to show; replaces any request still waiting.
|
|
65
|
+
* @returns Nothing; resolves once the canvas shows the last request asked,
|
|
66
|
+
* and rejects with what stopped it from being drawn.
|
|
67
|
+
*/
|
|
68
|
+
draw(request: SceneRequest): Promise<void> {
|
|
69
|
+
this.#asked = request;
|
|
70
|
+
const drawn = this.#queue.then(() => this.#catchUp());
|
|
71
|
+
this.#queue = drawn.catch(ignore);
|
|
72
|
+
return drawn;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async #catchUp(): Promise<void> {
|
|
76
|
+
const request = this.#asked;
|
|
77
|
+
if (request === null) return;
|
|
78
|
+
const drawn = this.#drawn;
|
|
79
|
+
if (
|
|
80
|
+
drawn !== null &&
|
|
81
|
+
sameFile(drawn.molfile, request.molfile) &&
|
|
82
|
+
sameMolecule3DSettings(drawn.settings, request.settings)
|
|
83
|
+
) {
|
|
84
|
+
await this.#measure(request);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
await this.#redraw(request);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The model stays: only the measurements over it can have changed.
|
|
91
|
+
async #measure(request: SceneRequest): Promise<void> {
|
|
92
|
+
const { molfile, measurements } = request;
|
|
93
|
+
const measured = this.#measured;
|
|
94
|
+
if (measured !== null && sameMeasurements(measured, measurements)) return;
|
|
95
|
+
// As for the model: a list that fails partway is drawn again next time.
|
|
96
|
+
this.#measured = null;
|
|
97
|
+
// An empty scene has nothing to measure; the list waits for a molecule.
|
|
98
|
+
if (molfile !== null) await this.viewer.showMeasurements(measurements);
|
|
99
|
+
this.#measured = measurements;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async #redraw(request: SceneRequest): Promise<void> {
|
|
103
|
+
const { molfile, settings, measurements, frameNewMolecule } = request;
|
|
104
|
+
const isNew =
|
|
105
|
+
molfile !== null &&
|
|
106
|
+
(this.#framed === null || !sameFile(this.#framed, molfile));
|
|
107
|
+
this.#framed = molfile;
|
|
108
|
+
// A link's camera is spent on the first molecule framed; afterwards the
|
|
109
|
+
// camera is the reader's and a new molecule is framed as usual.
|
|
110
|
+
const linked = isNew ? this.#takeLinkedCamera() : null;
|
|
111
|
+
const frameCamera = isNew && linked === null ? frameNewMolecule : 'none';
|
|
112
|
+
// Kept only once drawn: a draw that fails is drawn again by the next
|
|
113
|
+
// request, whatever that one changes.
|
|
114
|
+
this.#drawn = null;
|
|
115
|
+
await drawScene(this.viewer, molfile, settings, measurements, frameCamera);
|
|
116
|
+
this.#drawn = { molfile, settings };
|
|
117
|
+
this.#measured = measurements;
|
|
118
|
+
if (linked !== null) await this.viewer.setCamera(linked);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function sameFile(
|
|
123
|
+
first: Molecule3DFile | null,
|
|
124
|
+
second: Molecule3DFile | null,
|
|
125
|
+
): boolean {
|
|
126
|
+
if (first === null || second === null) return first === second;
|
|
127
|
+
return first.format === second.format && first.data === second.data;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function ignore(): void {
|
|
131
|
+
// A failure reached the caller of `draw`; the next draw still runs.
|
|
132
|
+
}
|
|
@@ -5,18 +5,18 @@
|
|
|
5
5
|
* The draw is coalesced into one animation frame, so dragging a slider costs
|
|
6
6
|
* one rebuild rather than one per pixel, and the camera is framed only when the
|
|
7
7
|
* molecule is new — a restyle that snapped the view back would undo the
|
|
8
|
-
* reader's orientation.
|
|
8
|
+
* reader's orientation. What each draw rebuilds is the scene drawer's call: a
|
|
9
|
+
* change of the measurements alone redraws them and nothing else.
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
12
|
import type { RefObject } from 'react';
|
|
12
13
|
import { useCallback, useEffect, useRef } from 'react';
|
|
13
14
|
|
|
14
|
-
import type { Molecule3DCamera } from '../core/camera.ts';
|
|
15
15
|
import type { Measurement } from '../core/measurement.ts';
|
|
16
16
|
import type { Molecule3DFile, Molecule3DSettings } from '../core/settings.ts';
|
|
17
17
|
|
|
18
|
-
import { drawScene } from './drawScene.ts';
|
|
19
18
|
import type { MoleculeViewer3DProps } from './moleculeViewer3DProps.ts';
|
|
19
|
+
import { SceneDrawer } from './sceneDrawer.ts';
|
|
20
20
|
import {
|
|
21
21
|
DEFAULT_CAMERA_SETTLE_DELAY,
|
|
22
22
|
useSharedCamera,
|
|
@@ -71,10 +71,7 @@ export function useMoleculeScene(options: MoleculeSceneOptions): MoleculeScene {
|
|
|
71
71
|
onFailureChange,
|
|
72
72
|
camera: cameraProps,
|
|
73
73
|
} = options;
|
|
74
|
-
const
|
|
75
|
-
molfile: Molecule3DFile;
|
|
76
|
-
viewer: Molecule3DViewer;
|
|
77
|
-
} | null>(null);
|
|
74
|
+
const drawerRef = useRef<SceneDrawer | null>(null);
|
|
78
75
|
const reportFailure = useRef(onFailureChange);
|
|
79
76
|
useEffect(() => {
|
|
80
77
|
reportFailure.current = onFailureChange;
|
|
@@ -92,31 +89,24 @@ export function useMoleculeScene(options: MoleculeSceneOptions): MoleculeScene {
|
|
|
92
89
|
useEffect(() => {
|
|
93
90
|
const viewer = viewerRef.current;
|
|
94
91
|
if (container === null || viewer === null) return;
|
|
92
|
+
// A new viewer starts from an empty scene, so it gets a drawer of its own.
|
|
93
|
+
if (drawerRef.current?.viewer !== viewer) {
|
|
94
|
+
drawerRef.current = new SceneDrawer(viewer, camera.takeLinkedCamera);
|
|
95
|
+
}
|
|
96
|
+
const drawer = drawerRef.current;
|
|
95
97
|
let cancelled = false;
|
|
96
98
|
const frame = requestAnimationFrame(() => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// The link's camera is spent on the first molecule framed; afterwards the
|
|
103
|
-
// camera is the reader's and a new molecule is framed as usual.
|
|
104
|
-
const linked: Molecule3DCamera | null = isNew
|
|
105
|
-
? camera.takeLinkedCamera()
|
|
106
|
-
: null;
|
|
107
|
-
const frameCamera = isNew && linked === null ? frameNewMolecule : 'none';
|
|
108
|
-
void drawScene(viewer, molfile, settings, measurements, frameCamera)
|
|
109
|
-
.then(() => {
|
|
110
|
-
if (cancelled) return;
|
|
111
|
-
if (linked !== null) void viewer.setCamera(linked);
|
|
112
|
-
reportFailure.current(null);
|
|
113
|
-
})
|
|
114
|
-
.catch((error: unknown) => {
|
|
99
|
+
drawer.draw({ molfile, settings, measurements, frameNewMolecule }).then(
|
|
100
|
+
() => {
|
|
101
|
+
if (!cancelled) reportFailure.current(null);
|
|
102
|
+
},
|
|
103
|
+
(error: unknown) => {
|
|
115
104
|
if (cancelled) return;
|
|
116
105
|
reportFailure.current(
|
|
117
106
|
error instanceof Error ? error.message : String(error),
|
|
118
107
|
);
|
|
119
|
-
}
|
|
108
|
+
},
|
|
109
|
+
);
|
|
120
110
|
});
|
|
121
111
|
return () => {
|
|
122
112
|
cancelled = true;
|
|
@@ -37,9 +37,7 @@ export interface SharedCameraOptions {
|
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* What {@link useSharedCamera} answers. The same object on every render: the
|
|
40
|
-
* draw effect keys on it, and a fresh one would
|
|
41
|
-
* it again — which makes molstar frame the model afresh and loses the camera a
|
|
42
|
-
* link had just restored.
|
|
40
|
+
* draw effect keys on it, and a fresh one would run it again for nothing.
|
|
43
41
|
*/
|
|
44
42
|
export interface SharedCamera {
|
|
45
43
|
/**
|