Skip to content

Commit

Permalink
#3019 - Rerender template previews after options have changed
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoblit committed Aug 11, 2023
1 parent efbfc72 commit d6eaec0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/ketcher-core/src/application/render/renderStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,35 @@ export class RenderStruct {
el: HTMLElement | null,
struct: Struct | null,
options: any = {},
invalidateCache = false,
) {
if (el && struct) {
const { cachePrefix = '', needCache = true } = options;
const cacheKey = `${cachePrefix}${struct.name}`;

if (invalidateCache) {
renderCache.clear();
}

if (renderCache.has(cacheKey) && needCache) {
el.innerHTML = renderCache.get(cacheKey);
return;
}

const preparedStruct = this.prepareStruct(struct);
preparedStruct.initHalfBonds();
preparedStruct.initNeighbors();
preparedStruct.setImplicitHydrogen();
preparedStruct.markFragments();

const rnd = new Render(el, {
autoScale: true,
...options,
});

preparedStruct.rescale();
rnd.setMolecule(preparedStruct);

if (needCache) {
renderCache.set(cacheKey, rnd.clientArea.innerHTML);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ const normalizeStruct = (molV2000StringOrStruct: string | Struct) => {
}
};

const StructRender = ({ struct, options, className }: IStructRenderProps) => {
const StructRender = ({
struct,
options,
className,
invalidateCache,
}: IStructRenderProps) => {
const renderRef = useRef<HTMLDivElement>(null);

useEffect(() => {
Expand All @@ -40,9 +45,14 @@ const StructRender = ({ struct, options, className }: IStructRenderProps) => {
if (container) {
container.innerHTML = '';
const normalizedStruct = normalizeStruct(struct);
RenderStruct.render(container, normalizedStruct, options);
RenderStruct.render(
container,
normalizedStruct,
options,
invalidateCache,
);
}
}, [struct, options]);
}, [struct, options, invalidateCache]);

return <Container ref={renderRef} className={className}></Container>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export interface IStructRenderProps {
struct: Struct;
options?: RenderOptions & { cachePrefix?: string; needCache?: boolean };
className?: string;
invalidateCache?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Struct } from 'ketcher-core';
import classes from './TemplateTable.module.less';
import { greekify } from '../../utils';
import { Icon, StructRender } from 'components';
import { isEqual } from 'lodash';

export interface Template {
struct: Struct;
Expand Down Expand Up @@ -63,6 +64,8 @@ function tmplName(tmpl: Template, i: number): string {
return tmpl.struct.name || `${tmpl.props.group} template ${i + 1}`;
}

let previousRenderOptions: any;

const TemplateTable: FC<TemplateTableProps> = (props) => {
const {
templates,
Expand All @@ -81,6 +84,12 @@ const TemplateTable: FC<TemplateTableProps> = (props) => {
}`}
>
{templates.map((tmpl, i) => {
let invalidateRenderCache = false;
if (!isEqual(previousRenderOptions, renderOptions)) {
previousRenderOptions = renderOptions;
invalidateRenderCache = true;
}

return (
<div
className={
Expand All @@ -105,6 +114,7 @@ const TemplateTable: FC<TemplateTableProps> = (props) => {
cachePrefix: 'templates',
downScale: true,
}}
invalidateCache={invalidateRenderCache}
/>
<div
className={`${classes.structTitle} ${
Expand Down

0 comments on commit d6eaec0

Please sign in to comment.