k5kit
Version:
Utilities for TypeScript and Svelte
60 lines (59 loc) • 2.38 kB
TypeScript
export type SelectionOptions<T> = {
scroll_to: (target: {
item: T;
index: number;
}) => void;
on_contextmenu?: (items: Set<T>) => void;
};
export declare class KSelection<T> {
#private;
/** Currently selected items. Disallowing assignment
* prevents the Svelte store from getting out of sync */
readonly items: Set<T>;
/** Full list of items that can be selected */
all: T[];
/** The last added index */
last_added: {
index: number;
item: T;
} | null;
/** An anchor index for shift selection. */
shift_anchor: {
index: number;
item: T;
} | null;
/** Whether the user is current mouseup is a click or a selection update */
possible_row_click: boolean;
scroll_to: SelectionOptions<T>['scroll_to'];
on_contextmenu: SelectionOptions<T>['on_contextmenu'];
constructor(all_items: T[], options: SelectionOptions<T>);
clear(): void;
/** Update the list of items that can be selected.
* Items that no longer exist are de-selected. */
update_all_items(all: T[]): void;
/** Get first selected index, or `null` if selection is empty */
find_first_index(): number | null;
/** Get first selected item, or `undefined` if selection is empty */
find_first(): T | undefined;
items_as_array(): T[];
get_selected_indexes(): number[];
add_index(index: number): void;
add_index_unchecked(index: number): void;
/** Shift-select to index */
shift_select_to(to_index: number): Set<T> | undefined;
/** Replace selection with the previous index, like perssing `ArrowUp` in a list. */
go_backward(): void;
/** Replace selection with the previous index, like perssing `ArrowDown` in a list. */
go_forward(): void;
/** Expand or shrink selection backwards (shift+up) */
shift_select_backward(): void;
/** Expand or shrink selection forwards (shift+down) */
shift_select_forward(): void;
mouse_down_select(e: MouseEvent, index: number): void;
/** This does also handle keydown events, which aren't row events */
handle_events(e: Event, index: number): void;
handle_mouse_down(e: MouseEvent, index: number): void;
handle_contextmenu(e: MouseEvent, index: number): void;
handle_click(e: MouseEvent, index: number): void;
handle_keydown(e: KeyboardEvent): void;
}