2018-07-15 05:26:26 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2019-01-19 08:23:39 +00:00
|
|
|
import { Redirect } from 'react-router-dom';
|
|
|
|
import { observable } from 'mobx';
|
|
|
|
import { observer } from 'mobx-react';
|
2019-03-13 04:35:35 +00:00
|
|
|
import { withTheme } from 'styled-components';
|
2018-11-19 02:28:22 +00:00
|
|
|
import RichMarkdownEditor from 'rich-markdown-editor';
|
2018-11-18 19:14:26 +00:00
|
|
|
import { uploadFile } from 'utils/uploadFile';
|
|
|
|
import isInternalUrl from 'utils/isInternalUrl';
|
2018-12-15 22:06:29 +00:00
|
|
|
import Embed from './Embed';
|
|
|
|
import embeds from '../../embeds';
|
2018-07-15 05:26:26 +00:00
|
|
|
|
|
|
|
type Props = {
|
2018-08-26 22:27:32 +00:00
|
|
|
defaultValue?: string,
|
2018-11-18 19:14:26 +00:00
|
|
|
readOnly?: boolean,
|
2018-12-15 22:06:29 +00:00
|
|
|
disableEmbeds?: boolean,
|
2018-11-19 06:03:10 +00:00
|
|
|
forwardedRef: *,
|
2018-11-18 19:14:26 +00:00
|
|
|
ui: *,
|
2018-07-15 05:26:26 +00:00
|
|
|
};
|
|
|
|
|
2019-01-19 08:23:39 +00:00
|
|
|
@observer
|
2018-07-15 05:26:26 +00:00
|
|
|
class Editor extends React.Component<Props> {
|
2019-01-19 08:23:39 +00:00
|
|
|
@observable redirectTo: ?string;
|
|
|
|
|
2018-11-18 19:14:26 +00:00
|
|
|
onUploadImage = async (file: File) => {
|
|
|
|
const result = await uploadFile(file);
|
|
|
|
return result.url;
|
|
|
|
};
|
|
|
|
|
|
|
|
onClickLink = (href: string) => {
|
|
|
|
// on page hash
|
|
|
|
if (href[0] === '#') {
|
|
|
|
window.location.href = href;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInternalUrl(href)) {
|
|
|
|
// relative
|
|
|
|
let navigateTo = href;
|
|
|
|
|
|
|
|
// probably absolute
|
|
|
|
if (href[0] !== '/') {
|
|
|
|
try {
|
|
|
|
const url = new URL(href);
|
|
|
|
navigateTo = url.pathname + url.hash;
|
|
|
|
} catch (err) {
|
|
|
|
navigateTo = href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 08:23:39 +00:00
|
|
|
this.redirectTo = navigateTo;
|
2018-11-18 19:14:26 +00:00
|
|
|
} else {
|
|
|
|
window.open(href, '_blank');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onShowToast = (message: string) => {
|
2019-04-18 02:11:23 +00:00
|
|
|
this.props.ui.showToast(message);
|
2018-11-18 19:14:26 +00:00
|
|
|
};
|
|
|
|
|
2018-12-15 22:06:29 +00:00
|
|
|
getLinkComponent = node => {
|
|
|
|
if (this.props.disableEmbeds) return;
|
|
|
|
|
|
|
|
const url = node.data.get('href');
|
|
|
|
const keys = Object.keys(embeds);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
const component = embeds[key];
|
|
|
|
|
|
|
|
for (const host of component.ENABLED) {
|
|
|
|
const matches = url.match(host);
|
|
|
|
if (matches) return Embed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-15 05:26:26 +00:00
|
|
|
render() {
|
2019-01-26 13:01:33 +00:00
|
|
|
if (this.redirectTo) return <Redirect to={this.redirectTo} push />;
|
2019-01-19 08:23:39 +00:00
|
|
|
|
2018-07-15 05:26:26 +00:00
|
|
|
return (
|
2018-11-19 02:28:22 +00:00
|
|
|
<RichMarkdownEditor
|
2018-11-19 06:03:10 +00:00
|
|
|
ref={this.props.forwardedRef}
|
2018-11-19 02:28:22 +00:00
|
|
|
uploadImage={this.onUploadImage}
|
|
|
|
onClickLink={this.onClickLink}
|
|
|
|
onShowToast={this.onShowToast}
|
2018-12-15 22:06:29 +00:00
|
|
|
getLinkComponent={this.getLinkComponent}
|
2018-11-19 02:28:22 +00:00
|
|
|
{...this.props}
|
|
|
|
/>
|
2018-07-15 05:26:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 04:35:35 +00:00
|
|
|
export default withTheme(
|
|
|
|
// $FlowIssue - https://github.com/facebook/flow/issues/6103
|
|
|
|
React.forwardRef((props, ref) => <Editor {...props} forwardedRef={ref} />)
|
|
|
|
);
|