2018-11-19 02:28:22 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2020-04-05 22:07:34 +00:00
|
|
|
import styled from 'styled-components';
|
2020-05-08 04:21:58 +00:00
|
|
|
import Textarea from 'react-autosize-textarea';
|
2020-05-20 03:39:34 +00:00
|
|
|
import { observer } from 'mobx-react';
|
2018-11-19 02:28:22 +00:00
|
|
|
import Editor from 'components/Editor';
|
|
|
|
import ClickablePadding from 'components/ClickablePadding';
|
2020-04-05 22:07:34 +00:00
|
|
|
import Flex from 'shared/components/Flex';
|
|
|
|
import parseTitle from 'shared/utils/parseTitle';
|
|
|
|
import Document from 'models/Document';
|
2020-05-20 03:39:34 +00:00
|
|
|
import DocumentMeta from './DocumentMeta';
|
2018-11-19 02:28:22 +00:00
|
|
|
|
2020-05-20 03:39:34 +00:00
|
|
|
type Props = {
|
2020-04-05 22:07:34 +00:00
|
|
|
onChangeTitle: (event: SyntheticInputEvent<>) => void,
|
|
|
|
title: string,
|
|
|
|
defaultValue: string,
|
|
|
|
document: Document,
|
|
|
|
isDraft: boolean,
|
2018-11-19 02:28:22 +00:00
|
|
|
readOnly?: boolean,
|
2020-05-20 03:39:34 +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> {
|
2019-08-09 06:09:09 +00:00
|
|
|
editor: ?Editor;
|
2018-11-19 02:28:22 +00:00
|
|
|
|
|
|
|
focusAtStart = () => {
|
2020-02-27 06:29:22 +00:00
|
|
|
if (this.editor) {
|
|
|
|
this.editor.focusAtStart();
|
|
|
|
}
|
2018-11-19 02:28:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
focusAtEnd = () => {
|
2020-02-27 06:29:22 +00:00
|
|
|
if (this.editor) {
|
|
|
|
this.editor.focusAtEnd();
|
|
|
|
}
|
2018-11-19 02:28:22 +00:00
|
|
|
};
|
|
|
|
|
2020-05-20 03:39:34 +00:00
|
|
|
getHeadings = () => {
|
|
|
|
if (this.editor) {
|
|
|
|
return this.editor.getHeadings();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2020-04-05 22:07:34 +00:00
|
|
|
handleTitleKeyDown = (event: SyntheticKeyboardEvent<>) => {
|
|
|
|
if (event.key === 'Enter' || event.key === 'Tab') {
|
|
|
|
event.preventDefault();
|
|
|
|
this.focusAtStart();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-19 02:28:22 +00:00
|
|
|
render() {
|
2020-05-20 03:39:34 +00:00
|
|
|
const { document, title, onChangeTitle, isDraft, readOnly } = this.props;
|
2020-04-05 22:07:34 +00:00
|
|
|
const { emoji } = parseTitle(title);
|
|
|
|
const startsWithEmojiAndSpace = !!(
|
|
|
|
emoji && title.match(new RegExp(`^${emoji}\\s`))
|
|
|
|
);
|
2018-11-19 02:28:22 +00:00
|
|
|
|
|
|
|
return (
|
2020-04-06 01:43:55 +00:00
|
|
|
<Flex auto column>
|
2020-04-05 22:07:34 +00:00
|
|
|
<Title
|
|
|
|
type="text"
|
|
|
|
onChange={onChangeTitle}
|
|
|
|
onKeyDown={this.handleTitleKeyDown}
|
|
|
|
placeholder="Start with a title…"
|
|
|
|
value={!title && readOnly ? 'Untitled' : title}
|
2020-05-20 03:39:34 +00:00
|
|
|
style={startsWithEmojiAndSpace ? { marginLeft: '-1.2em' } : undefined}
|
2020-04-05 22:07:34 +00:00
|
|
|
readOnly={readOnly}
|
|
|
|
autoFocus={!title}
|
2020-05-20 03:39:34 +00:00
|
|
|
maxLength={100}
|
2020-04-05 22:07:34 +00:00
|
|
|
/>
|
2020-05-20 03:39:34 +00:00
|
|
|
<DocumentMeta isDraft={isDraft} document={document} />
|
2019-07-08 02:25:45 +00:00
|
|
|
<Editor
|
2018-11-19 02:28:22 +00:00
|
|
|
ref={ref => (this.editor = ref)}
|
2020-04-05 22:07:34 +00:00
|
|
|
autoFocus={title && !this.props.defaultValue}
|
|
|
|
placeholder="…the rest is up to you"
|
2020-04-05 19:22:26 +00:00
|
|
|
grow
|
2018-11-19 02:28:22 +00:00
|
|
|
{...this.props}
|
|
|
|
/>
|
2019-12-19 05:00:36 +00:00
|
|
|
{!readOnly && <ClickablePadding onClick={this.focusAtEnd} grow />}
|
2020-04-05 22:07:34 +00:00
|
|
|
</Flex>
|
2018-11-19 02:28:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 04:21:58 +00:00
|
|
|
const Title = styled(Textarea)`
|
2020-04-05 22:50:37 +00:00
|
|
|
z-index: 1;
|
2020-04-05 22:07:34 +00:00
|
|
|
line-height: 1.25;
|
|
|
|
margin-top: 1em;
|
|
|
|
margin-bottom: 0.5em;
|
2020-04-11 16:27:14 +00:00
|
|
|
text: ${props => props.theme.text};
|
|
|
|
background: ${props => props.theme.background};
|
|
|
|
transition: ${props => props.theme.backgroundTransition};
|
2020-04-05 22:07:34 +00:00
|
|
|
color: ${props => props.theme.text};
|
|
|
|
font-size: 2.25em;
|
|
|
|
font-weight: 500;
|
|
|
|
outline: none;
|
|
|
|
border: 0;
|
|
|
|
padding: 0;
|
2020-05-08 04:21:58 +00:00
|
|
|
resize: none;
|
2020-04-05 22:07:34 +00:00
|
|
|
|
|
|
|
&::placeholder {
|
|
|
|
color: ${props => props.theme.placeholder};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-05-20 03:39:34 +00:00
|
|
|
export default DocumentEditor;
|