Skip to content

Commit 9fce320

Browse files
committed
fix(core): simplify NgtSelection API with exposing single method update
1 parent 6f90fa1 commit 9fce320

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

libs/core/src/lib/directives/selection.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,24 @@ import { getLocalState } from '../instance';
1515
@Directive({ standalone: true, selector: '[ngtSelection]' })
1616
export class NgtSelection {
1717
enabled = input(true, { alias: 'ngtSelection', transform: booleanAttribute });
18-
19-
private selection = signal<Array<ElementRef<Object3D> | Object3D>>([]);
20-
collection = this.selection.asReadonly();
21-
22-
select(...objects: Array<ElementRef<Object3D> | Object3D>) {
23-
this.selection.update((prev) => [...prev, ...objects]);
24-
}
25-
26-
unselect(...objects: Array<ElementRef<Object3D> | Object3D>) {
27-
this.selection.update((prev) => prev.filter((selected) => !objects.includes(selected)));
28-
}
18+
private source = signal<Array<ElementRef<Object3D> | Object3D>>([]);
19+
selected = this.source.asReadonly();
20+
update = this.source.update.bind(this.source);
2921
}
3022

3123
@Directive({ standalone: true, selector: 'ngt-group[ngtSelect], ngt-mesh[ngtSelect]' })
3224
export class NgtSelect {
3325
enabled = input(false, { transform: booleanAttribute, alias: 'ngtSelect' });
3426

35-
host = inject<ElementRef<Group | Mesh>>(ElementRef);
36-
3727
constructor() {
28+
const elementRef = inject<ElementRef<Group | Mesh>>(ElementRef);
3829
const selection = inject(NgtSelection);
3930
const autoEffect = injectAutoEffect();
4031

4132
afterNextRender(() => {
4233
autoEffect(
4334
() => {
44-
const host = this.host.nativeElement;
35+
const host = elementRef.nativeElement;
4536
if (!host) return;
4637

4738
const localState = getLocalState(host);
@@ -52,11 +43,11 @@ export class NgtSelect {
5243

5344
// ngt-mesh[ngtSelect]
5445
if (host.type === 'Mesh') {
55-
selection.select(host);
56-
return () => selection.unselect(host);
46+
selection.update((prev) => [...prev, host]);
47+
return () => selection.update((prev) => prev.filter((el) => el !== host));
5748
}
5849

59-
const [collection] = [untracked(selection.collection), localState.objects()];
50+
const [collection] = [untracked(selection.selected), localState.objects()];
6051
let changed = false;
6152
const current: Object3D[] = [];
6253
host.traverse((child) => {
@@ -66,9 +57,9 @@ export class NgtSelect {
6657

6758
if (!changed) return;
6859

69-
selection.select(...current);
60+
selection.update((prev) => [...prev, ...current]);
7061
return () => {
71-
selection.unselect(...current);
62+
selection.update((prev) => prev.filter((el) => !current.includes(el as Object3D)));
7263
};
7364
},
7465
{ allowSignalWrites: true },

libs/postprocessing/src/lib/effects/outline.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,17 @@ export class NgtpOutline {
149149
// NOTE: we run this effect if declarative NgtSelection is enabled
150150
const selectionEnabled = this.ngtSelection.enabled();
151151
if (!selectionEnabled) return;
152-
return this.handleSelectionChangeEffect(this.ngtSelection.collection, this.effect, this.invalidate);
152+
return this.handleSelectionChangeEffect(this.ngtSelection.selected, this.effect, this.invalidate);
153153
});
154154
});
155155
}
156156

157157
private handleSelectionChangeEffect(
158-
collection: () => Array<Object3D | ElementRef<Object3D>> | undefined,
158+
selected: () => Array<Object3D | ElementRef<Object3D>> | undefined,
159159
_effect: () => OutlineEffect,
160160
_invalidate: () => () => void,
161161
) {
162-
const selection = collection();
162+
const selection = selected();
163163
if (!selection || selection.length === 0) return;
164164

165165
const [effect, invalidate] = [_effect(), _invalidate()];

0 commit comments

Comments
 (0)