Moved files mapping to own service
This commit is contained in:
parent
805b997483
commit
c6b2632487
90
src/app/common/services/editor/files.service.ts
Normal file
90
src/app/common/services/editor/files.service.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
import { EditSession } from 'ace-builds';
|
||||
import { getModeForPath } from 'ace-builds/src-noconflict/ext-modelist';
|
||||
|
||||
import { TabsService } from './tabs/tabs.service';
|
||||
|
||||
import { NewtonFile } from '../../types/file.type';
|
||||
import { ServiceMessage } from '../../types/service-message.type';
|
||||
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class FilesService {
|
||||
files: Map<string, NewtonFile>;
|
||||
|
||||
|
||||
constructor(
|
||||
private tabsService: TabsService,
|
||||
) {
|
||||
this.files = new Map<string, NewtonFile>();
|
||||
}
|
||||
|
||||
|
||||
get(path: string): NewtonFile {
|
||||
return this.files.get(path);
|
||||
}
|
||||
|
||||
delete(file: NewtonFile) {
|
||||
file.session.destroy();
|
||||
window.fs.closeFile(file.path);
|
||||
this.files.delete(file.path);
|
||||
}
|
||||
|
||||
set(file: NewtonFile) {
|
||||
this.files.set(file.path, file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
async loadFilesList(files: Array<NewtonFile>): Promise<NewtonFile | undefined | null> {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const path = window.fs.getPathForFile(file);
|
||||
|
||||
if (!file || !path) continue;
|
||||
if ( this.files.get(path) ) continue;
|
||||
|
||||
await this.addFile(path, file);
|
||||
this.addTab(file);
|
||||
}
|
||||
|
||||
return files[ files.length - 1 ];
|
||||
}
|
||||
|
||||
async addFile(path: string, file: NewtonFile): Promise<void> {
|
||||
try {
|
||||
let pathParts = path.split("/");
|
||||
file.fname = pathParts[ pathParts.length - 1 ];
|
||||
file.path = path;
|
||||
file.hash = btoa(file.path);
|
||||
let data = await window.fs.getFileContents(file.path);
|
||||
file.session = new EditSession(data);
|
||||
|
||||
file.session.setMode(
|
||||
getModeForPath( file.path ).mode
|
||||
);
|
||||
|
||||
this.files.set(file.path, file);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`---- Error ----\nPath: ${path}\nMessage: ${error}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async addTab(file: NewtonFile) {
|
||||
let message = new ServiceMessage();
|
||||
message.action = "create-tab";
|
||||
message.message = file.fname;
|
||||
message.uuid = file.hash;
|
||||
message.data = file.path;
|
||||
|
||||
this.tabsService.setData(message);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
import { Component, ElementRef, ViewChild, TemplateRef, ComponentRef, ViewContainerRef } from '@angular/core';
|
||||
import { Subject, takeUntil } from 'rxjs';
|
||||
|
||||
import { EditSession } from 'ace-builds';
|
||||
import { getModeForPath } from 'ace-builds/src-noconflict/ext-modelist';
|
||||
|
||||
import { NewtonEditorComponent } from "./newton-editor/newton-editor.component";
|
||||
import { EditorsService } from '../common/services/editor/editors.service';
|
||||
import { TabsService } from '../common/services/editor/tabs/tabs.service';
|
||||
import { FilesService } from '../common/services/editor/files.service';
|
||||
|
||||
import { DndDirective } from '../common/directives/dnd.directive';
|
||||
import { NewtonFile } from '../common/types/file.type';
|
||||
@ -33,18 +30,15 @@ export class EditorsComponent {
|
||||
@ViewChild('containerRef', {read: ViewContainerRef}) containerRef!: ViewContainerRef;
|
||||
editors: Map<string, ComponentRef<NewtonEditorComponent>>;
|
||||
editorSettings: typeof EditorSettings;
|
||||
files: Map<string, NewtonFile>;
|
||||
activeEditor!: string;
|
||||
lspConfigData!: any;
|
||||
|
||||
|
||||
constructor(
|
||||
private editorsService: EditorsService,
|
||||
private tabsService: TabsService
|
||||
private filesService: FilesService
|
||||
) {
|
||||
this.editorSettings = EditorSettings;
|
||||
this.editors = new Map<string, ComponentRef<NewtonEditorComponent>>();
|
||||
this.files = new Map<string, NewtonFile>();
|
||||
}
|
||||
|
||||
|
||||
@ -122,7 +116,7 @@ export class EditorsComponent {
|
||||
this.editorsService.loadTabToEditor$().pipe(
|
||||
takeUntil(this.unsubscribe)
|
||||
).subscribe((path: string) => {
|
||||
let file = this.files.get(path);
|
||||
let file = this.filesService.get(path);
|
||||
let editorComponent = this.getActiveEditorComponent();
|
||||
let editor = editorComponent.editor;
|
||||
|
||||
@ -133,7 +127,7 @@ export class EditorsComponent {
|
||||
this.editorsService.closeTabRequested$().pipe(
|
||||
takeUntil(this.unsubscribe)
|
||||
).subscribe((path: string) => {
|
||||
let file = this.files.get(path);
|
||||
let file = this.filesService.get(path);
|
||||
let editors = [...this.editors.values()];
|
||||
|
||||
for (let i = 0; i < editors.length; i++) {
|
||||
@ -143,9 +137,7 @@ export class EditorsComponent {
|
||||
}
|
||||
}
|
||||
|
||||
file.session.destroy();
|
||||
this.files.delete(path);
|
||||
window.fs.closeFile(path);
|
||||
this.filesService.delete(file);
|
||||
});
|
||||
|
||||
}
|
||||
@ -155,11 +147,12 @@ export class EditorsComponent {
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
let file = new File([], "") as NewtonFile;
|
||||
|
||||
await this.addFile(paths[i], file);
|
||||
this.addTab(file);
|
||||
await this.filesService.addFile(paths[i], file);
|
||||
this.filesService.addTab(file);
|
||||
}
|
||||
|
||||
let file = this.files.get(paths[ paths.length - 1 ]);
|
||||
let path = paths[ paths.length - 1 ];
|
||||
let file = this.filesService.get(path);
|
||||
this.setSession(file);
|
||||
});
|
||||
|
||||
@ -218,7 +211,7 @@ export class EditorsComponent {
|
||||
}
|
||||
|
||||
protected onFileDropped(files: any) {
|
||||
this.loadFilesList(files).then((file: NewtonFile | undefined | null) => {
|
||||
this.filesService.loadFilesList(files).then((file: NewtonFile | undefined | null) => {
|
||||
this.setSession(file);
|
||||
});
|
||||
}
|
||||
@ -249,50 +242,4 @@ export class EditorsComponent {
|
||||
return editor;
|
||||
}
|
||||
|
||||
private async loadFilesList(files: Array<NewtonFile>): Promise<NewtonFile | undefined | null> {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const path = window.fs.getPathForFile(file);
|
||||
|
||||
if (!file || !path) continue;
|
||||
if ( this.files.get(path) ) continue;
|
||||
|
||||
await this.addFile(path, file);
|
||||
this.addTab(file);
|
||||
}
|
||||
|
||||
return files[ files.length - 1 ];
|
||||
}
|
||||
|
||||
private async addFile(path: string, file: NewtonFile): Promise<void> {
|
||||
try {
|
||||
let pathParts = path.split("/");
|
||||
file.fname = pathParts[ pathParts.length - 1 ];
|
||||
file.path = path;
|
||||
file.hash = btoa(file.path);
|
||||
let data = await window.fs.getFileContents(file.path);
|
||||
file.session = new EditSession(data);
|
||||
|
||||
file.session.setMode(
|
||||
getModeForPath( file.path ).mode
|
||||
);
|
||||
|
||||
this.files.set(file.path, file);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`---- Error ----\nPath: ${path}\nMessage: ${error}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async addTab(file: NewtonFile) {
|
||||
let message = new ServiceMessage();
|
||||
message.action = "create-tab";
|
||||
message.message = file.fname;
|
||||
message.uuid = file.hash;
|
||||
message.data = file.path;
|
||||
|
||||
this.tabsService.setData(message);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user