# Writer Draft/Publish Implementation Plan > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** Add draft-save and explicit publish workflow to hudozka-writer so editors can save work-in-progress without pushing changes live. **Architecture:** PocketBase `pages` collection gets a `draft` JSON field for WIP content alongside existing `doc` (published). The `draft: boolean` field is replaced with `published: boolean`. The editor loads `draft ?? doc`, tracks dirty state via JSON snapshot comparison, and provides Save (writes `draft`), Publish (writes `doc`, clears `draft`), and Discard (clears `draft`, reloads from `doc`) actions. **Tech Stack:** React, TipTap, PocketBase JS SDK **Spec:** `docs/superpowers/specs/2026-04-07-writer-draft-publish.md` --- ### Task 1: Update PbPage type **Files:** - Modify: `modules/hudozka-writer/src/types.ts:28-37` - [x] **Step 1: Update PbPage type definition** Replace the current `PbPage` type: ```ts export type PbPage = PbRecord & { title: string slug: string excerpt: string date: string cover: string doc: DocV1 tags: string[] published: boolean draft: DocV1 | null } ``` - [x] **Step 2: Verify build** Run: `cd modules/hudozka-writer && npx tsc --noEmit` Expected: errors in `App.tsx` where `draft` was used as boolean — that's expected, we fix it in Task 4. - [x] **Step 3: Commit** ```bash git add modules/hudozka-writer/src/types.ts git commit -m "feat(writer): update PbPage type for draft/publish workflow" ``` --- ### Task 2: Add Save/Publish/Discard handlers and dirty tracking to Editor **Files:** - Modify: `modules/hudozka-writer/src/components/Editor.tsx` - [x] **Step 1: Rewrite Editor component with draft support** Replace the full content of `Editor.tsx`: ```tsx import { useEditor, EditorContent } from "@tiptap/react" import StarterKit from "@tiptap/starter-kit" import { Markdown } from "@tiptap/markdown" import Link from "@tiptap/extension-link" import { useState, useCallback, useRef, useEffect } from "react" import { pb } from "../pb" import { docToTiptap, tiptapToDoc } from "../lib/serialize" import { Toolbar } from "./Toolbar" import { DraftBanner } from "./DraftBanner" import { WriterDocument, ImageBlock, DocumentBlock, EmbedBlock, CardGridBlock, } from "../nodes" import { BlockInsert } from "../extensions/BlockInsert" import { SlashCommands } from "../extensions/SlashCommands" import "../nodes/nodes.css" import "./Editor.css" import type { PbPage, DocV1 } from "../types" function serializeDoc(doc: DocV1): string { return JSON.stringify(doc) } export type EditorProps = { page: PbPage } export function Editor({ page }: EditorProps) { const [saving, setSaving] = useState(false) const [publishing, setPublishing] = useState(false) const [markdownMode, setMarkdownMode] = useState(false) const [markdownText, setMarkdownText] = useState("") const [dirty, setDirty] = useState(false) const [hasDraft, setHasDraft] = useState(page.draft !== null) const [publishedDoc, setPublishedDoc] = useState(page.doc) const snapshotRef = useRef("") const initialDoc = page.draft ?? page.doc const tiptapDoc = docToTiptap(initialDoc) // Set initial snapshot useEffect(() => { snapshotRef.current = serializeDoc(initialDoc) }, []) // eslint-disable-line react-hooks/exhaustive-deps const editor = useEditor({ extensions: [ WriterDocument, StarterKit.configure({ document: false, }), Markdown, Link.configure({ openOnClick: false }), ImageBlock, DocumentBlock, EmbedBlock, CardGridBlock, BlockInsert, SlashCommands, ], content: tiptapDoc, onUpdate: ({ editor }) => { const json = editor.getJSON() const doc = tiptapToDoc(json.content ?? []) const current = serializeDoc(doc) setDirty(current !== snapshotRef.current) }, }) const getCurrentDoc = useCallback((): DocV1 => { if (!editor) return { version: 1, blocks: [] } if (markdownMode) { editor.commands.setContent(markdownText, { contentType: "markdown" }) setMarkdownMode(false) } const json = editor.getJSON() return tiptapToDoc(json.content ?? []) }, [editor, markdownMode, markdownText]) const handleToggleMarkdown = useCallback(() => { if (!editor) return if (!markdownMode) { const md = editor.getMarkdown() setMarkdownText(md) setMarkdownMode(true) } else { editor.commands.setContent(markdownText, { contentType: "markdown" }) setMarkdownMode(false) } }, [editor, markdownMode, markdownText]) const handleSave = useCallback(async () => { if (!editor) return setSaving(true) try { const doc = getCurrentDoc() await pb.collection("pages").update(page.id, { draft: doc }) snapshotRef.current = serializeDoc(doc) setDirty(false) setHasDraft(true) } catch (err) { console.error("Save failed:", err) alert("Save failed. Check console.") } finally { setSaving(false) } }, [editor, page.id, getCurrentDoc]) const handlePublish = useCallback(async () => { if (!editor) return setPublishing(true) try { const doc = getCurrentDoc() await pb.collection("pages").update(page.id, { doc, draft: null }) snapshotRef.current = serializeDoc(doc) setDirty(false) setHasDraft(false) setPublishedDoc(doc) } catch (err) { console.error("Publish failed:", err) alert("Publish failed. Check console.") } finally { setPublishing(false) } }, [editor, page.id, getCurrentDoc]) const handleDiscard = useCallback(async () => { if (!editor) return try { await pb.collection("pages").update(page.id, { draft: null }) const tiptap = docToTiptap(publishedDoc) editor.commands.setContent(tiptap) snapshotRef.current = serializeDoc(publishedDoc) setDirty(false) setHasDraft(false) setMarkdownMode(false) } catch (err) { console.error("Discard failed:", err) alert("Discard failed. Check console.") } }, [editor, page.id, publishedDoc]) return (
{hasDraft && ( )} {markdownMode ? (