Skip to content

Commit 2d1e81a

Browse files
committed
Adds expand/collapse all buttons to Composer
1 parent 5632ca1 commit 2d1e81a

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/webviews/apps/plus/composer/components/details-panel.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { css, html, LitElement, nothing } from 'lit';
2-
import { customElement, property, query } from 'lit/decorators.js';
2+
import { customElement, property, query, state } from 'lit/decorators.js';
33
import { repeat } from 'lit/directives/repeat.js';
44
import Sortable from 'sortablejs';
55
import type { ComposerCommit, ComposerHunk } from '../../../../plus/composer/protocol';
@@ -67,6 +67,18 @@ export class DetailsPanel extends LitElement {
6767
.files-headline {
6868
font-size: 1.4rem;
6969
margin-block: 0 0.8rem;
70+
display: flex;
71+
align-items: center;
72+
justify-content: space-between;
73+
}
74+
75+
.files-headline__title {
76+
margin: 0;
77+
}
78+
79+
.files-headline__actions {
80+
display: flex;
81+
gap: 0.4rem;
7082
}
7183
7284
.files-list {
@@ -207,7 +219,7 @@ export class DetailsPanel extends LitElement {
207219
@property({ type: Boolean })
208220
filesChangedExpanded = true;
209221

210-
@property({ type: Set })
222+
@property({ type: Object })
211223
selectedHunkIds: Set<string> = new Set();
212224

213225
@property({ type: String })
@@ -231,6 +243,9 @@ export class DetailsPanel extends LitElement {
231243
@property({ type: Boolean })
232244
hasChanges: boolean = true;
233245

246+
@state()
247+
private defaultFilesExpanded: boolean = true;
248+
234249
private hunksSortables: Sortable[] = [];
235250
private isDraggingHunks = false;
236251
private draggedHunkIds: string[] = [];
@@ -507,6 +522,30 @@ export class DetailsPanel extends LitElement {
507522
);
508523
}
509524

525+
private handleCollapseAllFiles() {
526+
this.defaultFilesExpanded = false;
527+
}
528+
529+
private handleExpandAllFiles() {
530+
this.defaultFilesExpanded = true;
531+
}
532+
533+
private renderFilesChangedHeader(fileCount: number | string) {
534+
return html`
535+
<div class="files-headline">
536+
<h3 class="files-headline__title">Files Changed (${fileCount})</h3>
537+
<div class="files-headline__actions">
538+
<gl-button appearance="toolbar" @click=${this.handleExpandAllFiles} tooltip="Expand All">
539+
<code-icon icon="expand-all"></code-icon>
540+
</gl-button>
541+
<gl-button appearance="toolbar" @click=${this.handleCollapseAllFiles} tooltip="Collapse All">
542+
<code-icon icon="collapse-all"></code-icon>
543+
</gl-button>
544+
</div>
545+
</div>
546+
`;
547+
}
548+
510549
private renderFileHierarchy(hunks: ComposerHunk[]) {
511550
const fileGroups = groupHunksByFile(hunks);
512551

@@ -521,7 +560,11 @@ export class DetailsPanel extends LitElement {
521560
private renderFile(fileName: string, fileHunks: ComposerHunk[]) {
522561
// const fileChanges = getFileChanges(fileHunks);
523562

524-
return html`<gl-diff-file .filename=${fileName} .hunks=${fileHunks}></gl-diff-file>`;
563+
return html`<gl-diff-file
564+
.filename=${fileName}
565+
.hunks=${fileHunks}
566+
.defaultExpanded=${this.defaultFilesExpanded}
567+
></gl-diff-file>`;
525568
}
526569

527570
// private renderFileOld(fileName: string, fileHunks: ComposerHunk[]) {
@@ -602,7 +645,7 @@ export class DetailsPanel extends LitElement {
602645
<gl-commit-message .message=${this.getSectionTitle(this.selectedUnassignedSection)}></gl-commit-message>
603646
604647
<section>
605-
<h3 class="files-headline">Files Changed (${getUniqueFileNames(hunks).length})</h3>
648+
${this.renderFilesChangedHeader(getUniqueFileNames(hunks).length)}
606649
<div class="files-list" data-source="${this.selectedUnassignedSection}">
607650
${this.renderFileHierarchy(hunks)}
608651
</div>
@@ -628,7 +671,7 @@ export class DetailsPanel extends LitElement {
628671
></gl-commit-message>
629672
630673
<section>
631-
<h3 class="files-headline">Files Changed (${getFileCountForCommit(commit, this.hunks)})</h3>
674+
${this.renderFilesChangedHeader(getFileCountForCommit(commit, this.hunks))}
632675
<div class="files-list" data-commit-id=${commit.id}>${this.renderFileHierarchy(commitHunks)}</div>
633676
</section>
634677
</article>

src/webviews/apps/plus/composer/components/diff/diff-file.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export class GlDiffFile extends LitElement {
4141
@property({ type: Boolean, attribute: 'side-by-side' })
4242
sideBySide = false;
4343

44+
@property({ type: Boolean, attribute: 'default-expanded' })
45+
defaultExpanded = true;
46+
4447
@query('#diff')
4548
targetElement!: HTMLDivElement;
4649

@@ -64,10 +67,11 @@ export class GlDiffFile extends LitElement {
6467

6568
override updated(changedProperties: Map<string | number | symbol, unknown>) {
6669
super.updated(changedProperties);
70+
6771
if (changedProperties.has('filename') || changedProperties.has('hunks')) {
6872
this.processDiff();
6973
}
70-
if (changedProperties.has('diffText')) {
74+
if (changedProperties.has('diffText') || changedProperties.has('defaultExpanded')) {
7175
this.renderDiff();
7276
}
7377
}
@@ -77,7 +81,7 @@ export class GlDiffFile extends LitElement {
7781
}
7882

7983
private renderDiff() {
80-
if (!this.parsedDiff) {
84+
if (!this.parsedDiff || !this.filename) {
8185
this.targetElement.innerHTML = '';
8286
return;
8387
}
@@ -95,6 +99,11 @@ export class GlDiffFile extends LitElement {
9599
this.diff2htmlUi = new Diff2HtmlUI(this.targetElement, this.parsedDiff, config);
96100
this.diff2htmlUi.draw();
97101
// this.diff2htmlUi.highlightCode();
102+
103+
const detailsElement = this.targetElement?.querySelector('details');
104+
if (detailsElement) {
105+
detailsElement.open = this.defaultExpanded;
106+
}
98107
}
99108

100109
private processDiff() {

0 commit comments

Comments
 (0)