{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "diff-web",
  "title": "Diff (Web Components)",
  "description": "One file's changes or a run's, with Keep and Undo for each file and change, and Keep all and Undo all.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/diff.ts",
      "content": "import {\n  codeSpans,\n  type DiffActionDetails,\n  type DiffApi,\n  type DiffLineView,\n  type DiffOpenReason,\n  type DiffOptions,\n  type DiffState,\n  type DiffTarget,\n  diffPrimitive,\n  WORDS,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, type UiActionDetail, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\n\nconst PENDING = `<span data-kind-pending-mark data-status-mark data-status=\"running\" aria-hidden=\"true\"></span>`;\n\nconst DEFAULT_PARTS = `<kind-diff-summary><div data-diff-heading><kind-diff-summary-title></kind-diff-summary-title><kind-diff-summary-counts></kind-diff-summary-counts><kind-diff-summary-progress></kind-diff-summary-progress><kind-diff-summary-outcome></kind-diff-summary-outcome></div><kind-diff-summary-mark></kind-diff-summary-mark><kind-diff-summary-actions><kind-diff-undo-all-question></kind-diff-undo-all-question><kind-diff-undo-all><button></button></kind-diff-undo-all><kind-diff-keep-all><button>${PENDING}</button></kind-diff-keep-all><kind-diff-undo-all-cancel><button></button></kind-diff-undo-all-cancel><kind-diff-undo-all-confirm><button>${PENDING}</button></kind-diff-undo-all-confirm></kind-diff-summary-actions><kind-diff-summary-error></kind-diff-summary-error></kind-diff-summary><kind-diff-list></kind-diff-list>`;\n\nconst FILE_PARTS = `<kind-diff-file-trigger><button><span data-diff-name><kind-diff-file-path></kind-diff-file-path><kind-diff-file-old-path></kind-diff-file-old-path></span><kind-diff-file-status></kind-diff-file-status><kind-diff-file-counts></kind-diff-file-counts><kind-diff-file-mark></kind-diff-file-mark></button></kind-diff-file-trigger><kind-diff-file-actions><kind-diff-file-error></kind-diff-file-error><kind-diff-file-undo><button></button></kind-diff-file-undo><kind-diff-file-keep><button></button></kind-diff-file-keep><kind-diff-file-outcome></kind-diff-file-outcome></kind-diff-file-actions><kind-diff-file-content><kind-diff-file-note></kind-diff-file-note><kind-diff-viewport><kind-diff-table></kind-diff-table><kind-diff-show-all><button></button></kind-diff-show-all></kind-diff-viewport></kind-diff-file-content>`;\n\nconst LINE_CELLS = `<kind-diff-old-number></kind-diff-old-number><kind-diff-new-number></kind-diff-new-number><kind-diff-sign></kind-diff-sign><kind-diff-code></kind-diff-code>`;\n\nconst CHANGE_PARTS = `<kind-diff-change-title><span data-diff-change-text></span><kind-diff-change-error></kind-diff-change-error><kind-diff-change-undo><button></button></kind-diff-change-undo><kind-diff-change-keep><button></button></kind-diff-change-keep><kind-diff-change-outcome></kind-diff-change-outcome></kind-diff-change-title>`;\n\n// Parts this element filled in. A part someone wrote into keeps what they wrote, as React's children do. Kept\n// across instances, because UI SDK rebuilds one when the element returns to the page.\nconst owned = new WeakSet<Element>();\nconst drawn = new WeakMap<Element, unknown>();\n\n// Parts drawn from a template are this element's to fill in.\nconst fillWith = (el: Element, html: string) => {\n  el.innerHTML = html;\n  for (const part of el.querySelectorAll(\"*\")) owned.add(part);\n};\n\nconst ownable = (part: Element | null | undefined): part is Element =>\n  !!part && (owned.has(part) || (!part.firstElementChild && !part.textContent?.trim()));\n\n/** Draws `value` into an empty or owned part, once per value. */\nconst draw = <Value>(\n  part: Element | null | undefined,\n  value: Value,\n  paint: (part: Element, value: Value) => void,\n) => {\n  if (!ownable(part)) return;\n  owned.add(part);\n  if (drawn.get(part) === value) return;\n  drawn.set(part, value);\n  paint(part, value);\n};\n\nconst element = (part: Element, tag: string, attrs: Record<string, string> = {}, text = \"\") => {\n  const el = part.ownerDocument.createElement(tag);\n  for (const [name, value] of Object.entries(attrs)) el.setAttribute(name, value);\n  el.textContent = text;\n  return el;\n};\n\nconst text = (part: Element, value: string) => {\n  part.replaceChildren(\n    ...codeSpans(value).map((piece, index) => (index % 2 ? element(part, \"code\", {}, piece) : piece)),\n  );\n};\n\n// A button part's text goes into its button, after any running mark.\nconst label = (part: Element, value: string) => {\n  const button = part.querySelector(\"button\");\n  if (!button) return;\n  const mark = button.querySelector(\"[data-kind-pending-mark]\");\n  button.replaceChildren(...(mark ? [mark] : []), value);\n};\n\nconst counts = (\n  part: Element,\n  { added, removed, name }: { added: string; removed: string; name: string },\n) => {\n  const shown = element(part, \"span\", { \"aria-hidden\": \"true\" });\n  if (added) shown.append(element(part, \"span\", { \"data-diff-added\": \"\" }, added));\n  if (added && removed) shown.append(\" \");\n  if (removed) shown.append(element(part, \"span\", { \"data-diff-removed\": \"\" }, removed));\n  part.replaceChildren(shown, element(part, \"span\", { \"data-diff-hidden\": \"\" }, name));\n};\n\nconst outcome = (part: Element, { text: words, working }: { text: string; working: boolean }) => {\n  const mark = element(part, \"span\", {\n    \"data-status-mark\": \"\",\n    \"data-status\": \"running\",\n    \"aria-hidden\": \"true\",\n  });\n  part.replaceChildren(...(working ? [mark] : []), words);\n};\n\nconst code = (part: Element, line: DiffLineView) => {\n  if (line.kind === \"collapsed\") {\n    const button = element(part, \"kind-diff-collapsed\");\n    button.append(element(part, \"button\", {}, line.show));\n    part.replaceChildren(button, element(part, \"kind-diff-collapsed-text\", {}, line.hiddenText));\n    return;\n  }\n  const pieces = line.pieces.map((piece) => {\n    const run = element(part, \"span\", {}, piece.text);\n    if (piece.color) run.style.color = piece.color;\n    if (piece.changed) run.setAttribute(\"data-diff-changed\", \"\");\n    if (piece.fontStyle) run.setAttribute(`data-${piece.fontStyle}`, \"\");\n    return run;\n  });\n  const wrap = line.kind === \"add\" ? \"ins\" : line.kind === \"remove\" ? \"del\" : undefined;\n  const body = wrap ? element(part, wrap) : undefined;\n  body?.append(...pieces);\n  part.replaceChildren(\n    ...(line.prefix ? [element(part, \"span\", { \"data-diff-hidden\": \"\" }, `${line.prefix} `)] : []),\n    ...(body ? [body] : pieces),\n  );\n};\n\n// Children this element added to a list or table, by key, so it moves and removes only its own.\nconst rowsOf = new WeakMap<Element, Map<string, Element>>();\n\nconst place = (\n  parent: Element,\n  keys: readonly string[],\n  make: (key: string) => Element,\n  before: Node | null = null,\n) => {\n  const existing = rowsOf.get(parent) ?? new Map<string, Element>();\n  rowsOf.set(parent, existing);\n  const wanted = new Set(keys);\n  for (const [key, row] of existing) {\n    if (wanted.has(key)) continue;\n    row.remove();\n    existing.delete(key);\n  }\n  let next: Node | null = before;\n  for (let i = keys.length - 1; i >= 0; i--) {\n    const key = keys[i] as string;\n    let row = existing.get(key);\n    if (!row) {\n      row = make(key);\n      existing.set(key, row);\n    }\n    if (row.nextSibling !== next || row.parentNode !== parent) parent.insertBefore(row, next);\n    next = row;\n  }\n};\n\nconst drawLine = (row: Element, line: DiffLineView | undefined) => {\n  if (!line) return;\n  draw(row.querySelector(\":scope > kind-diff-code\"), line, code);\n};\n\nconst drawFile = (file: Element, path: string, state: DiffState) => {\n  const view = state.files[path];\n  if (!view) return;\n  const own = (tag: string) => file.querySelector(tag);\n  draw(own(\"kind-diff-file-path\"), view.path, (part, value) =>\n    part.replaceChildren(element(part, \"bdi\", {}, value)),\n  );\n  draw(own(\"kind-diff-file-old-path\"), view.oldPath ?? \"\", (part, value) =>\n    part.replaceChildren(element(part, \"bdi\", {}, value && `from ${value}`)),\n  );\n  draw(own(\"kind-diff-file-status\"), view.statusText, text);\n  draw(own(\"kind-diff-file-counts\"), `${view.added} ${view.removed} ${view.countsName}`, (part) =>\n    counts(part, {\n      added: view.added > 0 ? `+${view.added.toLocaleString(\"en-US\")}` : \"\",\n      removed: view.removed > 0 ? `\\u2212${view.removed.toLocaleString(\"en-US\")}` : \"\",\n      name: view.countsName,\n    }),\n  );\n  draw(own(\"kind-diff-file-undo\"), state.undoLabel, label);\n  draw(own(\"kind-diff-file-keep\"), state.keepLabel, label);\n  draw(own(\"kind-diff-file-error\"), view.error, text);\n  draw(own(\"kind-diff-file-outcome\"), view.outcome, (part, value) =>\n    outcome(part, { text: value, working: view.working !== undefined }),\n  );\n  draw(own(\"kind-diff-file-note\"), view.note, text);\n  draw(own(\"kind-diff-show-all\"), view.hidden, (part) =>\n    label(\n      part,\n      `Show full diff (${((state.tables[path]?.length ?? 0) + view.hidden).toLocaleString(\"en-US\")} lines)`,\n    ),\n  );\n  const table = own(\"kind-diff-table\");\n  if (!table || (!owned.has(table) && table.firstElementChild)) return;\n  owned.add(table);\n  let head = table.querySelector(\":scope > kind-diff-head\");\n  if (!head) {\n    head = element(table, \"kind-diff-head\");\n    for (const column of [\"old\", \"new\", \"sign\", \"code\"]) {\n      head.append(element(table, \"kind-diff-column\", { value: `${path}#${column}` }, COLUMNS[column]));\n    }\n    table.prepend(head);\n  }\n  const rows = state.tables[path] ?? [];\n  place(\n    table,\n    rows.map((row) => row.key),\n    (key) => {\n      const isChange = rows.find((row) => row.key === key)?.change;\n      const row = element(table, isChange ? \"kind-diff-change\" : \"kind-diff-line\", { value: key });\n      fillWith(row, isChange ? CHANGE_PARTS : LINE_CELLS);\n      return row;\n    },\n  );\n  for (const row of rows) {\n    const el = rowsOf.get(table)?.get(row.key);\n    if (!el) continue;\n    if (!row.change) {\n      drawLine(el, state.lines[row.key]);\n      continue;\n    }\n    const change = state.changes[row.key];\n    if (!change) continue;\n    const words = el.querySelector(\"[data-diff-change-text]\");\n    if (words && words.textContent !== change.title) words.textContent = change.title;\n    draw(el.querySelector(\"kind-diff-change-undo\"), state.undoLabel, label);\n    draw(el.querySelector(\"kind-diff-change-keep\"), state.keepLabel, label);\n    draw(el.querySelector(\"kind-diff-change-error\"), change.error, text);\n    draw(el.querySelector(\"kind-diff-change-outcome\"), change.outcome, (part, value) =>\n      outcome(part, { text: value, working: change.working !== undefined }),\n    );\n  }\n};\n\nconst COLUMNS: Record<string, string> = { old: \"Old line\", new: \"New line\", sign: \"Change\", code: \"Code\" };\n\n/**\n * `<kind-diff>` fills in its parts when it has no children: the summary, then a `kind-diff-file` for each file with\n * its lines. `files` is a property. The `can-keep` and `can-undo` attributes offer Keep and Undo, which fire `keep`\n * and `undo` with the target as `detail.value`; listeners call `event.detail.waitUntil(promise)` to show \"Keeping…\"\n * until it settles.\n */\nexport function defineDiff(): void {\n  if (customElements.get(\"kind-diff\")) return;\n  createParts(diffPrimitive, {\n    root: {\n      tag: \"kind-diff\",\n      kinds: { canKeep: \"boolean\", canUndo: \"boolean\" },\n      defaults: { perChange: true, agentConfirm: true },\n      // Without an attribute its callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const { canKeep, canUndo } = el as KindDiffElement;\n        return { ...(canKeep ? {} : { onKeep: undefined }), ...(canUndo ? {} : { onUndo: undefined }) };\n      },\n      // The actions are getters, offered only while available, which a copied list of methods would freeze.\n      api: [\"setOpen\", \"expand\", \"showAll\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) fillWith(el, DEFAULT_PARTS);\n        const paint = () => {\n          const state = instance.get() as DiffState;\n          const own = (tag: string) => el.querySelector(tag);\n          draw(own(\"kind-diff-summary-title\"), state.summary.title, text);\n          draw(own(\"kind-diff-summary-counts\"), `${state.summary.added} ${state.summary.removed}`, (part) =>\n            counts(part, {\n              added: state.summary.added > 0 ? `+${state.summary.added.toLocaleString(\"en-US\")}` : \"\",\n              removed:\n                state.summary.removed > 0 ? `\\u2212${state.summary.removed.toLocaleString(\"en-US\")}` : \"\",\n              name: state.summary.countsName,\n            }),\n          );\n          draw(own(\"kind-diff-summary-progress\"), state.summary.progress, text);\n          draw(own(\"kind-diff-summary-outcome\"), state.summary.progress, text);\n          draw(own(\"kind-diff-undo-all-question\"), state.summary.question, (part, value) =>\n            part.replaceChildren(\n              `${value} `,\n              element(part, \"span\", { \"data-diff-warning\": \"\" }, WORDS.cantBeUndone),\n            ),\n          );\n          draw(own(\"kind-diff-undo-all\"), WORDS.undoAll, label);\n          draw(own(\"kind-diff-keep-all\"), WORDS.keepAll, label);\n          draw(own(\"kind-diff-undo-all-cancel\"), \"Cancel\", label);\n          draw(own(\"kind-diff-undo-all-confirm\"), WORDS.undoAll, label);\n          draw(own(\"kind-diff-summary-error\"), state.allError, text);\n          const list = own(\"kind-diff-list\");\n          if (list && (owned.has(list) || !list.firstElementChild)) {\n            owned.add(list);\n            place(list, state.paths, (path) => {\n              const file = element(list, \"kind-diff-file\", { value: path });\n              fillWith(file, FILE_PARTS);\n              return file;\n            });\n          }\n          for (const file of el.querySelectorAll(\"kind-diff-file\")) {\n            const path = file.getAttribute(\"value\");\n            if (path !== null && file.closest(\"kind-diff\") === el) drawFile(file, path, state);\n          }\n        };\n        paint();\n        return instance.subscribe(paint);\n      },\n    },\n    summary: \"kind-diff-summary\",\n    \"summary-title\": \"kind-diff-summary-title\",\n    \"summary-counts\": \"kind-diff-summary-counts\",\n    \"summary-mark\": \"kind-diff-summary-mark\",\n    \"summary-progress\": \"kind-diff-summary-progress\",\n    \"summary-outcome\": \"kind-diff-summary-outcome\",\n    \"summary-actions\": \"kind-diff-summary-actions\",\n    \"undo-all\": { tag: \"kind-diff-undo-all\", target: wrappedButton, display: \"contents\" },\n    \"keep-all\": { tag: \"kind-diff-keep-all\", target: wrappedButton, display: \"contents\" },\n    \"undo-all-question\": \"kind-diff-undo-all-question\",\n    \"undo-all-cancel\": { tag: \"kind-diff-undo-all-cancel\", target: wrappedButton, display: \"contents\" },\n    \"undo-all-confirm\": { tag: \"kind-diff-undo-all-confirm\", target: wrappedButton, display: \"contents\" },\n    \"summary-error\": \"kind-diff-summary-error\",\n    list: \"kind-diff-list\",\n    file: \"kind-diff-file\",\n    \"file-trigger\": { tag: \"kind-diff-file-trigger\", target: wrappedButton, display: \"contents\" },\n    \"file-path\": \"kind-diff-file-path\",\n    \"file-old-path\": \"kind-diff-file-old-path\",\n    \"file-status\": \"kind-diff-file-status\",\n    \"file-counts\": \"kind-diff-file-counts\",\n    \"file-mark\": \"kind-diff-file-mark\",\n    \"file-actions\": \"kind-diff-file-actions\",\n    \"file-undo\": { tag: \"kind-diff-file-undo\", target: wrappedButton, display: \"contents\" },\n    \"file-keep\": { tag: \"kind-diff-file-keep\", target: wrappedButton, display: \"contents\" },\n    \"file-outcome\": \"kind-diff-file-outcome\",\n    \"file-error\": \"kind-diff-file-error\",\n    \"file-content\": \"kind-diff-file-content\",\n    \"file-note\": \"kind-diff-file-note\",\n    viewport: \"kind-diff-viewport\",\n    table: \"kind-diff-table\",\n    head: \"kind-diff-head\",\n    column: \"kind-diff-column\",\n    change: \"kind-diff-change\",\n    \"change-title\": \"kind-diff-change-title\",\n    \"change-undo\": { tag: \"kind-diff-change-undo\", target: wrappedButton, display: \"contents\" },\n    \"change-keep\": { tag: \"kind-diff-change-keep\", target: wrappedButton, display: \"contents\" },\n    \"change-outcome\": \"kind-diff-change-outcome\",\n    \"change-error\": \"kind-diff-change-error\",\n    line: \"kind-diff-line\",\n    \"old-number\": \"kind-diff-old-number\",\n    \"new-number\": \"kind-diff-new-number\",\n    sign: \"kind-diff-sign\",\n    code: \"kind-diff-code\",\n    collapsed: { tag: \"kind-diff-collapsed\", target: wrappedButton, display: \"contents\" },\n    \"collapsed-text\": \"kind-diff-collapsed-text\",\n    \"show-all\": { tag: \"kind-diff-show-all\", target: wrappedButton, display: \"contents\" },\n  });\n}\n\nexport type KindDiffEvents = {\n  keep: CustomEvent<{ value: DiffTarget } & DiffActionDetails & Pick<UiActionDetail, \"waitUntil\">>;\n  undo: CustomEvent<{ value: DiffTarget } & DiffActionDetails & Pick<UiActionDetail, \"waitUntil\">>;\n  openchange: UiChangeEvent<string[], DiffOpenReason>;\n};\n\nexport interface KindDiffElement\n  extends UiElement<KindDiffEvents>,\n    AttributeProperties<typeof diffPrimitive, \"api\"> {\n  canKeep: boolean;\n  canUndo: boolean;\n  files?: DiffOptions[\"files\"];\n  api: Pick<DiffApi, \"setOpen\" | \"expand\" | \"showAll\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-diff\": KindDiffElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/diff.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
