export type DraftStorage = Pick const prefix = 'workbench-draft-v1:' export function draftKey(scope: string[]) { return prefix + JSON.stringify(scope) } export function readDraft(storage: DraftStorage, scope: string[], empty: T, valid: (value: unknown) => value is T, olderVersions = false): T { const raw = storage.getItem(draftKey(scope)) if (raw !== null) { const record = JSON.parse(raw); if (record.cleared) return empty; if (!valid(record.value)) throw new Error('草稿格式无法读取'); return record.value } if (olderVersions) { const base = scope.slice(0,-1), current = Number(scope.at(-1)); let latest = -1, found: T | undefined for (let i=0;i=current || version<=latest) continue const record=JSON.parse(storage.getItem(key) ?? 'null'); if (record?.cleared) continue if (valid(record?.value)) { latest=version; found=record.value } } if(found) return found } return empty } export function writeDraft(storage: DraftStorage, scope: string[], value: T) { storage.setItem(draftKey(scope), JSON.stringify({ value }, (key, item) => key === 'sending' || key === 'error' ? undefined : item)) } export function clearDraft(storage: DraftStorage, scope: string[], allVersions = false) { if(allVersions) { const base=scope.slice(0,-1); const keys:string[]=[]; for(let i=0;i