Selection
import { Selection } from 'pixi-silk/selection';
The selected set of an editor, ordered by when each item was picked.
const selection = new Selection<Container>();
item.on('pointerdown', (e) => selection.pick(item, e.shiftKey ? 'toggle' : 'replace'));
selection.onChange(({ added, removed }) => console.log(added, removed));
Type Parameters#
| Type Parameter | Default type |
|---|---|
T | unknown |
Constructors#
Constructor#
new Selection<T = unknown>(options?: SelectionOptions<T>): Selection<T>;
Parameters#
| Parameter | Type |
|---|---|
options | SelectionOptions<T> |
Returns#
Selection<T>
Properties#
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
multiple | public | boolean | Allow more than one item. Change it at any time. | selection.ts:60 |
state | readonly | SelectionState<T> | Selected items, primary item and hovered item, as signals. | selection.ts:58 |
Accessors#
items#
Get Signature#
get items(): readonly T[];
Selected items, in order.
Returns
readonly T[]
primary#
Get Signature#
get primary(): T | null;
The primary item, or null when nothing is selected.
Returns
T | null
size#
Get Signature#
get size(): number;
How many items are selected.
Returns
number
Methods#
add()#
add(...items: T[]): this;
Adds items at the end. Selected items move to the end. The last one becomes primary. Items the filter rejects are skipped.
Parameters#
| Parameter | Type |
|---|---|
…items | T[] |
Returns#
this
clear()#
clear(): this;
Deselects everything.
Returns#
this
focus()#
focus(item: T): this;
Makes a selected item primary without changing the set. Selects it first when it is not.
Parameters#
| Parameter | Type |
|---|---|
item | T |
Returns#
this
has()#
has(item: T): boolean;
True when item is selected.
Parameters#
| Parameter | Type |
|---|---|
item | T |
Returns#
boolean
onChange()#
onChange(fn: (change: SelectionChange<T>) => void): () => void;
Calls fn after every change, with what joined and what left. A call that changes neither the
items nor the primary item emits nothing. Returns the unsubscribe function.
Parameters#
| Parameter | Type |
|---|---|
fn | (change: SelectionChange<T>) => void |
Returns#
() => void
pick()#
pick(item: T | null, mode?: SelectMode): this;
Applies one pick, the way a click does: replace, or add, remove or toggle with Shift.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
item | T | null | undefined |
mode | SelectMode | 'replace' |
Returns#
this
remove()#
remove(...items: T[]): this;
Removes items. Unselected items are ignored. If the primary item leaves, the last remaining item becomes primary.
Parameters#
| Parameter | Type |
|---|---|
…items | T[] |
Returns#
this
replace()#
replace(items: readonly T[]): this;
Makes items the whole selection, without duplicates. The last one becomes primary.
Parameters#
| Parameter | Type |
|---|---|
items | readonly T[] |
Returns#
this
selectWhere()#
selectWhere(
pool: Iterable<T>,
test: (item: T) => boolean,
mode?: SelectMode
): this;
Selects the items of pool that pass test, such as everything inside a marquee.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
pool | Iterable<T> | undefined |
test | (item: T) => boolean | undefined |
mode | SelectMode | 'replace' |
Returns#
this
toggle()#
toggle(item: T): this;
Selects item if it is not selected and deselects it if it is.
Parameters#
| Parameter | Type |
|---|---|
item | T |
Returns#
this