@@ -5,8 +5,8 @@ import getAtomsFromMF from '../utilities/getAtomsFromMF.js';
55
66export interface StateMoleculeExtended
77 extends
8- Required < Pick < StateMolecule , 'id' | 'molfile' | 'label' > > ,
9- Omit < StateMolecule , 'id' | 'molfile' | 'label' > {
8+ Required < Pick < StateMolecule , 'id' | 'molfile' | 'label' > > ,
9+ Omit < StateMolecule , 'id' | 'molfile' | 'label' > {
1010 mf : string ;
1111 em : number ;
1212 mw : number ;
@@ -22,24 +22,47 @@ export interface MoleculeBoundingRect {
2222}
2323
2424export const DRAGGABLE_STRUCTURE_INITIAL_BOUNDING_REACT : MoleculeBoundingRect =
25- {
26- x : 10 ,
27- y : 10 ,
28- width : 130 ,
29- height : 120 ,
30- } ;
25+ {
26+ x : 10 ,
27+ y : 10 ,
28+ width : 130 ,
29+ height : 120 ,
30+ } ;
3131
3232export type MoleculesView = Record < string , MoleculeView > ;
3333
34+ type MoleculeInput = { molecule : Molecule } | { molfile : string } ;
35+ type StateMoleculeOptions = Omit < Partial < StateMolecule > , 'molfile' > ;
36+ type InitializeMoleculeOptions = MoleculeInput & StateMoleculeOptions ;
37+
38+ function resolveMolecule ( options : MoleculeInput ) {
39+ if ( 'molecule' in options ) {
40+ const { molecule } = options ;
41+ return {
42+ molecule,
43+ molfile : molecule . toMolfileV3 ( ) ,
44+ } ;
45+ }
46+ const { molfile } = options ;
47+ return {
48+ molecule : Molecule . fromMolfile ( molfile ) ,
49+ molfile,
50+ } ;
51+ }
52+
53+ export function initMolecule (
54+ options : { molecule : Molecule } & StateMoleculeOptions ,
55+ ) : StateMoleculeExtended ;
56+ export function initMolecule (
57+ options : { molfile : string } & StateMoleculeOptions ,
58+ ) : StateMoleculeExtended ;
3459export function initMolecule (
35- options : Partial < StateMolecule > = { } ,
60+ options : InitializeMoleculeOptions ,
3661) : StateMoleculeExtended {
37- const id = options . id || crypto . randomUUID ( ) ;
38- const label = options . label || 'p#' ;
39- const molfile = options . molfile || '' ;
62+ const { id = crypto . randomUUID ( ) , label = 'p#' } = options ;
4063
41- const mol = Molecule . fromMolfile ( molfile ) ;
42- const mfInfo = mol . getMolecularFormula ( ) ;
64+ const { molecule , molfile } = resolveMolecule ( options ) ;
65+ const mfInfo = molecule . getMolecularFormula ( ) ;
4366
4467 return {
4568 id,
@@ -49,7 +72,7 @@ export function initMolecule(
4972 mf : mfInfo . formula ,
5073 em : mfInfo . absoluteWeight ,
5174 mw : mfInfo . relativeWeight ,
52- svg : mol . toSVG ( 50 , 50 ) ,
75+ svg : molecule . toSVG ( 50 , 50 ) ,
5376 atoms : getAtomsFromMF ( mfInfo . formula ) ,
5477 } ;
5578}
0 commit comments