Desktop: drag and drop photos into the post form
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import CloseIcon from "../components/icons/CloseIcon";
|
||||
import RulesModal from "./RulesModal";
|
||||
import { EMPTY_FORM, CATEGORIES } from "../config/feedConstants";
|
||||
@@ -60,11 +60,53 @@ export default function CreateUpdatePost({ onClose, onSuccess, initialData, admi
|
||||
compressing,
|
||||
fileInputRef,
|
||||
handleFileChange,
|
||||
addFiles,
|
||||
removeImage,
|
||||
getFilesForUpload,
|
||||
getExistingUrlsToKeep,
|
||||
} = useImageUpload(isEdit ? initialData.images : []);
|
||||
|
||||
// Перетаскивание фото (drag & drop) — только на десктопе.
|
||||
// dragenter/dragleave приходят и от дочерних элементов зоны, поэтому
|
||||
// считаем глубину, иначе подсветка мигает при движении над превью.
|
||||
const [dragOver, setDragOver] = useState(false);
|
||||
const dragDepthRef = useRef(0);
|
||||
const isFileDrag = e => Array.from(e.dataTransfer?.types || []).includes("Files");
|
||||
|
||||
const dropZoneHandlers = isDesktop ? {
|
||||
onDragEnter: e => {
|
||||
if (!isFileDrag(e)) return;
|
||||
e.preventDefault();
|
||||
dragDepthRef.current += 1;
|
||||
setDragOver(true);
|
||||
},
|
||||
onDragOver: e => {
|
||||
if (!isFileDrag(e)) return;
|
||||
e.preventDefault();
|
||||
e.dataTransfer.dropEffect = compressing ? "none" : "copy";
|
||||
},
|
||||
onDragLeave: e => {
|
||||
if (!isFileDrag(e)) return;
|
||||
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
|
||||
if (dragDepthRef.current === 0) setDragOver(false);
|
||||
},
|
||||
onDrop: e => {
|
||||
if (!isFileDrag(e)) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dragDepthRef.current = 0;
|
||||
setDragOver(false);
|
||||
if (!compressing) addFiles(e.dataTransfer.files);
|
||||
},
|
||||
} : {};
|
||||
|
||||
// Файл, отпущенный мимо зоны, браузер открыл бы вместо сайта — гасим это
|
||||
// на всём окне формы, иначе введённые данные пропадут.
|
||||
const blockStrayDrop = isDesktop ? {
|
||||
onDragOver: e => { if (isFileDrag(e)) e.preventDefault(); },
|
||||
onDrop: e => { if (isFileDrag(e)) e.preventDefault(); },
|
||||
} : {};
|
||||
|
||||
const { save, loading: saving, error: apiError } = usePostMutation({ adminMode });
|
||||
|
||||
const [resultModal, setResultModal] = useState(null); // null | "success" | "blocked"
|
||||
@@ -160,6 +202,7 @@ export default function CreateUpdatePost({ onClose, onSuccess, initialData, admi
|
||||
// На десктопе форма закрывается только крестиком: случайный клик мимо
|
||||
// окна не должен терять заполненные поля
|
||||
onClick={e => !isDesktop && e.target === e.currentTarget && !loading && onClose()}
|
||||
{...blockStrayDrop}
|
||||
>
|
||||
<div className="f-modal">
|
||||
|
||||
@@ -228,8 +271,11 @@ export default function CreateUpdatePost({ onClose, onSuccess, initialData, admi
|
||||
{errors.description && <p className="f-err-msg">{errors.description}</p>}
|
||||
</div>
|
||||
|
||||
{/* Фотографии */}
|
||||
<div className="f-m-group">
|
||||
{/* Фотографии: на десктопе вся группа — зона для перетаскивания */}
|
||||
<div
|
||||
className={`f-m-group${isDesktop ? " f-img-drop-zone" : ""}${dragOver ? " is-dragover" : ""}`}
|
||||
{...dropZoneHandlers}
|
||||
>
|
||||
<label className="f-m-label">{t('post.photosLabel')}</label>
|
||||
|
||||
{uploadedImages.length > 0 && (
|
||||
@@ -262,8 +308,13 @@ export default function CreateUpdatePost({ onClose, onSuccess, initialData, admi
|
||||
/>
|
||||
{compressing ? (
|
||||
<span>{t('post.processing')}</span>
|
||||
) : dragOver ? (
|
||||
<span>{t('post.dropRelease')}</span>
|
||||
) : (
|
||||
<span>{uploadedImages.length > 0 ? t('post.addMore') : t('post.addPhoto')}</span>
|
||||
<span className="f-img-upload-text">
|
||||
<span>{uploadedImages.length > 0 ? t('post.addMore') : t('post.addPhoto')}</span>
|
||||
{isDesktop && <span className="f-img-upload-sub">{t('post.dropHint')}</span>}
|
||||
</span>
|
||||
)}
|
||||
</label>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user