Files
it-tools-pro/src/components/AutosaveBar.vue
T
jinghui 9cbec90347 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
2026-11-15 12:24:14 +08:00

311 lines
7.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, ref } from 'vue';
import { loadHistory, restored, restoreVersion, saveNow, savedAt, status } from '../composable/autosave';
const now = ref(Date.now());
setInterval(() => {
now.value = Date.now();
}, 10_000);
const timeLabel = computed(() => {
if (!savedAt.value) return '';
const d = new Date(savedAt.value);
const hh = String(d.getHours()).padStart(2, '0');
const mm = String(d.getMinutes()).padStart(2, '0');
return `${hh}:${mm}`;
});
const saving = ref(false);
async function onSave() {
if (saving.value) return;
saving.value = true;
try {
await saveNow();
} finally {
saving.value = false;
}
}
/** ---- 找回历史版本 ---- */
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 '已恢复保存的内容';
switch (status.value) {
case 'dirty':
return '未保存';
case 'saving':
return '保存中…';
case 'error':
return '保存失败';
case 'saved':
return `已保存 ${timeLabel.value}`;
default:
return '自动保存已开启';
}
});
const chipClass = computed(() => ({
'as-restore': restored.value,
'as-dirty': !restored.value && status.value === 'dirty',
'as-saving': status.value === 'saving',
'as-error': status.value === 'error',
'as-saved': status.value === 'saved',
'as-idle': !restored.value && status.value === 'idle',
}));
const dotClass = computed(() => {
if (restored.value) return 'green';
switch (status.value) {
case 'dirty':
return 'orange';
case 'saving':
return 'gray blink';
case 'error':
return 'red';
default:
return 'green';
}
});
const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即保存'));
</script>
<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"
:disabled="saving"
@click="onSave"
>
{{ saving ? '保存中…' : btnLabel }}
</button>
</div>
</div>
</template>
<style scoped>
.autosave-bar {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 9999;
pointer-events: none;
opacity: 0;
transform: translateY(8px);
transition: opacity .25s ease, transform .25s ease;
}
.autosave-bar.show {
opacity: 1;
transform: translateY(0);
}
.as-chip {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 7px 12px;
border-radius: 999px;
background: rgba(30, 30, 30, .92);
color: #eaeaea;
font-size: 12px;
line-height: 1;
box-shadow: 0 4px 16px rgba(0, 0, 0, .28);
backdrop-filter: blur(6px);
pointer-events: auto;
}
.as-text {
white-space: nowrap;
}
.as-dot {
width: 7px;
height: 7px;
border-radius: 50%;
flex: none;
}
.as-dot.green { background: #3ecf8e; }
.as-dot.orange { background: #f5a524; }
.as-dot.red { background: #f31260; }
.as-dot.gray { background: #a1a1aa; }
.blink { animation: as-blink 1s infinite; }
@keyframes as-blink {
0%, 100% { opacity: 1; }
50% { opacity: .25; }
}
.as-btn {
border: 0;
border-radius: 999px;
padding: 5px 11px;
font-size: 12px;
line-height: 1;
cursor: pointer;
background: #3ecf8e;
color: #08221a;
font-weight: 600;
transition: filter .15s ease;
}
.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; }
}
</style>