Add spoilers back.

This commit is contained in:
Paul 2021-07-10 15:55:21 +01:00
parent ef5944b065
commit 392cb23541
3 changed files with 52 additions and 93 deletions

View file

@ -2,7 +2,6 @@ import { Attachment as AttachmentRJS } from "revolt.js/dist/api/objects";
import styles from "./Attachment.module.scss"; import styles from "./Attachment.module.scss";
import classNames from "classnames"; import classNames from "classnames";
import { Text } from "preact-i18n";
import { useContext, useState } from "preact/hooks"; import { useContext, useState } from "preact/hooks";
import { useIntermediate } from "../../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../../context/intermediate/Intermediate";
@ -11,6 +10,7 @@ import { AppContext } from "../../../../context/revoltjs/RevoltClient";
import AttachmentActions from "./AttachmentActions"; import AttachmentActions from "./AttachmentActions";
import TextFile from "./TextFile"; import TextFile from "./TextFile";
import { SizedGrid } from "./Grid"; import { SizedGrid } from "./Grid";
import Spoiler from "./Spoiler";
interface Props { interface Props {
attachment: AttachmentRJS; attachment: AttachmentRJS;
@ -24,7 +24,6 @@ export default function Attachment({ attachment, hasContent }: Props) {
const { openScreen } = useIntermediate(); const { openScreen } = useIntermediate();
const { filename, metadata } = attachment; const { filename, metadata } = attachment;
const [spoiler, setSpoiler] = useState(filename.startsWith("SPOILER_")); const [spoiler, setSpoiler] = useState(filename.startsWith("SPOILER_"));
const [loaded, setLoaded] = useState(false);
const url = client.generateFileURL( const url = client.generateFileURL(
attachment, attachment,
@ -36,7 +35,7 @@ export default function Attachment({ attachment, hasContent }: Props) {
case "Image": { case "Image": {
return ( return (
<SizedGrid width={metadata.width} height={metadata.height} <SizedGrid width={metadata.width} height={metadata.height}
className={classNames({ [styles.margin]: hasContent })}> className={classNames({ [styles.margin]: hasContent, spoiler })}>
<img src={url} alt={filename} <img src={url} alt={filename}
className={styles.image} className={styles.image}
loading="lazy" loading="lazy"
@ -46,15 +45,18 @@ export default function Attachment({ attachment, hasContent }: Props) {
onMouseDown={(ev) => onMouseDown={(ev) =>
ev.button === 1 && window.open(url, "_blank") ev.button === 1 && window.open(url, "_blank")
} /> } />
{ spoiler && <Spoiler set={setSpoiler} /> }
</SizedGrid> </SizedGrid>
) )
} }
case "Video": { case "Video": {
return ( return (
<div className={classNames(styles.container, { [styles.margin]: hasContent })} <div className={classNames(styles.container, { [styles.margin]: hasContent })}
style={{ '--width': metadata.width + 'px' }}> style={{ '--width': metadata.width + 'px' }}>
<AttachmentActions attachment={attachment} /> <AttachmentActions attachment={attachment} />
<SizedGrid width={metadata.width} height={metadata.height}> <SizedGrid width={metadata.width} height={metadata.height}
className={classNames({ spoiler })}>
<video src={url} alt={filename} <video src={url} alt={filename}
controls controls
loading="lazy" loading="lazy"
@ -64,58 +66,12 @@ export default function Attachment({ attachment, hasContent }: Props) {
ev.button === 1 && window.open(url, "_blank") ev.button === 1 && window.open(url, "_blank")
} }
/> />
{ spoiler && <Spoiler set={setSpoiler} /> }
</SizedGrid> </SizedGrid>
</div> </div>
) )
} }
/*return (
<div
className={styles.container}
onClick={() => spoiler && setSpoiler(false)}>
{spoiler && (
<div className={styles.overflow}>
<span>
<Text id="app.main.channel.misc.spoiler_attachment" />
</span>
</div>
)}
<img
src={url}
alt={filename}
width={metadata.width}
height={metadata.height}
loading="lazy"
data-has-content={hasContent}
data-spoiler={spoiler}
className={classNames(
styles.attachment,
styles.image,
loaded && styles.loaded,
metadata.width > metadata.height
? styles.long
: styles.tall,
)}
style={{
"--width": metadata.width,
"--height": metadata.height,
"--width-px": metadata.width + "px",
"--height-px": metadata.height + "px",
}}
onClick={() =>
openScreen({ id: "image_viewer", attachment })
}
onMouseDown={(ev) =>
ev.button === 1 && window.open(url, "_blank")
}
onLoad={() => setLoaded(true)}
/>
</div>
);
}*/
case "Audio": { case "Audio": {
return ( return (
<div <div
@ -126,48 +82,7 @@ export default function Attachment({ attachment, hasContent }: Props) {
</div> </div>
); );
} }
/*case "Video": {
return (
<div
className={styles.container}
onClick={() => spoiler && setSpoiler(false)}>
{spoiler && (
<div className={styles.overflow}>
<span>
<Text id="app.main.channel.misc.spoiler_attachment" />
</span>
</div>
)}
<div
data-spoiler={spoiler}
data-has-content={hasContent}
className={classNames(styles.attachment, styles.video)}>
<AttachmentActions attachment={attachment} />
<video
src={url}
width={metadata.width}
height={metadata.height}
loading="lazy"
className={classNames(
metadata.width > metadata.height
? styles.long
: styles.tall,
)}
style={{
"--width": metadata.width,
"--height": metadata.height,
"--width-px": metadata.width + "px",
"--height-px": metadata.height + "px",
}}
controls
onMouseDown={(ev) =>
ev.button === 1 && window.open(url, "_blank")
}
/>
</div>
</div>
);
}*/
case "Text": { case "Text": {
return ( return (
<div <div
@ -178,6 +93,7 @@ export default function Attachment({ attachment, hasContent }: Props) {
</div> </div>
); );
} }
default: { default: {
return ( return (
<div <div

View file

@ -3,6 +3,8 @@ import { Children } from "../../../../types/Preact";
const Grid = styled.div` const Grid = styled.div`
display: grid; display: grid;
overflow: hidden;
max-width: min(var(--attachment-max-width), 100%, var(--width)); max-width: min(var(--attachment-max-width), 100%, var(--width));
max-height: min(var(--attachment-max-height), var(--height)); max-height: min(var(--attachment-max-height), var(--height));
aspect-ratio: var(--aspect-ratio); aspect-ratio: var(--aspect-ratio);
@ -19,6 +21,14 @@ const Grid = styled.div`
grid-area: 1 / 1; grid-area: 1 / 1;
} }
&.spoiler {
img, video {
filter: blur(44px);
}
border-radius: var(--border-radius);
}
`; `;
export default Grid; export default Grid;

View file

@ -0,0 +1,33 @@
import { Text } from "preact-i18n";
import styled from "styled-components"
const Base = styled.div`
display: grid;
place-items: center;
z-index: 1;
grid-area: 1 / 1;
cursor: pointer;
user-select: none;
text-transform: uppercase;
span {
padding: 8px;
color: var(--foreground);
background: var(--primary-background);
border-radius: calc(var(--border-radius) * 4);
}
`;
interface Props {
set: (v: boolean) => void
}
export default function Spoiler({ set }: Props) {
return (
<Base onClick={() => set(false)}>
<span><Text id="app.main.channel.misc.spoiler_attachment" /></span>
</Base>
)
}