{
  "name": "react-example-virtual-selection-repro",
  "title": "react-example-virtual-selection-repro",
  "type": "registry:block",
  "description": "Reproduces pointer refocus in an editable nested node view.",
  "registryDependencies": [],
  "dependencies": [
    "prosekit@^0.23.2"
  ],
  "files": [
    {
      "path": "registry/src/react/examples/virtual-selection-repro/editor.tsx",
      "type": "registry:component",
      "target": "components/editor/examples/virtual-selection-repro/editor.tsx",
      "content": "'use client'\n\nimport 'prosekit/basic/style.css'\nimport 'prosekit/basic/typography.css'\nimport './style.css'\n\nimport { createEditor, type NodeJSON } from 'prosekit/core'\nimport { ProseKit } from 'prosekit/react'\nimport { useMemo, useState } from 'react'\n\nimport { defineExtension } from './extension.ts'\n\nconst defaultContent: NodeJSON = {\n  type: 'doc',\n  content: [\n    {\n      type: 'paragraph',\n      content: [{ type: 'text', text: 'Select the first paragraph below, then blur the editor.' }],\n    },\n    {\n      type: 'nestedEditable',\n      content: [\n        {\n          type: 'paragraph',\n          content: [{ type: 'text', text: 'Selected text inside the nested editable NodeView.' }],\n        },\n        {\n          type: 'paragraph',\n          content: [{ type: 'text', text: 'Click this target paragraph after blurring.' }],\n        },\n      ],\n    },\n  ],\n}\n\nexport default function Editor() {\n  const [legacy, setLegacy] = useState(false)\n  const editor = useMemo(() => {\n    return createEditor({ extension: defineExtension(legacy), defaultContent })\n  }, [legacy])\n\n  return (\n    <ProseKit editor={editor}>\n      <div className=\"box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white\">\n        <p>\n          {legacy\n            ? 'Legacy mode removes the decoration only on focus. Its failure is browser and DOM-shape dependent.'\n            : 'Fixed mode removes the decoration on pointerdown before focus.'}\n        </p>\n        <button onClick={() => setLegacy(value => !value)} type=\"button\">\n          Switch to {legacy ? 'fixed' : 'legacy'} mode\n        </button>\n        <p>Select the first nested paragraph, click “Blur editor”, then click the target paragraph once.</p>\n        <button data-testid=\"blur-editor\" type=\"button\">Blur editor</button>\n        <div className=\"relative w-full flex-1 box-border overflow-y-auto\">\n          <div ref={editor.mount} className=\"ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500\"></div>\n        </div>\n      </div>\n    </ProseKit>\n  )\n}\n"
    },
    {
      "path": "registry/src/react/examples/virtual-selection-repro/extension.ts",
      "type": "registry:component",
      "target": "components/editor/examples/virtual-selection-repro/extension.ts",
      "content": "import { defineBasicExtension } from 'prosekit/basic'\nimport { defineNodeSpec, definePlugin, union } from 'prosekit/core'\nimport { PluginKey, ProseMirrorPlugin, type Transaction } from 'prosekit/pm/state'\nimport { Decoration, DecorationSet } from 'prosekit/pm/view'\nimport { defineReactNodeView } from 'prosekit/react'\n\nimport { NestedEditableView } from './nested-editable-view.tsx'\n\nconst legacyVirtualSelectionKey = new PluginKey<boolean>('virtual-selection-repro-legacy')\n\nfunction getLegacyFocusMeta(tr: Transaction): boolean | undefined {\n  return tr.getMeta(legacyVirtualSelectionKey) as boolean | undefined\n}\n\nfunction defineLegacyVirtualSelection() {\n  return definePlugin(\n    new ProseMirrorPlugin<boolean>({\n      key: legacyVirtualSelectionKey,\n      state: {\n        init: () => false,\n        apply: (tr, value) => getLegacyFocusMeta(tr) ?? value,\n      },\n      props: {\n        handleDOMEvents: {\n          focus: (view) => {\n            view.dispatch(view.state.tr.setMeta(legacyVirtualSelectionKey, false))\n          },\n          blur: (view) => {\n            const { dom, root } = view\n            const domSelection = 'getSelection' in root ? root.getSelection() : window.getSelection()\n            if (domSelection?.anchorNode && dom.contains(domSelection.anchorNode)) {\n              domSelection.removeAllRanges()\n            }\n            view.dispatch(view.state.tr.setMeta(legacyVirtualSelectionKey, true))\n          },\n        },\n        decorations: (state) => {\n          const { selection } = state\n          if (!legacyVirtualSelectionKey.getState(state) || selection.empty || !selection.visible) {\n            return null\n          }\n          return DecorationSet.create(state.doc, [\n            Decoration.inline(selection.from, selection.to, {\n              class: 'prosekit-virtual-selection-repro-legacy',\n            }),\n          ])\n        },\n      },\n    }),\n  )\n}\n\nexport function defineExtension(legacy: boolean) {\n  return union(\n    defineBasicExtension(),\n    ...(legacy ? [defineLegacyVirtualSelection()] : []),\n    defineNodeSpec({\n      name: 'nestedEditable',\n      content: 'block+',\n      group: 'block',\n      toDOM: () => ['div', 0],\n    }),\n    defineReactNodeView({\n      name: 'nestedEditable',\n      component: NestedEditableView,\n      stopEvent: (event) => event.target instanceof Element && !!event.target.closest('button'),\n    }),\n  )\n}\n\nexport type EditorExtension = ReturnType<typeof defineExtension>\n"
    },
    {
      "path": "registry/src/react/examples/virtual-selection-repro/index.ts",
      "type": "registry:component",
      "target": "components/editor/examples/virtual-selection-repro/index.ts",
      "content": "export { default as ExampleEditor } from './editor.tsx'\n"
    },
    {
      "path": "registry/src/react/examples/virtual-selection-repro/nested-editable-view.tsx",
      "type": "registry:component",
      "target": "components/editor/examples/virtual-selection-repro/nested-editable-view.tsx",
      "content": "'use client'\n\nimport type { ReactNodeViewProps } from 'prosekit/react'\n\nexport function NestedEditableView(props: ReactNodeViewProps) {\n  return (\n    <div data-testid=\"nested-editable\" style={{ border: '1px solid currentColor', padding: '0.75rem' }}>\n      <div contentEditable={false}>\n        <button type=\"button\">Non-editable NodeView control</button>\n      </div>\n      <div ref={props.contentRef}></div>\n    </div>\n  )\n}\n"
    },
    {
      "path": "registry/src/react/examples/virtual-selection-repro/style.css",
      "type": "registry:component",
      "target": "components/editor/examples/virtual-selection-repro/style.css",
      "content": ".prosekit-virtual-selection-repro-legacy {\n  background: color-mix(in srgb, red 35%, transparent);\n}\n"
    }
  ],
  "meta": {
    "hasIcons": false,
    "hidden": true,
    "story": "virtual-selection-repro",
    "framework": "react",
    "accumulatedFiles": [
      "registry/src/react/examples/virtual-selection-repro/editor.tsx",
      "registry/src/react/examples/virtual-selection-repro/extension.ts",
      "registry/src/react/examples/virtual-selection-repro/index.ts",
      "registry/src/react/examples/virtual-selection-repro/nested-editable-view.tsx",
      "registry/src/react/examples/virtual-selection-repro/style.css"
    ],
    "internalDependencies": []
  },
  "$schema": "https://ui.shadcn.com/schema/registry-item.json"
}
