2017-05-12 00:23:56 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import * as React from "react";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { Link } from "react-router-dom";
|
|
|
|
import { StarredIcon } from "outline-icons";
|
|
|
|
import styled, { withTheme } from "styled-components";
|
2020-07-10 05:33:07 +00:00
|
|
|
import Flex from "components/Flex";
|
2020-06-20 20:59:15 +00:00
|
|
|
import Badge from "components/Badge";
|
|
|
|
import Tooltip from "components/Tooltip";
|
|
|
|
import Highlight from "components/Highlight";
|
|
|
|
import PublishingInfo from "components/PublishingInfo";
|
|
|
|
import DocumentMenu from "menus/DocumentMenu";
|
|
|
|
import Document from "models/Document";
|
2017-05-25 07:07:41 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
document: Document,
|
2017-07-09 18:26:17 +00:00
|
|
|
highlight?: ?string,
|
2018-08-05 01:32:56 +00:00
|
|
|
context?: ?string,
|
2017-07-15 05:15:56 +00:00
|
|
|
showCollection?: boolean,
|
2019-01-08 07:14:43 +00:00
|
|
|
showPublished?: boolean,
|
2019-04-09 04:47:27 +00:00
|
|
|
showPin?: boolean,
|
2019-10-06 01:42:03 +00:00
|
|
|
showDraft?: boolean,
|
2017-05-25 07:07:41 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2020-06-20 20:59:15 +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
|
|
|
`);
|
2017-08-03 03:45:09 +00:00
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
const StyledDocumentMenu = styled(DocumentMenu)`
|
|
|
|
position: absolute;
|
|
|
|
right: 16px;
|
|
|
|
top: 50%;
|
|
|
|
transform: translateY(-50%);
|
|
|
|
`;
|
|
|
|
|
2017-05-26 04:59:35 +00:00
|
|
|
const DocumentLink = styled(Link)`
|
|
|
|
display: block;
|
2019-07-08 04:40:34 +00:00
|
|
|
margin: 10px -8px;
|
2019-07-08 02:25:45 +00:00
|
|
|
padding: 6px 8px;
|
2017-05-26 04:59:35 +00:00
|
|
|
border-radius: 8px;
|
2017-06-01 03:23:09 +00:00
|
|
|
max-height: 50vh;
|
2017-06-26 00:21:33 +00:00
|
|
|
min-width: 100%;
|
2017-06-01 03:23:09 +00:00
|
|
|
overflow: hidden;
|
2018-03-01 07:28:36 +00:00
|
|
|
position: relative;
|
|
|
|
|
|
|
|
${StyledDocumentMenu} {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
2017-05-26 04:59:35 +00:00
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
&:hover,
|
|
|
|
&:active,
|
|
|
|
&:focus {
|
2019-03-13 04:35:35 +00:00
|
|
|
background: ${props => props.theme.listItemHoverBackground};
|
2017-06-01 03:23:09 +00:00
|
|
|
outline: none;
|
2017-09-03 01:43:12 +00:00
|
|
|
|
2018-03-01 07:28:36 +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;
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:59:35 +00:00
|
|
|
}
|
2018-03-01 07:28:36 +00:00
|
|
|
`;
|
2017-06-01 03:23:09 +00:00
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
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;
|
2020-07-21 02:03:14 +00:00
|
|
|
color: ${props => props.theme.text};
|
2020-06-20 20:59:15 +00:00
|
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
|
|
|
|
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
2018-03-01 07:28:36 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const Actions = styled(Flex)`
|
|
|
|
margin-left: 4px;
|
|
|
|
align-items: center;
|
2017-05-26 04:59:35 +00:00
|
|
|
`;
|
|
|
|
|
2018-07-25 04:13:54 +00:00
|
|
|
const Title = styled(Highlight)`
|
|
|
|
max-width: 90%;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
`;
|
|
|
|
|
2018-08-05 01:32:56 +00:00
|
|
|
const ResultContext = styled(Highlight)`
|
2018-08-05 05:46:55 +00:00
|
|
|
display: block;
|
2019-03-13 04:35:35 +00:00
|
|
|
color: ${props => props.theme.textTertiary};
|
2018-08-05 01:32:56 +00:00
|
|
|
font-size: 14px;
|
2018-08-05 05:46:55 +00:00
|
|
|
margin-top: -0.25em;
|
2018-08-05 01:32:56 +00:00
|
|
|
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> {
|
2019-08-09 06:09:09 +00:00
|
|
|
star = (ev: SyntheticEvent<>) => {
|
2017-08-03 03:45:09 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.props.document.star();
|
|
|
|
};
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
unstar = (ev: SyntheticEvent<>) => {
|
2017-08-03 03:45:09 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.props.document.unstar();
|
|
|
|
};
|
|
|
|
|
2018-08-05 01:32:56 +00:00
|
|
|
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
|
2020-06-20 20:59:15 +00:00
|
|
|
return tag.replace(/<b\b[^>]*>(.*?)<\/b>/gi, "$1");
|
2018-08-05 01:32:56 +00:00
|
|
|
};
|
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
render() {
|
2017-11-25 23:44:10 +00:00
|
|
|
const {
|
|
|
|
document,
|
|
|
|
showCollection,
|
2019-01-08 07:14:43 +00:00
|
|
|
showPublished,
|
2019-04-09 04:47:27 +00:00
|
|
|
showPin,
|
2019-10-06 01:42:03 +00:00
|
|
|
showDraft = true,
|
2017-11-25 23:44:10 +00:00
|
|
|
highlight,
|
2018-08-05 01:32:56 +00:00
|
|
|
context,
|
2017-11-25 23:44:10 +00:00
|
|
|
...rest
|
|
|
|
} = this.props;
|
2017-06-01 03:23:09 +00:00
|
|
|
|
2018-08-05 01:32:56 +00:00
|
|
|
const queryIsInTitle =
|
|
|
|
!!highlight &&
|
2019-10-28 05:34:11 +00:00
|
|
|
!!document.title.toLowerCase().includes(highlight.toLowerCase());
|
2018-08-05 01:32:56 +00:00
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
return (
|
2018-05-13 04:01:17 +00:00
|
|
|
<DocumentLink
|
2019-04-18 02:11:23 +00:00
|
|
|
to={{
|
|
|
|
pathname: document.url,
|
|
|
|
state: { title: document.title },
|
|
|
|
}}
|
2018-05-13 04:01:17 +00:00
|
|
|
{...rest}
|
|
|
|
>
|
2018-03-01 07:28:36 +00:00
|
|
|
<Heading>
|
2020-06-20 20:59:15 +00:00
|
|
|
<Title text={document.title || "Untitled"} highlight={highlight} />
|
2019-04-06 23:20:27 +00:00
|
|
|
{!document.isDraft &&
|
|
|
|
!document.isArchived && (
|
|
|
|
<Actions>
|
2019-04-18 02:11:23 +00:00
|
|
|
{document.isStarred ? (
|
2019-04-06 23:20:27 +00:00
|
|
|
<StyledStar onClick={this.unstar} solid />
|
|
|
|
) : (
|
|
|
|
<StyledStar onClick={this.star} />
|
|
|
|
)}
|
|
|
|
</Actions>
|
|
|
|
)}
|
2019-10-06 01:42:03 +00:00
|
|
|
{document.isDraft &&
|
|
|
|
showDraft && (
|
|
|
|
<Tooltip
|
|
|
|
tooltip="Only visible to you"
|
|
|
|
delay={500}
|
|
|
|
placement="top"
|
|
|
|
>
|
|
|
|
<Badge>Draft</Badge>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
2019-04-09 04:47:27 +00:00
|
|
|
<StyledDocumentMenu document={document} showPin={showPin} />
|
2018-03-01 07:28:36 +00:00
|
|
|
</Heading>
|
2018-08-05 01:32:56 +00:00
|
|
|
{!queryIsInTitle && (
|
|
|
|
<ResultContext
|
|
|
|
text={context}
|
|
|
|
highlight={highlight ? SEARCH_RESULT_REGEX : undefined}
|
|
|
|
processResult={this.replaceResultMarks}
|
|
|
|
/>
|
|
|
|
)}
|
2017-05-26 04:59:35 +00:00
|
|
|
<PublishingInfo
|
2017-07-16 16:24:45 +00:00
|
|
|
document={document}
|
2019-04-18 02:11:23 +00:00
|
|
|
showCollection={showCollection}
|
2019-01-08 07:14:43 +00:00
|
|
|
showPublished={showPublished}
|
2017-05-26 04:59:35 +00:00
|
|
|
/>
|
|
|
|
</DocumentLink>
|
2017-06-01 03:23:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 16:02:39 +00:00
|
|
|
|
2017-05-25 07:07:41 +00:00
|
|
|
export default DocumentPreview;
|