Newton-Editor/src/app/common/services/editor/files.service.ts

116 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Injectable, inject } from '@angular/core';
2025-06-20 05:50:43 +00:00
import { ReplaySubject, Observable } from 'rxjs';
2025-06-14 18:03:00 +00:00
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 {
2025-06-20 05:50:43 +00:00
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
private tabsService: TabsService = inject(TabsService);
2025-06-14 18:03:00 +00:00
files: Map<string, NewtonFile>;
constructor() {
2025-06-14 18:03:00 +00:00
this.files = new Map<string, NewtonFile>();
}
public get(path: string): NewtonFile {
2025-06-14 18:03:00 +00:00
return this.files.get(path);
}
public getAllPaths(): string[] {
return [...this.files.keys()];
}
public getAllFiles(): NewtonFile[] {
return [...this.files.values()];
}
public delete(file: NewtonFile) {
2025-06-14 18:03:00 +00:00
file.session.destroy();
window.fs.closeFile(file.path);
this.files.delete(file.path);
}
public set(file: NewtonFile) {
2025-06-14 18:03:00 +00:00
this.files.set(file.path, file);
}
public sendMessage(data: ServiceMessage): void {
2025-06-20 05:50:43 +00:00
this.messageSubject.next(data);
}
2025-06-14 18:03:00 +00:00
public getMessage$(): Observable<ServiceMessage> {
2025-06-20 05:50:43 +00:00
return this.messageSubject.asObservable();
}
2025-06-14 18:03:00 +00:00
public async loadFilesList(
files: Array<NewtonFile>
): Promise<NewtonFile | undefined | null> {
2025-06-14 18:03:00 +00:00
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 ];
}
public async addFile(
path: string,
file: NewtonFile,
loadFileContents: boolean = true,
data: string = ""
): Promise<void> {
2025-06-14 18:03:00 +00:00
try {
let pathParts = path.split("/");
file.fname = pathParts[ pathParts.length - 1 ];
file.path = path;
file.hash = btoa(file.path);
if (loadFileContents)
data = await window.fs.getFileContents(file.path);
file.session = new EditSession(data);
2025-06-14 18:03:00 +00:00
file.session.setMode(
getModeForPath( file.path ).mode
);
this.files.set(file.path, file);
} catch (error) {
console.log(
`---- Error ----\nPath: ${path}\nMessage: ${error}`
);
}
}
public async addTab(file: NewtonFile) {
let message = new ServiceMessage();
message.action = "create-tab";
message.fileName = file.fname;
message.fileUUID = file.hash;
message.filePath = file.path;
2025-06-14 18:03:00 +00:00
this.tabsService.sendMessage(message);
2025-06-14 18:03:00 +00:00
}
}