import { Extensible, ID } from "../../types/index.mjs"; import { RegistryEventCallback, RegistryEventName } from "../createRegistry/index.mjs"; import { ModelContext, ModelOptions, ModelTicket, ModelTicketInput } from "../createModel/index.mjs"; import { MaybeRefOrGetter } from "vue"; //#region src/composables/createSortable/index.d.ts /** * Input shape passed to `register` / `onboard`. Inherits all model fields. * * @template V Type of the per-ticket value. * * @example * ```ts * interface TaskTicket extends SortableTicketInput { * value: { id: number; label: string } * } * * const sortable = createSortable() * sortable.register({ value: { id: 1, label: 'A' } }) * ``` */ type SortableTicketInput = ModelTicketInput; /** * Output ticket returned by `register` / `onboard` / `get`. * Aliased to `ModelTicket` — sortable adds no per-ticket fields. * * @template Z The input ticket type (carries the value type V). */ type SortableTicket = ModelTicket; /** * Options accepted by `createSortable`. * * @example * ```ts * const sortable = createSortable({ * disabled: toRef(() => isReadOnlyMode.value), * }) * ``` */ interface SortableOptions extends Omit { /** * Mutation gate. When truthy, `move` / `swap` / `reorder` silently no-op. * * @default false * @remarks Per-ticket `disabled` is honored at the `move` and `swap` call sites * for that ticket. `reorder` **bypasses** per-ticket `disabled` — it's a bulk * operation declaring the canonical order, and applying that order may relocate * disabled tickets. `register` / `onboard` / `unregister` are NEVER gated — * those are composition, not user-driven reorder. * * @example * ```ts * const sortable = createSortable({ disabled: toRef(() => isSaving.value) }) * ``` */ disabled?: MaybeRefOrGetter; } /** * Payload of the `move:ticket` event. * * @example * ```ts * sortable.on('move:ticket', ({ ticket, from, to }) => { * console.log(ticket.id, from, to) * }) * ``` */ interface SortableMovePayload { ticket: E; from: number; to: number; } /** * Listener-binding signature for a sortable instance — a typed overload for * `move:ticket` plus the registry's generic event channel. * * @template E Output ticket type. */ type SortableEventListener = { (event: 'move:ticket', cb: (data: SortableMovePayload) => void): void; >(event: K, cb: RegistryEventCallback): void; }; /** * Public API of a sortable instance. * * @template Z Input ticket type. * @template E Output ticket type. */ interface SortableContext = SortableTicket> extends Omit, 'on' | 'off' | 'move'> { /** * Subscribe to the typed `move:ticket` event with a strongly-typed payload. * * @example * ```ts * sortable.on('move:ticket', ({ ticket, from, to }) => { * console.log(ticket.id, from, to) * }) * ``` */ on: SortableEventListener; /** * Unsubscribe from a `move:ticket` listener. Must be called with the same callback reference used to subscribe. * * @example * ```ts * function onMove ({ ticket, from, to }: SortableMovePayload) { * console.log(ticket.id, from, to) * } * * sortable.on('move:ticket', onMove) * // later... * sortable.off('move:ticket', onMove) * ``` */ off: SortableEventListener; /** * Move a ticket to a target index. Other tickets shift to fill. * Emits `move:ticket` once when the index actually changes — a move whose * `toIndex` already equals the ticket's current index is a no-op. * No-ops when the root sortable is disabled or when the ticket is disabled. * * @param id Ticket id to move. * @param toIndex Target index (zero-based). * @returns The moved ticket, or `undefined` when the move was gated or the id is unknown. * * @example * ```ts * sortable.move(ticket.id, 2) * ``` */ move: (id: ID, toIndex: number) => E | undefined; /** * Swap two tickets' positions. Emits `move:ticket` twice — once per ticket. * No-ops when the root sortable is disabled or when either ticket is disabled. * * @param a Ticket id at one position. * @param b Ticket id at the other position. * * @example * ```ts * sortable.swap(a.id, b.id) * ``` */ swap: (a: ID, b: ID) => void; /** * Set the canonical order in one shot. * * @param ids Permutation of currently-registered ticket ids. * * @remarks * Validates `ids.length === registry.size` and that every id is unique and known. * Logs a warning via `useLogger()` and silently no-ops if any check fails — drag * handlers that race against concurrent unregisters won't crash. * * Honors root `disabled` (whole call no-ops). Per-ticket `disabled` is **ignored** — * `reorder` is a bulk operation declaring the canonical order, and applying that * order may move disabled tickets. If you want disabled tickets pinned, exclude * them from the ids you pass. * * @example * ```ts * sortable.reorder([c.id, a.id, b.id]) * ``` */ reorder: (ids: ID[]) => void; } /** * Create an ordered-list state primitive. * * @param options Sortable options. * @returns A {@link SortableContext} that extends {@link ModelContext}. * * @example * ```ts * const sortable = createSortable() * sortable.onboard(todos.value.map(value => ({ value }))) * ``` */ declare function createSortable = SortableTicket>(_options?: SortableOptions): SortableContext; //#endregion export { SortableContext, SortableEventListener, SortableMovePayload, SortableOptions, SortableTicket, SortableTicketInput, createSortable };