1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/client/src/components/CardModal/DescriptionEdit.jsx

154 lines
3.5 KiB
React
Raw Normal View History

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';
import { Button, Form } from 'semantic-ui-react';
import SimpleMDE from 'react-simplemde-editor';
import { useClickAwayListener } from '../../lib/hooks';
import { useNestedRef } from '../../hooks';
2019-08-31 04:07:25 +05:00
import styles from './DescriptionEdit.module.scss';
2019-08-31 04:07:25 +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
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);
}
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(() => {
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) => {
if (event.ctrlKey && event.key === 'Enter') {
close();
2019-08-31 04:07:25 +05:00
}
},
[close],
2019-08-31 04:07:25 +05:00
);
const handleSubmit = useCallback(() => {
close();
}, [close]);
2019-08-31 04:07:25 +05: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(
() => ({
autoDownloadFontAwesome: false,
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}>
{/* 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}>
<Button positive ref={handleButtonRef} content={t('action.save')} />
2019-08-31 04:07:25 +05:00
</div>
</Form>
);
});
DescriptionEdit.propTypes = {
2019-08-31 04:07:25 +05:00
children: PropTypes.element.isRequired,
defaultValue: PropTypes.string,
onUpdate: PropTypes.func.isRequired,
};
DescriptionEdit.defaultProps = {
2019-08-31 04:07:25 +05:00
defaultValue: undefined,
};
export default React.memo(DescriptionEdit);