Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2655 - Add dynamic load for Miew (#2661) #2668

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ketcher-react/src/script/editor/tool/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class TemplateTool {
})
}

async mousedown(event) {
async mousedown(event: MouseEvent) {
this.event = event

if (this.functionalGroups.size) {
Expand Down Expand Up @@ -187,7 +187,7 @@ class TemplateTool {
this.editor.hover(null)

this.dragCtx = {
xy0: this.editor.render.page2obj(this.event),
xy0: this.editor.render.page2obj(event),
item: this.editor.findItem(this.event, this.findItems)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
}
}

.loadingContainer {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}

.miewDarkTheme {
// stylelint-disable-next-line selector-pseudo-class-no-unknown
:global(.atom-info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
***************************************************************************/

import { Dialog } from '../../../../components'
import { lazy, Suspense, useCallback, useRef, useState } from 'react'
import { Dialog, LoadingCircles } from '../../../../components'
import {
FormatterFactory,
Struct,
Expand All @@ -26,9 +27,9 @@ import classes from './Miew.module.less'
import { connect } from 'react-redux'
import { load } from '../../../../../state'
import { pick } from 'lodash/fp'
import Viewer from 'miew-react'
import { Miew as MiewAsType } from 'miew'
import { useCallback, useRef } from 'react'

const Viewer = lazy(() => import('miew-react'))

type MiewDialogProps = {
miewOpts: any
Expand Down Expand Up @@ -117,6 +118,7 @@ const MiewDialog = ({
...prop
}: Props) => {
const miewRef = useRef<MiewAsType>()
const [isInitialized, setIsIsInitialized] = useState(false)

const onMiewInit = useCallback(
(miew: MiewAsType) => {
Expand All @@ -129,7 +131,10 @@ const MiewDialog = ({
.then((res) =>
miew.load(res, { sourceType: 'immediate', fileType: 'cml' })
)
.then(() => miew.setOptions(miewOpts))
.then(() => {
miew.setOptions(miewOpts)
setIsIsInitialized(true)
})
.catch((ex) => console.error(ex.message))
},
[miewOpts, server, struct]
Expand All @@ -150,7 +155,12 @@ const MiewDialog = ({
params={prop}
buttons={[
'Cancel',
<button key="apply" onClick={exportCML} className={classes.applyButton}>
<button
key="apply"
onClick={exportCML}
className={classes.applyButton}
disabled={!isInitialized}
>
Apply
</button>
]}
Expand All @@ -163,7 +173,15 @@ const MiewDialog = ({
miewTheme === 'dark' ? classes.miewDarkTheme : ''
}`}
>
<Viewer onInit={onMiewInit} />
<Suspense
fallback={
<div className={classes.loadingContainer}>
<LoadingCircles />
</div>
}
>
<Viewer onInit={onMiewInit} />
</Suspense>
</div>
</div>
</Dialog>
Expand Down