// @flow import { lighten } from "polished"; import * as React from "react"; import { withRouter, type RouterHistory } from "react-router-dom"; import styled, { withTheme } from "styled-components"; import UiStore from "stores/UiStore"; import ErrorBoundary from "components/ErrorBoundary"; import Tooltip from "components/Tooltip"; import embeds from "../embeds"; import isInternalUrl from "utils/isInternalUrl"; import { uploadFile } from "utils/uploadFile"; const RichMarkdownEditor = React.lazy(() => import("rich-markdown-editor")); const EMPTY_ARRAY = []; type Props = { id?: string, defaultValue?: string, readOnly?: boolean, grow?: boolean, disableEmbeds?: boolean, ui?: UiStore, }; type PropsWithRef = Props & { forwardedRef: React.Ref, history: RouterHistory, }; class Editor extends React.Component { onUploadImage = async (file: File) => { const result = await uploadFile(file, { documentId: this.props.id }); return result.url; }; onClickLink = (href: string, event: MouseEvent) => { // on page hash if (href[0] === "#") { window.location.href = href; return; } if (isInternalUrl(href) && !event.metaKey && !event.shiftKey) { // 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 if (href) { window.open(href, "_blank"); } }; onShowToast = (message: string) => { if (this.props.ui) { this.props.ui.showToast(message); } }; render() { return ( ); } } const StyledEditor = styled(RichMarkdownEditor)` flex-grow: ${(props) => (props.grow ? 1 : 0)}; justify-content: start; > div { transition: ${(props) => props.theme.backgroundTransition}; } .notice-block.tip, .notice-block.warning { font-weight: 500; } .heading-name { cursor: default; } /* pseudo element allows us to add spacing for fixed header */ /* ref: https://stackoverflow.com/a/28824157 */ .heading-name::before { content: ""; display: ${(props) => (props.readOnly ? "block" : "none")}; height: 72px; margin: -72px 0 0; } .heading-anchor { margin-top: 72px !important; } p { a { color: ${(props) => props.theme.text}; border-bottom: 1px solid ${(props) => lighten(0.5, props.theme.text)}; text-decoration: none !important; font-weight: 500; &:hover { border-bottom: 1px solid ${(props) => props.theme.text}; text-decoration: none; } } } `; const EditorTooltip = ({ children, ...props }) => ( {children} ); const Span = styled.span` outline: none; `; const EditorWithRouterAndTheme = withRouter(withTheme(Editor)); export default React.forwardRef((props, ref) => ( ));