Block a user
@abds10/draft (1.0.0)
Published 2026-10-10 08:24:13 +00:00 by deploy
Installation
@abds10:registry=https://git.abds10.cn/api/packages/deploy/npm/npm install @abds10/draft@1.0.0"@abds10/draft": "1.0.0"About this package
客户端草稿暂存:长流程增量落库与断点续做(《登录接入与数据安全规范》R5)
@abds10/draft
客户端草稿暂存 —— 长流程「增量落库 + 断点续做」的兜底层。
规范:工作区
docs/standards/登录接入与数据安全规范.md的 R5。 定位:服务端持久化仍是事实来源;本包保证「同一浏览器里的刷新 / 关页 / 断网 / 掉登录不丢」。 与 SessionGuard 分工:SessionGuard 管「401 不丢」,本包管「刷新 / 关页不丢」。
安装
npm install @abds10/draft
(私有包,.npmrc 指向 Gitea registry,见各项目部署文档。)
纯存储层(@abds10/draft)
零依赖、零框架,可在任何浏览器环境使用:
import { draftKey, readDraft, writeDraft, clearDraft } from '@abds10/draft'
const key = draftKey('chapter', novelId, chapterId)
writeDraft(key, { title, content })
const rec = readDraft<{ title: string; content: string }>(key) // { value, savedAt } | null
clearDraft(key)
- key 自动加前缀
abds10:draft:,避免与其它 localStorage 项冲突; - 损坏 JSON、缺字段、隐私模式、配额满一律安全失败(返回 null / 静默忽略),绝不打断编辑。
React Hook(@abds10/draft/react)
import { draftKey, readDraft, clearDraft } from '@abds10/draft'
import { useDraftAutosave } from '@abds10/draft/react'
const key = draftKey('question', questionId ?? 'new')
const draft = useDraftAutosave(key, { stem, options, explanation }, {
enabled: true,
baseline: serverSnapshot, // 与服务器一致就不写草稿
isEmpty: (v) => !v.stem.trim(), // 空表单不产生草稿
})
// 打开时:readDraft(key) 与服务器不一致 → 提示「恢复 / 丢弃」
// 保存成功:draft.clear()
// UI 提示:draft.savedAt ? '草稿已暂存 ' + 时间 : null
| 行为 | 说明 |
|---|---|
| 去抖写入 | 默认 800ms |
| 强制落盘 | 连续变化超过 maxWaitMs(默认 15s)也会写一次 —— 流式生成不丢 |
| 离场 flush | pagehide / visibilitychange(hidden) / 组件卸载 |
| 基线跳过 | value 与 baseline 相同则不写(避免"没改也提示恢复") |
| 判空清理 | isEmpty(value) 为真时清除草稿 |
开发
pnpm --filter @abds10/draft run verify
Dependencies
Development Dependencies
| ID | Version |
|---|---|
| @types/node | ^22 |
| @types/react | ^19 |
Peer Dependencies
| ID | Version |
|---|---|
| react | >=18 |
Details
2026-10-10 08:24:13 +00:00
Assets (1)
Versions (1)
View all
npm
10
UNLICENSED
latest
5.7 KiB
draft-1.0.0.tgz
5.7 KiB
1.0.0
2026-10-10
鄂公网安备42010302002985号