slate/components/core/SlatePreviewBlock.js

359 lines
8.9 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";
const STYLES_IMAGE_ROW = css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
2020-09-04 07:16:19 +03:00
height: 160px;
overflow: hidden;
2020-09-04 07:16:19 +03:00
margin: 0px -18px;
@media (max-width: ${Constants.sizes.mobile}px) {
justify-content: center;
}
`;
const STYLES_ITEM_BOX = css`
2020-09-04 07:16:19 +03:00
width: 160px;
height: 160px;
margin: 0px 18px;
display: flex;
align-items: center;
justify-content: center;
2020-09-04 07:16:19 +03:00
box-shadow: 0px 0px 0px 1px rgba(229, 229, 229, 0.5) 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) {
margin: 0 auto;
}
2020-09-05 04:40:23 +03:00
:hover {
color: ${Constants.system.brand};
}
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;
margin: 0px -8px;
`;
const STYLES_ITEM_BOX_SMALL = css`
width: 56px;
height: 56px;
margin: 0px 8px;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0px 0px 0px 1px rgba(229, 229, 229, 0.5) inset;
`;
export function SlatePreviewRow(props) {
let numItems = props.numItems || 5;
let objects =
props.slate.data.objects.length > numItems
? props.slate.data.objects.slice(0, numItems)
: props.slate.data.objects;
return (
2020-09-04 07:16:19 +03:00
<div
css={props.small ? STYLES_IMAGE_ROW_SMALL : STYLES_IMAGE_ROW}
style={props.containerStyle}
>
2020-09-04 02:54:05 +03:00
{objects.map((each) => (
2020-09-04 07:16:19 +03:00
<div
2020-09-05 00:37:50 +03:00
key={each.id}
2020-09-04 07:16:19 +03:00
css={props.small ? STYLES_ITEM_BOX_SMALL : STYLES_ITEM_BOX}
style={props.style}
>
<SlateMediaObjectPreview
2020-09-05 07:45:50 +03:00
charCap={30}
type={each.type}
url={each.url}
2020-09-04 07:16:19 +03:00
style={{ border: "none", ...props.previewStyle }}
2020-09-01 02:09:57 +03:00
title={each.title || each.name}
small={props.small}
/>
</div>
))}
</div>
);
}
const STYLES_BLOCK = css`
border: 1px solid ${Constants.system.border};
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-04 07:16:19 +03:00
max-width: 1026px;
cursor: pointer;
`;
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-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-09-04 07:16:19 +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-09-03 02:17:31 +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-09-04 07:32:51 +03:00
export default 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-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-09-04 07:16:19 +03:00
if (!this.props.editing && !this.props.slate.data.objects.length) {
return 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={
this.props.editing
? [
{
text: "Copy URL",
onClick: (e) =>
this._handleCopy(
e,
`https://slate.host/${this.props.username}/${this.props.slate.slatename}`
2020-09-05 00:37:50 +03:00
),
},
{
text: "Copy slate ID",
onClick: (e) => this._handleCopy(e, this.props.slate.id),
},
]
: [
{
text: "Copy URL",
onClick: (e) =>
this._handleCopy(
e,
`https://slate.host/${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={
this.props.external
? { backgroundColor: Constants.system.white, border: "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-09-03 02:17:31 +03:00
{this.props.editing ? (
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={{
color: "rgba(0,0,0,0.25)",
borderColor: "rgba(0,0,0,0.25)",
}}
>
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" />
{this.state.showMenu ? (
<div css={STYLES_CONTEXT_MENU}>{contextMenu}</div>
) : null}
</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" }} />
)}
{this.props.slate.data.objects.length ? (
<SlatePreviewRow
{...this.props}
previewStyle={this.props.previewStyle}
/>
) : (
2020-09-06 02:41:12 +03:00
<div css={STYLES_IMAGE_ROW}>
<div css={STYLES_CREATE_NEW}>
<SVG.Plus height="24px" />
<div>Add Files</div>
</div>
2020-09-04 07:16:19 +03:00
</div>
)}
2020-09-01 07:44:56 +03:00
</div>
);
}
}