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 d5b5d4fc27 feat: Document hover cards (#1346)
* stash

* refactor

* refactor, styling

* tweaks

* pointer

* styling

* fi: Hide when printing

* fix: No hover cards on shared links

* remove suppressions no longer needed

* fix: Don't show hover cards when editing, they get in the way

* fix: Prevent hover card from going off rhs edge of screen

* fix: Remount hover card when changing between links

* fix: allow one part domains in links (#1350)

* allow one part domains in links

* no TLD when only one part domain

* return null for parseDomain of empty string

* fix fiddly hover preview behavior

* WIP

* refactor hover preview

* fix: Non-rounded bottom corners

* fix: Fixes an edgecase where mounting the nested editor in hovercard causesdocument to scroll if there is a hash in the url

* fix: Incorrect document preview rendering

* lint

Co-authored-by: Nan Yu <thenanyu@gmail.com>
Co-authored-by: Nan Yu <nan@getoutline.com>
2020-07-16 21:26:23 -07:00

137 lines
3.4 KiB
JavaScript

// @flow
import * as React from "react";
import styled from "styled-components";
import Textarea from "react-autosize-textarea";
import { observable } from "mobx";
import { observer } from "mobx-react";
import Editor from "components/Editor";
import ClickablePadding from "components/ClickablePadding";
import Flex from "components/Flex";
import HoverPreview from "components/HoverPreview";
import parseTitle from "shared/utils/parseTitle";
import Document from "models/Document";
import DocumentMeta from "components/DocumentMeta";
type Props = {
onChangeTitle: (event: SyntheticInputEvent<>) => void,
title: string,
defaultValue: string,
document: Document,
isDraft: boolean,
isShare: boolean,
readOnly?: boolean,
};
@observer
class DocumentEditor extends React.Component<Props> {
@observable activeLinkEvent: ?MouseEvent;
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();
}
};
handleLinkActive = (event: MouseEvent) => {
this.activeLinkEvent = event;
};
handleLinkInactive = () => {
this.activeLinkEvent = null;
};
render() {
const {
document,
title,
onChangeTitle,
isDraft,
isShare,
readOnly,
} = this.props;
const { emoji } = parseTitle(title);
const startsWithEmojiAndSpace = !!(emoji && title.startsWith(`${emoji} `));
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"
onHoverLink={this.handleLinkActive}
scrollTo={window.location.hash}
grow
{...this.props}
/>
{!readOnly && <ClickablePadding onClick={this.focusAtEnd} grow />}
{this.activeLinkEvent &&
!isShare &&
readOnly && (
<HoverPreview
node={this.activeLinkEvent.target}
event={this.activeLinkEvent}
onClose={this.handleLinkInactive}
/>
)}
</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;