2020-08-09 01:19:35 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Constants from "~/common/constants";
|
2020-11-11 04:44:21 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-09-01 02:09:57 +03:00
|
|
|
import * as SVG from "~/common/svg";
|
2020-12-09 07:22:17 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
2020-08-09 01:19:35 +03:00
|
|
|
|
2020-11-30 08:24:22 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-09-18 07:28:32 +03:00
|
|
|
import { FileTypeIcon } from "~/components/core/FileTypeIcon";
|
2020-10-05 00:30:28 +03:00
|
|
|
import { Blurhash } from "react-blurhash";
|
|
|
|
import { isBlurhashValid } from "blurhash";
|
2020-08-09 01:19:35 +03:00
|
|
|
|
2020-10-01 03:41:53 +03:00
|
|
|
const STYLES_IMAGE_CONTAINER = css`
|
2020-12-11 05:59:17 +03:00
|
|
|
background-color: ${Constants.system.white};
|
2020-10-01 03:41:53 +03:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
background-size: cover;
|
|
|
|
background-position: 50% 50%;
|
2021-01-21 09:26:02 +03:00
|
|
|
|
|
|
|
@media (max-width: ${Constants.sizes.mobile}px) {
|
|
|
|
border-radius: 8px;
|
|
|
|
}
|
2020-10-01 03:41:53 +03:00
|
|
|
`;
|
|
|
|
|
2020-08-09 01:19:35 +03:00
|
|
|
const STYLES_IMAGE = css`
|
2020-12-11 05:59:17 +03:00
|
|
|
background-color: ${Constants.system.white};
|
2020-08-09 01:19:35 +03:00
|
|
|
display: block;
|
2020-08-22 20:30:03 +03:00
|
|
|
pointer-events: none;
|
2020-08-27 07:24:49 +03:00
|
|
|
transition: 200ms ease all;
|
2020-12-11 05:59:17 +03:00
|
|
|
max-width: 100%;
|
|
|
|
max-height: 100%;
|
2020-08-09 01:19:35 +03:00
|
|
|
`;
|
|
|
|
|
2020-08-22 12:32:40 +03:00
|
|
|
const STYLES_ENTITY = css`
|
2020-12-09 07:22:17 +03:00
|
|
|
position: relative;
|
2020-08-09 01:19:35 +03:00
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
2020-12-11 05:59:17 +03:00
|
|
|
border: 1px solid ${Constants.system.foreground};
|
2020-10-05 00:30:28 +03:00
|
|
|
background-color: ${Constants.system.white};
|
2020-08-09 01:19:35 +03:00
|
|
|
font-size: 24px;
|
|
|
|
display: flex;
|
2020-09-01 02:09:57 +03:00
|
|
|
flex-direction: column;
|
2020-08-09 01:19:35 +03:00
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
cursor: pointer;
|
2020-08-22 20:30:03 +03:00
|
|
|
pointer-events: none;
|
2020-09-01 02:09:57 +03:00
|
|
|
padding: 8px;
|
2020-09-04 07:16:19 +03:00
|
|
|
font-size: 0.9rem;
|
2020-09-01 02:09:57 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_TITLE = css`
|
2020-12-09 07:22:17 +03:00
|
|
|
width: calc(100% - 32px);
|
2020-09-01 02:09:57 +03:00
|
|
|
margin-top: 8px;
|
|
|
|
overflow: hidden;
|
2020-12-09 07:22:17 +03:00
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
2020-12-06 06:15:26 +03:00
|
|
|
color: ${Constants.system.textGray};
|
2020-12-09 07:22:17 +03:00
|
|
|
font-size: 16px;
|
2020-12-06 06:15:26 +03:00
|
|
|
font-family: ${Constants.font.medium};
|
2020-08-09 01:19:35 +03:00
|
|
|
`;
|
|
|
|
|
2020-10-05 00:30:28 +03:00
|
|
|
const STYLES_BLUR_CONTAINER = css`
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
overflow: hidden;
|
|
|
|
`;
|
|
|
|
|
2020-08-09 01:19:35 +03:00
|
|
|
export default class SlateMediaObjectPreview extends React.Component {
|
2020-09-05 07:45:50 +03:00
|
|
|
static defaultProps = {
|
|
|
|
charCap: 30,
|
|
|
|
};
|
|
|
|
|
2020-10-05 00:30:28 +03:00
|
|
|
state = {
|
|
|
|
showImage: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2020-11-30 02:47:08 +03:00
|
|
|
componentDidMount = () => {
|
2020-11-11 04:44:21 +03:00
|
|
|
if (this.props.type && Validations.isPreviewableImage(this.props.type)) {
|
2020-11-30 02:47:08 +03:00
|
|
|
this.loadImage(this.props.url);
|
|
|
|
} else if (this.props.coverImage) {
|
|
|
|
this.loadImage(this.props.coverImage.url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate = (prevProps) => {
|
2020-12-04 02:43:57 +03:00
|
|
|
if (prevProps.coverImage?.url !== this.props.coverImage?.url && this.props.coverImage?.url) {
|
2020-11-30 02:47:08 +03:00
|
|
|
this.loadImage(this.props.coverImage.url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loadImage = async (url) => {
|
2020-12-12 02:22:54 +03:00
|
|
|
const img = new Image();
|
|
|
|
img.onload = () => this.setState({ showImage: true });
|
|
|
|
img.src = url;
|
2020-10-05 00:30:28 +03:00
|
|
|
};
|
|
|
|
|
2020-08-09 01:19:35 +03:00
|
|
|
render() {
|
2020-11-30 02:47:08 +03:00
|
|
|
let url;
|
2020-11-11 04:44:21 +03:00
|
|
|
if (this.props.type && Validations.isPreviewableImage(this.props.type)) {
|
2020-11-30 02:47:08 +03:00
|
|
|
url = this.props.url;
|
|
|
|
} else if (this.props.coverImage) {
|
|
|
|
url = this.props.coverImage.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
let blurhash =
|
|
|
|
this.props.blurhash && isBlurhashValid(this.props.blurhash)
|
|
|
|
? this.props.blurhash
|
|
|
|
: this.props.coverImage?.blurhash && isBlurhashValid(this.props.coverImage?.blurhash)
|
|
|
|
? this.props.coverImage?.blurhash
|
|
|
|
: null;
|
|
|
|
if (this.state.error) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
css={STYLES_ENTITY}
|
|
|
|
style={{
|
|
|
|
...this.props.imageStyle,
|
|
|
|
backgroundColor: Constants.system.foreground,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SVG.FileNotFound height="24px" />
|
|
|
|
{this.props.iconOnly ? null : <div css={STYLES_TITLE}>File not found</div>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-10-01 03:41:53 +03:00
|
|
|
if (this.props.centeredImage) {
|
|
|
|
return (
|
2020-10-05 00:30:28 +03:00
|
|
|
<React.Fragment>
|
2021-01-21 04:07:40 +03:00
|
|
|
{this.state.showImage || !blurhash ? (
|
2020-10-05 00:30:28 +03:00
|
|
|
<div
|
|
|
|
css={STYLES_IMAGE_CONTAINER}
|
|
|
|
style={{
|
|
|
|
backgroundImage: `url(${url})`,
|
|
|
|
...this.props.imageStyle,
|
|
|
|
}}
|
|
|
|
/>
|
2021-01-21 04:07:40 +03:00
|
|
|
) : (
|
2020-10-05 00:30:28 +03:00
|
|
|
<div css={STYLES_BLUR_CONTAINER}>
|
|
|
|
<Blurhash
|
2020-11-30 02:47:08 +03:00
|
|
|
hash={blurhash}
|
|
|
|
height="100%"
|
|
|
|
width="100%"
|
|
|
|
style={this.props.imageStyle}
|
2020-10-05 00:30:28 +03:00
|
|
|
resolutionX={32}
|
|
|
|
resolutionY={32}
|
|
|
|
punch={1}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
2020-10-01 03:41:53 +03:00
|
|
|
);
|
|
|
|
}
|
2020-10-05 00:30:28 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2021-01-21 04:07:40 +03:00
|
|
|
{this.state.showImage || !blurhash ? (
|
2020-10-05 00:30:28 +03:00
|
|
|
<img
|
|
|
|
css={STYLES_IMAGE}
|
2020-11-30 02:47:08 +03:00
|
|
|
style={{
|
|
|
|
...this.props.imageStyle,
|
|
|
|
}}
|
2020-10-05 00:30:28 +03:00
|
|
|
src={url}
|
|
|
|
/>
|
2021-01-21 04:07:40 +03:00
|
|
|
) : (
|
2020-10-05 00:30:28 +03:00
|
|
|
<Blurhash
|
2020-11-30 02:47:08 +03:00
|
|
|
hash={blurhash}
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
2020-10-05 00:30:28 +03:00
|
|
|
resolutionX={32}
|
|
|
|
resolutionY={32}
|
|
|
|
punch={1}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
2020-09-18 06:40:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:22:17 +03:00
|
|
|
const title = this.props.title;
|
|
|
|
// this.props.title && this.props.title.length > this.props.charCap
|
|
|
|
// ? this.props.title.substring(0, this.props.charCap) + "..."
|
|
|
|
// : this.props.title;
|
|
|
|
let extension = Strings.getFileExtension(this.props.title);
|
|
|
|
if (extension && extension.length) {
|
|
|
|
extension = extension.toUpperCase();
|
|
|
|
}
|
2020-11-11 04:44:21 +03:00
|
|
|
let element = (
|
|
|
|
<FileTypeIcon
|
|
|
|
type={this.props.type}
|
2020-12-09 07:22:17 +03:00
|
|
|
height={this.props.previewPanel ? "26px" : "20px"}
|
2020-12-06 06:15:26 +03:00
|
|
|
style={{ color: Constants.system.textGray }}
|
2020-11-11 04:44:21 +03:00
|
|
|
/>
|
|
|
|
);
|
2020-11-30 02:47:08 +03:00
|
|
|
return (
|
|
|
|
<article
|
|
|
|
css={STYLES_ENTITY}
|
|
|
|
style={{
|
|
|
|
...this.props.style,
|
|
|
|
border: this.props.previewPanel ? `1px solid ${Constants.system.bgGray}` : "auto",
|
|
|
|
}}
|
|
|
|
>
|
2020-12-09 07:22:17 +03:00
|
|
|
<div style={{ display: "flex", alignItems: "center", justifyContent: "center" }}>
|
|
|
|
<img
|
|
|
|
src="https://slate.textile.io/ipfs/bafkreib5mnvds3cpe7ot7ibrakmrnja2hv5tast3giiarpl5nun7jpdt5m"
|
|
|
|
alt=""
|
|
|
|
height={this.props.previewPanel ? "80" : "64"}
|
|
|
|
/>
|
|
|
|
<div style={{ position: "absolute" }}>{element}</div>
|
|
|
|
</div>
|
2020-11-30 02:47:08 +03:00
|
|
|
{this.props.title && !this.props.iconOnly && !this.props.previewPanel ? (
|
2020-12-09 07:22:17 +03:00
|
|
|
<div style={{ position: "absolute", bottom: 16, left: 16, width: "inherit" }}>
|
|
|
|
<div css={STYLES_TITLE}>{title}</div>
|
|
|
|
{extension ? (
|
|
|
|
<div
|
|
|
|
css={STYLES_TITLE}
|
|
|
|
style={{
|
|
|
|
fontSize: 12,
|
|
|
|
color: Constants.system.textGrayLight,
|
|
|
|
fontFamily: Constants.font.medium,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{extension}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2020-11-30 02:47:08 +03:00
|
|
|
) : null}
|
|
|
|
</article>
|
|
|
|
);
|
2020-08-09 01:19:35 +03:00
|
|
|
}
|
|
|
|
}
|