Skip to content

Commit

Permalink
feat: scroll current file into the view
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-crouzet committed Dec 9, 2024
1 parent c5badfd commit bdd980f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 8 additions & 2 deletions projects/ngx-monaco-tree-test/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {CommonModule} from "@angular/common";
import {NgxMonacoTreeComponent} from "../../../ngx-monaco-tree/src/lib/ngx-monaco-tree.component";
import {DragAndDropEvent} from "../../../ngx-monaco-tree/src/lib/monaco-tree-file/monaco-tree-file.type";

const TOO_MANY_FILES_IN_FOLDER = 200;

@Component({
selector: 'app-root',
standalone: true,
Expand Down Expand Up @@ -44,6 +46,10 @@ export class AppComponent {
{ name: 'environment.ts' },
],
},
{
name: 'folder-with-too-many-files',
content: Array.from({ length: TOO_MANY_FILES_IN_FOLDER }).map((_, index) => ({ name: `file_${index + 1}.ts`}))
},
{
name: 'favicon.ico',
},
Expand Down Expand Up @@ -76,9 +82,9 @@ export class AppComponent {
];

changeCurrentFile() {
this.currentFile = this.currentFile === 'src/environments/environment.ts'
this.currentFile = this.currentFile === `src/folder-with-too-many-files/file_${TOO_MANY_FILES_IN_FOLDER}.ts`
? 'src/app/app.component.html'
: 'src/environments/environment.ts';
: `src/folder-with-too-many-files/file_${TOO_MANY_FILES_IN_FOLDER}.ts`;
}

handleContextMenu(action: ContextMenuAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {MonacoTreeContextMenuComponent} from "../monaco-tree-context-menu/monaco
import {NgForOf, NgIf, NgStyle} from "@angular/common";
import {DragDropModule, CdkDragDrop, CdkDrag, CdkDropList} from '@angular/cdk/drag-drop';


function getAbsolutePosition(element: any) {
const r = { x: element.offsetLeft, y: element.offsetTop };
if (element.offsetParent) {
Expand Down Expand Up @@ -56,10 +55,18 @@ export class MonacoTreeFileComponent implements OnChanges {
changes['current']
&& !!this.current
&& this.current.startsWith(this.path)
&& !this.open
&& this.current !== this.path
) {
this.toggle(false);
if (!this.open && this.current !== this.path) {
this.toggle(false);
}
if (this.current === this.path) {
// Needed as `scrollIntoViewIfNeeded` is not supported on Firefox
if (this.eRef.nativeElement.scrollIntoViewIfNeeded) {
this.eRef.nativeElement.scrollIntoViewIfNeeded();
} else {
this.eRef.nativeElement.scrollIntoView();
}
}
}
}

Expand Down

0 comments on commit bdd980f

Please sign in to comment.