Skip to content

Commit ea8a898

Browse files
authored
Merge pull request #53 from matthieu-crouzet/feat/scroll-to-current-file
feat: scroll current file into the view
2 parents c5badfd + bdd980f commit ea8a898

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {CommonModule} from "@angular/common";
55
import {NgxMonacoTreeComponent} from "../../../ngx-monaco-tree/src/lib/ngx-monaco-tree.component";
66
import {DragAndDropEvent} from "../../../ngx-monaco-tree/src/lib/monaco-tree-file/monaco-tree-file.type";
77

8+
const TOO_MANY_FILES_IN_FOLDER = 200;
9+
810
@Component({
911
selector: 'app-root',
1012
standalone: true,
@@ -44,6 +46,10 @@ export class AppComponent {
4446
{ name: 'environment.ts' },
4547
],
4648
},
49+
{
50+
name: 'folder-with-too-many-files',
51+
content: Array.from({ length: TOO_MANY_FILES_IN_FOLDER }).map((_, index) => ({ name: `file_${index + 1}.ts`}))
52+
},
4753
{
4854
name: 'favicon.ico',
4955
},
@@ -76,9 +82,9 @@ export class AppComponent {
7682
];
7783

7884
changeCurrentFile() {
79-
this.currentFile = this.currentFile === 'src/environments/environment.ts'
85+
this.currentFile = this.currentFile === `src/folder-with-too-many-files/file_${TOO_MANY_FILES_IN_FOLDER}.ts`
8086
? 'src/app/app.component.html'
81-
: 'src/environments/environment.ts';
87+
: `src/folder-with-too-many-files/file_${TOO_MANY_FILES_IN_FOLDER}.ts`;
8288
}
8389

8490
handleContextMenu(action: ContextMenuAction) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {MonacoTreeContextMenuComponent} from "../monaco-tree-context-menu/monaco
1212
import {NgForOf, NgIf, NgStyle} from "@angular/common";
1313
import {DragDropModule, CdkDragDrop, CdkDrag, CdkDropList} from '@angular/cdk/drag-drop';
1414

15-
1615
function getAbsolutePosition(element: any) {
1716
const r = { x: element.offsetLeft, y: element.offsetTop };
1817
if (element.offsetParent) {
@@ -56,10 +55,18 @@ export class MonacoTreeFileComponent implements OnChanges {
5655
changes['current']
5756
&& !!this.current
5857
&& this.current.startsWith(this.path)
59-
&& !this.open
60-
&& this.current !== this.path
6158
) {
62-
this.toggle(false);
59+
if (!this.open && this.current !== this.path) {
60+
this.toggle(false);
61+
}
62+
if (this.current === this.path) {
63+
// Needed as `scrollIntoViewIfNeeded` is not supported on Firefox
64+
if (this.eRef.nativeElement.scrollIntoViewIfNeeded) {
65+
this.eRef.nativeElement.scrollIntoViewIfNeeded();
66+
} else {
67+
this.eRef.nativeElement.scrollIntoView();
68+
}
69+
}
6370
}
6471
}
6572

0 commit comments

Comments
 (0)