2018-07-15 05:26:26 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import * as React from "react";
|
|
|
|
import { withRouter, type RouterHistory } from "react-router-dom";
|
|
|
|
import { observable } from "mobx";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { lighten } from "polished";
|
|
|
|
import styled, { withTheme } from "styled-components";
|
|
|
|
import RichMarkdownEditor from "rich-markdown-editor";
|
|
|
|
import { uploadFile } from "utils/uploadFile";
|
|
|
|
import isInternalUrl from "utils/isInternalUrl";
|
|
|
|
import Tooltip from "components/Tooltip";
|
|
|
|
import UiStore from "stores/UiStore";
|
2020-07-17 04:26:23 +00:00
|
|
|
import embeds from "../embeds";
|
2018-07-15 05:26:26 +00:00
|
|
|
|
2020-05-20 03:39:34 +00:00
|
|
|
const EMPTY_ARRAY = [];
|
|
|
|
|
2018-07-15 05:26:26 +00:00
|
|
|
type Props = {
|
2020-01-16 17:42:42 +00:00
|
|
|
id: string,
|
2018-08-26 22:27:32 +00:00
|
|
|
defaultValue?: string,
|
2018-11-18 19:14:26 +00:00
|
|
|
readOnly?: boolean,
|
2019-12-19 05:00:36 +00:00
|
|
|
grow?: boolean,
|
2018-12-15 22:06:29 +00:00
|
|
|
disableEmbeds?: boolean,
|
2020-02-17 01:28:24 +00:00
|
|
|
history: RouterHistory,
|
2019-08-09 06:09:09 +00:00
|
|
|
forwardedRef: React.Ref<RichMarkdownEditor>,
|
|
|
|
ui: UiStore,
|
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) => {
|
2020-01-16 17:42:42 +00:00
|
|
|
const result = await uploadFile(file, { documentId: this.props.id });
|
2018-11-18 19:14:26 +00:00
|
|
|
return result.url;
|
|
|
|
};
|
|
|
|
|
|
|
|
onClickLink = (href: string) => {
|
|
|
|
// on page hash
|
2020-06-20 20:59:15 +00:00
|
|
|
if (href[0] === "#") {
|
2018-11-18 19:14:26 +00:00
|
|
|
window.location.href = href;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInternalUrl(href)) {
|
|
|
|
// relative
|
|
|
|
let navigateTo = href;
|
|
|
|
|
|
|
|
// probably absolute
|
2020-06-20 20:59:15 +00:00
|
|
|
if (href[0] !== "/") {
|
2018-11-18 19:14:26 +00:00
|
|
|
try {
|
|
|
|
const url = new URL(href);
|
|
|
|
navigateTo = url.pathname + url.hash;
|
|
|
|
} catch (err) {
|
|
|
|
navigateTo = href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 01:28:24 +00:00
|
|
|
this.props.history.push(navigateTo);
|
2018-11-18 19:14:26 +00:00
|
|
|
} else {
|
2020-06-20 20:59:15 +00:00
|
|
|
window.open(href, "_blank");
|
2018-11-18 19:14:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-07-15 05:26:26 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2020-05-20 03:39:34 +00:00
|
|
|
<StyledEditor
|
|
|
|
ref={this.props.forwardedRef}
|
|
|
|
uploadImage={this.onUploadImage}
|
|
|
|
onClickLink={this.onClickLink}
|
|
|
|
onShowToast={this.onShowToast}
|
|
|
|
embeds={this.props.disableEmbeds ? EMPTY_ARRAY : embeds}
|
|
|
|
tooltip={EditorTooltip}
|
|
|
|
{...this.props}
|
|
|
|
/>
|
2018-07-15 05:26:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
const StyledEditor = styled(RichMarkdownEditor)`
|
2020-01-03 06:49:40 +00:00
|
|
|
flex-grow: ${props => (props.grow ? 1 : 0)};
|
2019-07-08 02:25:45 +00:00
|
|
|
justify-content: start;
|
|
|
|
|
|
|
|
> div {
|
|
|
|
transition: ${props => props.theme.backgroundTransition};
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:03:14 +00:00
|
|
|
.notice-block.tip,
|
|
|
|
.notice-block.warning {
|
|
|
|
font-weight: 500;
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
p {
|
|
|
|
a {
|
|
|
|
color: ${props => props.theme.link};
|
|
|
|
border-bottom: 1px solid ${props => lighten(0.5, props.theme.link)};
|
2020-07-21 02:03:14 +00:00
|
|
|
text-decoration: none !important;
|
2019-07-08 02:25:45 +00:00
|
|
|
font-weight: 500;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
border-bottom: 1px solid ${props => props.theme.link};
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 04:17:25 +00:00
|
|
|
`;
|
|
|
|
|
2019-08-30 07:27:40 +00:00
|
|
|
const EditorTooltip = ({ children, ...props }) => (
|
|
|
|
<Tooltip offset="0, 16" delay={150} {...props}>
|
|
|
|
<span>{children}</span>
|
|
|
|
</Tooltip>
|
2019-07-10 04:17:25 +00:00
|
|
|
);
|
2019-07-04 04:32:21 +00:00
|
|
|
|
2020-02-27 06:29:22 +00:00
|
|
|
const EditorWithRouterAndTheme = withRouter(withTheme(Editor));
|
|
|
|
|
|
|
|
export default React.forwardRef((props, ref) => (
|
|
|
|
<EditorWithRouterAndTheme {...props} forwardedRef={ref} />
|
|
|
|
));
|