slate/components/core/SlatePreviewBlock.js

568 lines
15 KiB
JavaScript
Raw Normal View History

2020-09-04 07:32:51 +03:00
import * as React from "react";
import * as Constants from "~/common/constants";
2020-09-01 07:44:56 +03:00
import * as SVG from "~/common/svg";
import { css } from "@emotion/react";
2020-09-04 07:32:51 +03:00
import { ProcessedText } from "~/components/system/components/Typography";
2020-09-05 00:37:50 +03:00
import { Boundary } from "~/components/system/components/fragments/Boundary";
import { PopoverNavigation } from "~/components/system/components/PopoverNavigation";
import { TooltipWrapper } from "~/components/system/components/fragments/GlobalTooltip";
import { dispatchCustomEvent } from "~/common/custom-events";
2020-09-04 07:32:51 +03:00
import SlateMediaObjectPreview from "~/components/core/SlateMediaObjectPreview";
2020-10-02 02:44:22 +03:00
const MARGIN = 12;
const MIN_WIDTH = 144;
2020-10-01 03:41:53 +03:00
const STYLES_MOBILE_HIDDEN = css`
@media (max-width: ${Constants.sizes.mobile}px) {
display: none;
}
`;
const STYLES_MOBILE_ONLY = css`
@media (min-width: ${Constants.sizes.mobile}px) {
display: none;
}
`;
2020-09-23 23:52:00 +03:00
const STYLES_CREATE_NEW = css`
color: ${Constants.system.darkGray};
box-shadow: 0px 0px 0px 1px rgba(229, 229, 229, 0.5) inset;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 160px;
height: 160px;
2020-10-02 02:44:22 +03:00
margin: 0px ${MARGIN}px;
2020-09-23 23:52:00 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-10-01 03:41:53 +03:00
margin: 0;
border-radius: 8;
width: 100%;
height: 100%;
2020-09-23 23:52:00 +03:00
}
`;
const STYLES_IMAGE_ROW = css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
2020-09-04 07:16:19 +03:00
height: 160px;
2020-10-02 02:44:22 +03:00
${"" /* justify-content: space-between; */}
overflow: hidden;
2020-10-02 02:44:22 +03:00
margin: 0 -${MARGIN}px;
@media (max-width: ${Constants.sizes.mobile}px) {
justify-content: center;
2020-09-23 23:52:00 +03:00
margin: 0 -8px;
}
`;
const STYLES_ITEM_BOX = css`
2020-09-04 07:16:19 +03:00
width: 160px;
height: 160px;
2020-10-02 02:44:22 +03:00
margin: 0px ${MARGIN}px;
display: flex;
align-items: center;
justify-content: center;
2020-09-27 23:11:04 +03:00
box-shadow: 0px 0px 0px 1px ${Constants.system.lightBorder} inset;
2020-09-05 04:40:23 +03:00
cursor: pointer;
2020-09-04 07:16:19 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-09-23 23:52:00 +03:00
margin: 0 8px;
2020-09-04 07:16:19 +03:00
}
2020-09-05 04:40:23 +03:00
:hover {
color: ${Constants.system.brand};
}
2020-09-23 23:52:00 +03:00
`;
2020-09-08 00:45:58 +03:00
2020-09-23 23:52:00 +03:00
const STYLES_EMPTY_BOX = css`
width: 160px;
height: 160px;
2020-10-02 02:44:22 +03:00
margin: 0px ${MARGIN}px;
2020-09-08 00:45:58 +03:00
2020-09-23 23:52:00 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
margin: 0 8px;
2020-09-08 00:45:58 +03:00
}
2020-09-04 07:16:19 +03:00
`;
const STYLES_IMAGE_ROW_SMALL = css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 56px;
overflow: hidden;
2020-09-23 23:52:00 +03:00
margin: 0 -8px;
2020-09-04 07:16:19 +03:00
`;
const STYLES_ITEM_BOX_SMALL = css`
width: 56px;
height: 56px;
margin: 0px 8px;
display: flex;
align-items: center;
justify-content: center;
2020-09-27 23:11:04 +03:00
box-shadow: 0px 0px 0px 1px ${Constants.system.lightBorder} inset;
2020-09-23 23:52:00 +03:00
`;
2020-09-08 00:45:58 +03:00
2020-09-23 23:52:00 +03:00
const STYLES_EMPTY_BOX_SMALL = css`
width: 56px;
height: 56px;
margin: 0px 8px;
`;
2020-10-02 02:44:22 +03:00
export class SlatePreviewRow extends React.Component {
render() {
let numItems = this.props.numItems || 5;
let objects;
if (this.props.slate.data.objects.length === 0) {
objects = [
<div css={STYLES_CREATE_NEW} key="add-files">
<SVG.Plus height="24px" />
<div>Add Files</div>
</div>,
];
} else {
let trimmed =
this.props.slate.data.objects.length > numItems
? this.props.slate.data.objects.slice(0, numItems)
: this.props.slate.data.objects;
objects = trimmed.map((each) => (
<div
key={each.id}
css={this.props.small ? STYLES_ITEM_BOX_SMALL : STYLES_ITEM_BOX}
style={{
height: this.props.imageSize,
width: this.props.imageSize,
...this.props.style,
}}
>
<SlateMediaObjectPreview
2020-10-05 00:30:28 +03:00
blurhash={each.blurhash}
2020-10-02 02:44:22 +03:00
charCap={30}
type={each.type}
url={each.url}
2020-10-05 00:30:28 +03:00
style={this.props.previewStyle}
2020-10-02 02:44:22 +03:00
title={each.title || each.name}
2020-10-05 00:30:28 +03:00
iconOnly={this.props.small}
2020-10-02 02:44:22 +03:00
/>
</div>
));
}
// let numExtra = this.props.numItems
// ? this.props.numItems - objects.length
// : 5 - objects.length;
// let extra = [];
// for (let i = 0; i < numExtra; i++) {
// extra.push(
// <div
// key={`extra-${i}`}
// css={this.props.small ? STYLES_EMPTY_BOX_SMALL : STYLES_EMPTY_BOX}
// />
// );
// }
return (
2020-09-23 23:52:00 +03:00
<div
2020-10-02 02:44:22 +03:00
css={this.props.small ? STYLES_IMAGE_ROW_SMALL : STYLES_IMAGE_ROW}
style={{ height: this.props.imageSize, ...this.props.containerStyle }}
2020-09-23 23:52:00 +03:00
>
2020-10-02 02:44:22 +03:00
{objects}
{/* {extra} */}
2020-09-23 23:52:00 +03:00
</div>
);
}
}
const STYLES_BLOCK = css`
2020-10-05 00:30:28 +03:00
box-shadow: 0 0 0 1px ${Constants.system.lightBorder} inset, 0 0 40px 0 ${Constants.system.shadow};
2020-09-04 07:16:19 +03:00
border-radius: 8px;
padding: 32px 40px;
font-size: 12px;
text-align: left;
2020-09-04 01:42:08 +03:00
margin: 24px auto 48px auto;
2020-09-27 23:11:04 +03:00
max-width: ${Constants.sizes.desktop}px;
cursor: pointer;
2020-10-01 03:41:53 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
padding: 16px;
margin: 24px auto;
}
`;
2020-09-01 07:44:56 +03:00
const STYLES_TITLE_LINE = css`
width: 100%;
display: flex;
2020-09-01 07:44:56 +03:00
align-items: center;
font-size: ${Constants.typescale.lvl1};
2020-09-01 07:44:56 +03:00
margin-bottom: 16px;
2020-09-06 05:45:27 +03:00
overflow-wrap: break-word;
2020-10-01 03:41:53 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
display: none;
}
`;
2020-09-01 07:44:56 +03:00
const STYLES_COPY_INPUT = css`
pointer-events: none;
position: absolute;
opacity: 0;
`;
2020-09-03 02:17:31 +03:00
const STYLES_TAG = css`
margin-right: 16px;
2020-09-03 02:17:31 +03:00
padding: 4px 8px;
2020-09-04 01:42:08 +03:00
border-radius: 2px;
2020-09-04 07:16:19 +03:00
border: 1px solid ${Constants.system.black};
color: ${Constants.system.black};
font-family: ${Constants.font.semiBold};
font-size: 0.9rem;
`;
const STYLES_BODY = css`
font-family: ${Constants.font.text};
font-size: 0.9rem;
margin-bottom: 24px;
line-height: 20px;
2020-09-04 07:32:51 +03:00
white-space: pre-wrap;
2020-09-06 07:32:39 +03:00
word-wrap: break-word;
2020-10-01 03:41:53 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
display: none;
}
2020-09-04 07:16:19 +03:00
`;
2020-09-05 00:37:50 +03:00
const STYLES_ICON_BOX = css`
2020-09-05 04:40:23 +03:00
height: 32px;
width: 32px;
display: flex;
2020-09-05 00:37:50 +03:00
align-items: center;
justify-content: center;
2020-09-05 04:40:23 +03:00
position: relative;
color: ${Constants.system.darkGray};
`;
const STYLES_CONTEXT_MENU = css`
position: absolute;
2020-09-05 00:37:50 +03:00
`;
const STYLES_TITLE = css`
font-size: ${Constants.typescale.lvl2};
font-family: ${Constants.font.semiBold};
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
margin-right: 16px;
`;
2020-10-02 02:44:22 +03:00
export class SlatePreviewBlock extends React.Component {
2020-09-01 07:44:56 +03:00
_ref;
2020-09-05 00:37:50 +03:00
_test;
2020-09-01 07:44:56 +03:00
state = {
2020-09-05 04:40:23 +03:00
showMenu: false,
2020-09-05 00:37:50 +03:00
copyValue: "",
2020-10-01 03:41:53 +03:00
windowWidth: 360,
};
componentDidMount = () => {
this.calculateWidth();
window.addEventListener("resize", this.calculateWidth);
};
componentWillUnmount = () => {
window.removeEventListener("resize", this.calculateWidth);
};
calculateWidth = () => {
this.setState({ windowWidth: window.innerWidth });
2020-09-05 00:37:50 +03:00
};
2020-09-01 07:44:56 +03:00
_handleCopy = (e, value) => {
2020-09-04 07:16:19 +03:00
e.stopPropagation();
2020-09-05 00:37:50 +03:00
this.setState({ copyValue: value }, () => {
2020-09-01 07:44:56 +03:00
this._ref.select();
document.execCommand("copy");
2020-09-05 04:40:23 +03:00
this._handleHide();
2020-09-01 07:44:56 +03:00
});
};
2020-09-05 00:37:50 +03:00
_handleClick = (e) => {
e.stopPropagation();
2020-09-05 04:40:23 +03:00
if (this.state.showMenu) {
this._handleHide();
return;
}
this.setState({ showMenu: true });
// dispatchCustomEvent({
// name: "show-tooltip",
// detail: {
// id: `slate-tooltip-${this.props.slate.id}`,
// },
// });
2020-09-05 00:37:50 +03:00
};
_handleHide = (e) => {
2020-09-05 04:40:23 +03:00
this.setState({ showMenu: false });
// dispatchCustomEvent({
// name: "hide-tooltip",
// detail: {
// id: `slate-tooltip-${this.props.slate.id}`,
// },
// });
2020-09-05 00:37:50 +03:00
};
2020-09-01 07:44:56 +03:00
render() {
2020-10-05 00:30:28 +03:00
if (!this.props.isOwner && !this.props.slate.data.objects.length) {
2020-09-04 07:16:19 +03:00
return null;
}
2020-10-05 00:30:28 +03:00
let first = this.props.slate.data.objects ? this.props.slate.data.objects[0] : null;
2020-09-05 00:37:50 +03:00
let contextMenu = (
<React.Fragment>
<Boundary
captureResize={true}
captureScroll={false}
enabled
onOutsideRectEvent={this._handleHide}
>
2020-09-05 00:37:50 +03:00
<PopoverNavigation
style={{
top: "16px",
2020-09-05 04:40:23 +03:00
right: "-12px",
2020-09-05 00:37:50 +03:00
}}
navigation={
2020-10-05 00:30:28 +03:00
this.props.isOwner
2020-09-05 00:37:50 +03:00
? [
{
text: "Copy URL",
onClick: (e) =>
this._handleCopy(
e,
2020-10-05 00:30:28 +03:00
`${window.location.hostname}/${this.props.username}/${this.props.slate.slatename}`
2020-09-05 00:37:50 +03:00
),
},
{
text: "Copy slate ID",
2020-09-05 00:37:50 +03:00
onClick: (e) => this._handleCopy(e, this.props.slate.id),
},
]
: [
{
text: "Copy URL",
onClick: (e) =>
this._handleCopy(
e,
2020-10-05 00:30:28 +03:00
`${window.location.hostname}/${this.props.username}/${this.props.slate.slatename}`
2020-09-05 00:37:50 +03:00
),
},
]
}
/>
</Boundary>
<input
readOnly
ref={(c) => {
this._ref = c;
}}
value={this.state.copyValue}
css={STYLES_COPY_INPUT}
/>
</React.Fragment>
);
2020-09-01 07:44:56 +03:00
return (
2020-09-05 00:37:50 +03:00
<div
css={STYLES_BLOCK}
style={
2020-10-05 00:30:28 +03:00
this.props.external ? { backgroundColor: Constants.system.white, boxShadow: "none" } : {}
}
>
2020-09-01 07:44:56 +03:00
<div css={STYLES_TITLE_LINE}>
<div css={STYLES_TITLE}>{this.props.slate.data.name}</div>
2020-10-05 00:30:28 +03:00
{this.props.isOwner ? (
2020-09-03 02:17:31 +03:00
this.props.slate.data.public ? (
2020-09-04 01:42:08 +03:00
<div
css={STYLES_TAG}
2020-09-04 07:16:19 +03:00
style={{
borderColor: Constants.system.brand,
color: Constants.system.brand,
}}
>
2020-09-04 01:42:08 +03:00
Public
</div>
2020-09-03 02:17:31 +03:00
) : (
2020-09-04 07:42:50 +03:00
<div
css={STYLES_TAG}
style={{
2020-09-27 23:11:04 +03:00
color: Constants.system.darkGray,
borderColor: Constants.system.darkGray,
}}
>
2020-09-04 07:16:19 +03:00
Private
</div>
2020-09-03 02:17:31 +03:00
)
2020-09-05 04:40:23 +03:00
) : (
<div />
)}
{this.props.external ? null : this.props.username ? (
2020-09-05 00:37:50 +03:00
<div
style={{ marginLeft: "auto" }}
2020-09-05 00:37:50 +03:00
ref={(c) => {
this._test = c;
}}
>
2020-09-05 04:40:23 +03:00
{/* <TooltipWrapper
2020-09-05 00:37:50 +03:00
id={`slate-tooltip-${this.props.slate.id}`}
type="body"
content={contextMenu}
horizontal="left"
vertical="below"
2020-09-05 04:40:23 +03:00
> */}
<div css={STYLES_ICON_BOX} onClick={this._handleClick}>
<SVG.MoreHorizontal height="24px" />
2020-10-05 00:30:28 +03:00
{this.state.showMenu ? <div css={STYLES_CONTEXT_MENU}>{contextMenu}</div> : null}
2020-09-05 04:40:23 +03:00
</div>
{/* </TooltipWrapper> */}
2020-09-01 07:44:56 +03:00
</div>
) : null}
2020-09-01 07:44:56 +03:00
</div>
2020-09-04 07:16:19 +03:00
{this.props.slate.data.body ? (
2020-09-04 07:32:51 +03:00
<div css={STYLES_BODY}>
<ProcessedText text={this.props.slate.data.body} />
</div>
2020-09-04 07:16:19 +03:00
) : (
<div style={{ height: "8px" }} />
)}
2020-10-01 03:41:53 +03:00
<span css={STYLES_MOBILE_ONLY}>
2020-10-05 00:30:28 +03:00
<div css={STYLES_TITLE} style={{ marginBottom: 8, fontSize: Constants.typescale.lvl1 }}>
2020-10-01 03:41:53 +03:00
{this.props.slate.data.name}
</div>
<div style={{ marginBottom: 16, fontSize: 12 }}>
{this.props.slate.data.objects.length} Object
{this.props.slate.data.objects.length === 1 ? "" : "s"}
</div>
<div
style={{
width: "100%",
height: `${this.state.windowWidth - 80}px`,
}}
>
{first ? (
<SlateMediaObjectPreview
2020-10-05 00:30:28 +03:00
blurhash={first.blurhash}
2020-10-01 03:41:53 +03:00
centeredImage
charCap={30}
type={first.type}
url={first.url}
style={{ borderRadius: 8 }}
imageStyle={{ borderRadius: 8 }}
title={first.title || first.name}
/>
) : (
<div css={STYLES_CREATE_NEW} key="add-files">
<SVG.Plus height="24px" />
<div>Add Files</div>
</div>
)}
</div>
</span>
<span css={STYLES_MOBILE_HIDDEN}>
<SlatePreviewRow
{...this.props}
2020-10-02 02:44:22 +03:00
imageSize={this.props.imageSize}
2020-10-01 03:41:53 +03:00
previewStyle={this.props.previewStyle}
/>
</span>
2020-09-01 07:44:56 +03:00
</div>
);
}
}
2020-10-02 02:44:22 +03:00
const STYLES_LINK = css`
color: ${Constants.system.black};
text-decoration: none;
`;
export default class SlatePreviewBlocks extends React.Component {
state = {
imageSize: 56,
};
componentDidMount = () => {
this.calculateWidth();
this.debounceInstance = this.debounce(this.calculateWidth, 350);
window.addEventListener("resize", this.debounceInstance);
};
componentWillUnmount = () => {
window.removeEventListener("resize", this.debounceInstance);
};
debounce = (fn, time) => {
let timer;
return () => {
window.clearTimeout(timer);
timer = window.setTimeout(fn, time);
};
};
calculateWidth = () => {
let windowWidth = window.innerWidth;
if (windowWidth > Constants.sizes.mobile) {
if (this.props.external) {
windowWidth -= 48;
} else {
windowWidth -= Constants.sizes.navigation + 96;
}
2020-10-02 02:44:22 +03:00
windowWidth = Math.min(windowWidth, Constants.sizes.desktop);
windowWidth -= 80; //NOTE(martina): 48px padding on scene page, 40px padding on block
for (let i = this.props.numItems || 5; i > 0; i--) {
let width = (windowWidth - MARGIN * 2 * (i - 1)) / i;
if (width < MIN_WIDTH) {
continue;
}
this.setState({ imageSize: width });
return;
}
}
this.setState({ imageSize: windowWidth - 48 - 32 }); //NOTE(martina): 24px padding on scene page, 16px padding on block on mobile
};
render() {
if (this.props.external) {
return this.props.slates.map((slate) => (
2020-10-05 00:30:28 +03:00
<a key={slate.id} href={`/${this.props.username}/${slate.slatename}`} css={STYLES_LINK}>
2020-10-02 02:44:22 +03:00
<SlatePreviewBlock
external
imageSize={this.state.imageSize}
2020-10-02 04:36:52 +03:00
username={this.props.username}
2020-10-02 02:44:22 +03:00
slate={slate}
/>
</a>
));
}
return this.props.slates.map((slate) => (
<div
key={slate.id}
onClick={() =>
this.props.onAction({
type: "NAVIGATE",
value: "V1_NAVIGATION_SLATE",
data: { decorator: "SLATE", ...slate },
})
}
>
<SlatePreviewBlock
2020-10-05 00:30:28 +03:00
isOwner={this.props.isOwner}
2020-10-02 02:44:22 +03:00
username={this.props.username}
imageSize={this.state.imageSize}
slate={slate}
/>
</div>
));
}
}