Files
JayuCloud/JY-Artist-WeChat/utils/form-draft.js
T
2026-06-29 23:26:43 +08:00

106 lines
3.5 KiB
JavaScript

/**
* 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.
*/
const DRAFT_PREFIX = 'miniDraft:'
/**
* 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.
*/
function buildDraftKey(key) {
return `${DRAFT_PREFIX}${key}`
}
/**
* Purpose: loadDraft supports form-draft.js by holding or executing the load 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.
*/
function loadDraft(key, defaultValue) {
if (!key) {
return defaultValue
}
try {
const value = wx.getStorageSync(buildDraftKey(key))
return value === undefined || value === '' ? defaultValue : value
} catch (error) {
return defaultValue
}
}
/**
* 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.
*/
function saveDraft(key, value) {
if (!key) {
return
}
try {
wx.setStorageSync(buildDraftKey(key), {
savedAt: Date.now(),
value
})
} catch (error) {}
}
/**
* 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.
*/
function clearDraft(key) {
if (!key) {
return
}
try {
wx.removeStorageSync(buildDraftKey(key))
} catch (error) {}
}
/**
* 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.
*/
function getDraftValue(key, defaultValue) {
const record = loadDraft(key, null)
if (!record || typeof record !== 'object' || !Object.prototype.hasOwnProperty.call(record, 'value')) {
return defaultValue
}
return record.value
}
/**
* 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.
*/
function scheduleDraftSave(context, key, buildValue, delay) {
if (!context) {
return
}
if (context.__draftSaveTimer) {
clearTimeout(context.__draftSaveTimer)
}
context.__draftSaveTimer = setTimeout(() => {
saveDraft(key, buildValue())
}, delay || 300)
}
/**
* 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.
*/
function clearDraftTimer(context) {
if (context && context.__draftSaveTimer) {
clearTimeout(context.__draftSaveTimer)
context.__draftSaveTimer = null
}
}
module.exports = {
getDraftValue,
saveDraft,
clearDraft,
scheduleDraftSave,
clearDraftTimer
}