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/components/DocumentPreview/DocumentPreview.js

187 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-05-12 00:23:56 +00:00
// @flow
2018-05-05 23:16:08 +00:00
import * as React from 'react';
import { observer } from 'mobx-react';
2017-05-17 07:11:13 +00:00
import { Link } from 'react-router-dom';
2017-06-28 05:15:29 +00:00
import Document from 'models/Document';
2018-06-10 02:10:30 +00:00
import styled, { withTheme } from 'styled-components';
2019-03-13 04:35:35 +00:00
import { darken } from 'polished';
import Flex from 'shared/components/Flex';
import Highlight from 'components/Highlight';
2018-05-03 04:51:39 +00:00
import { StarredIcon } from 'outline-icons';
import PublishingInfo from './components/PublishingInfo';
import DocumentMenu from 'menus/DocumentMenu';
type Props = {
document: Document,
2017-07-09 18:26:17 +00:00
highlight?: ?string,
context?: ?string,
showCollection?: boolean,
showPublished?: boolean,
2018-11-07 05:58:32 +00:00
ref?: *,
};
2018-06-10 02:10:30 +00:00
const StyledStar = withTheme(styled(({ solid, theme, ...props }) => (
2019-03-13 04:35:35 +00:00
<StarredIcon color={theme.text} {...props} />
2017-10-21 22:34:00 +00:00
))`
2018-07-25 04:13:54 +00:00
flex-shrink: 0;
2017-09-03 01:43:12 +00:00
opacity: ${props => (props.solid ? '1 !important' : 0)};
2017-10-01 23:00:18 +00:00
transition: all 100ms ease-in-out;
&:hover {
transform: scale(1.1);
}
&:active {
transform: scale(0.95);
}
2018-06-10 02:10:30 +00:00
`);
const StyledDocumentMenu = styled(DocumentMenu)`
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
`;
const DocumentLink = styled(Link)`
display: block;
2017-06-26 03:32:05 +00:00
margin: 0 -16px;
padding: 10px 16px;
border-radius: 8px;
border: 2px solid transparent;
max-height: 50vh;
min-width: 100%;
overflow: hidden;
position: relative;
${StyledDocumentMenu} {
opacity: 0;
}
&:hover,
&:active,
&:focus {
2019-03-13 04:35:35 +00:00
background: ${props => props.theme.listItemHoverBackground};
border: 2px solid ${props => props.theme.listItemHoverBorder};
outline: none;
2017-09-03 01:43:12 +00:00
${StyledStar}, ${StyledDocumentMenu} {
2017-11-10 22:14:30 +00:00
opacity: 0.5;
2017-09-03 01:43:12 +00:00
&:hover {
opacity: 1;
}
}
}
&:focus {
2019-03-13 04:35:35 +00:00
border: 2px solid ${props => darken(0.5, props.theme.listItemHoverBorder)};
}
`;
const Heading = styled.h3`
display: flex;
align-items: center;
height: 24px;
margin-top: 0;
margin-bottom: 0.25em;
2018-07-25 04:13:54 +00:00
overflow: hidden;
white-space: nowrap;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
`;
const Actions = styled(Flex)`
margin-left: 4px;
align-items: center;
`;
2018-07-25 04:13:54 +00:00
const Title = styled(Highlight)`
max-width: 90%;
overflow: hidden;
text-overflow: ellipsis;
`;
const ResultContext = styled(Highlight)`
display: block;
2019-03-13 04:35:35 +00:00
color: ${props => props.theme.textTertiary};
font-size: 14px;
margin-top: -0.25em;
margin-bottom: 0.25em;
`;
const SEARCH_RESULT_REGEX = /<b\b[^>]*>(.*?)<\/b>/gi;
2017-11-10 22:14:30 +00:00
@observer
2018-05-05 23:16:08 +00:00
class DocumentPreview extends React.Component<Props> {
star = (ev: SyntheticEvent<*>) => {
ev.preventDefault();
ev.stopPropagation();
this.props.document.star();
};
2018-05-05 23:16:08 +00:00
unstar = (ev: SyntheticEvent<*>) => {
ev.preventDefault();
ev.stopPropagation();
this.props.document.unstar();
};
replaceResultMarks = (tag: string) => {
// don't use SEARCH_RESULT_REGEX here as it causes
// an infinite loop to trigger a regex inside it's own callback
return tag.replace(/<b\b[^>]*>(.*?)<\/b>/gi, '$1');
};
render() {
const {
document,
showCollection,
showPublished,
highlight,
context,
...rest
} = this.props;
const queryIsInTitle =
!!highlight &&
!!document.title.toLowerCase().match(highlight.toLowerCase());
return (
2018-05-13 04:01:17 +00:00
<DocumentLink
to={{
pathname: document.url,
state: { title: document.title },
}}
{...rest}
>
<Heading>
2018-07-25 04:13:54 +00:00
<Title text={document.title} highlight={highlight} />
2018-08-07 06:22:20 +00:00
{!document.isDraft && (
<Actions>
{document.starred ? (
<StyledStar onClick={this.unstar} solid />
) : (
<StyledStar onClick={this.star} />
)}
</Actions>
)}
<StyledDocumentMenu document={document} />
</Heading>
{!queryIsInTitle && (
<ResultContext
text={context}
highlight={highlight ? SEARCH_RESULT_REGEX : undefined}
processResult={this.replaceResultMarks}
/>
)}
<PublishingInfo
document={document}
collection={showCollection ? document.collection : undefined}
showPublished={showPublished}
/>
</DocumentLink>
);
}
}
export default DocumentPreview;