2024-10-31 14:56:11 +01:00
|
|
|
import React, { useCallback, useImperativeHandle, useMemo, useRef, useState } 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';
|
2024-10-31 14:56:11 +01:00
|
|
|
import { useClickAwayListener } from '../../lib/hooks';
|
|
|
|
|
|
|
|
import { useNestedRef } from '../../hooks';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import styles from './DescriptionEdit.module.scss';
|
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 15:05:21 +01:00
|
|
|
const [value, setValue] = useState(null);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2024-10-31 14:56:11 +01:00
|
|
|
const editorWrapperRef = useRef(null);
|
|
|
|
const codemirrorRef = useRef(null);
|
|
|
|
const [buttonRef, handleButtonRef] = useNestedRef();
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
2020-04-30 05:27:46 +05:00
|
|
|
close();
|
|
|
|
}, [close]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2024-10-31 14:56:11 +01:00
|
|
|
const handleAwayClick = useCallback(() => {
|
|
|
|
if (!isOpened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
close();
|
|
|
|
}, [isOpened, close]);
|
|
|
|
|
|
|
|
const handleClickAwayCancel = useCallback(() => {
|
|
|
|
codemirrorRef.current.focus();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const clickAwayProps = useClickAwayListener(
|
|
|
|
[editorWrapperRef, buttonRef],
|
|
|
|
handleAwayClick,
|
|
|
|
handleClickAwayCancel,
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleGetCodemirrorInstance = useCallback((codemirror) => {
|
|
|
|
codemirrorRef.current = codemirror;
|
|
|
|
}, []);
|
|
|
|
|
2022-11-20 15:05:21 +01:00
|
|
|
const mdEditorOptions = useMemo(
|
|
|
|
() => ({
|
2024-05-16 21:02:58 +02:00
|
|
|
autoDownloadFontAwesome: false,
|
2022-11-20 20:48:42 +07:00
|
|
|
autofocus: true,
|
|
|
|
spellChecker: false,
|
|
|
|
status: false,
|
|
|
|
toolbar: [
|
|
|
|
'bold',
|
|
|
|
'italic',
|
|
|
|
'heading',
|
|
|
|
'strikethrough',
|
|
|
|
'|',
|
|
|
|
'quote',
|
|
|
|
'unordered-list',
|
|
|
|
'ordered-list',
|
|
|
|
'table',
|
|
|
|
'|',
|
|
|
|
'link',
|
|
|
|
'image',
|
|
|
|
'|',
|
|
|
|
'undo',
|
|
|
|
'redo',
|
|
|
|
'|',
|
|
|
|
'guide',
|
|
|
|
],
|
2022-11-20 15:05:21 +01:00
|
|
|
}),
|
|
|
|
[],
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!isOpened) {
|
|
|
|
return React.cloneElement(children, {
|
|
|
|
onClick: handleChildrenClick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
2024-10-31 14:56:11 +01:00
|
|
|
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
|
|
|
|
<div {...clickAwayProps} ref={editorWrapperRef}>
|
|
|
|
<SimpleMDE
|
|
|
|
value={value}
|
|
|
|
options={mdEditorOptions}
|
|
|
|
placeholder={t('common.enterDescription')}
|
|
|
|
className={styles.field}
|
|
|
|
getCodemirrorInstance={handleGetCodemirrorInstance}
|
|
|
|
onKeyDown={handleFieldKeyDown}
|
|
|
|
onChange={setValue}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-08-31 04:07:25 +05:00
|
|
|
<div className={styles.controls}>
|
2024-10-31 14:56:11 +01:00
|
|
|
<Button positive ref={handleButtonRef} content={t('action.save')} />
|
2019-08-31 04:07:25 +05:00
|
|
|
</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);
|