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.
outline/app/scenes/Document/components/Header.js

288 lines
7.3 KiB
JavaScript
Raw Normal View History

2018-07-01 16:16:38 +00:00
// @flow
import * as React from 'react';
2018-07-01 17:20:57 +00:00
import { throttle } from 'lodash';
2018-07-01 16:16:38 +00:00
import { observable } from 'mobx';
import { observer, inject } from 'mobx-react';
import { Redirect } from 'react-router-dom';
2018-07-01 16:16:38 +00:00
import styled from 'styled-components';
import breakpoint from 'styled-components-breakpoint';
import { EditIcon, PlusIcon } from 'outline-icons';
2019-04-18 06:13:25 +00:00
import { transparentize, darken } from 'polished';
2018-07-01 16:16:38 +00:00
import Document from 'models/Document';
import AuthStore from 'stores/AuthStore';
import { documentEditUrl } from 'utils/routeHelpers';
import { meta } from 'utils/keyboard';
2018-07-01 16:16:38 +00:00
import Flex from 'shared/components/Flex';
import Breadcrumb from 'shared/components/Breadcrumb';
2018-07-01 16:16:38 +00:00
import DocumentMenu from 'menus/DocumentMenu';
import NewChildDocumentMenu from 'menus/NewChildDocumentMenu';
2018-08-07 06:58:34 +00:00
import DocumentShare from 'scenes/DocumentShare';
import Button from 'components/Button';
2018-08-07 06:58:34 +00:00
import Modal from 'components/Modal';
import Badge from 'components/Badge';
2018-07-01 16:16:38 +00:00
import Collaborators from 'components/Collaborators';
import { Action, Separator } from 'components/Actions';
type Props = {
document: Document,
isDraft: boolean,
isEditing: boolean,
isSaving: boolean,
isPublishing: boolean,
publishingIsDisabled: boolean,
2018-07-01 16:16:38 +00:00
savingIsDisabled: boolean,
onDiscard: () => void,
2018-07-01 16:16:38 +00:00
onSave: ({
done?: boolean,
publish?: boolean,
autosave?: boolean,
}) => void,
auth: AuthStore,
2018-07-01 16:16:38 +00:00
};
@observer
class Header extends React.Component<Props> {
@observable isScrolled = false;
2018-08-07 06:58:34 +00:00
@observable showShareModal = false;
@observable redirectTo: ?string;
2018-07-01 16:16:38 +00:00
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
2018-07-01 17:20:57 +00:00
updateIsScrolled = () => {
2018-07-01 16:16:38 +00:00
this.isScrolled = window.scrollY > 75;
};
2018-07-01 17:20:57 +00:00
handleScroll = throttle(this.updateIsScrolled, 50);
2018-07-01 16:16:38 +00:00
handleEdit = () => {
this.redirectTo = documentEditUrl(this.props.document);
2018-07-01 16:16:38 +00:00
};
handleSave = () => {
this.props.onSave({ done: true });
};
handlePublish = () => {
this.props.onSave({ done: true, publish: true });
};
handleShareLink = async (ev: SyntheticEvent<>) => {
2018-08-07 06:58:34 +00:00
const { document } = this.props;
if (!document.shareUrl) await document.share();
this.showShareModal = true;
};
handleCloseShareModal = () => {
this.showShareModal = false;
};
2018-07-01 17:20:57 +00:00
handleClickTitle = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
};
2018-07-01 16:16:38 +00:00
render() {
if (this.redirectTo) return <Redirect to={this.redirectTo} push />;
2018-07-01 16:16:38 +00:00
const {
document,
isEditing,
isDraft,
isPublishing,
isSaving,
savingIsDisabled,
publishingIsDisabled,
auth,
2018-07-01 16:16:38 +00:00
} = this.props;
const canShareDocuments =
auth.team && auth.team.sharing && !document.isArchived;
const canToggleEmbeds = auth.team && auth.team.documentEmbeds;
const canEdit = !document.isArchived && !isEditing;
2018-07-01 16:16:38 +00:00
return (
<Actions
align="center"
justify="space-between"
readOnly={!isEditing}
isCompact={this.isScrolled}
shrink={false}
2018-07-01 16:16:38 +00:00
>
2018-08-07 06:58:34 +00:00
<Modal
isOpen={this.showShareModal}
onRequestClose={this.handleCloseShareModal}
title="Share document"
>
<DocumentShare
document={document}
onSubmit={this.handleCloseShareModal}
/>
</Modal>
2018-07-01 16:16:38 +00:00
<Breadcrumb document={document} />
2018-07-01 17:20:57 +00:00
<Title isHidden={!this.isScrolled} onClick={this.handleClickTitle}>
{document.title} {document.isArchived && <Badge>Archived</Badge>}
2018-07-01 17:20:57 +00:00
</Title>
2018-07-01 16:16:38 +00:00
<Wrapper align="center" justify="flex-end">
{!isDraft && !isEditing && <Collaborators document={document} />}
2018-07-01 18:54:24 +00:00
{isSaving &&
!isPublishing && (
<Action>
<Status>Saving</Status>
</Action>
)}
2018-08-07 06:58:34 +00:00
{!isDraft &&
!isEditing &&
canShareDocuments && (
2018-08-07 06:58:34 +00:00
<Action>
<Button
onClick={this.handleShareLink}
title="Share document"
neutral
small
>
2018-08-07 06:58:34 +00:00
Share
</Button>
2018-08-07 06:58:34 +00:00
</Action>
)}
2018-07-01 16:16:38 +00:00
{isEditing && (
<React.Fragment>
<Action>
<Button
2018-07-01 16:16:38 +00:00
onClick={this.handleSave}
2019-03-31 06:36:02 +00:00
title={`Save changes (${meta}+Enter)`}
2018-07-01 16:16:38 +00:00
disabled={savingIsDisabled}
isSaving={isSaving}
neutral={isDraft}
small
2018-07-01 16:16:38 +00:00
>
{isDraft ? 'Save Draft' : 'Done Editing'}
</Button>
2018-07-01 16:16:38 +00:00
</Action>
</React.Fragment>
)}
{isDraft && (
<Action>
<Button
onClick={this.handlePublish}
2019-03-31 06:36:02 +00:00
title="Publish document"
disabled={publishingIsDisabled}
small
>
{isPublishing ? 'Publishing…' : 'Publish'}
</Button>
</Action>
)}
{canEdit && (
2018-07-01 16:16:38 +00:00
<Action>
<Button
icon={<EditIcon />}
onClick={this.handleEdit}
neutral
small
>
Edit
</Button>
2018-07-01 16:16:38 +00:00
</Action>
)}
{canEdit &&
2018-07-01 16:16:38 +00:00
!isDraft && (
<Action>
<NewChildDocumentMenu
document={document}
label={
<Button icon={<PlusIcon />} neutral>
New doc
</Button>
}
/>
</Action>
2018-07-01 16:16:38 +00:00
)}
{!isEditing && (
<React.Fragment>
<Separator />
<Action>
<DocumentMenu
document={document}
showToggleEmbeds={canToggleEmbeds}
showPrint
/>
</Action>
</React.Fragment>
)}
2018-07-01 16:16:38 +00:00
</Wrapper>
</Actions>
);
}
}
const Status = styled.div`
color: ${props => props.theme.slate};
`;
const Wrapper = styled(Flex)`
2018-07-01 17:20:57 +00:00
width: 100%;
align-self: flex-end;
${breakpoint('tablet')`
width: 33.3%;
`};
2018-07-01 16:16:38 +00:00
`;
const Actions = styled(Flex)`
2018-07-01 17:20:57 +00:00
position: sticky;
2018-07-01 16:16:38 +00:00
top: 0;
right: 0;
2018-07-01 17:20:57 +00:00
left: 0;
z-index: 1;
2019-03-13 04:35:35 +00:00
background: ${props => transparentize(0.1, props.theme.background)};
2018-07-01 16:16:38 +00:00
border-bottom: 1px solid
2019-04-18 06:13:25 +00:00
${props =>
props.isCompact
? darken(0.05, props.theme.sidebarBackground)
: 'transparent'};
2018-07-01 16:16:38 +00:00
padding: 12px;
2018-07-01 19:42:21 +00:00
transition: all 100ms ease-out;
transform: translate3d(0, 0, 0);
2018-07-01 16:16:38 +00:00
-webkit-backdrop-filter: blur(20px);
@media print {
display: none;
}
${breakpoint('tablet')`
2018-07-01 17:20:57 +00:00
padding: ${props => (props.isCompact ? '12px' : `24px 24px 0`)};
2018-07-01 16:16:38 +00:00
`};
`;
const Title = styled.div`
font-size: 16px;
font-weight: 600;
text-align: center;
align-items: center;
2018-07-01 16:16:38 +00:00
justify-content: center;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
transition: opacity 100ms ease-in-out;
opacity: ${props => (props.isHidden ? '0' : '1')};
2018-07-01 17:20:57 +00:00
cursor: ${props => (props.isHidden ? 'default' : 'pointer')};
display: none;
width: 0;
${breakpoint('tablet')`
display: flex;
2018-07-25 04:13:54 +00:00
flex-grow: 1;
2018-07-01 17:20:57 +00:00
`};
2018-07-01 16:16:38 +00:00
`;
export default inject('auth')(Header);