* WIP * document migration * fix: Handle clashing keyboard events * fix: convert getSummary * fix: parseDocumentIds * lint * fix: Remove unused plugin * Move editor version to header Add editor version check for API endpoints * fix: Editor update auto-reload Bump RME * test * bump rme * Remove slate flow types, improve themeing, bump rme * bump rme * fix: parseDocumentIds returning duplicate ID's, improved regression tests * test * fix: Missing code styles * lint * chore: Upgrade v2 migration to use AST * Bump RME * Update welcome doc * add highlight to keyboard shortcuts ref * theming improvements * fix: Code comments show as headings, closes #1255 * loop * fix: TOC highlighting * lint * add: Automated backup of docs before migration * Update embeds to new format * fix: React warning * bump to final editor version 10.0.0 * test
117 lines
2.8 KiB
JavaScript
117 lines
2.8 KiB
JavaScript
// @flow
|
|
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';
|
|
import embeds from '../../embeds';
|
|
|
|
const EMPTY_ARRAY = [];
|
|
|
|
type Props = {
|
|
id: string,
|
|
defaultValue?: string,
|
|
readOnly?: boolean,
|
|
grow?: boolean,
|
|
disableEmbeds?: boolean,
|
|
history: RouterHistory,
|
|
forwardedRef: React.Ref<RichMarkdownEditor>,
|
|
ui: UiStore,
|
|
};
|
|
|
|
@observer
|
|
class Editor extends React.Component<Props> {
|
|
@observable redirectTo: ?string;
|
|
|
|
onUploadImage = async (file: File) => {
|
|
const result = await uploadFile(file, { documentId: this.props.id });
|
|
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;
|
|
}
|
|
}
|
|
|
|
this.props.history.push(navigateTo);
|
|
} else {
|
|
window.open(href, '_blank');
|
|
}
|
|
};
|
|
|
|
onShowToast = (message: string) => {
|
|
this.props.ui.showToast(message);
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<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}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
const StyledEditor = styled(RichMarkdownEditor)`
|
|
flex-grow: ${props => (props.grow ? 1 : 0)};
|
|
justify-content: start;
|
|
|
|
> div {
|
|
transition: ${props => props.theme.backgroundTransition};
|
|
}
|
|
|
|
p {
|
|
a {
|
|
color: ${props => props.theme.link};
|
|
border-bottom: 1px solid ${props => lighten(0.5, props.theme.link)};
|
|
font-weight: 500;
|
|
|
|
&:hover {
|
|
border-bottom: 1px solid ${props => props.theme.link};
|
|
text-decoration: none;
|
|
}
|
|
}
|
|
}
|
|
`;
|
|
|
|
const EditorTooltip = ({ children, ...props }) => (
|
|
<Tooltip offset="0, 16" delay={150} {...props}>
|
|
<span>{children}</span>
|
|
</Tooltip>
|
|
);
|
|
|
|
const EditorWithRouterAndTheme = withRouter(withTheme(Editor));
|
|
|
|
// $FlowIssue - https://github.com/facebook/flow/issues/6103
|
|
export default React.forwardRef((props, ref) => (
|
|
<EditorWithRouterAndTheme {...props} forwardedRef={ref} />
|
|
));
|