This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/scenes/Document/components/Editor.js
Tom Moor 9274005cbb feat: Upgrade editor (#1227)
* 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
2020-05-19 20:39:34 -07:00

109 lines
2.7 KiB
JavaScript

// @flow
import * as React from 'react';
import styled from 'styled-components';
import Textarea from 'react-autosize-textarea';
import { observer } from 'mobx-react';
import Editor from 'components/Editor';
import ClickablePadding from 'components/ClickablePadding';
import Flex from 'shared/components/Flex';
import parseTitle from 'shared/utils/parseTitle';
import Document from 'models/Document';
import DocumentMeta from './DocumentMeta';
type Props = {
onChangeTitle: (event: SyntheticInputEvent<>) => void,
title: string,
defaultValue: string,
document: Document,
isDraft: boolean,
readOnly?: boolean,
};
@observer
class DocumentEditor extends React.Component<Props> {
editor: ?Editor;
focusAtStart = () => {
if (this.editor) {
this.editor.focusAtStart();
}
};
focusAtEnd = () => {
if (this.editor) {
this.editor.focusAtEnd();
}
};
getHeadings = () => {
if (this.editor) {
return this.editor.getHeadings();
}
return [];
};
handleTitleKeyDown = (event: SyntheticKeyboardEvent<>) => {
if (event.key === 'Enter' || event.key === 'Tab') {
event.preventDefault();
this.focusAtStart();
}
};
render() {
const { document, title, onChangeTitle, isDraft, readOnly } = this.props;
const { emoji } = parseTitle(title);
const startsWithEmojiAndSpace = !!(
emoji && title.match(new RegExp(`^${emoji}\\s`))
);
return (
<Flex auto column>
<Title
type="text"
onChange={onChangeTitle}
onKeyDown={this.handleTitleKeyDown}
placeholder="Start with a title…"
value={!title && readOnly ? 'Untitled' : title}
style={startsWithEmojiAndSpace ? { marginLeft: '-1.2em' } : undefined}
readOnly={readOnly}
autoFocus={!title}
maxLength={100}
/>
<DocumentMeta isDraft={isDraft} document={document} />
<Editor
ref={ref => (this.editor = ref)}
autoFocus={title && !this.props.defaultValue}
placeholder="…the rest is up to you"
grow
{...this.props}
/>
{!readOnly && <ClickablePadding onClick={this.focusAtEnd} grow />}
</Flex>
);
}
}
const Title = styled(Textarea)`
z-index: 1;
line-height: 1.25;
margin-top: 1em;
margin-bottom: 0.5em;
text: ${props => props.theme.text};
background: ${props => props.theme.background};
transition: ${props => props.theme.backgroundTransition};
color: ${props => props.theme.text};
font-size: 2.25em;
font-weight: 500;
outline: none;
border: 0;
padding: 0;
resize: none;
&::placeholder {
color: ${props => props.theme.placeholder};
}
`;
export default DocumentEditor;