feat(autosave): 草稿找回语义对齐——恢复提示常驻到用户编辑、chip 显示草稿时间与体积、丢弃草稿、找回面板标「当前草稿」徽标

This commit is contained in:
jinghui committed 2026-11-15 15:08:22 +08:00
1 parent 9cbec90347
commit d9aab5d6ef
2 files changed
+114 -11

No files matched your search

+69 -5
View File
@@ -1,7 +1,15 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { loadHistory, restored, restoreVersion, saveNow, savedAt, status } from '../composable/autosave';
import { curTs, discardDraft, loadHistory, restored, restoredBytes, restoreVersion, saveNow, savedAt, status } from '../composable/autosave';
/** 当前功能名:面板每次打开时实时取工具页标题(不用 computed 缓存 DOM,避免路由切换后过期) */
const curToolName = ref('当前工具');
function refreshToolName() {
const h1 = document.querySelector('.tool-content h1, .tool-content .tool-header h1, h1');
const t = (h1?.textContent || '').trim();
curToolName.value = t || '当前工具';
}
const now = ref(Date.now());
setInterval(() => {
@@ -39,6 +47,7 @@ async function toggleHist() {
histOpen.value = false;
return;
}
refreshToolName();
histOpen.value = true;
histLoading.value = true;
try {
@@ -48,6 +57,19 @@ async function toggleHist() {
}
}
/** 丢弃服务端当前草稿:状态条提示消失,历史列表同步刷新 */
const discarding = ref(false);
async function onDiscard() {
if (discarding.value) return;
discarding.value = true;
try {
const ok = await discardDraft();
if (ok && histOpen.value) histItems.value = await loadHistory();
} finally {
discarding.value = false;
}
}
async function onPick(ts: number) {
if (picking.value) return;
picking.value = true;
@@ -73,7 +95,10 @@ function fmtSize(b: number): string {
/** 当前状态文案 —— 打开页面即常驻可见(idle = 自动保存已开启) */
const textLabel = computed(() => {
if (restored.value) return '已恢复保存的内容';
if (restored.value) {
const sz = restoredBytes.value ? ` · ${fmtSize(restoredBytes.value)}` : '';
return `已恢复当前功能草稿 ${timeLabel.value}${sz}`;
}
switch (status.value) {
case 'dirty':
return '未保存';
@@ -124,17 +149,25 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
<button class="as-x" @click="histOpen = false">×</button>
</div>
<div class="as-panel-body">
<div class="as-cur">
当前功能:<b>{{ curToolName }}</b>
<small>草稿按工具隔离,只显示本工具的保存记录</small>
</div>
<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"
v-for="(it, i) in histItems"
:key="it.ts"
class="as-hitem"
:class="{ 'as-cur-item': i === 0 && curTs > 0 && it.ts === curTs }"
:disabled="picking"
:title="`点击恢复到 ${fmtTime(it.ts)} 的内容`"
@click="onPick(it.ts)"
>
<span class="as-hdate">{{ fmtTime(it.ts) }}</span>
<span class="as-hdate">
<span v-if="i === 0 && curTs > 0 && it.ts === curTs" class="as-cur-tag">当前草稿</span>
{{ fmtTime(it.ts) }}
</span>
<span class="as-hsize">{{ fmtSize(it.bytes) }}</span>
</button>
</div>
@@ -143,6 +176,15 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
<div class="as-chip" :class="chipClass">
<span class="as-dot" :class="dotClass" />
<span class="as-text">{{ textLabel }}</span>
<button
v-if="restored"
class="as-btn ghost danger"
:disabled="discarding || saving"
title="丢弃服务端保存的这份草稿(当前页面内容不受影响)"
@click="onDiscard"
>
{{ discarding ? '丢弃中…' : '丢弃草稿' }}
</button>
<button
class="as-btn ghost"
:disabled="saving || histLoading || picking"
@@ -231,6 +273,8 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
font-weight: 500;
}
.as-btn.ghost:hover { border-color: rgba(255, 255, 255, .55); color: #fff; filter: none; }
.as-btn.ghost.danger { color: #f31260; border-color: rgba(243, 18, 96, .5); }
.as-btn.ghost.danger:hover { border-color: #f31260; color: #ff6b9d; }
.as-error .as-btn {
background: #f31260;
color: #fff;
@@ -302,8 +346,28 @@ const btnLabel = computed(() => (status.value === 'error' ? '重试' : '立即
}
.as-hitem:hover { background: rgba(62, 207, 142, .22); }
.as-hitem:disabled { opacity: .6; cursor: default; }
.as-hdate { white-space: nowrap; }
.as-hdate { white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.as-hsize { color: #a1a1aa; white-space: nowrap; }
.as-cur {
padding: 6px 9px 7px;
font-size: 12px;
color: #eaeaea;
background: rgba(62, 207, 142, .12);
border-radius: 8px;
line-height: 1.5;
}
.as-cur b { color: #3ecf8e; }
.as-cur small { display: block; color: #a1a1aa; font-size: 11px; }
.as-cur-item { outline: 1px solid rgba(62, 207, 142, .45); }
.as-cur-tag {
background: #3ecf8e;
color: #08221a;
font-size: 10px;
font-weight: 700;
border-radius: 4px;
padding: 1px 5px;
flex: none;
}
@media (max-width: 640px) {
.autosave-bar { right: 12px; bottom: 76px; }
}
+45 -6
View File
@@ -18,6 +18,10 @@ export type AutosaveStatus = 'idle' | 'dirty' | 'saving' | 'saved' | 'error';
export const status = ref<AutosaveStatus>('idle');
export const savedAt = ref(0);
export const restored = ref(false);
/** 已恢复草稿的体积(字节,utf8),给状态条显示用 */
export const restoredBytes = ref(0);
/** 服务端当前草稿的时间戳(0 = 无当前草稿,如已丢弃),面板按它决定「当前草稿」徽标 */
export const curTs = ref(0);
export const hasUnsaved = ref(false);
const INTERVAL_MS = 60_000; // 每 1 分钟
@@ -186,6 +190,7 @@ export async function saveNow(): Promise<boolean> {
hasUnsaved.value = false;
savedAt.value = Date.now();
status.value = 'saved';
restored.value = false;
return true;
}
status.value = 'error';
@@ -212,14 +217,43 @@ async function tryRestore(): Promise<void> {
restored.value = true;
status.value = 'saved';
savedAt.value = Number(body.data.ts) * 1000 || Date.now();
setTimeout(() => {
restored.value = false;
}, 4000);
try {
restoredBytes.value = new TextEncoder().encode(JSON.stringify(body.data.state)).length;
} catch {
restoredBytes.value = 0;
}
// 不做定时消失:恢复提示常驻到用户开始编辑(markDirty 会清掉),
// 让「当前功能的草稿」始终显示出来、随时可操作(丢弃 / 找回历史)。
} catch {
/* 静默失败,不阻断编辑 */
}
}
/** 丢弃服务端当前草稿(页面上已显示的内容不动,只是不再保留该草稿) */
export async function discardDraft(): Promise<boolean> {
if (!toolKey) return false;
try {
const { status: st } = await apiFetch('/draft/delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...licenseHeaders(toolKey) },
body: JSON.stringify({ machine: getMachineId(), tool: toolKey }),
});
if (st === 200) {
restored.value = false;
restoredBytes.value = 0;
savedAt.value = 0;
dirty = false;
hasUnsaved.value = false;
status.value = 'idle';
curTs.value = 0;
return true;
}
return false;
} catch {
return false;
}
}
/** 历史版本清单(右下角「找回」面板用),失败返回空数组 */
export async function loadHistory(): Promise<{ ts: number; bytes: number }[]> {
if (!toolKey) return [];
@@ -228,6 +262,7 @@ export async function loadHistory(): Promise<{ ts: number; bytes: number }[]> {
`/draft/history?machine=${encodeURIComponent(getMachineId())}&tool=${encodeURIComponent(toolKey)}`,
);
if (st !== 200 || !Array.isArray(body?.data?.items)) return [];
curTs.value = Number(body.data.curTs) || 0;
return body.data.items;
} catch {
return [];
@@ -250,9 +285,11 @@ export async function restoreVersion(ts: number): Promise<boolean> {
hasUnsaved.value = false;
status.value = 'saved';
savedAt.value = ts * 1000 || Date.now();
window.setTimeout(() => {
restored.value = false;
}, 4000);
try {
restoredBytes.value = new TextEncoder().encode(JSON.stringify(body.data.state)).length;
} catch {
restoredBytes.value = 0;
}
return true;
} catch {
return false;
@@ -272,6 +309,8 @@ export function attach(tool: string): void {
dirty = false;
hasUnsaved.value = false;
restored.value = false;
restoredBytes.value = 0;
curTs.value = 0;
status.value = 'idle';
savedAt.value = 0;
if (!bound) {