Advertisement
Pandaaaa906

Untitled

Jun 15th, 2022
1,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useEffect, useRef, useState} from 'react';
  2. import PropTypes from 'prop-types';
  3. import {Editor} from "ketcher-react";
  4. import 'ketcher-react/dist/index.css'
  5. import config from "react-global-configuration";
  6. import {RemoteStructServiceProvider} from "ketcher-core";
  7.  
  8.  
  9. const URL_PREFIX = config.get('url_prefix', '')
  10. const defaultStructServiceProvider = new RemoteStructServiceProvider(`${URL_PREFIX}/api/ketcher`)
  11.  
  12.  
  13. MoleculeEditor.propTypes = {
  14.     value: PropTypes.string,
  15.     onChange: PropTypes.func,
  16.     structServiceProvider: PropTypes.object,
  17.     buttons: PropTypes.object,
  18. };
  19.  
  20. function MoleculeEditor({value, onChange, buttons, structServiceProvider = defaultStructServiceProvider}) {
  21.     const [editorReady, setEditorReady] = useState(false)
  22.     const ref = useRef()
  23.  
  24.     const handleChange = async ()=>{
  25.         if(typeof onChange !== 'function'){return}
  26.         const ketcher = ref?.current
  27.         if(!ketcher){return}
  28.         const smiles = await ketcher.getSmiles()
  29.         if(!smiles){
  30.             onChange(null)
  31.         }else{
  32.             const struct = ketcher.editor.struct()
  33.             let moltext
  34.             window.ketcher = ketcher
  35.             if(struct.isRxn()){
  36.                 moltext = await ketcher.getRxn()
  37.             }else{
  38.                 moltext = await ketcher.getMolfile()
  39.             }
  40.             onChange(moltext)
  41.         }
  42.     }
  43.  
  44.     const waitForEditor = ()=>{
  45.         if(editorReady){return}
  46.         const ketcher = ref?.current
  47.         if(typeof ketcher !== 'undefined'){
  48.             setEditorReady(true)
  49.         }else{
  50.             setTimeout(waitForEditor, 100);
  51.         }
  52.     }
  53.  
  54.     useEffect( ()=>{
  55.         if(!editorReady){return}
  56.         const ketcher = ref?.current
  57.         if(!ketcher){return}
  58.         ketcher.getSmiles().then(smiles=>{
  59.             if(!smiles && !value){return}
  60.             ketcher.getMolfile().then(moltext=>{
  61.                 if(moltext===value || !value){return}
  62.                 ketcher.setMolecule(value)
  63.             })
  64.         })
  65.     }, [value, editorReady])
  66.  
  67.  
  68.     useEffect(()=>{
  69.         waitForEditor()
  70.     }, [])
  71.  
  72.     const onInit = (ketcher)=>{
  73.         ref.current = ketcher
  74.     }
  75.  
  76.  
  77.     return (
  78.         <div onBlur={handleChange} style={{height: 'inherit'}}>
  79.             <Editor
  80.                 staticResourcesUrl={process.env.PUBLIC_URL}
  81.                 structServiceProvider={structServiceProvider}
  82.                 buttons={buttons}
  83.                 errorHandler={()=>{}}
  84.                 onInit={onInit}
  85.             />
  86.         </div>
  87.     );
  88. }
  89.  
  90. export default MoleculeEditor;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement