Files
apes-sidebar/src/client/editor-dirty.ts
T
Menghuan1918 4224c78a98 fix(editor): 原生页签关闭与桌面退出保留未保存草稿(#1072 / #1071)
宿主原生页签条的关闭路径只有一个同步清理钩子(唯一的阻止方式是 throw,
会把异常抛给 replaceTab / 公共 close() 调用方),桌面 Cmd+Q 则完全在壳内
完成(before-quit → app.exitRequested),页面没有可取消事件;壳内也没有
will-prevent-unload 处理器。插件因此不伪造拦截,改为降低损失:

- 草稿变脏时按 1 s 节流把实时文档写入 localStorage(sessionId::path,
  单条 200k 字符 / 总计 600k / 最多 8 条 FIFO)
- 原生 registerCloseHandler('editor') 与 beforeunload 时立即 flush
- 重开同一文件提示恢复;保存、确认丢弃、确认刷新/换文件即删除备份

新增 src/client/editor-draft-backup.ts;docs/host-contracts.md §C 记录宿主
限制与将来的接法。测试:新增 editor-draft-backup.spec.tsx(8 例)+ guard
spec 2 例;去掉任一修复点后对应用例均转红。
2026-11-04 21:04:16 +08:00

148 lines
5.7 KiB
TypeScript

/**
* Unsaved-edit registry: which editor tabs currently hold a draft that
* differs from disk. The editor host registers its tab's dirty state here
* (the toolbar report already carries it), so the flows that would silently
* drop the draft can ask first:
*
* - the sidebar's tab close (the X button, the middle click, the tab context
* menu, and the tree's close-on-rename/delete path all land there),
* - the browser unload (refresh / close tab / navigate away) through a
* `beforeunload` guard.
*
* A confirmed discard also drops the file's durable draft backup
* (`editor-draft-backup.ts`): that backup is the same draft and must not be
* offered back on the next open.
*
* Module-level and keyed by `sessionId::tabId`: a tab id is unique only WITHIN
* one session — the native right column counts its own `tab1`…, and the bottom
* workbench reuses `editor:<path>` for the same file in every session. Keyed by
* the bare tab id, session B's editor mounting over the same id DELETED session
* A's record (its mount reports `dirty === false`), so A's still-live draft
* outlived its guard and a refresh dropped it without a word. The native
* surface accounts the same way (`viewKey`, native/tab-adapter.tsx).
* An entry is written while a tab is dirty and deleted the moment it goes
* clean (save) or its host unmounts (tab closed, in-place path switch), so a
* stale entry can never keep a guard armed.
*/
import { clearDraftBackup } from './editor-draft-backup.ts'
/** One registered draft: the id it was recorded under, its session, and the file. */
interface DirtyEntry {
tabId: string
sessionId: string
path: string
}
const dirtyTabs = new Map<string, DirtyEntry>()
const listeners = new Set<() => void>()
/** Monotonic change counter: a stable `useSyncExternalStore` snapshot. */
let revision = 0
/** The map key of one tab: ids only identify a tab inside their session. */
function keyOf(sessionId: string, tabId: string): string {
return `${sessionId}::${tabId}`
}
/** Notify every subscriber (the `beforeunload` guard). */
function notify(): void {
revision += 1
for (const listener of [...listeners]) listener()
}
/**
* Publish one tab's dirty state: `true` registers the draft (session + path
* ride along for scoped queries), `false` / `undefined` clears it.
*/
export function setEditorDirty(tabId: string, dirty: boolean, sessionId: string, path: string): void {
const key = keyOf(sessionId, tabId)
if (!dirty) {
if (dirtyTabs.delete(key)) notify()
return
}
const previous = dirtyTabs.get(key)
if (previous !== undefined && previous.path === path) return
dirtyTabs.set(key, { tabId, sessionId, path })
notify()
}
/**
* Drop a tab's entry (its editor host unmounted). Idempotent.
* @param sessionId - the owning session; omitted drops the id in EVERY session
* (the shape tests use for cleanup — a caller that knows its session should
* always name it, or it would clear a same-numbered tab elsewhere).
*/
export function clearEditorDirty(tabId: string, sessionId?: string): void {
if (sessionId !== undefined) {
if (dirtyTabs.delete(keyOf(sessionId, tabId))) notify()
return
}
let removed = false
for (const [key, entry] of dirtyTabs) {
if (entry.tabId !== tabId) continue
dirtyTabs.delete(key)
removed = true
}
if (removed) notify()
}
/**
* Whether one tab holds an unsaved draft.
* @param sessionId - the owning session; omitted matches the id in any session
* (the conservative read: a caller that cannot name the session should still
* warn rather than pass a live draft through).
*/
export function isEditorDirty(tabId: string, sessionId?: string): boolean {
if (sessionId !== undefined) return dirtyTabs.has(keyOf(sessionId, tabId))
for (const entry of dirtyTabs.values()) {
if (entry.tabId === tabId) return true
}
return false
}
/** How many tabs hold unsaved drafts across every session (the unload guard). */
export function dirtyCount(): number {
return dirtyTabs.size
}
/** Subscribe to dirty-set changes; returns the disposer. */
export function subscribeEditorDirty(listener: () => void): () => void {
listeners.add(listener)
return () => { listeners.delete(listener) }
}
/** The change counter (a stable `useSyncExternalStore` snapshot). */
export function editorDirtyRevision(): number {
return revision
}
/**
* Ask before dropping `tabId`'s unsaved draft. Returns true when the caller
* may proceed (the tab is clean, the user confirmed, or no `confirm` is
* available — letting a tab become unclosable would be worse than skipping one
* prompt). On confirm the entry is cleared immediately, so a follow-up close in
* the same tick does not double-prompt; the editor host's own cleanup would do
* it one commit later anyway.
*
* Shared by the sidebar's tab close and the file tree's close-on-delete so
* both paths warn exactly once, with the same copy, and drop the durable
* backup of every discarded draft.
* @param sessionId - the owning session (see {@link isEditorDirty} for the
* omitted case).
*/
export function confirmDiscardDraft(tabId: string, message: string, sessionId?: string): boolean {
const matches = [...dirtyTabs.entries()].filter(([key, entry]) =>
entry.tabId === tabId && (sessionId === undefined || key === keyOf(sessionId, tabId)))
if (matches.length === 0) return true
const confirmed = typeof window.confirm === 'function' ? window.confirm(message) : true
if (!confirmed) return false
for (const [key, entry] of matches) {
dirtyTabs.delete(key)
// The durable backup is the same draft: a confirmed discard must not be
// offered back on the next open.
clearDraftBackup(entry.sessionId, entry.path)
}
notify()
return true
}