{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "edit-in-place",
  "title": "Edit in place",
  "description": "Text that becomes a field in place, on ui-sdk's createInlineEdit: when the field opens, Enter, Escape and focus leaving, and a rename an agent can make. The Inline edit's primitive.",
  "dependencies": [
    "@ui-sdk/core"
  ],
  "files": [
    {
      "path": "registry/lib/edit-in-place.ts",
      "content": "import {\n  type ChangeDetails,\n  createChange,\n  createInlineEdit,\n  definePrimitive,\n  type InlineEditReason,\n  isComposing,\n} from '@ui-sdk/core'\n\n/*\n * Text that becomes a field in place, built on ui-sdk's `createInlineEdit`:\n * the Inline edit's primitive. The component around it owns the value, the\n * save and whether the field is open, and passes them in as options; this\n * says when the field opens, fills it, hears Enter, Escape and focus leaving,\n * and hands each commit to `commit`, which says whether the field closes.\n * An agent renames it with `setValue`, as a person who typed the text and\n * pressed Enter would.\n */\n\n/** Why a value was saved: a key, focus leaving, or code and agents (`imperative`). */\ntype EditInPlaceReason = InlineEditReason\n\ntype EditInPlaceOptions = {\n  /** The value on show, which a commit of the same text leaves as it is. */\n  value?: string\n  /** Saves a value: called through `save`, with who asked for it in `details.source`. */\n  onValueChange?: (value: string, details: ChangeDetails<EditInPlaceReason>) => void\n  /** Whether the field is open. */\n  editing?: boolean\n  onEditingChange?: (editing: boolean) => void\n  /** The text the field opens with; the value by default. */\n  opening?: () => string\n  /**\n   * Decides a commit of `text`: saves it through `save`, or doesn't, and says\n   * whether the field closes. Without it, the trimmed text is saved.\n   */\n  commit?: (\n    text: string,\n    details: { reason: EditInPlaceReason; event?: Event },\n  ) => boolean\n  /** The save the last commit started, while it runs: an agent's rename waits for it. */\n  saving?: () => Promise<void> | undefined\n  /** A save is running that the field waits for: Escape does nothing. */\n  busy?: boolean\n  /** Whether the field has been open since the component mounted, so it leaves focus where it is. */\n  openSinceMount?: () => boolean\n  disabled?: boolean\n}\n\n/** The one value this edits, as `createInlineEdit` names what it edits. */\nconst TEXT = 'text'\n\nconst editInPlace = definePrimitive({\n  name: 'inline-edit',\n  options: {\n    value: 'string',\n    onValueChange: 'callback',\n    editing: 'boolean',\n    onEditingChange: 'callback',\n    opening: 'value',\n    commit: 'action',\n    saving: 'value',\n    busy: 'boolean',\n    openSinceMount: 'value',\n    disabled: 'boolean',\n  },\n  controlled: ['value'],\n  state: (options: EditInPlaceOptions) => ({\n    value: options.value ?? '',\n    session: options.editing ? TEXT : (null as string | null),\n    disabled: !!options.disabled,\n  }),\n  sync: (options) => ({\n    ...(options.value === undefined ? {} : { value: options.value }),\n    ...(options.editing === undefined ? {} : { session: options.editing ? TEXT : null }),\n    disabled: !!options.disabled,\n  }),\n  api: (context) => {\n    const { store, options } = context\n    const change = createChange(context)\n    const save = (value: string, details: { reason: EditInPlaceReason; event?: Event }) =>\n      change('value', value, details)\n    const decide = (\n      text: string,\n      details: { reason: EditInPlaceReason; event?: Event },\n    ) => options().commit?.(text, details) ?? save(text.trim(), details)\n    const edit = createInlineEdit(context, {\n      key: 'session',\n      value: () => store.get().value,\n      commit: (_id, text, details) => decide(text, details),\n      disabled: () => !!options().disabled,\n    })\n    return {\n      edit,\n      save,\n      /** Opens the field, as a press on the text does. */\n      open: (reason: EditInPlaceReason = 'imperative', event?: Event) =>\n        edit.start(TEXT, { reason, event }),\n      /**\n       * Saves `text` as if it was typed and Enter pressed: an agent's rename.\n       * A save that takes a while returns its promise, which fails with the\n       * reason the save failed.\n       */\n      setValue: (\n        text: string,\n        details: { reason?: EditInPlaceReason; event?: Event } = {},\n      ) => {\n        const done = decide(text, {\n          reason: details.reason ?? 'imperative',\n          event: details.event,\n        })\n        return options().saving?.() ?? done\n      },\n      /** Closes the field without saving, unless a save it waits for runs. */\n      cancel: () => {\n        if (!options().busy) edit.cancel()\n      },\n      /** What the field opens with. */\n      opening: () => options().opening?.() ?? store.get().value,\n      /** Tells the owner the field opened or closed. */\n      notify: (editing: boolean) => options().onEditingChange?.(editing),\n      /** Whether the field is the one open since the component mounted. */\n      openSinceMount: () => options().openSinceMount?.() ?? false,\n    }\n  },\n  slots: (slot) => ({\n    root: slot({\n      attrs: (state) => ({\n        role: 'group',\n        'data-editing': state.session ? '' : undefined,\n        'data-disabled': state.disabled ? '' : undefined,\n      }),\n      on: {\n        // Escape puts the text back from anywhere in the field, its buttons included; only the field closes, not a dialog it sits in.\n        keydown: (event, { api, store }) => {\n          if (!store.get().session || event.key !== 'Escape' || isComposing(event)) return\n          event.preventDefault()\n          event.stopPropagation()\n          api.cancel()\n        },\n        // Focus that leaves the field, its buttons included, saves; another window taking focus isn't a press elsewhere.\n        focusout: (event, { api, store, el }) => {\n          if (!store.get().session || !document.hasFocus()) return\n          if (el?.contains(event.relatedTarget as Node | null)) return\n          api.edit.end('focus-out', event)\n        },\n      },\n      // The owner hears the field open and close, however it happened.\n      bind: (_el, { store, api }) => {\n        let open = store.get().session !== null\n        return store.subscribe(() => {\n          const now = store.get().session !== null\n          if (now === open) return\n          open = now\n          api.notify(now)\n        })\n      },\n    }),\n    input: slot({\n      on: {\n        keydown: (event, { api }) => {\n          // Enter that picks a word in an input method editor isn't a save, and a held Enter would open the field it just closed.\n          if (event.key !== 'Enter' || isComposing(event)) return\n          event.preventDefault()\n          if (!event.repeat) api.edit.end('enter-key', event)\n        },\n      },\n      bind: (el, { api }) => {\n        const input = el as HTMLInputElement\n        // `bindInput` focuses the field, which is right for one that opens\n        // now; one open from the first paint leaves focus where it is, so\n        // its focus and select, which focuses too, wait for a person.\n        const still = api.openSinceMount()\n        if (still) {\n          input.focus = () => {}\n          input.select = () => {}\n        }\n        const unbind = api.edit.bindInput(input)\n        if (still) {\n          delete (input as Partial<HTMLInputElement>).focus\n          delete (input as Partial<HTMLInputElement>).select\n        }\n        input.value = api.opening()\n        // Picked from the end back, with the start in view, so a long text stays where it was.\n        input.setSelectionRange(0, input.value.length, 'backward')\n        input.scrollLeft = 0\n        return unbind\n      },\n    }),\n    text: slot({\n      attrs: () => ({ type: 'button' }),\n      press: ({ api }, event) => api.open('imperative', event),\n      on: {\n        keydown: (event, { api }) => {\n          // A held key repeats its press, which would save the field it just opened.\n          if (event.repeat && (event.key === 'Enter' || event.key === ' ')) {\n            event.preventDefault()\n          } else if (event.key === 'F2') {\n            event.preventDefault()\n            api.open('edit-key', event)\n          }\n        },\n      },\n    }),\n  }),\n  agent: { actions: ['setValue'] },\n})\n\nexport { editInPlace }\nexport type { EditInPlaceOptions, EditInPlaceReason }\n",
      "type": "registry:lib",
      "target": "@lib/edit-in-place.ts"
    }
  ],
  "type": "registry:lib"
}