# Writer Editor Improvements 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:** Improve the hudozka-writer Tiptap editor with proper markdown serialization, a block insertion menu, file block editing, and enhanced card grid display. **Architecture:** All work is inside `modules/hudozka-writer/`. The editor uses Tiptap 3 with React 19 node views and PocketBase backend. We fix the broken serialization layer first (foundation for everything else), then add UI features on top. No external state library — all state stays in React hooks + Tiptap. **Tech Stack:** Tiptap 3, `@tiptap/markdown`, `@tiptap/suggestion` (new dep), React 19, PocketBase, Vite 8, TypeScript --- ## File Map | File | Action | Responsibility | |------------------------------------|--------|---------------------------------------------------------| | `src/lib/serialize.ts` | Modify | Use MarkdownManager for text block ↔ Tiptap conversion | | `src/lib/markdown.ts` | Create | Shared MarkdownManager instance factory | | `src/components/Editor.tsx` | Modify | Add markdown toggle state, textarea mode, new extensions | | `src/components/Editor.css` | Modify | Styles for markdown textarea | | `src/components/Toolbar.tsx` | Modify | Add MD toggle button | | `src/components/Toolbar.css` | Modify | Style for MD toggle active state | | `src/components/BlockMenu.tsx` | Create | Shared block type dropdown used by "+" and slash | | `src/components/BlockMenu.css` | Create | BlockMenu styles | | `src/components/FilePicker.tsx` | Create | File browse/upload modal (mirrors ImagePicker) | | `src/components/FilePicker.css` | Create | FilePicker styles (reuses picker-* classes) | | `src/extensions/SlashCommands.ts` | Create | Tiptap extension for `/` slash commands | | `src/extensions/BlockInsert.tsx` | Create | Tiptap plugin view for hover "+" button | | `src/extensions/BlockInsert.css` | Create | Styles for "+" button and positioning | | `src/nodes/DocumentBlock.tsx` | Modify | Add interactive editing (file picker, title input) | | `src/nodes/CardGridBlock.tsx` | Modify | Fetch and display resolved page titles | | `src/nodes/nodes.css` | Modify | Styles for document block controls, card grid list | | `src/nodes/index.ts` | Modify | Export new extensions | | `src/types.ts` | Modify | Add PbFile type | --- ### Task 1: Fix Markdown Serialization **Files:** - Create: `src/lib/markdown.ts` - Modify: `src/lib/serialize.ts` This is the foundation — fixes the lossy text block round-trip. - [x] **Step 1: Create shared MarkdownManager factory** Create `src/lib/markdown.ts`: ```ts import { MarkdownManager } from "@tiptap/markdown" import StarterKit from "@tiptap/starter-kit" import Link from "@tiptap/extension-link" let manager: MarkdownManager | null = null export function getMarkdownManager(): MarkdownManager { if (!manager) { manager = new MarkdownManager({ extensions: [ StarterKit, Link, ], }) } return manager } ``` - [x] **Step 2: Rewrite textBlockToTiptap to use markdown parsing** In `src/lib/serialize.ts`, replace the `textBlockToTiptap` function. The new version parses markdown into Tiptap JSON using the MarkdownManager: Replace the import block at the top: ```ts import type { JSONContent } from "@tiptap/core" import type { DocV1, DocV1Block, DocV1BlockText, DocV1BlockImage, DocV1BlockDocument, DocV1BlockEmbed, DocV1BlockCardGrid, } from "../types" import { generateBlockId } from "./id" import { getMarkdownManager } from "./markdown" ``` Replace `textBlockToTiptap`: ```ts function textBlockToTiptap(block: DocV1BlockText): JSONContent[] { const manager = getMarkdownManager() const parsed = manager.parse(block.text) return parsed.content ?? [] } ``` - [x] **Step 3: Rewrite text serialization to use markdown output** Replace `tiptapTextContent` and `textNodesToMarkdown` with: ```ts function textNodesToMarkdown(nodes: JSONContent[]): string { const manager = getMarkdownManager() const doc: JSONContent = { type: "doc", content: nodes } return manager.serialize(doc) } ``` Remove the old `tiptapTextContent` function entirely — it's no longer used. - [x] **Step 4: Verify build passes** Run: ```bash cd modules/hudozka-writer && npx tsc --noEmit ``` Expected: no errors. - [x] **Step 5: Manual test round-trip** (skipped - not automatable) Run `npm run dev` in the writer module. Open a page with formatted text (bold, headings, lists). Verify: 1. Formatted text renders correctly in the editor 2. Use the toolbar to add bold/italic/headings 3. Save the page 4. Reload — formatting is preserved - [x] **Step 6: Commit** ```bash git add modules/hudozka-writer/src/lib/markdown.ts modules/hudozka-writer/src/lib/serialize.ts git commit -m "fix: use @tiptap/markdown for text block serialization" ``` --- ### Task 2: Global Markdown Toggle **Files:** - Modify: `src/components/Editor.tsx` - Modify: `src/components/Editor.css` - Modify: `src/components/Toolbar.tsx` - Modify: `src/components/Toolbar.css` - [x] **Step 1: Add MD toggle button to Toolbar** In `src/components/Toolbar.tsx`, add `markdownMode` and `onToggleMarkdown` props: ```tsx export type ToolbarProps = { editor: Editor | null onSave: () => void saving: boolean markdownMode: boolean onToggleMarkdown: () => void } ``` Add a new toolbar group before the save group (before the closing `` of the toolbar): ```tsx
``` Update the destructured props: ```tsx export function Toolbar({ editor, onSave, saving, markdownMode, onToggleMarkdown }: ToolbarProps) { ``` - [x] **Step 2: Add markdown mode state and textarea to Editor** In `src/components/Editor.tsx`, add state and toggle logic: ```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 } from "react" import { pb } from "../pb" import { docToTiptap, tiptapToDoc } from "../lib/serialize" import { Toolbar } from "./Toolbar" import { WriterDocument, ImageBlock, DocumentBlock, EmbedBlock, CardGridBlock, } from "../nodes" import "../nodes/nodes.css" import "./Editor.css" import type { PbPage, DocV1 } from "../types" export type EditorProps = { page: PbPage } export function Editor({ page }: EditorProps) { const [saving, setSaving] = useState(false) const [markdownMode, setMarkdownMode] = useState(false) const [markdownText, setMarkdownText] = useState("") const tiptapDoc = docToTiptap(page.doc) const editor = useEditor({ extensions: [ WriterDocument, StarterKit.configure({ document: false, }), Markdown, Link.configure({ openOnClick: false }), ImageBlock, DocumentBlock, EmbedBlock, CardGridBlock, ], content: tiptapDoc, }) const handleToggleMarkdown = useCallback(() => { if (!editor) return if (!markdownMode) { // Switching to markdown mode: serialize editor content const md = editor.storage.markdown.getMarkdown() setMarkdownText(md) setMarkdownMode(true) } else { // Switching back to rendered mode: parse markdown into editor editor.commands.setContent(markdownText) setMarkdownMode(false) } }, [editor, markdownMode, markdownText]) const handleSave = useCallback(async () => { if (!editor) return setSaving(true) try { if (markdownMode) { // Apply markdown text back to editor before saving editor.commands.setContent(markdownText) setMarkdownMode(false) } const json = editor.getJSON() const doc: DocV1 = tiptapToDoc(json.content ?? []) await pb.collection("pages").update(page.id, { doc }) } catch (err) { console.error("Save failed:", err) alert("Save failed. Check console.") } finally { setSaving(false) } }, [editor, page.id, markdownMode, markdownText]) return (
{markdownMode ? (