Skip to content

Commit

Permalink
#4965 - fixed tool reset on view only enabling
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-sloboda committed Sep 10, 2024
1 parent 10fd996 commit 1766e08
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/ketcher-react/src/script/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class Editor implements KetcherEditor {
private updateToolAfterOptionsChange(wasViewOnlyEnabled: boolean) {
const isViewOnlyEnabled = this.render.options.viewOnlyMode;
if (!wasViewOnlyEnabled && isViewOnlyEnabled === true) {
this.tool('select');
this.event.change.dispatch('force');
}
}

Expand Down
32 changes: 18 additions & 14 deletions packages/ketcher-react/src/script/ui/state/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,31 @@ export default function initEditor(dispatch, getState) {
const updateAction = debounce(100, () => dispatch({ type: 'UPDATE' }));
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));

function resetToSelect(dispatch) {
// eslint-disable-line no-shadow
const state = global.currentState;
const activeTool = state.actionState.activeTool.tool;
if (activeTool === 'select') return;
const selectMode = state.toolbar.visibleTools.select;
const resetOption = state.options.settings.resetToSelect;
if (resetOption === true || resetOption === activeTool)
// example: 'paste'
dispatch({ type: 'ACTION', action: acts[selectMode].action });
else updateAction();
}
const resetToSelect =
(force = false) =>
(dispatch) => {
// eslint-disable-line no-shadow
const state = global.currentState;
const activeTool = state.actionState.activeTool.tool;
if (activeTool === 'select') return;
const selectMode = state.toolbar.visibleTools.select;
const resetOption = state.options.settings.resetToSelect;
if (resetOption === true || resetOption === activeTool || force === true)
// example: 'paste'
dispatch({ type: 'ACTION', action: acts[selectMode].action });
else updateAction();
};

return {
onInit: (editor) => {
dispatch({ type: 'INIT', editor });
},
onChange: (action) => {
if (action === undefined) sleep(0).then(() => dispatch(resetToSelect));
if (action === undefined) sleep(0).then(() => dispatch(resetToSelect()));
// Editor switched to view only mode
if (action === 'force') dispatch(resetToSelect(true));
// new tool in reducer
else dispatch(resetToSelect);
else dispatch(resetToSelect());
},
onSelectionChange: () => {
updateAction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Recognize from '../../process/Recognize/Recognize';
import { fileOpener } from '../../../../../utils/';
import { DialogActionButton } from './components/DialogActionButton';
import { ViewSwitcher } from './components/ViewSwitcher';
import { getFormatMimeTypeByFileName } from 'ketcher-core';
import { getFormatMimeTypeByFileName, ketcherProvider } from 'ketcher-core';
interface OpenProps {
server: any;
errorHandler: (err: string) => void;
Expand All @@ -42,7 +42,13 @@ const MODAL_STATES = {
presentationViewer: 'presentationViewer',
};

const FooterContent = ({ structStr, openHandler, copyHandler, onCancel }) => {
const FooterContent = ({
structStr,
openHandler,
copyHandler,
onCancel,
isAddToCanvasDisabled,
}) => {
return (
<div className={classes.footerContent}>
<button onClick={onCancel} className={classes.cancelButton}>
Expand All @@ -58,7 +64,7 @@ const FooterContent = ({ structStr, openHandler, copyHandler, onCancel }) => {
/>
<DialogActionButton
key="copyButton"
disabled={!structStr}
disabled={!structStr || isAddToCanvasDisabled}
clickHandler={copyHandler}
styles={classes.copyButton}
label="Add to Canvas"
Expand Down Expand Up @@ -88,6 +94,7 @@ const Open: FC<Props> = (props) => {
const [opener, setOpener] = useState<any>();
const [currentState, setCurrentState] = useState(MODAL_STATES.idle);
const [isLoading, setIsLoading] = useState(false);
const ketcher = ketcherProvider.getKetcher();

useEffect(() => {
if (server) {
Expand Down Expand Up @@ -162,6 +169,7 @@ const Open: FC<Props> = (props) => {
openHandler={openHandler}
copyHandler={copyHandler}
onCancel={rest.onCancel}
isAddToCanvasDisabled={ketcher.editor.render.options.viewOnlyMode}
/>
) : undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ import { range } from 'lodash/fp';
import { recognize } from '../../../../../state/server';
import { DialogActionButton } from 'src/script/ui/views/modal/components/document/Open/components/DialogActionButton';
import { Icon, StructRender } from 'components';
import { ketcherProvider } from 'ketcher-core';

function isImage(file) {
return file?.type?.includes('image');
}

function FooterContent({ onImage, structStr, openHandler, copyHandler }) {
function FooterContent({
onImage,
structStr,
openHandler,
copyHandler,
isAddToCanvasDisabled,
}) {
return (
<div className={classes.footerContent}>
<OpenButton
Expand All @@ -59,7 +66,7 @@ function FooterContent({ onImage, structStr, openHandler, copyHandler }) {
/>
<DialogActionButton
key="copyButton"
disabled={!structStr}
disabled={!structStr || isAddToCanvasDisabled}
clickHandler={copyHandler}
styles={classes.primaryButton}
label="Add to Canvas"
Expand Down Expand Up @@ -94,6 +101,7 @@ function RecognizeDialog(prop) {
structStr && !(structStr instanceof Promise)
? { structStr, fragment }
: null;
const ketcher = ketcherProvider.getKetcher();

useEffect(() => {
onRecognize(file, version);
Expand Down Expand Up @@ -126,6 +134,7 @@ function RecognizeDialog(prop) {
openHandler={openHandler}
structStr={structStr}
copyHandler={copyHandler}
isAddToCanvasDisabled={ketcher.editor.render.options.viewOnlyMode}
/>
}
buttons={[]}
Expand Down

0 comments on commit 1766e08

Please sign in to comment.