Files
writingkit/dist/drafts.js
T
CatoandClaude Sonnet 5.5 99fe5ae1c0 feat(react): ArticleComposer — the shared place to write (#1)
* feat(react): ArticleComposer — the shared place to write; assist, drafts and text-edit move into the core

Owner, 2026-11-06: the experience belongs to the package. The composer built in
BiasLens (title, text, Speak/Photo/Chart, preview, drafts, publish bar) becomes
@bitbaum/writingkit/react on chatkit's DictationField, so every product gets a
microphone and writing help in the title and the text without building them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

* build: rebuild dist against published chatkit 0.23.0; lockfile

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-06 09:29:50 +01:00

57 lines
1.7 KiB
JavaScript

/**
* A draft lives on the device until it is published, and signing in never
* loses it: what was typed under the anonymous key moves to the account's.
* Storage is passed in, so a test (or a server render) needs no browser.
*/
export function readDraft(storage, key) {
try {
const raw = storage.getItem(key);
if (!raw)
return null;
const draft = JSON.parse(raw);
if (typeof draft.title !== "string" || typeof draft.body !== "string")
return null;
return {
title: draft.title,
body: draft.body,
savedAt: typeof draft.savedAt === "string"
? draft.savedAt
: new Date(0).toISOString(),
extra: draft.extra,
};
}
catch {
return null;
}
}
/** False when storage is full or blocked: the text is still on screen. */
export function writeDraft(storage, key, draft) {
try {
storage.setItem(key, JSON.stringify(draft));
return true;
}
catch {
return false;
}
}
/** The draft to offer: this key's, or else the anonymous one, moved to this key. */
export function restoreDraft(storage, key, anonymousKey) {
const own = readDraft(storage, key);
if (own && (own.title || own.body))
return own;
if (!anonymousKey || anonymousKey === key)
return null;
const carried = readDraft(storage, anonymousKey);
if (!carried || (!carried.title && !carried.body))
return null;
if (writeDraft(storage, key, carried)) {
try {
storage.removeItem(anonymousKey);
}
catch {
/* the copy exists; the old one is harmless */
}
}
return carried;
}