Skip to content

Commit 0d9c9c4

Browse files
Allow managing files for an exercise
1 parent 1e38639 commit 0d9c9c4

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
<mat-form-field *ngIf="files.length > 1">
2-
<mat-label>Selected File</mat-label>
3-
<mat-select [(ngModel)]="selectedFiles" multiple (ngModelChange)="update()">
4-
<mat-option *ngFor="let file of files" [value]="file">
5-
{{ file }}
6-
</mat-option>
7-
</mat-select>
8-
</mat-form-field>
1+
<div>
2+
<div *ngFor="let file of selectedFiles; let i = index">
3+
<mat-checkbox [(ngModel)]="file.selected" (ngModelChange)="update()">
4+
<input
5+
[ngModel]="file.name"
6+
(ngModelChange)="updateFileName(i, file.name, $event)"
7+
/>
8+
</mat-checkbox>
9+
<button mat-icon-button (click)="deleteFile(file.name)">
10+
<mat-icon>cancel</mat-icon>
11+
</button>
12+
</div>
13+
<button mat-button [disabled]="code[defaultNewFileName]" (click)="addFile()">
14+
Add file
15+
</button>
16+
</div>
917

1018
<code-demo
1119
[allowSwitchingFiles]="false"
1220
[(ngModel)]="code"
1321
(ngModelChange)="update()"
14-
[files]="selectedFiles"
22+
[files]="openFiles"
1523
[ui]="ui"
1624
[bootstrap]="false"
1725
></code-demo>

apps/codelab/src/app/admin/content/presentation-editor/wrappers/custom-component-editors/codelab-code-demo-console/codelab-code-demo-console.component.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { Component, Input } from '@angular/core';
22
import { ContentService } from '../../../services/content.service';
33
import { ContentSlide, CustomBlock } from '../../../types';
44

5+
interface SelectableFiles {
6+
selected: boolean;
7+
name: string;
8+
}
9+
510
@Component({
611
selector: 'codelab-code-demo-console-editor',
712
templateUrl: './codelab-code-demo-console.component.html',
@@ -14,14 +19,27 @@ export class CodelabCodeDemoConsoleComponent {
1419
@Input() block!: CustomBlock;
1520
@Input() slide!: ContentSlide;
1621
@Input() presentationId!: string;
17-
@Input() selectedFiles = [];
22+
@Input() selectedFiles: SelectableFiles[] = [];
23+
readonly defaultNewFileName = 'new.ts';
24+
25+
openFiles: string[] = [];
1826

1927
constructor(private readonly contentService: ContentService) {}
2028

2129
update() {
2230
this.files = Object.keys(this.code);
2331
this.selectedFiles =
24-
this.selectedFiles.length === 0 ? [this.files[0]] : this.selectedFiles;
32+
this.selectedFiles.length === 0
33+
? this.files.map((name, i) => ({
34+
name,
35+
selected: i === 0
36+
}))
37+
: this.selectedFiles;
38+
39+
this.openFiles = this.selectedFiles
40+
.filter(file => file.selected)
41+
.map(({ name }) => name);
42+
2543
this.contentService.updateBlock(this.presentationId, this.slide.id, {
2644
...this.block,
2745
props: {
@@ -30,4 +48,29 @@ export class CodelabCodeDemoConsoleComponent {
3048
}
3149
});
3250
}
51+
52+
updateFileName(index: number, oldName: string, newName: string) {
53+
if (!this.code[newName]) {
54+
this.selectedFiles[index].name = newName;
55+
console.assert(!!this.code[oldName]);
56+
this.code[newName] = this.code[oldName];
57+
delete this.code[oldName];
58+
}
59+
this.update();
60+
}
61+
62+
addFile() {
63+
if (!this.code[this.defaultNewFileName]) {
64+
this.selectedFiles.push({
65+
name: this.defaultNewFileName,
66+
selected: false
67+
});
68+
this.code[this.defaultNewFileName] = '// code';
69+
}
70+
}
71+
72+
deleteFile(name: string) {
73+
delete this.code[name];
74+
this.selectedFiles = this.selectedFiles.filter(file => file.name !== name);
75+
}
3376
}

apps/codelab/src/app/admin/content/presentation-editor/wrappers/custom-component-editors/custom-component-editors.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { AngularFireStorageModule } from '@angular/fire/storage';
1717
import { MatSelectModule } from '@angular/material/select';
1818
import { CodelabExerciseEditorComponent } from './codelab-exercise-preview-editor/codelab-exercise-editor.component';
1919
import { CodelabComponentsModule } from '../../../../../components/codelab-components.module';
20+
import { MatButtonModule } from '@angular/material/button';
2021

2122
@NgModule({
2223
declarations: [
@@ -38,7 +39,8 @@ import { CodelabComponentsModule } from '../../../../../components/codelab-compo
3839
MatIconModule,
3940
NgxFileDropModule,
4041
MatSelectModule,
41-
AngularFireStorageModule
42+
AngularFireStorageModule,
43+
MatButtonModule
4244
]
4345
})
4446
export class CustomComponentEditorsModule {}

0 commit comments

Comments
 (0)