2018-09-10 09:55:26 -07:00
|
|
|
import React from 'react';
|
2018-09-18 15:15:01 -07:00
|
|
|
import ReactMde, { ReactMdeTypes, ReactMdeCommands, ReactMdeProps } from 'react-mde';
|
2018-09-22 22:09:12 -07:00
|
|
|
import classnames from 'classnames';
|
2018-09-18 15:15:01 -07:00
|
|
|
import { convert, MARKDOWN_TYPE } from 'utils/markdown';
|
2018-09-22 22:09:12 -07:00
|
|
|
import './style.less';
|
2018-09-10 09:55:26 -07:00
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
const commands: { [key in MARKDOWN_TYPE]: ReactMdeProps['commands'] } = {
|
|
|
|
[MARKDOWN_TYPE.FULL]: [
|
|
|
|
[
|
|
|
|
ReactMdeCommands.headerCommand,
|
|
|
|
ReactMdeCommands.boldCommand,
|
|
|
|
ReactMdeCommands.italicCommand,
|
|
|
|
ReactMdeCommands.codeCommand,
|
|
|
|
ReactMdeCommands.strikethroughCommand,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
ReactMdeCommands.linkCommand,
|
|
|
|
ReactMdeCommands.quoteCommand,
|
|
|
|
ReactMdeCommands.imageCommand,
|
|
|
|
],
|
|
|
|
[ReactMdeCommands.orderedListCommand, ReactMdeCommands.unorderedListCommand],
|
|
|
|
],
|
|
|
|
[MARKDOWN_TYPE.REDUCED]: [
|
|
|
|
[
|
|
|
|
ReactMdeCommands.boldCommand,
|
|
|
|
ReactMdeCommands.italicCommand,
|
|
|
|
ReactMdeCommands.codeCommand,
|
|
|
|
ReactMdeCommands.strikethroughCommand,
|
|
|
|
],
|
|
|
|
[ReactMdeCommands.linkCommand, ReactMdeCommands.quoteCommand],
|
|
|
|
[ReactMdeCommands.orderedListCommand, ReactMdeCommands.unorderedListCommand],
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2018-09-10 09:55:26 -07:00
|
|
|
interface Props {
|
2019-02-08 09:14:16 -08:00
|
|
|
readOnly?: boolean | null;
|
2018-09-18 15:15:01 -07:00
|
|
|
type?: MARKDOWN_TYPE;
|
2018-09-20 11:58:47 -07:00
|
|
|
initialMarkdown?: string;
|
2018-09-10 09:55:26 -07:00
|
|
|
onChange(markdown: string): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2018-11-20 08:38:01 -08:00
|
|
|
randomKey: string;
|
2018-10-19 15:03:37 -07:00
|
|
|
mdeState: ReactMdeTypes.MdeState | null;
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class MarkdownEditor extends React.PureComponent<Props, State> {
|
2018-09-20 11:58:47 -07:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
const mdeState = props.initialMarkdown ? { markdown: props.initialMarkdown } : null;
|
2018-11-20 08:38:01 -08:00
|
|
|
this.state = {
|
|
|
|
mdeState,
|
|
|
|
randomKey: Math.random().toString(),
|
|
|
|
};
|
2018-09-20 11:58:47 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 08:38:01 -08:00
|
|
|
reset() {
|
|
|
|
this.setState({
|
|
|
|
randomKey: Math.random().toString(),
|
|
|
|
mdeState: null,
|
|
|
|
});
|
|
|
|
}
|
2018-09-10 09:55:26 -07:00
|
|
|
|
|
|
|
render() {
|
2018-09-18 15:15:01 -07:00
|
|
|
const type = this.props.type || MARKDOWN_TYPE.FULL;
|
2019-02-08 09:14:16 -08:00
|
|
|
const { readOnly } = this.props;
|
2018-11-20 08:38:01 -08:00
|
|
|
const { mdeState, randomKey } = this.state;
|
2018-09-10 09:55:26 -07:00
|
|
|
return (
|
2018-09-22 22:09:12 -07:00
|
|
|
<div
|
|
|
|
className={classnames({
|
|
|
|
MarkdownEditor: true,
|
|
|
|
['is-reduced']: type === MARKDOWN_TYPE.REDUCED,
|
|
|
|
})}
|
|
|
|
>
|
2018-09-13 14:05:36 -07:00
|
|
|
<ReactMde
|
2018-11-20 08:38:01 -08:00
|
|
|
key={randomKey}
|
2018-09-13 14:05:36 -07:00
|
|
|
onChange={this.handleChange}
|
2018-11-20 08:38:01 -08:00
|
|
|
editorState={mdeState as ReactMdeTypes.MdeState}
|
2018-09-13 14:05:36 -07:00
|
|
|
generateMarkdownPreview={this.generatePreview}
|
2018-09-18 15:15:01 -07:00
|
|
|
commands={commands[type]}
|
2019-02-08 09:14:16 -08:00
|
|
|
readOnly={!!readOnly}
|
2018-09-13 14:05:36 -07:00
|
|
|
layout="tabbed"
|
|
|
|
/>
|
2018-09-22 22:09:12 -07:00
|
|
|
</div>
|
2018-09-10 09:55:26 -07:00
|
|
|
);
|
|
|
|
}
|
2018-11-20 08:38:01 -08:00
|
|
|
|
|
|
|
private handleChange = (mdeState: ReactMdeTypes.MdeState) => {
|
|
|
|
this.setState({ mdeState });
|
|
|
|
this.props.onChange(mdeState.markdown || '');
|
|
|
|
};
|
|
|
|
|
|
|
|
private generatePreview = (md: string) => {
|
|
|
|
return Promise.resolve(convert(md, this.props.type));
|
|
|
|
};
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
2018-09-18 15:15:01 -07:00
|
|
|
|
|
|
|
export { MARKDOWN_TYPE } from 'utils/markdown';
|