diff --git a/packages/editor/App.tsx b/packages/editor/App.tsx index 30a5d42..8bd48b6 100644 --- a/packages/editor/App.tsx +++ b/packages/editor/App.tsx @@ -57,6 +57,7 @@ import type { ArchivedPlan } from '@plannotator/ui/components/sidebar/ArchiveBro import { PlanDiffViewer } from '@plannotator/ui/components/plan-diff/PlanDiffViewer'; import type { PlanDiffMode } from '@plannotator/ui/components/plan-diff/PlanDiffModeSwitcher'; import { DEMO_PLAN_CONTENT } from './demoPlan'; +import { useCheckboxOverrides } from './hooks/useCheckboxOverrides'; type NoteAutoSaveResults = { obsidian?: boolean; @@ -805,12 +806,32 @@ const App: React.FC = () => { if (id && window.innerWidth < 768) setIsPanelOpen(true); }, []); - const handleDeleteAnnotation = (id: string) => { + // Core annotation removal — highlight cleanup + state filter + selection clear + const removeAnnotation = (id: string) => { viewerRef.current?.removeHighlight(id); setAnnotations(prev => prev.filter(a => a.id !== id)); if (selectedAnnotationId === id) setSelectedAnnotationId(null); }; + // Interactive checkbox toggling with annotation tracking + const checkbox = useCheckboxOverrides({ + blocks, + annotations, + addAnnotation: handleAddAnnotation, + removeAnnotation, + }); + + const handleDeleteAnnotation = (id: string) => { + // If this is a checkbox annotation, revert the visual override + if (id.startsWith('ann-checkbox-')) { + const ann = annotations.find(a => a.id === id); + if (ann) { + checkbox.revertOverride(ann.blockId); + } + } + removeAnnotation(id); + }; + const handleEditAnnotation = (id: string, updates: Partial) => { setAnnotations(prev => prev.map(ann => ann.id === id ? { ...ann, ...updates } : ann @@ -1439,6 +1460,8 @@ const App: React.FC = () => { imageBaseDir={imageBaseDir} copyLabel={annotateSource === 'message' ? 'Copy message' : annotateSource === 'file' || annotateSource === 'folder' ? 'Copy file' : undefined} archiveInfo={archive.currentInfo} + onToggleCheckbox={checkbox.toggle} + checkboxOverrides={checkbox.overrides} /> diff --git a/packages/editor/hooks/useCheckboxOverrides.ts b/packages/editor/hooks/useCheckboxOverrides.ts new file mode 100644 index 0000000..288b658 --- /dev/null +++ b/packages/editor/hooks/useCheckboxOverrides.ts @@ -0,0 +1,118 @@ +/** + * Checkbox Overrides Hook + * + * Manages interactive checkbox toggling in the plan viewer. Each toggle creates + * a COMMENT annotation capturing the action and section context; toggling back + * to the original state removes the override and deletes the annotation. + */ + +import { useState, useEffect, useCallback, useRef } from 'react'; +import { Annotation, AnnotationType, Block } from '@plannotator/ui/types'; + +export interface UseCheckboxOverridesOptions { + blocks: Block[]; + annotations: Annotation[]; + addAnnotation: (ann: Annotation) => void; + removeAnnotation: (id: string) => void; +} + +export interface UseCheckboxOverridesReturn { + /** Visual override state passed to the Viewer as `checkboxOverrides` */ + overrides: Map; + /** Toggle handler passed to the Viewer as `onToggleCheckbox` */ + toggle: (blockId: string, checked: boolean) => void; + /** Revert an override when a checkbox annotation is deleted from the panel */ + revertOverride: (blockId: string) => void; +} + +export function useCheckboxOverrides({ + blocks, + annotations, + addAnnotation, + removeAnnotation, +}: UseCheckboxOverridesOptions): UseCheckboxOverridesReturn { + const [overrides, setOverrides] = useState>(new Map()); + + // Refs so callbacks don't need annotations/blocks in their dep arrays + const blocksRef = useRef(blocks); + blocksRef.current = blocks; + const annotationsRef = useRef(annotations); + annotationsRef.current = annotations; + + // Clean up stale overrides when blocks change (e.g. markdown reloaded) + useEffect(() => { + if (overrides.size === 0) return; + const blockIds = new Set(blocks.map(b => b.id)); + const stale = [...overrides.keys()].filter(id => !blockIds.has(id)); + if (stale.length > 0) { + setOverrides(prev => { + const next = new Map(prev); + stale.forEach(id => next.delete(id)); + return next; + }); + } + }, [blocks]); + + const toggle = useCallback((blockId: string, checked: boolean) => { + const blocks = blocksRef.current; + const annotations = annotationsRef.current; + const block = blocks.find(b => b.id === blockId); + const isRevertingToOriginal = block && checked === block.checked; + + if (isRevertingToOriginal) { + // Undo: remove the override and delete ALL checkbox annotations for this block + setOverrides(prev => { + const next = new Map(prev); + next.delete(blockId); + return next; + }); + const toDelete = annotations.filter(a => a.blockId === blockId && a.id.startsWith('ann-checkbox-')); + toDelete.forEach(a => removeAnnotation(a.id)); + } else { + // Toggle: remove any existing checkbox annotations for this block first (prevents duplicates from rapid clicks) + const existing = annotations.filter(a => a.blockId === blockId && a.id.startsWith('ann-checkbox-')); + existing.forEach(a => removeAnnotation(a.id)); + + setOverrides(prev => { + const next = new Map(prev); + next.set(blockId, checked); + return next; + }); + if (block) { + // Find the nearest heading above this block for section context + const blockIdx = blocks.indexOf(block); + let sectionHeading = ''; + for (let i = blockIdx - 1; i >= 0; i--) { + if (blocks[i].type === 'heading') { + sectionHeading = blocks[i].content; + break; + } + } + + const action = checked ? 'Mark as completed' : 'Mark as not completed'; + const context = sectionHeading ? ` (under "${sectionHeading}")` : ` (line ${block.startLine})`; + const ann: Annotation = { + id: `ann-checkbox-${blockId}-${Date.now()}`, + blockId, + startOffset: 0, + endOffset: block.content.length, + type: AnnotationType.COMMENT, + text: `${action}${context}: ${block.content}`, + originalText: block.content, + createdA: Date.now(), + }; + addAnnotation(ann); + } + } + }, [addAnnotation, removeAnnotation]); + + const revertOverride = useCallback((blockId: string) => { + setOverrides(prev => { + const next = new Map(prev); + next.delete(blockId); + return next; + }); + }, []); + + return { overrides, toggle, revertOverride }; +} diff --git a/packages/ui/components/Viewer.tsx b/packages/ui/components/Viewer.tsx index 7daa401..77151fe 100644 --- a/packages/ui/components/Viewer.tsx +++ b/packages/ui/components/Viewer.tsx @@ -69,6 +69,9 @@ interface ViewerProps { /** Label for the copy button (default: "Copy plan") */ copyLabel?: string; archiveInfo?: { status: 'approved' | 'denied' | 'unknown'; timestamp: string; title: string } | null; + // Checkbox toggle props + onToggleCheckbox?: (blockId: string, checked: boolean) => void; + checkboxOverrides?: Map; } export interface ViewerHandle { @@ -137,6 +140,8 @@ export const Viewer = forwardRef(({ imageBaseDir, copyLabel, archiveInfo, + onToggleCheckbox, + checkboxOverrides, }, ref) => { const [copied, setCopied] = useState(false); const [lightbox, setLightbox] = useState<{ src: string; alt: string } | null>(null); @@ -572,7 +577,7 @@ export const Viewer = forwardRef(({ group.type === 'list-group' ? (
{group.blocks.map(block => ( - setLightbox({ src, alt })} key={block.id} block={block} onOpenLinkedDoc={onOpenLinkedDoc} /> + setLightbox({ src, alt })} key={block.id} block={block} onOpenLinkedDoc={onOpenLinkedDoc} onToggleCheckbox={onToggleCheckbox} checkboxOverrides={checkboxOverrides} /> ))}
) : group.block.type === 'code' && isMermaidLanguage(group.block.language) ? ( @@ -610,7 +615,7 @@ export const Viewer = forwardRef(({ isHovered={inputMethod !== 'pinpoint' && hoveredCodeBlock?.block.id === group.block.id} /> ) : ( - setLightbox({ src, alt })} key={group.block.id} block={group.block} onOpenLinkedDoc={onOpenLinkedDoc} /> + setLightbox({ src, alt })} key={group.block.id} block={group.block} onOpenLinkedDoc={onOpenLinkedDoc} onToggleCheckbox={onToggleCheckbox} checkboxOverrides={checkboxOverrides} /> ) )} @@ -967,7 +972,14 @@ function groupBlocks(blocks: Block[]): RenderGroup[] { return groups; } -const BlockRenderer: React.FC<{ block: Block; onOpenLinkedDoc?: (path: string) => void; imageBaseDir?: string; onImageClick?: (src: string, alt: string) => void }> = ({ block, onOpenLinkedDoc, imageBaseDir, onImageClick }) => { +const BlockRenderer: React.FC<{ + block: Block; + onOpenLinkedDoc?: (path: string) => void; + imageBaseDir?: string; + onImageClick?: (src: string, alt: string) => void; + onToggleCheckbox?: (blockId: string, checked: boolean) => void; + checkboxOverrides?: Map; +}> = ({ block, onOpenLinkedDoc, imageBaseDir, onImageClick, onToggleCheckbox, checkboxOverrides }) => { switch (block.type) { case 'heading': const Tag = `h${block.level || 1}` as keyof JSX.IntrinsicElements; @@ -992,20 +1004,29 @@ const BlockRenderer: React.FC<{ block: Block; onOpenLinkedDoc?: (path: string) = case 'list-item': { const indent = (block.level || 0) * 1.25; // 1.25rem per level const isCheckbox = block.checked !== undefined; + const isChecked = checkboxOverrides?.has(block.id) + ? checkboxOverrides.get(block.id)! + : block.checked; + const isInteractive = isCheckbox && !!onToggleCheckbox; return (
- + { e.stopPropagation(); onToggleCheckbox!(block.id, !isChecked); } : undefined} + role={isInteractive ? 'checkbox' : undefined} + aria-checked={isInteractive ? isChecked : undefined} + > {isCheckbox ? ( - block.checked ? ( + isChecked ? ( ) : ( - + ) @@ -1015,7 +1036,7 @@ const BlockRenderer: React.FC<{ block: Block; onOpenLinkedDoc?: (path: string) = )} - +