Skip to content

Commit 90165b1

Browse files
dgp1130AndrewKushnir
authored andcommitted
refactor(devtools): refactors property view drag-and-drop behavior (angular#60286)
The main goal of this change is to remove `categoryOrder` which effectively hard-codes the supported length of `panels`. Adding another item to `panels` is not rendered unless that is added to `categoryOrder`. My solution to this is to make the set of categories a signal, with each category able to produce the data inside it. This allow `CdkDragDrop` to rearrange categories but then still produce the correct data in the template without needing a separate array to track order. Also removed `hidden` and inlined it in the template, since the logic was the same for every panel. `moveItemInArray` is unfortunately an in-place move, so I needed to manually clone the array to ensure `panels` observes an immutable update which works better with signals and change detection. PR Close angular#60286
1 parent de2bfc0 commit 90165b1

2 files changed

Lines changed: 24 additions & 37 deletions

File tree

devtools/projects/ng-devtools/src/lib/devtools-tabs/directive-explorer/property-tab/property-view/property-view-body.component.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
</mat-expansion-panel>
2323
</div>
2424
}
25-
@for (index of categoryOrder; track $index) {
25+
@for (panel of panels(); track $index) {
2626
<div class="mat-accordion-content" cdkDrag>
27-
@let panel = panels()[index];
28-
@if (!panel.hidden) {
27+
@if (panel.controls().dataSource.data.length > 0) {
2928
<mat-expansion-panel [class]="panel.class" [expanded]="true">
3029
<mat-expansion-panel-header collapsedHeight="25px" expandedHeight="25px">
3130
<mat-panel-title>{{ panel.title }}</mat-panel-title>
3231
</mat-expansion-panel-header>
3332
<ng-property-view-tree
34-
[dataSource]="panel.controls.dataSource"
35-
[treeControl]="panel.controls.treeControl"
33+
[dataSource]="panel.controls().dataSource"
34+
[treeControl]="panel.controls().treeControl"
3635
(updateValue)="updateValue($event)"
3736
(inspect)="handleInspect($event)"
3837
/>

devtools/projects/ng-devtools/src/lib/devtools-tabs/directive-explorer/property-tab/property-view/property-view-body.component.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {CdkDragDrop, moveItemInArray, CdkDropList, CdkDrag} from '@angular/cdk/drag-drop';
10-
import {Component, computed, forwardRef, input, output} from '@angular/core';
10+
import {Component, computed, forwardRef, input, output, signal} from '@angular/core';
1111
import {DirectivePosition, SerializedInjectedService} from 'protocol';
1212

1313
import {
@@ -44,37 +44,23 @@ export class PropertyViewBodyComponent {
4444

4545
readonly inspect = output<{node: FlatNode; directivePosition: DirectivePosition}>();
4646

47-
categoryOrder = [0, 1, 2];
48-
49-
readonly panels = computed<
47+
protected readonly panels = signal([
5048
{
51-
title: string;
52-
hidden: boolean;
53-
controls: DirectiveTreeData;
54-
class: string;
55-
}[]
56-
>(() => {
57-
return [
58-
{
59-
title: 'Inputs',
60-
hidden: this.directiveInputControls().dataSource.data.length === 0,
61-
controls: this.directiveInputControls(),
62-
class: 'cy-inputs',
63-
},
64-
{
65-
title: 'Outputs',
66-
hidden: this.directiveOutputControls().dataSource.data.length === 0,
67-
controls: this.directiveOutputControls(),
68-
class: 'cy-outputs',
69-
},
70-
{
71-
title: 'Properties',
72-
hidden: this.directiveStateControls().dataSource.data.length === 0,
73-
controls: this.directiveStateControls(),
74-
class: 'cy-properties',
75-
},
76-
];
77-
});
49+
title: 'Inputs',
50+
controls: () => this.directiveInputControls(),
51+
class: 'cy-inputs',
52+
},
53+
{
54+
title: 'Outputs',
55+
controls: () => this.directiveOutputControls(),
56+
class: 'cy-outputs',
57+
},
58+
{
59+
title: 'Properties',
60+
controls: () => this.directiveStateControls(),
61+
class: 'cy-properties',
62+
},
63+
]);
7864

7965
readonly controlsLoaded = computed(() => {
8066
return (
@@ -89,7 +75,9 @@ export class PropertyViewBodyComponent {
8975
}
9076

9177
drop(event: CdkDragDrop<any, any>): void {
92-
moveItemInArray(this.categoryOrder, event.previousIndex, event.currentIndex);
78+
const panels = this.panels();
79+
moveItemInArray(panels, event.previousIndex, event.currentIndex);
80+
this.panels.set(Array.from(panels)); // Clone array for immutable update.
9381
}
9482

9583
handleInspect(node: FlatNode): void {

0 commit comments

Comments
 (0)