{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "docx-layout-blocks",
  "title": "DOCX Layout Blocks",
  "description": "A read-only DOCX viewer with typed layout blocks and source highlights.",
  "registryDependencies": [
    "@extend/docx-viewer",
    "@extend/layout-blocks-panel",
    "@extend/pdf-block-resizable-shell"
  ],
  "files": [
    {
      "path": "registry/new-york-v4/blocks/docx-layout-blocks.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport {\n  DocxBlockHighlights,\n  extractDocumentBlocks,\n  flattenDocumentBlocks,\n  revealDocxSource,\n  type DocumentBlock,\n} from \"@extend-ai/react-docx\"\n\nimport {\n  DocxViewerPreview,\n  type DocxViewerLayoutProps,\n} from \"@/components/extend/docx-viewer\"\nimport {\n  LayoutBlocksPanel,\n  layoutBlockStyles,\n  type LayoutBlockItem,\n} from \"@/components/extend/layout-blocks-panel\"\nimport { PdfBlockResizableShell } from \"@/components/extend/pdf-block-resizable-shell\"\n\nconst blockTypes = {\n  text: { type: \"paragraph\" },\n  heading: { type: \"heading\" },\n  section_heading: { type: \"heading\", label: \"Section heading\" },\n  figure: { type: \"figure\" },\n  table: { type: \"table\" },\n  table_head: { type: \"table\", label: \"Table header\" },\n  table_cell: { type: \"table\", label: \"Table cell\" },\n  key_value: { type: \"paragraph\", label: \"Key-value pair\" },\n  page_number: { type: \"page_number\" },\n  barcode: { type: \"figure\", label: \"Barcode\" },\n  formula: { type: \"paragraph\", label: \"Formula\" },\n  header: { type: \"header\" },\n  footer: { type: \"footer\" },\n  watermark: { type: \"figure\", label: \"Watermark\" },\n  legend: { type: \"paragraph\", label: \"Legend\" },\n} satisfies Record<\n  DocumentBlock[\"type\"],\n  Pick<LayoutBlockItem, \"type\" | \"label\">\n>\nfunction presentation(\n  block: DocumentBlock\n): Pick<LayoutBlockItem, \"type\" | \"label\"> {\n  return block.type === \"text\" && block.details.numbering\n    ? { type: \"list\" }\n    : blockTypes[block.type]\n}\nfunction blockColor(block: DocumentBlock) {\n  return layoutBlockStyles[presentation(block).type].color\n}\nfunction BlockLayout({\n  children,\n  model,\n  viewportRef,\n  isLoading,\n  loadError,\n}: DocxViewerLayoutProps) {\n  const [result, setResult] = React.useState<{\n    model: NonNullable<typeof model>\n    blocks: DocumentBlock[]\n    error?: string\n  }>()\n  const [activeId, setActiveId] = React.useState<string>()\n  const pendingReveal = React.useRef<DocumentBlock | null>(null)\n  React.useEffect(() => {\n    if (!model) return\n    let current = true\n    void extractDocumentBlocks(model).then(\n      (output) => {\n        if (current)\n          setResult({ model, blocks: flattenDocumentBlocks(output.blocks) })\n      },\n      (error: unknown) => {\n        if (current)\n          setResult({\n            model,\n            blocks: [],\n            error:\n              error instanceof Error\n                ? error.message\n                : \"Unable to read layout blocks.\",\n          })\n      }\n    )\n    return () => {\n      current = false\n      pendingReveal.current = null\n    }\n  }, [model])\n  const parsed = result?.model === model ? result : undefined\n  const blocks = parsed?.blocks\n  const rows = React.useMemo(\n    () =>\n      blocks?.map((block) => ({\n        ...presentation(block),\n        id: block.id,\n        text: block.content,\n        block,\n      })) ?? [],\n    [blocks]\n  )\n  const active = blocks?.find((block) => block.id === activeId) ?? blocks?.[0]\n  const scrollToMountedBlock = React.useCallback(() => {\n    const block = pendingReveal.current\n    const viewport = viewportRef.current\n    const source = block?.sourceRefs[0]\n    if (!viewport || !block || !source) return\n    const target =\n      viewport.querySelector(\n        `[data-docx-block-highlight=\"${CSS.escape(block.id)}\"]`\n      ) ??\n      viewport.querySelector(\n        `[data-docx-source-id=\"${CSS.escape(source.nodeId)}\"]`\n      )\n    if (!target) return\n    pendingReveal.current = null\n    const bounds = target.getBoundingClientRect()\n    const frame = viewport.getBoundingClientRect()\n    viewport.scrollBy({\n      top: bounds.top - frame.top - viewport.clientHeight / 4,\n      left:\n        bounds.left < frame.left || bounds.right > frame.right\n          ? bounds.left - frame.left\n          : 0,\n      behavior: \"instant\",\n    })\n  }, [viewportRef])\n  function focusBlock({ block }: { block: DocumentBlock }) {\n    setActiveId(block.id)\n    pendingReveal.current = block\n    scrollToMountedBlock()\n    // Ask the viewer to mount the source page. Its next geometry update finishes the scroll.\n    const source = pendingReveal.current?.sourceRefs[0]\n    if (source && viewportRef.current)\n      revealDocxSource(viewportRef.current, source.nodeId)\n  }\n  const message =\n    loadError ||\n    parsed?.error ||\n    (isLoading || (model && !parsed)\n      ? \"Reading layout blocks…\"\n      : !model\n        ? \"Open a DOCX to inspect its layout blocks.\"\n        : \"No layout blocks found.\")\n  return (\n    <PdfBlockResizableShell\n      autoSaveId=\"docx-layout-blocks-panels\"\n      heightClassName=\"h-[720px]\"\n      left={\n        <>\n          {children}\n          {blocks?.length ? (\n            <DocxBlockHighlights\n              rootRef={viewportRef}\n              blocks={blocks}\n              block={active}\n              colorForBlock={blockColor}\n              onGeometry={scrollToMountedBlock}\n            />\n          ) : null}\n        </>\n      }\n      right={\n        <LayoutBlocksPanel\n          heading=\"Layout blocks\"\n          ariaLabel=\"DOCX layout blocks\"\n          className=\"h-full\"\n          blocks={rows}\n          activeBlockId={active?.id}\n          emptyMessage={message}\n          onBlockFocus={focusBlock}\n        />\n      }\n    />\n  )\n}\nexport function DocxLayoutBlocks({ file }: { file?: string }) {\n  const [isDark, setIsDark] = React.useState(false)\n  return (\n    <DocxViewerPreview\n      src={file}\n      className=\"h-full\"\n      defaultZoom=\"fit-width\"\n      layout={BlockLayout}\n      isDark={isDark}\n      onIsDarkChange={setIsDark}\n    />\n  )\n}\n",
      "type": "registry:component",
      "target": "@components/blocks/docx-layout-blocks.tsx"
    }
  ],
  "categories": [
    "documents",
    "blocks"
  ],
  "type": "registry:block",
  "dependencies": [
    "@extend-ai/react-docx@0.15.0"
  ]
}
