1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

added dnd-kit for images

This commit is contained in:
Fedor Katurov 2022-06-20 21:50:09 +07:00
parent a811951a63
commit dc1d66cec6
10 changed files with 227 additions and 77 deletions

View file

@ -3,7 +3,7 @@ import React, { FC, useCallback } from 'react';
import { SortEnd } from 'react-sortable-hoc';
import { SortableAudioGrid } from '~/components/editors/SortableAudioGrid';
import { SortableImageGrid } from '~/components/editors/SortableImageGrid';
import { OnSortEnd, SortableImageGrid } from '~/components/editors/SortableImageGrid';
import { COMMENT_FILE_TYPES } from '~/constants/uploads';
import { useFileDropZone } from '~/hooks';
import { IFile } from '~/types';
@ -29,8 +29,8 @@ const CommentFormAttaches: FC = () => {
const hasAudioAttaches = filesAudios.length > 0 || pendingAudios.length > 0;
const hasAttaches = hasImageAttaches || hasAudioAttaches;
const onImageMove = useCallback(
({ oldIndex, newIndex }: SortEnd) => {
const onImageMove = useCallback<OnSortEnd>(
({ oldIndex, newIndex }) => {
setFiles([
...filesAudios,
...(moveArrItem(
@ -83,12 +83,8 @@ const CommentFormAttaches: FC = () => {
<SortableImageGrid
onDelete={onFileDelete}
onSortEnd={onImageMove}
axis="xy"
items={filesImages}
locked={pendingImages}
pressDelay={50}
helperClass={styles.helper}
size={120}
/>
)}

View file

@ -2,7 +2,7 @@ import React, { FC, useCallback } from 'react';
import { SortEnd } from 'react-sortable-hoc';
import { SortableImageGrid } from '~/components/editors/SortableImageGrid';
import { OnSortEnd, SortableImageGrid } from '~/components/editors/SortableImageGrid';
import { useWindowSize } from '~/hooks/dom/useWindowSize';
import { UploadStatus } from '~/store/uploader/UploaderStore';
import { IFile } from '~/types';
@ -19,8 +19,8 @@ interface IProps {
const ImageGrid: FC<IProps> = ({ files, setFiles, locked }) => {
const { innerWidth } = useWindowSize();
const onMove = useCallback(
({ oldIndex, newIndex }: SortEnd) => {
const onMove = useCallback<OnSortEnd>(
({ oldIndex, newIndex }) => {
setFiles(
moveArrItem(
oldIndex,
@ -39,17 +39,7 @@ const ImageGrid: FC<IProps> = ({ files, setFiles, locked }) => {
[setFiles, files]
);
return (
<SortableImageGrid
onDelete={onDrop}
onSortEnd={onMove}
axis="xy"
items={files}
locked={locked}
pressDelay={innerWidth < 768 ? 200 : 0}
helperClass={styles.helper}
/>
);
return <SortableImageGrid onDelete={onDrop} onSortEnd={onMove} items={files} locked={locked} />;
};
export { ImageGrid };

View file

@ -2,5 +2,4 @@
.helper {
opacity: 0.5;
z-index: 10000 !important;
}

View file

@ -1,7 +1,24 @@
import React, { useCallback } from 'react';
import React, { FC, useCallback, useMemo, useState } from 'react';
import {
closestCenter,
DndContext,
DragOverlay,
DragStartEvent,
KeyboardSensor,
MouseSensor,
PointerSensor,
TouchSensor,
useSensor,
useSensors,
} from '@dnd-kit/core';
import { DragEndEvent } from '@dnd-kit/core/dist/types';
import {
rectSortingStrategy,
SortableContext,
sortableKeyboardCoordinates,
} from '@dnd-kit/sortable';
import classNames from 'classnames';
import { SortableContainer } from 'react-sortable-hoc';
import { SortableImageGridItem } from '~/components/editors/SortableImageGridItem';
import { ImageUpload } from '~/components/upload/ImageUpload';
@ -12,43 +29,111 @@ import { getURL } from '~/utils/dom';
import styles from './styles.module.scss';
const SortableImageGrid = SortableContainer(
({
items,
locked,
onDelete,
className,
}: {
items: IFile[];
locked: UploadStatus[];
onDelete: (file_id: IFile['id']) => void;
size?: number;
className?: string;
}) => {
const preventEvent = useCallback(event => event.preventDefault(), []);
export type OnSortEnd = (props: { oldIndex: number; newIndex: number }) => void;
return (
<div className={classNames(styles.grid, className)} onDropCapture={preventEvent}>
{items
.filter(file => file && file.id)
.map((file, index) => (
<SortableImageGridItem key={file.id} index={index} collection={0}>
<ImageUpload
id={file.id}
thumb={getURL(file, ImagePresets.cover)}
onDrop={onDelete}
/>
</SortableImageGridItem>
interface SortableImageGridProps {
onSortEnd: OnSortEnd;
items: IFile[];
locked: UploadStatus[];
onDelete: (file_id: IFile['id']) => void;
className?: string;
}
const SortableImageGrid: FC<SortableImageGridProps> = ({
items,
locked,
onDelete,
className,
onSortEnd,
}) => {
const [draggingItem, setDraggingItem] = useState<IFile | null>(null);
const preventEvent = useCallback(event => event.preventDefault(), []);
const sensors = useSensors(
useSensor(TouchSensor, {
activationConstraint: {
delay: 200,
tolerance: 5,
},
}),
useSensor(MouseSensor)
);
const ids = useMemo(() => items.map(it => it.id ?? 0), [items]);
const onDragEnd = useCallback(
({ active, over }: DragEndEvent) => {
setDraggingItem(null);
if (active.id === over?.id || !over?.id) {
return;
}
const oldIndex = items.findIndex(it => it.id === active.id);
const newIndex = items.findIndex(it => it.id === over.id);
onSortEnd({ oldIndex, newIndex });
},
[items]
);
const onDragStart = useCallback(
({ active }: DragStartEvent) => {
if (!active.id) {
return;
}
const activeItem = items.find(it => it.id === active.id);
setDraggingItem(activeItem ?? null);
},
[items]
);
return (
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragEnd={onDragEnd}
onDragStart={onDragStart}
>
<SortableContext items={ids} strategy={rectSortingStrategy}>
<div className={classNames(styles.grid, className)} onDropCapture={preventEvent}>
{items
.filter(file => file && file.id)
.map((file, index) => (
<SortableImageGridItem
key={file.id}
id={file.id!}
className={file.id === draggingItem?.id ? styles.dragging : undefined}
>
<ImageUpload
id={file.id}
thumb={getURL(file, ImagePresets.cover)}
onDrop={onDelete}
/>
</SortableImageGridItem>
))}
{locked.map((item, index) => (
<ImageUpload
thumb={item.thumbnail}
progress={item.progress}
is_uploading
key={item.id}
/>
))}
{locked.map((item, index) => (
<SortableImageGridItem key={item.id} index={index} collection={1} disabled>
<ImageUpload thumb={item.thumbnail} progress={item.progress} is_uploading />
</SortableImageGridItem>
))}
</div>
);
}
);
<DragOverlay>
{draggingItem ? (
<div className={styles.overlay}>
<ImageUpload thumb={getURL(draggingItem, ImagePresets.cover)} />
</div>
) : null}
</DragOverlay>
</div>
</SortableContext>
</DndContext>
);
};
export { SortableImageGrid };

View file

@ -12,3 +12,12 @@
grid-template-columns: repeat(auto-fill, minmax(30vw, 1fr));
}
}
.overlay {
box-shadow: rgba(0, 0, 0, 0.3) 5px 5px 10px 2px;
border-radius: $radius;
}
.dragging {
opacity: 0.1;
}

View file

@ -1,11 +1,44 @@
import React from 'react';
import React, { FC } from 'react';
import { SortableElement } from 'react-sortable-hoc';
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import classNames from 'classnames';
import styles from './styles.module.scss';
const SortableImageGridItem = SortableElement(({ children }) => (
<div className={styles.item}>{children}</div>
));
interface SortableImageGridItemProps {
id: number;
disabled?: boolean;
className?: string;
}
const SortableImageGridItem: FC<SortableImageGridItemProps> = ({
children,
id,
disabled = false,
className,
}) => {
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
id,
disabled,
});
const style = {
transform: CSS.Transform.toString(transform),
transition,
};
return (
<div
ref={setNodeRef}
style={style}
{...attributes}
{...listeners}
className={classNames(styles.item, className)}
>
{children}
</div>
);
};
export { SortableImageGridItem };