2022-11-20 20:48:42 +07:00
|
|
|
import React, { useCallback, useImperativeHandle, useState, useMemo } from 'react';
|
2019-08-31 04:07:25 +05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-11-20 20:48:42 +07:00
|
|
|
import { Button, Form } from 'semantic-ui-react';
|
|
|
|
import SimpleMDE from 'react-simplemde-editor';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
import { useClosableForm, useField } from '../../hooks';
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import styles from './DescriptionEdit.module.scss';
|
2022-11-20 20:48:42 +07:00
|
|
|
import 'easymde/dist/easymde.min.css';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
const DescriptionEdit = React.forwardRef(({ children, defaultValue, onUpdate }, ref) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const [isOpened, setIsOpened] = useState(false);
|
2022-11-20 20:48:42 +07:00
|
|
|
const [value, , setValue] = useField(null);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const open = useCallback(() => {
|
|
|
|
setIsOpened(true);
|
|
|
|
setValue(defaultValue || '');
|
|
|
|
}, [defaultValue, setValue]);
|
|
|
|
|
|
|
|
const close = useCallback(() => {
|
|
|
|
const cleanValue = value.trim() || null;
|
|
|
|
|
|
|
|
if (cleanValue !== defaultValue) {
|
|
|
|
onUpdate(cleanValue);
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:27:46 +05:00
|
|
|
setIsOpened(false);
|
|
|
|
setValue(null);
|
|
|
|
}, [defaultValue, onUpdate, value, setValue]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
useImperativeHandle(
|
|
|
|
ref,
|
|
|
|
() => ({
|
|
|
|
open,
|
|
|
|
close,
|
|
|
|
}),
|
|
|
|
[open, close],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleChildrenClick = useCallback(() => {
|
2019-10-03 03:02:46 +05:00
|
|
|
if (!getSelection().toString()) {
|
|
|
|
open();
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}, [open]);
|
|
|
|
|
|
|
|
const handleFieldKeyDown = useCallback(
|
2020-03-25 00:15:47 +05:00
|
|
|
(event) => {
|
2019-10-03 03:02:46 +05:00
|
|
|
if (event.ctrlKey && event.key === 'Enter') {
|
2020-04-30 05:27:46 +05:00
|
|
|
close();
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
},
|
2020-04-30 05:27:46 +05:00
|
|
|
[close],
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
2022-11-20 20:48:42 +07:00
|
|
|
const [, handleControlMouseOver, handleControlMouseOut] = useClosableForm(close, isOpened);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
2020-04-30 05:27:46 +05:00
|
|
|
close();
|
|
|
|
}, [close]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2022-11-20 20:48:42 +07:00
|
|
|
const mdEditorOptions = useMemo(() => {
|
|
|
|
return {
|
|
|
|
autofocus: true,
|
|
|
|
spellChecker: false,
|
|
|
|
status: false,
|
|
|
|
toolbar: [
|
|
|
|
'bold',
|
|
|
|
'italic',
|
|
|
|
'heading',
|
|
|
|
'strikethrough',
|
|
|
|
'|',
|
|
|
|
'quote',
|
|
|
|
'unordered-list',
|
|
|
|
'ordered-list',
|
|
|
|
'table',
|
|
|
|
'|',
|
|
|
|
'link',
|
|
|
|
'image',
|
|
|
|
'|',
|
|
|
|
'fullscreen',
|
|
|
|
'|',
|
|
|
|
'undo',
|
|
|
|
'redo',
|
|
|
|
'|',
|
|
|
|
'guide',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}, []);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!isOpened) {
|
|
|
|
return React.cloneElement(children, {
|
|
|
|
onClick: handleChildrenClick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
2022-11-20 20:48:42 +07:00
|
|
|
<SimpleMDE
|
2019-08-31 04:07:25 +05:00
|
|
|
value={value}
|
|
|
|
placeholder={t('common.enterDescription')}
|
|
|
|
className={styles.field}
|
2022-11-20 20:48:42 +07:00
|
|
|
options={mdEditorOptions}
|
2019-08-31 04:07:25 +05:00
|
|
|
onKeyDown={handleFieldKeyDown}
|
2022-11-20 20:48:42 +07:00
|
|
|
onChange={setValue}
|
2019-08-31 04:07:25 +05:00
|
|
|
/>
|
2022-11-20 20:48:42 +07:00
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
<div className={styles.controls}>
|
|
|
|
{/* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */}
|
|
|
|
<Button
|
|
|
|
positive
|
|
|
|
content={t('action.save')}
|
|
|
|
onMouseOver={handleControlMouseOver}
|
|
|
|
onMouseOut={handleControlMouseOut}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
DescriptionEdit.propTypes = {
|
2019-08-31 04:07:25 +05:00
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
defaultValue: PropTypes.string,
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
DescriptionEdit.defaultProps = {
|
2019-08-31 04:07:25 +05:00
|
|
|
defaultValue: undefined,
|
|
|
|
};
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
export default React.memo(DescriptionEdit);
|