1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-29 18:19:46 +02:00

feat: Add gallery for attachments

This commit is contained in:
Maksim Eltyshev 2022-06-20 18:27:39 +02:00
parent 0bf4004046
commit 8f4d60c46f
22 changed files with 351 additions and 102 deletions

View file

@ -1,88 +1,153 @@
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { Button } from 'semantic-ui-react';
import { Gallery, Item as GalleryItem } from 'react-photoswipe-gallery';
import { Button, Grid } from 'semantic-ui-react';
import { useToggle } from '../../../lib/hooks';
import Item from './Item';
import styles from './Attachments.module.scss';
const Attachments = React.memo(({ items, onUpdate, onDelete, onCoverUpdate }) => {
const [t] = useTranslation();
const [isOpened, toggleOpened] = useToggle();
const INITIALLY_VISIBLE = 4;
const handleToggleClick = useCallback(() => {
toggleOpened();
}, [toggleOpened]);
const Attachments = React.memo(
({ items, onUpdate, onDelete, onCoverUpdate, onGalleryOpen, onGalleryClose }) => {
const [t] = useTranslation();
const [isAllVisible, toggleAllVisible] = useToggle();
const handleCoverSelect = useCallback(
(id) => {
onCoverUpdate(id);
},
[onCoverUpdate],
);
const handleCoverSelect = useCallback(
(id) => {
onCoverUpdate(id);
},
[onCoverUpdate],
);
const handleCoverDeselect = useCallback(() => {
onCoverUpdate(null);
}, [onCoverUpdate]);
const handleCoverDeselect = useCallback(() => {
onCoverUpdate(null);
}, [onCoverUpdate]);
const handleUpdate = useCallback(
(id, data) => {
onUpdate(id, data);
},
[onUpdate],
);
const handleUpdate = useCallback(
(id, data) => {
onUpdate(id, data);
},
[onUpdate],
);
const handleDelete = useCallback(
(id) => {
onDelete(id);
},
[onDelete],
);
const handleDelete = useCallback(
(id) => {
onDelete(id);
},
[onDelete],
);
const visibleItems = isOpened ? items : items.slice(0, 4);
const handleBeforeGalleryOpen = useCallback(
(gallery) => {
onGalleryOpen();
return (
<>
{visibleItems.map((item) => (
<Item
key={item.id}
name={item.name}
url={item.url}
coverUrl={item.coverUrl}
createdAt={item.createdAt}
isCover={item.isCover}
isPersisted={item.isPersisted}
onCoverSelect={() => handleCoverSelect(item.id)}
onCoverDeselect={handleCoverDeselect}
onUpdate={(data) => handleUpdate(item.id, data)}
onDelete={() => handleDelete(item.id)}
/>
))}
{items.length > 4 && (
<Button
fluid
content={
isOpened
? t('action.showFewerAttachments')
: t('action.showAllAttachments', {
hidden: items.length - visibleItems.length,
})
gallery.on('destroy', () => {
onGalleryClose();
});
},
[onGalleryOpen, onGalleryClose],
);
const handleToggleAllVisibleClick = useCallback(() => {
toggleAllVisible();
}, [toggleAllVisible]);
const galleryItemsNode = items.map((item, index) => {
const props = item.coverUrl
? {
width: item.imageWidth,
height: item.imageHeight,
}
className={styles.toggleButton}
onClick={handleToggleClick}
/>
)}
</>
);
});
: {
content: (
<Grid verticalAlign="middle" className={styles.contentWrapper}>
<Grid.Column textAlign="center" className={styles.content}>
{t('common.thereIsNoPreviewAvailableForThisAttachment')}
</Grid.Column>
</Grid>
),
};
const isVisible = isAllVisible || index < INITIALLY_VISIBLE;
return (
<GalleryItem
{...props} // eslint-disable-line react/jsx-props-no-spreading
key={item.id}
original={item.url}
caption={item.name}
>
{({ ref, open }) =>
isVisible ? (
<Item
ref={ref}
name={item.name}
url={item.url}
coverUrl={item.coverUrl}
createdAt={item.createdAt}
isCover={item.isCover}
isPersisted={item.isPersisted}
onClick={item.coverUrl ? open : undefined}
onCoverSelect={() => handleCoverSelect(item.id)}
onCoverDeselect={handleCoverDeselect}
onUpdate={(data) => handleUpdate(item.id, data)}
onDelete={() => handleDelete(item.id)}
/>
) : (
<span ref={ref} />
)
}
</GalleryItem>
);
});
return (
<>
<Gallery
withCaption
withDownloadButton
options={{
showHideAnimationType: 'none',
closeTitle: '',
zoomTitle: '',
arrowPrevTitle: '',
arrowNextTitle: '',
errorMsg: '',
}}
onBeforeOpen={handleBeforeGalleryOpen}
>
{galleryItemsNode}
</Gallery>
{items.length > INITIALLY_VISIBLE && (
<Button
fluid
content={
isAllVisible
? t('action.showFewerAttachments')
: t('action.showAllAttachments', {
hidden: items.length - INITIALLY_VISIBLE,
})
}
className={styles.toggleButton}
onClick={handleToggleAllVisibleClick}
/>
)}
</>
);
},
);
Attachments.propTypes = {
items: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
onCoverUpdate: PropTypes.func.isRequired,
onGalleryOpen: PropTypes.func.isRequired,
onGalleryClose: PropTypes.func.isRequired,
};
export default Attachments;

View file

@ -1,4 +1,14 @@
:global(#app) {
.content {
color: #fff;
font-size: 20px;
font-weight: 700;
}
.contentWrapper {
height: 100%;
}
.toggleButton {
background: transparent;
box-shadow: none;

View file

@ -8,24 +8,32 @@ import EditPopup from './EditPopup';
import styles from './Item.module.scss';
const Item = React.memo(
({
name,
url,
coverUrl,
createdAt,
isCover,
isPersisted,
onCoverSelect,
onCoverDeselect,
onUpdate,
onDelete,
}) => {
const Item = React.forwardRef(
(
{
name,
url,
coverUrl,
createdAt,
isCover,
isPersisted,
onCoverSelect,
onCoverDeselect,
onClick,
onUpdate,
onDelete,
},
ref,
) => {
const [t] = useTranslation();
const handleClick = useCallback(() => {
window.open(url, '_blank');
}, [url]);
if (onClick) {
onClick();
} else {
window.open(url, '_blank');
}
}, [url, onClick]);
const handleToggleCoverClick = useCallback(
(event) => {
@ -54,7 +62,7 @@ const Item = React.memo(
return (
/* eslint-disable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */
<div className={styles.wrapper} onClick={handleClick}>
<div ref={ref} className={styles.wrapper} onClick={handleClick}>
{/* eslint-enable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */}
<div
@ -133,6 +141,7 @@ Item.propTypes = {
createdAt: PropTypes.instanceOf(Date),
isCover: PropTypes.bool.isRequired,
isPersisted: PropTypes.bool.isRequired,
onClick: PropTypes.func,
onCoverSelect: PropTypes.func.isRequired,
onCoverDeselect: PropTypes.func.isRequired,
onUpdate: PropTypes.func.isRequired,
@ -143,6 +152,7 @@ Item.defaultProps = {
url: undefined,
coverUrl: undefined,
createdAt: undefined,
onClick: undefined,
};
export default Item;
export default React.memo(Item);

View file

@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
@ -73,6 +73,8 @@ const CardModal = React.memo(
}) => {
const [t] = useTranslation();
const isGalleryOpened = useRef(false);
const handleNameUpdate = useCallback(
(newName) => {
onUpdate({
@ -124,6 +126,22 @@ const CardModal = React.memo(
});
}, [isSubscribed, onUpdate]);
const handleGalleryOpen = useCallback(() => {
isGalleryOpened.current = true;
}, []);
const handleGalleryClose = useCallback(() => {
isGalleryOpened.current = false;
}, []);
const handleClose = useCallback(() => {
if (isGalleryOpened.current) {
return;
}
onClose();
}, [onClose]);
const userIds = users.map((user) => user.id);
const labelIds = labels.map((label) => label.id);
@ -328,6 +346,8 @@ const CardModal = React.memo(
onUpdate={onAttachmentUpdate}
onDelete={onAttachmentDelete}
onCoverUpdate={handleCoverUpdate}
onGalleryOpen={handleGalleryOpen}
onGalleryClose={handleGalleryClose}
/>
</div>
</div>
@ -445,7 +465,7 @@ const CardModal = React.memo(
);
return (
<Modal open closeIcon size="small" centered={false} onClose={onClose}>
<Modal open closeIcon size="small" centered={false} onClose={handleClose}>
{canEdit ? (
<AttachmentAddZone onCreate={onAttachmentCreate}>{contentNode}</AttachmentAddZone>
) : (

View file

@ -10,6 +10,7 @@ import CoreWrapperContainer from '../containers/CoreWrapperContainer';
import NotFound from './NotFound';
import 'react-datepicker/dist/react-datepicker.css';
import 'photoswipe/dist/photoswipe.css';
import '../lib/custom-ui/styles.css';
import '../styles.module.scss';

View file

@ -32,7 +32,7 @@ const createData = (timer) => {
const TimerEditStep = React.memo(({ defaultValue, onUpdate, onBack, onClose }) => {
const [t] = useTranslation();
const [data, handleFieldChange, setData] = useForm(() => createData(defaultValue));
const [isEditing, toggleEdit] = useToggle();
const [isEditing, toggleEditing] = useToggle();
const hoursField = useRef(null);
const minutesField = useRef(null);
@ -55,10 +55,10 @@ const TimerEditStep = React.memo(({ defaultValue, onUpdate, onBack, onClose }) =
onClose();
}, [defaultValue, onUpdate, onClose]);
const handleToggleEditClick = useCallback(() => {
const handleToggleEditingClick = useCallback(() => {
setData(createData(defaultValue));
toggleEdit();
}, [defaultValue, setData, toggleEdit]);
toggleEditing();
}, [defaultValue, setData, toggleEditing]);
const handleSubmit = useCallback(() => {
const parts = {
@ -149,7 +149,7 @@ const TimerEditStep = React.memo(({ defaultValue, onUpdate, onBack, onClose }) =
type="button"
icon={isEditing ? 'close' : 'edit'}
className={styles.iconButton}
onClick={handleToggleEditClick}
onClick={handleToggleEditingClick}
/>
</div>
{isEditing && <Button positive content={t('action.save')} />}