Files
hudozka/docs/specs/20260407-writer-draft-publish.md
T
tmshv f874b58dd9 Migrate to npm-workspaces monorepo (#294)
* spec update

* update md files

* add plan: monorepo

* feat: add empty apps and packages directories

* Add shared tsconfig.base.json

* feat add workspaces

* feat: move hudozka-writer into apps/

* feat: move migrate-to-pocketbase into apps/

* feat: split package.json for apps/hudozka workspace

* feat: extend tsconfig.base.json in apps/hudozka

* add dockerfile path

* feat: verify Phase 3 monorepo migration of Next app

* feat: scaffold @hudozka/utils package

* feat: add utils barrel export

* feat: extract utilities into hudozka-utils package

* feat: extract hooks into hudozka-hooks package

* Extract Box/Button/Panel/Title/Overlay into @hudozka/ui

* Scope Next ESLint rules to apps/hudozka in root flat config

* Update README and CLAUDE.md for monorepo layout

* disable image optimisation
2026-04-25 01:33:06 +03:00

86 lines
3.6 KiB
Markdown

# Writer Draft/Publish Feature
## Overview
Add draft-save and explicit publish workflow to hudozka-writer so editors can save work-in-progress without pushing changes live.
## Data Layer
PocketBase `pages` collection fields (already applied by user):
| Field | Type | Purpose |
|--------------|---------|--------------------------------------------------------|
| `doc` | JSON | Published document content (read by the main site) |
| `draft` | JSON | Unpublished work-in-progress, `null` when no draft |
| `published` | boolean | Page visibility (renamed from `draft`, inverted logic) |
## Editor Load Behavior
When opening a page:
- If `draft` is not null: load `draft` into the editor, show a banner "You have unpublished changes" with a **Discard** button.
- If `draft` is null: load `doc` into the editor, no banner.
## Save / Publish / Discard
| Action | API call | Effect |
|-------------|----------------------------------------------------|-------------------------------------------|
| **Save** | `update(id, { draft: editorContent })` | Persists WIP without affecting live site |
| **Publish** | `update(id, { doc: editorContent, draft: null })` | Pushes content live, clears draft |
| **Discard** | `update(id, { draft: null })` | Drops WIP, reloads editor from `doc` |
No confirmation dialog on Discard.
## Toolbar Changes
Current layout: `[formatting...] [MD] [Save]`
New layout: `[formatting...] [MD] [Save] [Publish]`
- **Save** button: enabled only when editor content differs from the last-saved state. Writes to `draft`.
- **Publish** button: enabled when there are any unpublished changes (unsaved edits or existing `draft`). Writes to `doc`, clears `draft`.
After Save: Save button becomes disabled (no new changes), Publish remains enabled (draft exists).
After Publish: both buttons become disabled (content is live, no draft).
## Draft Banner
Shown above the editor when `draft` was loaded. Simple bar with text and a Discard button:
```
[You have unpublished changes] [Discard]
```
Clicking Discard: sends API call to clear `draft`, reloads editor content from `doc`, hides the banner.
## Page Visibility Toggle
A checkbox in the app header (near page title/slug): `[ ] Published`
- Checked = page visible on site (`published: true`)
- Unchecked = page hidden (`published: false`)
- Toggling sends `update(id, { published: !current })` immediately
- Independent of save/publish content flow
## Dirty Tracking
Track whether editor content has changed since last load/save:
- On load: snapshot the initial content (serialized `DocV1`)
- On every editor change: compare current serialized content with snapshot
- After Save: update snapshot to current content
- After Publish: update snapshot to current content
- After Discard: update snapshot to `doc` content
This drives the enabled/disabled state of Save and Publish buttons.
## Type Changes
Update `PbPage` in `modules/hudozka-writer/src/types.ts`:
- Replace `draft: boolean` with `published: boolean` and `draft: DocV1 | null`
## Files to Modify
- `modules/hudozka-writer/src/types.ts` — update `PbPage` type
- `modules/hudozka-writer/src/components/Editor.tsx` — draft loading, save/publish/discard handlers, dirty tracking
- `modules/hudozka-writer/src/components/Toolbar.tsx` — add Publish button, accept dirty/hasDraft props
- `modules/hudozka-writer/src/App.tsx` — add published toggle in header, update `handleDuplicate` for new field names