Skip to content

Commit

Permalink
#3393 - Add new keyboard shortcut "ctrl+alt+V" for pasting as SMARTS
Browse files Browse the repository at this point in the history
  • Loading branch information
AKZhuk committed Oct 12, 2023
1 parent 4925269 commit fbbf4a3
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,17 @@ function getPropertiesByFormat(format: SupportedFormat) {
return formatProperties[format];
}

export { formatProperties, getPropertiesByFormat, getPropertiesByImgFormat };
function getFormatMimeTypeByFileName(fileName: string) {
const fileExtension = '.' + fileName.split('.').pop();
const format = Object.values(formatProperties).find((properties) => {
return properties.extensions.includes(fileExtension);
});
return format?.mime;
}

export {
formatProperties,
getPropertiesByFormat,
getPropertiesByImgFormat,
getFormatMimeTypeByFileName,
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Component, createRef } from 'react';
import clsx from 'clsx';
import classes from './cliparea.module.less';
import { KetcherLogger } from 'ketcher-core';
import { isControlKey } from '../../data/convert/keynorm';

const ieCb = window.clipboardData;

Expand Down Expand Up @@ -73,6 +74,17 @@ class ClipArea extends Component {
event.preventDefault();
}
},
keydown: async (event) => {
if (this.props.focused() && this.props.onPaste) {
if (isControlKey(event) && event.altKey && event.code === 'KeyV') {
const clipboardData = await navigator.clipboard.read();
const data = await pasteByKeydown(clipboardData);
if (data) {
this.props.onPaste(data, true);
}
}
}
},
};

Object.keys(this.listeners).forEach((en) => {
Expand Down Expand Up @@ -148,6 +160,19 @@ function paste(cb, formats) {
return data;
}

async function pasteByKeydown(cb) {
const data = {};
if (!cb && ieCb) {
data['text/plain'] = ieCb.getData('text');
} else {
for (const item of cb) {
const textPlain = await item.getType('text/plain');
data['text/plain'] = await textPlain.text();
}
}
return data;
}

export const actions = ['cut', 'copy', 'paste'];

export function exec(action) {
Expand Down
4 changes: 4 additions & 0 deletions packages/ketcher-react/src/script/ui/data/convert/keynorm.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ function normalizeKeyEvent(event, base = false) {
: modifiers(KN.base[event.keyCode], event, true);
}

export function isControlKey(event) {
return mac ? event.metaKey : event.ctrlKey;
}

function keyNorm(obj) {
if (obj instanceof KeyboardEvent)
// eslint-disable-line no-undef
Expand Down
10 changes: 8 additions & 2 deletions packages/ketcher-react/src/script/ui/state/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,21 @@ export function initClipboard(dispatch) {
editor.selection(null);
return data;
},
onPaste(data) {
onPaste(data, isSmarts: boolean) {
const structStr =
data[ChemicalMimeType.KET] ||
data[ChemicalMimeType.Mol] ||
data[ChemicalMimeType.Rxn] ||
data['text/plain'];

if (structStr || !rxnTextPlain.test(data['text/plain']))
loadStruct(structStr, { fragment: true, isPaste: true });
isSmarts
? loadStruct(structStr, {
fragment: true,
isPaste: true,
'input-format': ChemicalMimeType.DaylightSmarts,
})
: loadStruct(structStr, { fragment: true, isPaste: true });
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const mapDispatchToProps = (dispatch): DispatchProps => ({
load(result.structStr, {
badHeaderRecover: true,
fragment: result.fragment,
'input-format': result['input-format'],
}),
// TODO: Removed ownProps.onOk call. consider refactoring of load function in release 2.4
// See PR #731 (https://github.com/epam/ketcher/pull/731)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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';

interface OpenProps {
server: any;
Expand Down Expand Up @@ -115,7 +116,8 @@ const Open: FC<Props> = (props) => {
const { onOk } = rest;

const copyHandler = () => {
onOk({ structStr, fragment: true });
const format = getFormatMimeTypeByFileName(fileName);
onOk({ structStr, fragment: true, 'input-format': format });
};

const openHandler = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ self.onmessage = (e: MessageEvent<InputMessage<CommandData>>) => {
case Command.Layout: {
const data: LayoutCommandData = message.data as LayoutCommandData;
handle(
(indigo, indigoOptions) =>
indigo.layout(data.struct, data.format, indigoOptions),
(indigo, indigoOptions) => {
const response = indigo.layout(
data.struct,
data.format,
indigoOptions,
);
const { struct } = JSON.parse(response);
return struct;
},
data.options,
Command.Layout,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ class IndigoService implements StructService {
reject(msg.error);
}
};

if (options?.['input-format']) {
delete options['input-format'];
}
const commandOptions: CommandOptions = {
...this.defaultOptions,
...options,
Expand Down Expand Up @@ -315,6 +317,7 @@ class IndigoService implements StructService {
const commandOptions: CommandOptions = {
...this.defaultOptions,
...options,
'output-content-type': 'application/json',
};

const commandData: LayoutCommandData = {
Expand Down

0 comments on commit fbbf4a3

Please sign in to comment.