2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: DraftStorageMode supports form-draft.ts by holding or executing the draft storage mode page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export type DraftStorageMode = 'local' | 'session'
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: DraftRecord supports form-draft.ts by holding or executing the draft record page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export interface DraftRecord<T> {
|
|
|
|
|
savedAt: number
|
|
|
|
|
payload: T
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: getStorage supports form-draft.ts by holding or executing the get storage page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
const getStorage = (mode: DraftStorageMode) => (mode === 'session' ? window.sessionStorage : window.localStorage)
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: buildDraftStorageKey supports form-draft.ts by holding or executing the build draft storage key page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export const buildDraftStorageKey = (...parts: Array<string | number | undefined | null>) => {
|
|
|
|
|
const normalizedParts = parts
|
|
|
|
|
.filter((part): part is string | number => part !== undefined && part !== null && String(part).trim().length > 0)
|
|
|
|
|
.map(part => String(part).trim())
|
2026-06-24 15:04:17 +08:00
|
|
|
return `fingertipdesigner:draft:${normalizedParts.join(':')}`
|
2026-03-26 12:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: saveDraftRecord supports form-draft.ts by holding or executing the save draft record page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export const saveDraftRecord = <T>(key: string, payload: T, mode: DraftStorageMode = 'local') => {
|
|
|
|
|
const record: DraftRecord<T> = {
|
|
|
|
|
savedAt: Date.now(),
|
|
|
|
|
payload
|
|
|
|
|
}
|
|
|
|
|
getStorage(mode).setItem(key, JSON.stringify(record))
|
|
|
|
|
return record
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: loadDraftRecord supports form-draft.ts by holding or executing the load draft record page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export const loadDraftRecord = <T>(key: string, mode: DraftStorageMode = 'local') => {
|
|
|
|
|
const rawValue = getStorage(mode).getItem(key)
|
|
|
|
|
if (!rawValue) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(rawValue) as DraftRecord<T>
|
|
|
|
|
} catch {
|
|
|
|
|
getStorage(mode).removeItem(key)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: clearDraftRecord supports form-draft.ts by holding or executing the clear draft record page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-03-26 12:04:02 +08:00
|
|
|
export const clearDraftRecord = (key: string, mode: DraftStorageMode = 'local') => {
|
|
|
|
|
getStorage(mode).removeItem(key)
|
|
|
|
|
}
|