Skip to content
pixi-silk
GitHub repository
Class

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 ParameterDefault type
Tunknown

Constructors#

Constructor#

new Selection<T = unknown>(options?: SelectionOptions<T>): Selection<T>;

Parameters#

ParameterType
optionsSelectionOptions<T>

Returns#

Selection<T>

Properties#

PropertyModifierTypeDescriptionDefined in
multiplepublicbooleanAllow more than one item. Change it at any time.selection.ts:60
statereadonlySelectionState<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#

ParameterType
…itemsT[]

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#

ParameterType
itemT

Returns#

this


has()#

has(item: T): boolean;

True when item is selected.

Parameters#

ParameterType
itemT

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#

ParameterType
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#

ParameterTypeDefault value
itemT | nullundefined
modeSelectMode'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#

ParameterType
…itemsT[]

Returns#

this


replace()#

replace(items: readonly T[]): this;

Makes items the whole selection, without duplicates. The last one becomes primary.

Parameters#

ParameterType
itemsreadonly 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#

ParameterTypeDefault value
poolIterable<T>undefined
test(item: T) => booleanundefined
modeSelectMode'replace'

Returns#

this


toggle()#

toggle(item: T): this;

Selects item if it is not selected and deselects it if it is.

Parameters#

ParameterType
itemT

Returns#

this