{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "editor-markdown-editor",
  "type": "registry:component",
  "title": "MarkdownEditor",
  "description": "Markdown editing with the rendered result beside it",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-dock.json",
    "https://gpuixui.sudokw.com/gpuix-primitives.json",
    "https://gpuixui.sudokw.com/gpuix-tokens.json",
    "https://gpuixui.sudokw.com/gpuix-tw.json"
  ],
  "files": [
    {
      "path": "packages/editor/src/internal/controllable.ts",
      "target": "@ui/gpuixui/editor/internal/controllable.ts",
      "type": "registry:lib",
      "content": "import { useCallback, useState } from \"react\";\n\nexport type ControllableState<Value> = readonly [Value, (next: Value) => void];\n\nexport interface ControllableStateOptions<Value> {\n  readonly value: Value | undefined;\n\n  readonly defaultValue: Value;\n\n  readonly onChange?: (next: Value) => void;\n}\n\nexport const useControllableState = <Value>({\n  value,\n  defaultValue,\n  onChange,\n}: ControllableStateOptions<Value>): ControllableState<Value> => {\n  const [internal, setInternal] = useState(defaultValue);\n  const controlled = value !== undefined;\n\n  const setValue = useCallback(\n    (next: Value) => {\n      if (!controlled) setInternal(next);\n      if (value !== next) onChange?.(next);\n    },\n    [controlled, value, onChange],\n  );\n\n  return [controlled ? value : internal, setValue];\n};\n"
    },
    {
      "path": "packages/editor/src/internal/recipe.ts",
      "target": "@ui/gpuixui/editor/internal/recipe.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport { classesToStyle } from \"../../tw/index.js\";\nimport type { Theme } from \"../../tokens/index.js\";\n\nexport const recipeStyle = (recipe: string, theme: Theme): StyleDesc =>\n  classesToStyle(recipe, theme).style;\n"
    },
    {
      "path": "packages/editor/src/internal/text.ts",
      "target": "@ui/gpuixui/editor/internal/text.ts",
      "type": "registry:lib",
      "content": "import { accessibleName } from \"../../primitives/core/index.js\";\n\nexport const draftOf = (value: unknown): string =>\n  typeof value === \"string\" ? value : \"\";\n\nexport const labelOf = (label: unknown, fallback: string): string =>\n  accessibleName(draftOf(label)) ?? fallback;\n"
    },
    {
      "path": "packages/editor/src/markdown/logic.ts",
      "target": "@ui/gpuixui/editor/markdown/logic.ts",
      "type": "registry:lib",
      "content": "import * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport * as Str from \"effect/String\";\nimport * as Schema from \"effect/Schema\";\n\nexport const MARKDOWN_MODES = [\"split\", \"source\", \"preview\"] as const;\n\nexport const MarkdownMode = Schema.Literals(MARKDOWN_MODES);\n\nexport type MarkdownMode = typeof MarkdownMode.Type;\n\nexport const isMarkdownMode = Schema.is(MarkdownMode);\n\nexport const modeOf = (\n  value: unknown,\n  fallback: MarkdownMode = \"split\",\n): MarkdownMode => (isMarkdownMode(value) ? value : fallback);\n\nconst FENCE = /^\\s{0,3}(`{3,}|~{3,})/;\n\ninterface OpenFence {\n  readonly family: \"ticks\" | \"tildes\";\n  readonly length: number;\n}\n\nconst foldFence = (\n  open: Option.Option<OpenFence>,\n  line: string,\n): Option.Option<OpenFence> => {\n  const match = FENCE.exec(line);\n  const marker = match?.[1];\n  if (match === null || marker === undefined) return open;\n  const family = Str.startsWith(\"`\")(marker) ? \"ticks\" : \"tildes\";\n  const rest = Str.trim(Str.slice(match[0].length)(line));\n  return Option.match(open, {\n    onNone: () => Option.some({ family, length: marker.length }),\n    onSome: (held) =>\n      held.family === family &&\n      marker.length >= held.length &&\n      Str.isEmpty(rest)\n        ? Option.none()\n        : open,\n  });\n};\n\nexport const openFence = (source: string): Option.Option<string> =>\n  Option.map(\n    Arr.reduce(Str.split(source, \"\\n\"), Option.none<OpenFence>(), foldFence),\n    (held) => Str.repeat(held.length)(held.family === \"ticks\" ? \"`\" : \"~\"),\n  );\n\nexport const closeFences = (source: string): string =>\n  Option.match(openFence(source), {\n    onNone: () => source,\n    onSome: (fence) =>\n      Str.endsWith(\"\\n\")(source) ? `${source}${fence}` : `${source}\\n${fence}`,\n  });\n\nexport { draftOf } from \"../internal/text.js\";\n\nexport interface MarkdownStats {\n  readonly lines: number;\n\n  readonly words: number;\n\n  readonly characters: number;\n}\n\nexport const markdownStats = (source: string): MarkdownStats => ({\n  lines: Str.isEmpty(source) ? 0 : Arr.length(Str.split(source, \"\\n\")),\n  words: Arr.length(Arr.filter(Str.split(source, /\\s+/), Str.isNonEmpty)),\n  characters: Arr.length(Arr.fromIterable(source)),\n});\n\nexport const statsLabel = (stats: MarkdownStats): string =>\n  `${stats.lines} ${stats.lines === 1 ? \"line\" : \"lines\"} · ${stats.words} ${\n    stats.words === 1 ? \"word\" : \"words\"\n  }`;\n\nconst SPLIT_MIN_SHARE = 0.25;\nconst SPLIT_MAX_SHARE = 0.75;\n\nexport const splitBounds = (\n  available: number,\n): { readonly min: number; readonly max: number } => {\n  const width = Math.max(0, available);\n  return {\n    min: Math.floor(width * SPLIT_MIN_SHARE),\n    max: Math.floor(width * SPLIT_MAX_SHARE),\n  };\n};\n\nexport const splitOf = (value: unknown, available: number): number => {\n  const { min, max } = splitBounds(available);\n  return typeof value === \"number\" && Number.isFinite(value)\n    ? Math.min(Math.max(Math.floor(value), min), max)\n    : Math.floor(Math.max(0, available) / 2);\n};\n"
    },
    {
      "path": "packages/editor/src/markdown/markdown-editor.tsx",
      "target": "@ui/gpuixui/editor/markdown/markdown-editor.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { StyleDesc } from \"@gpuix/react\";\nimport type { ReactElement } from \"react\";\nimport { mergeStyles } from \"@gpuix/react/floating\";\nimport { DIVIDER_WIDTH, SplitPane } from \"../../dock/index.js\";\nimport { Button, ScrollArea } from \"../../primitives/index.js\";\nimport {\n  fieldHelp,\n  fieldHint,\n  hintHost,\n  renderFieldHint,\n} from \"../../primitives/components/hint.js\";\nimport { disabledOpacity, nativeTextTheme, textStyle } from \"../../tokens/index.js\";\nimport { useTheme } from \"../../tokens/react.js\";\nimport { cn } from \"../../tw/index.js\";\nimport * as Option from \"effect/Option\";\nimport { useControllableState } from \"../internal/controllable.js\";\nimport { recipeStyle } from \"../internal/recipe.js\";\nimport { labelOf } from \"../internal/text.js\";\nimport {\n  closeFences,\n  draftOf,\n  MARKDOWN_MODES,\n  markdownStats,\n  modeOf,\n  splitBounds,\n  splitOf,\n  statsLabel,\n  type MarkdownMode,\n} from \"./logic.js\";\n\nconst EDITOR_ROOT =\n  \"flex flex-col overflow-hidden rounded-lg border border-input bg-background\";\nconst TOOLBAR = \"flex flex-row items-center gap-2 border-b border-border px-3\";\nconst BODY = \"flex flex-row min-h-0\";\nconst SOURCE_VIEW = \"flex flex-col flex-grow min-w-0\";\nconst PREVIEW_VIEW = \"flex flex-col flex-grow min-w-0\";\n\nconst MODE_LABELS: Readonly<Record<MarkdownMode, string>> = {\n  split: \"Split\",\n  source: \"Source\",\n  preview: \"Preview\",\n};\n\nexport interface MarkdownEditorProps {\n  readonly value?: string;\n\n  readonly defaultValue?: string;\n\n  readonly onValueChange?: (value: string) => void;\n\n  readonly mode?: MarkdownMode;\n\n  readonly defaultMode?: MarkdownMode;\n\n  readonly onModeChange?: (mode: MarkdownMode) => void;\n\n  readonly label?: string;\n\n  readonly placeholder?: string;\n\n  readonly height?: number;\n\n  readonly width?: number;\n\n  readonly splitSize?: number;\n\n  readonly defaultSplitSize?: number;\n\n  readonly onSplitSizeChange?: (size: number) => void;\n\n  readonly readOnly?: boolean;\n\n  readonly disabled?: boolean;\n\n  readonly className?: string;\n  readonly style?: StyleDesc;\n  readonly testId?: string;\n}\n\nconst DEFAULT_HEIGHT = 280;\nconst DEFAULT_WIDTH = 640;\nconst DEFAULT_LABEL = \"Markdown editor\";\n\nconst bodyHeight = (height: number): number =>\n  Number.isFinite(height) && height > 0 ? height : DEFAULT_HEIGHT;\n\nconst sourceStyle = (\n  theme: ReturnType<typeof useTheme>,\n  width: number,\n  height: number,\n): StyleDesc => ({\n  width,\n  height,\n  padding: theme.spacing.md,\n  backgroundColor: theme.colors.background,\n  color: theme.colors.foreground,\n  borderWidth: 0,\n});\n\nexport const MarkdownEditor = ({\n  value,\n  defaultValue = \"\",\n  onValueChange,\n  mode: modeProp,\n  defaultMode,\n  onModeChange,\n  label,\n  placeholder,\n  height = DEFAULT_HEIGHT,\n  width = DEFAULT_WIDTH,\n  splitSize,\n  defaultSplitSize,\n  onSplitSizeChange,\n  readOnly = false,\n  disabled = false,\n  className,\n  style,\n  testId,\n}: MarkdownEditorProps) => {\n  const theme = useTheme();\n\n  const name = labelOf(label, DEFAULT_LABEL);\n  const [source, setSource] = useControllableState({\n    value: value === undefined ? undefined : draftOf(value),\n    defaultValue: draftOf(defaultValue),\n    onChange: onValueChange,\n  });\n  const [mode, setMode] = useControllableState({\n    value: modeProp,\n    defaultValue: modeOf(defaultMode),\n    onChange: onModeChange,\n  });\n  const view = modeOf(mode);\n  const body = bodyHeight(height);\n  const total = Number.isFinite(width) && width > 0 ? width : DEFAULT_WIDTH;\n  const available = Math.max(0, total - DIVIDER_WIDTH);\n  const split = splitBounds(available);\n  const [splitAt, setSplitAt] = useControllableState({\n    value: splitSize,\n    defaultValue: defaultSplitSize ?? Math.floor(available / 2),\n    onChange: onSplitSizeChange,\n  });\n  const splitWidth = splitOf(splitAt, available);\n  const paneWidth = view === \"split\" ? splitWidth : total;\n  const stats = markdownStats(source);\n  const showsSource = view !== \"preview\";\n  const showsPreview = view !== \"source\";\n  const help = fieldHelp({ placeholder }, source);\n\n  const sourcePane = (width: number): ReactElement => {\n    const chrome = sourceStyle(theme, width, body);\n    const hint = fieldHint({ placeholder, disabled }, theme, source, {\n      origin: \"host\",\n      align: \"top\",\n      style: chrome,\n    });\n    return (\n      <div\n        style={mergeStyles(\n          recipeStyle(SOURCE_VIEW, theme),\n          hintHost({ width }),\n        )}\n      >\n        <textarea\n          value={source}\n          onChange={(event) => {\n            if (!readOnly && !disabled) setSource(event.value ?? \"\");\n          }}\n          theme={nativeTextTheme(theme, \"background\")}\n          aria-label={`${name} source`}\n          aria-description={help ?? \"Markdown source\"}\n          readOnly={readOnly || disabled}\n          tabIndex={disabled ? -1 : 0}\n          testId=\"markdown-source\"\n          style={chrome}\n        />\n        {Option.match(hint, {\n          onNone: () => null,\n          onSome: (painted) => renderFieldHint(painted, \"markdown-source\"),\n        })}\n      </div>\n    );\n  };\n\n  const previewPane = (width?: number): ReactElement => (\n    <div\n      style={mergeStyles(\n        recipeStyle(PREVIEW_VIEW, theme),\n        width === undefined ? {} : { width },\n      )}\n    >\n      <ScrollArea\n        axis=\"vertical\"\n        focusable={!disabled}\n        label={`${name} preview`}\n        testId=\"markdown-preview\"\n        viewportTestId=\"markdown-preview-viewport\"\n        padding=\"sm\"\n        style={{ height: body }}\n      >\n        {source.trim() === \"\" ? (\n          <text\n            testId=\"markdown-empty\"\n            style={{\n              ...textStyle(theme, \"body\"),\n              color: theme.colors.mutedForeground,\n            }}\n          >\n            Nothing to preview yet.\n          </text>\n        ) : (\n          <markdown\n            testId=\"markdown-rendered\"\n            source={closeFences(source)}\n            theme={nativeTextTheme(theme, \"background\")}\n            style={{ color: theme.colors.foreground }}\n          />\n        )}\n      </ScrollArea>\n    </div>\n  );\n\n  return (\n    <div\n      testId={testId}\n      style={mergeStyles(\n        mergeStyles(\n          mergeStyles(recipeStyle(cn(EDITOR_ROOT, className), theme), {\n            width: total,\n          }),\n          disabled ? { opacity: disabledOpacity } : {},\n        ),\n        style,\n      )}\n    >\n      <div style={recipeStyle(TOOLBAR, theme)}>\n        {MARKDOWN_MODES.map((candidate) => (\n          <Button\n            key={candidate}\n            variant={view === candidate ? \"secondary\" : \"ghost\"}\n            size=\"sm\"\n            label={MODE_LABELS[candidate]}\n            testId={`markdown-mode-${candidate}`}\n            aria-selected={view === candidate}\n            disabled={disabled}\n            onActivate={() => setMode(candidate)}\n          >\n            {MODE_LABELS[candidate]}\n          </Button>\n        ))}\n        <div style={{ flexGrow: 1 }} />\n        <text\n          testId=\"markdown-stats\"\n          style={{\n            ...textStyle(theme, \"caption\"),\n            color: theme.colors.mutedForeground,\n          }}\n        >\n          {statsLabel(stats)}\n        </text>\n      </div>\n      <div style={recipeStyle(BODY, theme)}>\n        {showsSource && showsPreview ? (\n          <SplitPane\n            size={splitWidth}\n            min={split.min}\n            max={split.max}\n            onResize={setSplitAt}\n            disabled={disabled}\n            dividerLabel={`${name} split`}\n            testId={testId === undefined ? undefined : `${testId}-split`}\n            first={sourcePane(splitWidth)}\n            second={previewPane()}\n          />\n        ) : showsSource ? (\n          sourcePane(paneWidth)\n        ) : showsPreview ? (\n          previewPane(paneWidth)\n        ) : null}\n      </div>\n    </div>\n  );\n};\n\nexport { MARKDOWN_MODES };\n"
    }
  ],
  "docs": "[MarkdownEditor](https://gpuixui.sudokw.com/editor/markdown-editor) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/editor/markdown-editor",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}