Skip to content

Commit c5badfd

Browse files
authored
Merge pull request #48 from matthieu-crouzet/fix/current-file-changes
fix: handle change on the input currentFile
2 parents 6ecb5ff + 5b08834 commit c5badfd

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

projects/ngx-monaco-tree-test/src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<input [(ngModel)]="dark" type="checkbox" />
55
<span class="slider round"></span>
66
</label>
7+
<button type="button" (click)="changeCurrentFile()">Change current file</button>
78
</div>
89
<monaco-tree
910
[theme]="dark ? 'vs-dark' : 'vs-light'"

projects/ngx-monaco-tree-test/src/app/app.component.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ input:checked + .slider:before {
6363
.slider.round:before {
6464
border-radius: 50%;
6565
}
66+
67+
.form > button {
68+
background-color: #2196f3;
69+
color: white;
70+
align-self: stretch;
71+
border: none;
72+
border-radius: 34px;
73+
padding: 0 1rem;
74+
cursor: pointer;
75+
}
76+
77+
.form > button:hover {
78+
background-color: #1a74be;
79+
}

projects/ngx-monaco-tree-test/src/app/app.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class AppComponent {
7575
},
7676
];
7777

78+
changeCurrentFile() {
79+
this.currentFile = this.currentFile === 'src/environments/environment.ts'
80+
? 'src/app/app.component.html'
81+
: 'src/environments/environment.ts';
82+
}
83+
7884
handleContextMenu(action: ContextMenuAction) {
7985
if (action[0] === 'new_directory') {
8086
const filename = window.prompt('name');

projects/ngx-monaco-tree/src/lib/monaco-tree-file/monaco-tree-file.component.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ElementRef, EventEmitter, HostListener, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
1+
import {Component, ElementRef, EventEmitter, HostListener, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
22
import { extensions } from '../../utils/extension-icon';
33
import { files } from '../../utils/file-icon';
44
import { folders } from '../../utils/folder-icon';
@@ -30,7 +30,7 @@ function getAbsolutePosition(element: any) {
3030
templateUrl: './monaco-tree-file.component.html',
3131
styleUrls: ['./monaco-tree-file.component.scss']
3232
})
33-
export class MonacoTreeFileComponent implements OnInit {
33+
export class MonacoTreeFileComponent implements OnChanges {
3434
@Input() name = '';
3535
@Input() path = '';
3636
@Input() color?: string|null|undefined = '';
@@ -51,11 +51,16 @@ export class MonacoTreeFileComponent implements OnInit {
5151
constructor(private eRef: ElementRef) {
5252
}
5353

54-
55-
ngOnInit(): void {
56-
if (!!this.current && this.current.startsWith(this.path) && !this.open) {
57-
this.toggle();
58-
}
54+
ngOnChanges(changes: SimpleChanges): void {
55+
if (
56+
changes['current']
57+
&& !!this.current
58+
&& this.current.startsWith(this.path)
59+
&& !this.open
60+
&& this.current !== this.path
61+
) {
62+
this.toggle(false);
63+
}
5964
}
6065

6166
contextMenu: Array<ContextMenuElementSeparator|ContextMenuElementText> = [
@@ -95,10 +100,10 @@ export class MonacoTreeFileComponent implements OnInit {
95100
return files[this.name as keyof typeof files];
96101

97102
} else {
98-
let splited = this.name.split('.')
99-
while(splited.length > 0) {
100-
splited = splited.slice(1)
101-
const ext = splited.join('.')
103+
let splitted = this.name.split('.')
104+
while(splitted.length > 0) {
105+
splitted = splitted.slice(1)
106+
const ext = splitted.join('.')
102107
if(ext && Object.keys(extensions).includes(ext)) {
103108
return extensions[ext as keyof typeof extensions];
104109
}
@@ -109,9 +114,11 @@ export class MonacoTreeFileComponent implements OnInit {
109114
}
110115
}
111116

112-
toggle() {
117+
toggle(shouldEmit = true) {
113118
this.open = !this.open;
114-
this.clickFile.emit(this.name)
119+
if (shouldEmit) {
120+
this.clickFile.emit(this.name)
121+
}
115122
}
116123

117124
get style() {

0 commit comments

Comments
 (0)