2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: DRAFT_PREFIX supports form-draft.js by holding or executing the draft prefix 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-04-20 16:02:57 +08:00
|
|
|
const DRAFT_PREFIX = 'adminMiniDraft:'
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: buildDraftKey supports form-draft.js by holding or executing the build draft key page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function buildDraftKey(key) {
|
|
|
|
|
return `${DRAFT_PREFIX}${key}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: getDraftValue supports form-draft.js by holding or executing the get draft value page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function getDraftValue(key, defaultValue) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const record = wx.getStorageSync(buildDraftKey(key))
|
|
|
|
|
if (!record || typeof record !== 'object' || !Object.prototype.hasOwnProperty.call(record, 'value')) {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
return record.value
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: saveDraft supports form-draft.js by holding or executing the save draft page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function saveDraft(key, value) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
wx.setStorageSync(buildDraftKey(key), {
|
|
|
|
|
savedAt: Date.now(),
|
|
|
|
|
value
|
|
|
|
|
})
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: clearDraft supports form-draft.js by holding or executing the clear draft page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function clearDraft(key) {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
wx.removeStorageSync(buildDraftKey(key))
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: scheduleDraftSave supports form-draft.js by holding or executing the schedule draft save page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function scheduleDraftSave(context, key, buildValue, delay) {
|
|
|
|
|
if (!context) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (context.__draftSaveTimer) {
|
|
|
|
|
clearTimeout(context.__draftSaveTimer)
|
|
|
|
|
}
|
|
|
|
|
context.__draftSaveTimer = setTimeout(() => {
|
|
|
|
|
saveDraft(key, buildValue())
|
|
|
|
|
}, delay || 300)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: clearDraftTimer supports form-draft.js by holding or executing the clear draft timer page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-04-20 16:02:57 +08:00
|
|
|
function clearDraftTimer(context) {
|
|
|
|
|
if (context && context.__draftSaveTimer) {
|
|
|
|
|
clearTimeout(context.__draftSaveTimer)
|
|
|
|
|
context.__draftSaveTimer = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getDraftValue,
|
|
|
|
|
saveDraft,
|
|
|
|
|
clearDraft,
|
|
|
|
|
scheduleDraftSave,
|
|
|
|
|
clearDraftTimer
|
|
|
|
|
}
|