feat(v1.4.6): 文件夹右键批量重命名文件(前缀/后缀/全替换+子目录+实时预览)
This commit is contained in:
1 parent
7d0023949b
commit
58900d0d45
10 files changed
+596
-11
No files matched your search
@@ -19,3 +19,9 @@ export async function status(taskId) {
|
||||
export async function cancel(taskId) {
|
||||
return api.post(`/batch/cancel/${taskId}`);
|
||||
}
|
||||
|
||||
/** 批量重命名预览(不执行),返回 { count, total, samples: [{from,to}], skipped } */
|
||||
export async function renamePreview(payload) {
|
||||
// payload: { libraryId, folder, options: { mode, text, includeSubdirs } }
|
||||
return api.post('/batch/rename-preview', payload);
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { Replace, Loader2, X, Play, Square, CheckCircle2, AlertTriangle, ArrowRight } from 'lucide-react';
|
||||
import { batchAPI } from '../api';
|
||||
|
||||
/**
|
||||
* 批量重命名弹窗(文件夹级)
|
||||
* 模式:
|
||||
* prefix 前缀添加 —— 在原文件名前加文本
|
||||
* suffix 后缀添加 —— 在原文件名后加文本
|
||||
* replace 全部改名 —— 原文件名整体替换为文本(同目录多个文件自动加序号 -001)
|
||||
*
|
||||
* 流程:配置(实时预览)→ 启动任务(立即返回 taskId)→ 1s 轮询进度 → 完成/取消
|
||||
*/
|
||||
|
||||
const MODES = [
|
||||
{ key: 'prefix', label: '前面加', desc: '在原文件名前加文本', hint: '文本 + 原文件名 + 扩展名' },
|
||||
{ key: 'suffix', label: '后面加', desc: '在原文件名后加文本', hint: '原文件名 + 文本 + 扩展名' },
|
||||
{ key: 'replace', label: '全部改名', desc: '原文件名替换为文本', hint: '同目录多个文件自动编号:文本-001、文本-002…' }
|
||||
];
|
||||
|
||||
const ILLEGAL_RE = /[\\/:*?"<>|]/;
|
||||
|
||||
function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
|
||||
const [phase, setPhase] = useState('config'); // config | running | done
|
||||
const [mode, setMode] = useState('prefix');
|
||||
const [text, setText] = useState('');
|
||||
const [includeSubdirs, setIncludeSubdirs] = useState(true);
|
||||
const [preview, setPreview] = useState(null); // { count, total, samples, skipped }
|
||||
const [previewErr, setPreviewErr] = useState('');
|
||||
const [starting, setStarting] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const [taskId, setTaskId] = useState(null);
|
||||
const [progress, setProgress] = useState(null);
|
||||
const pollRef = useRef(null);
|
||||
const previewTimerRef = useRef(null);
|
||||
|
||||
const stopPolling = useCallback(() => {
|
||||
if (pollRef.current) {
|
||||
clearInterval(pollRef.current);
|
||||
pollRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
// 打开时重置
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
setPhase('config');
|
||||
setMode('prefix');
|
||||
setText('');
|
||||
setIncludeSubdirs(true);
|
||||
setPreview(null);
|
||||
setPreviewErr('');
|
||||
setError('');
|
||||
setTaskId(null);
|
||||
setProgress(null);
|
||||
setStarting(false);
|
||||
return () => stopPolling();
|
||||
}, [isOpen, stopPolling]);
|
||||
|
||||
// 实时预览(防抖 300ms)
|
||||
useEffect(() => {
|
||||
if (!isOpen || phase !== 'config') return;
|
||||
if (previewTimerRef.current) clearTimeout(previewTimerRef.current);
|
||||
const t = text.trim();
|
||||
if (!t) {
|
||||
setPreview(null);
|
||||
setPreviewErr('');
|
||||
return;
|
||||
}
|
||||
previewTimerRef.current = setTimeout(async () => {
|
||||
try {
|
||||
const resp = await batchAPI.renamePreview({
|
||||
libraryId, folder,
|
||||
options: { mode, text: t, includeSubdirs }
|
||||
});
|
||||
setPreview(resp);
|
||||
setPreviewErr('');
|
||||
} catch (err) {
|
||||
setPreview(null);
|
||||
setPreviewErr(err?.message || '预览失败');
|
||||
}
|
||||
}, 300);
|
||||
return () => { if (previewTimerRef.current) clearTimeout(previewTimerRef.current); };
|
||||
}, [isOpen, phase, mode, text, includeSubdirs, libraryId, folder]);
|
||||
|
||||
const startPolling = useCallback((id) => {
|
||||
stopPolling();
|
||||
pollRef.current = setInterval(async () => {
|
||||
try {
|
||||
const s = await batchAPI.status(id);
|
||||
setProgress(s);
|
||||
if (s.status !== 'running') {
|
||||
stopPolling();
|
||||
setPhase('done');
|
||||
onFinished?.(s);
|
||||
}
|
||||
} catch (_) { /* 网络抖动,继续轮询 */ }
|
||||
}, 1000);
|
||||
}, [stopPolling, onFinished]);
|
||||
|
||||
const handleStart = async () => {
|
||||
setError('');
|
||||
const t = text.trim();
|
||||
if (!t) {
|
||||
setError('请输入重命名文本');
|
||||
return;
|
||||
}
|
||||
if (ILLEGAL_RE.test(t)) {
|
||||
setError('文本包含文件名非法字符 \\ / : * ? " < > |');
|
||||
return;
|
||||
}
|
||||
setStarting(true);
|
||||
try {
|
||||
const resp = await batchAPI.start({
|
||||
libraryId, folder, type: 'rename',
|
||||
options: { mode, text: t, includeSubdirs }
|
||||
});
|
||||
setTaskId(resp.taskId);
|
||||
setPhase('running');
|
||||
setProgress({
|
||||
status: 'running', total: resp.total, done: 0, failed: 0,
|
||||
skipped: 0, current: '', errors: []
|
||||
});
|
||||
startPolling(resp.taskId);
|
||||
} catch (err) {
|
||||
setError(err?.message || '启动失败');
|
||||
} finally {
|
||||
setStarting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCancel = async () => {
|
||||
if (!taskId) return;
|
||||
try { await batchAPI.cancel(taskId); } catch (_) { /* 已结束 */ }
|
||||
stopPolling();
|
||||
setPhase('done');
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
stopPolling();
|
||||
onClose();
|
||||
};
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
const pct = progress && progress.total > 0
|
||||
? Math.round(((progress.done + progress.failed + progress.skipped) / progress.total) * 100)
|
||||
: 0;
|
||||
const modeInfo = MODES.find((m) => m.key === mode);
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" onClick={handleClose}>
|
||||
<div
|
||||
className="bg-white dark:bg-gray-800 rounded-xl shadow-2xl w-full max-w-xl max-h-[90vh] overflow-hidden flex flex-col"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{/* 头部 */}
|
||||
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<Replace className="w-5 h-5 text-violet-500" />
|
||||
<h2 className="text-lg font-semibold text-gray-900 dark:text-white">批量重命名文件</h2>
|
||||
<span className="text-xs px-2 py-0.5 rounded-full bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-300 truncate max-w-[240px]">
|
||||
{folder}
|
||||
</span>
|
||||
</div>
|
||||
<button onClick={handleClose} className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-700">
|
||||
<X className="w-5 h-5 text-gray-500" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 内容 */}
|
||||
<div className="flex-1 overflow-y-auto p-5">
|
||||
{phase === 'config' && (
|
||||
<>
|
||||
{/* 重命名模式 */}
|
||||
<div className="mb-4">
|
||||
<div className="text-xs font-medium text-gray-500 dark:text-gray-400 mb-1.5">重命名方式</div>
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
{MODES.map((m) => (
|
||||
<button
|
||||
key={m.key}
|
||||
onClick={() => setMode(m.key)}
|
||||
className={`px-3 py-2.5 rounded-lg text-sm border transition-colors ${mode === m.key
|
||||
? 'border-violet-500 bg-violet-50 dark:bg-violet-900/40 text-violet-600 dark:text-violet-300 font-medium'
|
||||
: 'border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:border-gray-400'}`}
|
||||
>
|
||||
<div>{m.label}</div>
|
||||
<div className="text-[10px] font-normal opacity-70 mt-0.5">{m.desc}</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="text-[11px] text-gray-400 dark:text-gray-500 mt-1.5">
|
||||
规则:{modeInfo.hint}(扩展名始终保留)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 文本输入 */}
|
||||
<div className="mb-4">
|
||||
<div className="text-xs font-medium text-gray-500 dark:text-gray-400 mb-1.5">文本内容</div>
|
||||
<input
|
||||
type="text"
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
placeholder={mode === 'replace' ? '例如:风景素材(全部文件改为 风景素材-001.jpg …)' : '例如:2026_'}
|
||||
maxLength={100}
|
||||
autoFocus
|
||||
className="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white text-sm focus:outline-none focus:ring-2 focus:ring-violet-500"
|
||||
/>
|
||||
{ILLEGAL_RE.test(text) && (
|
||||
<div className="text-[11px] text-red-500 mt-1">包含文件名非法字符 \ / : * ? " < > |</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 包含子目录 */}
|
||||
<label className="flex items-center gap-2 mb-4 cursor-pointer select-none">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={includeSubdirs}
|
||||
onChange={(e) => setIncludeSubdirs(e.target.checked)}
|
||||
className="w-4 h-4 rounded accent-violet-500"
|
||||
/>
|
||||
<span className="text-sm text-gray-700 dark:text-gray-300">包含子目录(递归处理所有层级文件)</span>
|
||||
</label>
|
||||
|
||||
{/* 实时预览 */}
|
||||
<div className="rounded-lg border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 p-3">
|
||||
<div className="text-xs font-medium text-gray-500 dark:text-gray-400 mb-2">预览</div>
|
||||
{!text.trim() && (
|
||||
<div className="text-[11px] text-gray-400">输入文本后显示改名效果</div>
|
||||
)}
|
||||
{text.trim() && previewErr && (
|
||||
<div className="text-[11px] text-red-500">{previewErr}</div>
|
||||
)}
|
||||
{text.trim() && !previewErr && !preview && (
|
||||
<div className="text-[11px] text-gray-400 flex items-center gap-1.5">
|
||||
<Loader2 className="w-3 h-3 animate-spin" /> 计算中…
|
||||
</div>
|
||||
)}
|
||||
{preview && (
|
||||
<>
|
||||
<div className="text-[11px] text-gray-600 dark:text-gray-300 mb-2">
|
||||
将重命名 <b className="text-violet-500">{preview.count}</b> 个文件
|
||||
{preview.skipped > 0 && <span className="text-gray-400">({preview.skipped} 个跳过)</span>}
|
||||
{!includeSubdirs && <span className="text-gray-400">,仅当前文件夹</span>}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
{preview.samples.map((s, i) => (
|
||||
<div key={i} className="flex items-center gap-2 text-[11px] font-mono">
|
||||
<span className="text-gray-500 dark:text-gray-400 truncate max-w-[45%]">{s.from}</span>
|
||||
<ArrowRight className="w-3 h-3 text-violet-400 flex-shrink-0" />
|
||||
<span className="text-violet-500 truncate">{s.to}</span>
|
||||
</div>
|
||||
))}
|
||||
{preview.total > preview.samples.length && (
|
||||
<div className="text-[10px] text-gray-400 pt-0.5">
|
||||
…共 {preview.total} 条
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mt-3 flex items-center gap-2 text-sm text-red-500">
|
||||
<AlertTriangle className="w-4 h-4" /> {error}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{phase === 'running' && (
|
||||
<div className="py-4">
|
||||
<div className="flex items-center justify-between text-sm text-gray-600 dark:text-gray-300 mb-2">
|
||||
<span className="flex items-center gap-2">
|
||||
<Loader2 className="w-4 h-4 animate-spin text-violet-500" />
|
||||
重命名中 {progress ? `${progress.done + progress.failed + progress.skipped}/${progress.total}` : ''}
|
||||
</span>
|
||||
<span className="tabular-nums">{pct}%</span>
|
||||
</div>
|
||||
<div className="h-2 rounded-full bg-gray-200 dark:bg-gray-700 overflow-hidden">
|
||||
<div className="h-full bg-violet-500 transition-all" style={{ width: `${pct}%` }} />
|
||||
</div>
|
||||
{progress?.current && (
|
||||
<div className="mt-2 text-[11px] text-gray-400 truncate">{progress.current}</div>
|
||||
)}
|
||||
<div className="flex justify-center mt-5">
|
||||
<button
|
||||
onClick={handleCancel}
|
||||
className="px-4 py-2 rounded-lg text-sm border border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-2"
|
||||
>
|
||||
<Square className="w-3.5 h-3.5" /> 停止(已完成的保留)
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{phase === 'done' && (
|
||||
<div className="py-4">
|
||||
<div className="flex items-center gap-2 mb-4">
|
||||
<CheckCircle2 className="w-5 h-5 text-emerald-500" />
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{progress?.status === 'cancelled' ? '已停止' : '重命名完成'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-3 gap-3 text-center">
|
||||
<div className="rounded-lg bg-emerald-50 dark:bg-emerald-900/30 p-3">
|
||||
<div className="text-xl font-semibold text-emerald-600 dark:text-emerald-300 tabular-nums">{progress?.done ?? 0}</div>
|
||||
<div className="text-[11px] text-gray-500 dark:text-gray-400">已重命名</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-gray-100 dark:bg-gray-700/60 p-3">
|
||||
<div className="text-xl font-semibold text-gray-600 dark:text-gray-300 tabular-nums">{progress?.skipped ?? 0}</div>
|
||||
<div className="text-[11px] text-gray-500 dark:text-gray-400">已跳过</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-red-50 dark:bg-red-900/30 p-3">
|
||||
<div className="text-xl font-semibold text-red-600 dark:text-red-300 tabular-nums">{progress?.failed ?? 0}</div>
|
||||
<div className="text-[11px] text-gray-500 dark:text-gray-400">失败</div>
|
||||
</div>
|
||||
</div>
|
||||
{progress?.errors?.length > 0 && (
|
||||
<div className="mt-4 rounded-lg border border-red-200 dark:border-red-800 bg-red-50 dark:bg-red-900/20 p-3">
|
||||
<div className="text-xs font-medium text-red-600 dark:text-red-300 mb-1.5">失败明细</div>
|
||||
<div className="space-y-1">
|
||||
{progress.errors.slice(0, 8).map((e, i) => (
|
||||
<div key={i} className="text-[11px] text-red-500 dark:text-red-400 font-mono truncate">
|
||||
{e.path}: {e.error}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 底部按钮 */}
|
||||
<div className="px-5 py-4 border-t border-gray-200 dark:border-gray-700 flex justify-end gap-2.5">
|
||||
{phase === 'config' && (
|
||||
<>
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="px-4 py-2 rounded-lg text-sm border border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
onClick={handleStart}
|
||||
disabled={starting || !text.trim() || ILLEGAL_RE.test(text)}
|
||||
className="px-4 py-2 rounded-lg text-sm bg-violet-500 hover:bg-violet-600 text-white flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{starting ? <Loader2 className="w-4 h-4 animate-spin" /> : <Play className="w-4 h-4" />}
|
||||
开始重命名{preview ? `(${preview.count} 个)` : ''}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{phase === 'running' && (
|
||||
<button
|
||||
onClick={handleCancel}
|
||||
className="px-4 py-2 rounded-lg text-sm border border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
|
||||
>
|
||||
停止
|
||||
</button>
|
||||
)}
|
||||
{phase === 'done' && (
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="px-4 py-2 rounded-lg text-sm bg-violet-500 hover:bg-violet-600 text-white"
|
||||
>
|
||||
完成
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default BatchRenameModal;
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { Trash2, Edit3, Move, Copy, FolderPlus, Star, Droplets, Eraser, Scissors, Film } from 'lucide-react';
|
||||
import { Trash2, Edit3, Move, Copy, FolderPlus, Star, Droplets, Eraser, Scissors, Film, Replace } from 'lucide-react';
|
||||
|
||||
function ContextMenu({ isOpen, position, onClose, options }) {
|
||||
const menuRef = useRef(null);
|
||||
@@ -173,6 +173,11 @@ export const menuItems = {
|
||||
label: '批量加水印…',
|
||||
action
|
||||
}),
|
||||
batchRename: (action) => ({
|
||||
icon: <Replace size={16} />,
|
||||
label: '批量重命名文件…',
|
||||
action
|
||||
}),
|
||||
batchRemoveWatermark: (action) => ({
|
||||
icon: <Eraser size={16} />,
|
||||
label: '批量去水印…',
|
||||
|
||||
@@ -38,7 +38,7 @@ function Header() {
|
||||
// 授权系统 hub:版本 / 公告 / 在线人数
|
||||
const hub = useHub();
|
||||
const announcements = hub.announcements || [];
|
||||
const appVersion = hub.version ? `v${String(hub.version).replace(/^v/, '')}` : 'v1.4.5';
|
||||
const appVersion = hub.version ? `v${String(hub.version).replace(/^v/, '')}` : 'v1.4.6';
|
||||
|
||||
// 从全局 store 获取筛选状态
|
||||
const selectedFormats = filters.formats || [];
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useState, useEffect } from 'react';
|
||||
import { X, ScanText, Info, Save, FlaskConical, Github, ExternalLink, Wand2 } from 'lucide-react';
|
||||
import { settingsAPI } from '../api';
|
||||
|
||||
const APP_VERSION = 'v1.4.5';
|
||||
const APP_VERSION = 'v1.4.6';
|
||||
const REPO_URL = 'https://gitea.88531.cn/jinghui/flypic';
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@ import UndoToast from './UndoToast';
|
||||
import FolderSelector from './FolderSelector';
|
||||
import ConflictDialog from './ConflictDialog';
|
||||
import BatchWatermarkModal from './BatchWatermarkModal';
|
||||
import BatchRenameModal from './BatchRenameModal';
|
||||
import { createLogger } from '../utils/logger';
|
||||
|
||||
const logger = createLogger('Sidebar');
|
||||
@@ -54,6 +55,7 @@ function Sidebar() {
|
||||
const [contextMenu, setContextMenu] = useState({ isOpen: false, position: null, folder: null });
|
||||
// 批量水印弹窗 { mode: 'add'|'remove', folder }
|
||||
const [batchWatermark, setBatchWatermark] = useState(null);
|
||||
const [batchRename, setBatchRename] = useState(null);
|
||||
const [undoToast, setUndoToast] = useState({ isVisible: false, message: '', count: 0 });
|
||||
const [undoHistory, setUndoHistory] = useState([]); // 撤销历史栈,支持多次撤销
|
||||
const [dragOverFolder, setDragOverFolder] = useState(null); // 拖拽悬停的文件夹
|
||||
@@ -1513,6 +1515,18 @@ function Sidebar() {
|
||||
bumpRefresh(); // 让图片区重新加载(覆盖模式下缩略图已更新)
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* 批量重命名弹窗 */}
|
||||
<BatchRenameModal
|
||||
isOpen={!!batchRename}
|
||||
folder={batchRename?.folder}
|
||||
libraryId={currentLibraryId}
|
||||
onClose={() => setBatchRename(null)}
|
||||
onFinished={(result) => {
|
||||
logger.file(`批量重命名完成: ${batchRename?.folder}`, result);
|
||||
bumpRefresh(); // 让图片区重新加载(文件名/路径已更新)
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* Library Management */}
|
||||
<div className="p-4 border-b border-gray-200 dark:border-gray-700">
|
||||
@@ -1779,6 +1793,10 @@ function Sidebar() {
|
||||
menuItems.rename(() => handleStartRenameFolder(contextMenu.folder)),
|
||||
menuItems.move(() => handleMoveFolderClick(contextMenu.folder.path)),
|
||||
menuItems.divider(),
|
||||
menuItems.batchRename(() => {
|
||||
setContextMenu({ isOpen: false, position: null, folder: null });
|
||||
setBatchRename({ folder: contextMenu.folder.path });
|
||||
}),
|
||||
menuItems.batchAddWatermark(() => {
|
||||
setContextMenu({ isOpen: false, position: null, folder: null });
|
||||
setBatchWatermark({ mode: 'add', folder: contextMenu.folder.path });
|
||||
|
||||
Reference in new issue
Block a user