feat(autosave): 找回历史版本——「找回」面板一键恢复任意保存档

- AutosaveBar 新增「找回」按钮与历史面板:列出该工具近20份保存留档(时间精确到秒),点任意一条立即恢复成该版继续编辑
- autosave.ts 导出 loadHistory/restoreVersion
- relay 新增 /draft/history 与 /draft/load?ts= 端点;save 落盘留档20份,同秒连续保存只覆盖不重复占档
- README 自动保存章节补「找回历史版本」;Home NEW_FEATURES 置顶条目
- 同步 in-repo relay 副本 server.py
This commit is contained in:
jinghui committed 2026-11-15 12:24:14 +08:00
1 parent ce512f1966
commit 9cbec90347
5 files changed
+352 -13

No files matched your search

+152 -2
View File
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { restored, saveNow, savedAt, status } from '../composable/autosave';
import { loadHistory, restored, restoreVersion, saveNow, savedAt, status } from '../composable/autosave';
const now = ref(Date.now());
setInterval(() => {
@@ -28,9 +28,52 @@ async function onSave() {
}
}
/** ---- 找回历史版本 ---- */
const histOpen = ref(false);
const histLoading = ref(false);
const histItems = ref<{ ts: number; bytes: number }[]>([]);
const picking = ref(false);
async function toggleHist() {
if (histOpen.value) {
histOpen.value = false;
return;
}
histOpen.value = true;
histLoading.value = true;
try {
histItems.value = await loadHistory();
} finally {
histLoading.value = false;
}
}
async function onPick(ts: number) {
if (picking.value) return;
picking.value = true;
try {
const ok = await restoreVersion(ts);
if (ok) histOpen.value = false;
} finally {
picking.value = false;
}
}
function fmtTime(ts: number): string {
const d = new Date(ts * 1000);
const p = (n: number) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
function fmtSize(b: number): string {
if (b < 1024) return `${b} B`;
if (b < 1024 * 1024) return `${(b / 1024).toFixed(1)} KB`;
return `${(b / 1024 / 1024).toFixed(1)} MB`;
}
/** 当前状态文案 —— 打开页面即常驻可见(idle = 自动保存已开启) */
const textLabel = computed(() => {
if (restored.value) return '已恢复上次未保存的内容';
if (restored.value) return '已恢复保存的内容';
switch (status.value) {
case 'dirty':
return '未保存';
@@ -74,9 +117,40 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
<template>
<!-- 常驻右下角:任何工具页打开即可见 -->
<div class="autosave-bar show">
<!-- 找回历史版本面板 -->
<div v-if="histOpen" class="as-panel">
<div class="as-panel-hd">
<span>找回历史版本</span>
<button class="as-x" @click="histOpen = false">×</button>
</div>
<div class="as-panel-body">
<div v-if="histLoading" class="as-empty">加载中…</div>
<div v-else-if="!histItems.length" class="as-empty">暂无历史版本<br /><small>每次保存都会自动留档</small></div>
<button
v-for="it in histItems"
:key="it.ts"
class="as-hitem"
:disabled="picking"
:title="`点击恢复到 ${fmtTime(it.ts)} 的内容`"
@click="onPick(it.ts)"
>
<span class="as-hdate">{{ fmtTime(it.ts) }}</span>
<span class="as-hsize">{{ fmtSize(it.bytes) }}</span>
</button>
</div>
</div>
<div class="as-chip" :class="chipClass">
<span class="as-dot" :class="dotClass" />
<span class="as-text">{{ textLabel }}</span>
<button
class="as-btn ghost"
:disabled="saving || histLoading || picking"
title="找回历史保存的版本,点击恢复后可继续编辑"
@click="toggleHist"
>
{{ picking ? '恢复中…' : '找回' }}
</button>
<button
v-if="status !== 'saving'"
class="as-btn"
@@ -150,10 +224,86 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
}
.as-btn:hover { filter: brightness(1.08); }
.as-btn:disabled { opacity: .6; cursor: default; }
.as-btn.ghost {
background: transparent;
color: #cfcfcf;
border: 1px solid rgba(255, 255, 255, .28);
font-weight: 500;
}
.as-btn.ghost:hover { border-color: rgba(255, 255, 255, .55); color: #fff; filter: none; }
.as-error .as-btn {
background: #f31260;
color: #fff;
}
/* 历史版本面板 */
.as-panel {
position: absolute;
right: 0;
bottom: calc(100% + 8px);
width: 258px;
background: rgba(30, 30, 30, .97);
border: 1px solid rgba(255, 255, 255, .12);
border-radius: 12px;
box-shadow: 0 8px 28px rgba(0, 0, 0, .4);
pointer-events: auto;
overflow: hidden;
color: #eaeaea;
}
.as-panel-hd {
display: flex;
align-items: center;
justify-content: space-between;
padding: 9px 12px;
font-size: 12px;
font-weight: 600;
border-bottom: 1px solid rgba(255, 255, 255, .1);
}
.as-x {
border: 0;
background: transparent;
color: #a1a1aa;
font-size: 16px;
line-height: 1;
cursor: pointer;
padding: 0 2px;
}
.as-x:hover { color: #fff; }
.as-panel-body {
max-height: 250px;
overflow-y: auto;
padding: 6px;
display: flex;
flex-direction: column;
gap: 4px;
}
.as-empty {
text-align: center;
color: #a1a1aa;
font-size: 12px;
padding: 18px 8px;
line-height: 1.6;
}
.as-empty small { opacity: .7; }
.as-hitem {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
width: 100%;
border: 0;
border-radius: 8px;
padding: 7px 9px;
background: rgba(255, 255, 255, .06);
color: #eaeaea;
font-size: 12px;
cursor: pointer;
text-align: left;
transition: background .15s ease;
}
.as-hitem:hover { background: rgba(62, 207, 142, .22); }
.as-hitem:disabled { opacity: .6; cursor: default; }
.as-hdate { white-space: nowrap; }
.as-hsize { color: #a1a1aa; white-space: nowrap; }
@media (max-width: 640px) {
.autosave-bar { right: 12px; bottom: 76px; }
}
+41 -10
View File
@@ -204,16 +204,8 @@ async function tryRestore(): Promise<void> {
`/draft/load?machine=${encodeURIComponent(getMachineId())}&tool=${encodeURIComponent(toolKey)}`,
);
if (st !== 200 || !body?.data?.state) return;
const cur = snapshot();
if (cur) {
// 已有用户输入则不打扰,仅在几乎全空时恢复
const filled = Object.values(cur).filter(v => {
if (typeof v === 'string') return v.trim() !== '';
if (Array.isArray(v)) return v.length > 0;
return v === true;
}).length;
if (filled > 0) return;
}
// 往返期间用户已开始编辑则不打扰;进页面时页面值都是工具默认值,直接恢复
if (dirty) return;
await nextTick();
const applied = applyState(body.data.state);
if (applied <= 0) return; // 字段对不上(工具改版等)则不打扰用户
@@ -228,6 +220,45 @@ async function tryRestore(): Promise<void> {
}
}
/** 历史版本清单(右下角「找回」面板用),失败返回空数组 */
export async function loadHistory(): Promise<{ ts: number; bytes: number }[]> {
if (!toolKey) return [];
try {
const { status: st, body } = await apiFetch(
`/draft/history?machine=${encodeURIComponent(getMachineId())}&tool=${encodeURIComponent(toolKey)}`,
);
if (st !== 200 || !Array.isArray(body?.data?.items)) return [];
return body.data.items;
} catch {
return [];
}
}
/** 回填指定历史版本到当前页面(找回重新编辑),返回是否成功 */
export async function restoreVersion(ts: number): Promise<boolean> {
if (!toolKey || !ts) return false;
try {
const { status: st, body } = await apiFetch(
`/draft/load?machine=${encodeURIComponent(getMachineId())}&tool=${encodeURIComponent(toolKey)}&ts=${ts}`,
);
if (st !== 200 || !body?.data?.state) return false;
await nextTick();
const applied = applyState(body.data.state);
if (applied <= 0) return false;
restored.value = true;
dirty = false;
hasUnsaved.value = false;
status.value = 'saved';
savedAt.value = ts * 1000 || Date.now();
window.setTimeout(() => {
restored.value = false;
}, 4000);
return true;
} catch {
return false;
}
}
function stopTimer(): void {
if (timer !== null) {
window.clearInterval(timer);
+1
View File
@@ -11,6 +11,7 @@ import { useRecentTools } from '@/composable/recent-tools';
// 近期新功能亮点(手动维护,新工具上线时加一条)
// ★铁规则:每完成一批新功能/新工具,必须在这里最前面加条目(新→旧),否则首页看不到
const NEW_FEATURES = [
{ path: '/qrcode-generator', icon: '🕘', name: '草稿找回历史版本', desc: '右下角自动保存条新增「找回」按钮,列出该工具近 20 份保存历史(时间精确到秒),点任意一条立即恢复成那一版继续编辑' },
{ path: '/admin', icon: '📁', name: '后台文件管理', desc: '默认聚合所有用户文件与AI生成图片,每页50条自动分页显示总记录数,图片100×100缩略图、其他文件显示类型,全部支持单独下载' },
{ path: '/ai-gen', icon: '🗂️', name: '图片历史铁规则', desc: '所有生图功能右侧 50×50 缩略图历史竖排、点击看大图,按登录账号/机器码只看本人的,生成即保存到后台指定目录,全部功能历史最多显示100条' },
{ path: '/face-swap', icon: '🎭', name: 'AI 换脸', desc: '上传源人脸与目标照片,本地 AI 一键换脸(insightface 人脸检测+inswapper),授权功能支持免费试用' },