2018-11-19 02:28:22 +00:00
|
|
|
// @flow
|
2020-07-17 04:26:23 +00:00
|
|
|
import { observable } from "mobx";
|
2021-07-22 22:17:18 +00:00
|
|
|
import { inject, observer } from "mobx-react";
|
2020-08-09 05:53:59 +00:00
|
|
|
import * as React from "react";
|
2021-07-19 18:12:53 +00:00
|
|
|
import { type TFunction, withTranslation } from "react-i18next";
|
2021-07-22 22:17:18 +00:00
|
|
|
import PoliciesStore from "stores/PoliciesStore";
|
2020-06-20 20:59:15 +00:00
|
|
|
import Document from "models/Document";
|
2020-08-09 05:53:59 +00:00
|
|
|
import ClickablePadding from "components/ClickablePadding";
|
2020-08-12 04:01:03 +00:00
|
|
|
import DocumentMetaWithViews from "components/DocumentMetaWithViews";
|
2021-01-30 05:36:09 +00:00
|
|
|
import Editor, { type Props as EditorProps } from "components/Editor";
|
2020-08-09 05:53:59 +00:00
|
|
|
import Flex from "components/Flex";
|
|
|
|
import HoverPreview from "components/HoverPreview";
|
2021-11-09 04:52:17 +00:00
|
|
|
import EditableTitle from "./EditableTitle";
|
2021-09-11 05:46:57 +00:00
|
|
|
import MultiplayerEditor from "./MultiplayerEditor";
|
2020-08-12 04:01:03 +00:00
|
|
|
import { documentHistoryUrl } from "utils/routeHelpers";
|
2018-11-19 02:28:22 +00:00
|
|
|
|
2021-01-30 05:36:09 +00:00
|
|
|
type Props = {|
|
|
|
|
...EditorProps,
|
2021-11-09 04:52:17 +00:00
|
|
|
onChangeTitle: (text: string) => void,
|
2020-04-05 22:07:34 +00:00
|
|
|
title: string,
|
|
|
|
document: Document,
|
|
|
|
isDraft: boolean,
|
2021-05-23 02:34:05 +00:00
|
|
|
shareId: ?string,
|
2021-09-11 05:46:57 +00:00
|
|
|
multiplayer?: boolean,
|
2021-01-30 05:36:09 +00:00
|
|
|
onSave: ({ done?: boolean, autosave?: boolean, publish?: boolean }) => any,
|
2020-09-01 01:31:13 +00:00
|
|
|
innerRef: { current: any },
|
2021-04-06 00:19:31 +00:00
|
|
|
children: React.Node,
|
2021-07-22 22:17:18 +00:00
|
|
|
policies: PoliciesStore,
|
2021-07-19 18:12:53 +00:00
|
|
|
t: TFunction,
|
2021-01-30 05:36:09 +00:00
|
|
|
|};
|
2018-11-19 02:28:22 +00:00
|
|
|
|
2020-04-05 22:07:34 +00:00
|
|
|
@observer
|
2018-11-19 02:28:22 +00:00
|
|
|
class DocumentEditor extends React.Component<Props> {
|
2020-07-17 04:26:23 +00:00
|
|
|
@observable activeLinkEvent: ?MouseEvent;
|
2021-07-05 20:35:46 +00:00
|
|
|
ref = React.createRef<HTMLDivElement | HTMLInputElement>();
|
2018-11-19 02:28:22 +00:00
|
|
|
|
|
|
|
focusAtStart = () => {
|
2020-09-01 01:31:13 +00:00
|
|
|
if (this.props.innerRef.current) {
|
|
|
|
this.props.innerRef.current.focusAtStart();
|
2020-02-27 06:29:22 +00:00
|
|
|
}
|
2018-11-19 02:28:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
focusAtEnd = () => {
|
2020-09-01 01:31:13 +00:00
|
|
|
if (this.props.innerRef.current) {
|
|
|
|
this.props.innerRef.current.focusAtEnd();
|
2020-02-27 06:29:22 +00:00
|
|
|
}
|
2018-11-19 02:28:22 +00:00
|
|
|
};
|
|
|
|
|
2020-09-04 20:23:27 +00:00
|
|
|
insertParagraph = () => {
|
|
|
|
if (this.props.innerRef.current) {
|
|
|
|
const { view } = this.props.innerRef.current;
|
|
|
|
const { dispatch, state } = view;
|
|
|
|
dispatch(state.tr.insert(0, state.schema.nodes.paragraph.create()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-17 04:26:23 +00:00
|
|
|
handleLinkActive = (event: MouseEvent) => {
|
|
|
|
this.activeLinkEvent = event;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleLinkInactive = () => {
|
|
|
|
this.activeLinkEvent = null;
|
|
|
|
};
|
|
|
|
|
2021-11-09 04:52:17 +00:00
|
|
|
handleGoToNextInput = (insertParagraph: boolean) => {
|
|
|
|
if (insertParagraph) {
|
|
|
|
this.insertParagraph();
|
|
|
|
}
|
|
|
|
this.focusAtStart();
|
|
|
|
};
|
|
|
|
|
2018-11-19 02:28:22 +00:00
|
|
|
render() {
|
2020-07-17 04:26:23 +00:00
|
|
|
const {
|
|
|
|
document,
|
|
|
|
title,
|
|
|
|
onChangeTitle,
|
|
|
|
isDraft,
|
2021-05-23 02:34:05 +00:00
|
|
|
shareId,
|
2020-07-17 04:26:23 +00:00
|
|
|
readOnly,
|
2020-09-01 01:31:13 +00:00
|
|
|
innerRef,
|
2021-04-06 00:19:31 +00:00
|
|
|
children,
|
2021-07-22 22:17:18 +00:00
|
|
|
policies,
|
2021-09-11 05:46:57 +00:00
|
|
|
multiplayer,
|
2021-07-19 18:12:53 +00:00
|
|
|
t,
|
2021-01-30 05:36:09 +00:00
|
|
|
...rest
|
2020-07-17 04:26:23 +00:00
|
|
|
} = this.props;
|
2021-01-11 07:13:58 +00:00
|
|
|
|
2021-09-11 05:46:57 +00:00
|
|
|
const EditorComponent = multiplayer ? MultiplayerEditor : Editor;
|
2018-11-19 02:28:22 +00:00
|
|
|
|
|
|
|
return (
|
2020-04-06 01:43:55 +00:00
|
|
|
<Flex auto column>
|
2021-11-09 04:52:17 +00:00
|
|
|
<EditableTitle
|
|
|
|
value={title}
|
|
|
|
readOnly={readOnly}
|
|
|
|
document={document}
|
|
|
|
onGoToNextInput={this.handleGoToNextInput}
|
|
|
|
onChange={onChangeTitle}
|
|
|
|
/>
|
2021-06-14 01:24:02 +00:00
|
|
|
{!shareId && (
|
|
|
|
<DocumentMetaWithViews
|
|
|
|
isDraft={isDraft}
|
|
|
|
document={document}
|
|
|
|
to={documentHistoryUrl(document)}
|
2021-07-03 14:00:10 +00:00
|
|
|
rtl={
|
|
|
|
this.ref.current
|
|
|
|
? window.getComputedStyle(this.ref.current).direction === "rtl"
|
|
|
|
: false
|
|
|
|
}
|
2021-06-14 01:24:02 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-09-11 05:46:57 +00:00
|
|
|
<EditorComponent
|
2020-09-01 01:31:13 +00:00
|
|
|
ref={innerRef}
|
2021-01-30 05:36:09 +00:00
|
|
|
autoFocus={!!title && !this.props.defaultValue}
|
2021-07-19 18:12:53 +00:00
|
|
|
placeholder={t("…the rest is up to you")}
|
2020-09-01 01:31:13 +00:00
|
|
|
onHoverLink={this.handleLinkActive}
|
|
|
|
scrollTo={window.location.hash}
|
2021-01-30 07:59:48 +00:00
|
|
|
readOnly={readOnly}
|
2021-05-23 02:34:05 +00:00
|
|
|
shareId={shareId}
|
2020-09-01 01:31:13 +00:00
|
|
|
grow
|
2021-01-30 05:36:09 +00:00
|
|
|
{...rest}
|
2020-09-01 01:31:13 +00:00
|
|
|
/>
|
|
|
|
{!readOnly && <ClickablePadding onClick={this.focusAtEnd} grow />}
|
2021-05-23 02:34:05 +00:00
|
|
|
{this.activeLinkEvent && !shareId && readOnly && (
|
2020-09-01 01:31:13 +00:00
|
|
|
<HoverPreview
|
|
|
|
node={this.activeLinkEvent.target}
|
|
|
|
event={this.activeLinkEvent}
|
|
|
|
onClose={this.handleLinkInactive}
|
2020-08-15 00:23:58 +00:00
|
|
|
/>
|
2020-09-01 01:31:13 +00:00
|
|
|
)}
|
2021-04-06 00:19:31 +00:00
|
|
|
{children}
|
2020-04-05 22:07:34 +00:00
|
|
|
</Flex>
|
2018-11-19 02:28:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:17:18 +00:00
|
|
|
export default withTranslation()<DocumentEditor>(
|
|
|
|
inject("policies")(DocumentEditor)
|
|
|
|
);
|