2025-05-28 02:10:45 +00:00
|
|
|
import { Component, ElementRef, ViewChild, TemplateRef, ComponentRef, ViewContainerRef } from '@angular/core';
|
|
|
|
import { Subject, takeUntil } from 'rxjs';
|
|
|
|
|
2025-06-13 01:31:08 +00:00
|
|
|
import { NewtonEditorComponent } from "./newton-editor/newton-editor.component";
|
2025-05-28 02:10:45 +00:00
|
|
|
import { EditorsService } from '../common/services/editor/editors.service';
|
2025-06-14 18:03:00 +00:00
|
|
|
import { FilesService } from '../common/services/editor/files.service';
|
2025-05-28 02:10:45 +00:00
|
|
|
|
|
|
|
import { DndDirective } from '../common/directives/dnd.directive';
|
|
|
|
import { NewtonFile } from '../common/types/file.type';
|
|
|
|
import { ServiceMessage } from '../common/types/service-message.type';
|
|
|
|
import { EditorSettings } from "../common/configs/editor.config";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'editors',
|
|
|
|
standalone: true,
|
|
|
|
imports: [
|
|
|
|
DndDirective
|
|
|
|
],
|
|
|
|
templateUrl: './editors.component.html',
|
|
|
|
styleUrl: './editors.component.css',
|
|
|
|
host: {
|
|
|
|
'class': 'row'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export class EditorsComponent {
|
|
|
|
private unsubscribe = new Subject<void>();
|
|
|
|
|
|
|
|
@ViewChild('containerRef', {read: ViewContainerRef}) containerRef!: ViewContainerRef;
|
2025-06-13 01:31:08 +00:00
|
|
|
editors: Map<string, ComponentRef<NewtonEditorComponent>>;
|
2025-05-28 02:10:45 +00:00
|
|
|
editorSettings: typeof EditorSettings;
|
|
|
|
activeEditor!: string;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private editorsService: EditorsService,
|
2025-06-14 18:03:00 +00:00
|
|
|
private filesService: FilesService
|
2025-05-28 02:10:45 +00:00
|
|
|
) {
|
|
|
|
this.editorSettings = EditorSettings;
|
2025-06-13 01:31:08 +00:00
|
|
|
this.editors = new Map<string, ComponentRef<NewtonEditorComponent>>();
|
2025-05-28 02:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ngAfterViewInit(): void {
|
2025-05-30 05:30:54 +00:00
|
|
|
this.loadSubscribers();
|
2025-06-01 05:49:30 +00:00
|
|
|
this.loadMainSubscribers();
|
2025-05-30 05:30:54 +00:00
|
|
|
|
2025-06-14 03:36:01 +00:00
|
|
|
let leftEditor = this.createEditor();
|
|
|
|
let rightEditor = this.createEditor();
|
2025-06-12 04:02:45 +00:00
|
|
|
|
2025-06-14 03:36:01 +00:00
|
|
|
this.activeEditor = leftEditor.instance.uuid;
|
|
|
|
leftEditor.instance.isDefault = true;
|
|
|
|
leftEditor.instance.rightSiblingUUID = rightEditor.instance.uuid;
|
|
|
|
rightEditor.instance.leftSiblingUUID = leftEditor.instance.uuid;
|
2025-05-30 05:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadSubscribers() {
|
2025-06-14 03:36:01 +00:00
|
|
|
|
|
|
|
this.editorsService.selectSessionLeftRequested$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((uuid: string) => {
|
|
|
|
let editorComponent = this.editors.get(uuid).instance;
|
|
|
|
if (!editorComponent.leftSiblingUUID) return;
|
|
|
|
let siblingComponent = this.editors.get(editorComponent.leftSiblingUUID).instance;
|
|
|
|
siblingComponent.editor.focus()
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editorsService.selectSessionRightRequested$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((uuid: string) => {
|
|
|
|
let editorComponent = this.editors.get(uuid).instance;
|
|
|
|
if (!editorComponent.rightSiblingUUID) return;
|
|
|
|
let siblingComponent = this.editors.get(editorComponent.rightSiblingUUID).instance;
|
|
|
|
siblingComponent.editor.focus()
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editorsService.moveSessionLeftRequested$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((uuid: string) => {
|
|
|
|
let editorComponent = this.editors.get(uuid).instance;
|
|
|
|
if (!editorComponent.leftSiblingUUID) return;
|
|
|
|
let siblingComponent = this.editors.get(editorComponent.leftSiblingUUID).instance;
|
|
|
|
let session = editorComponent.editor.getSession();
|
|
|
|
let siblingSession = siblingComponent.editor.getSession();
|
|
|
|
|
|
|
|
if (session == siblingSession) return;
|
|
|
|
siblingComponent.editor.setSession(session);
|
|
|
|
editorComponent.newBuffer();
|
|
|
|
siblingComponent.editor.focus()
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editorsService.moveSessionRightRequested$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((uuid: string) => {
|
|
|
|
let editorComponent = this.editors.get(uuid).instance;
|
|
|
|
if (!editorComponent.rightSiblingUUID) return;
|
|
|
|
let siblingComponent = this.editors.get(editorComponent.rightSiblingUUID).instance;
|
|
|
|
let session = editorComponent.editor.getSession();
|
|
|
|
let siblingSession = siblingComponent.editor.getSession();
|
|
|
|
|
|
|
|
if (session == siblingSession) return;
|
|
|
|
siblingComponent.editor.setSession(session);
|
|
|
|
editorComponent.newBuffer();
|
|
|
|
siblingComponent.editor.focus()
|
|
|
|
});
|
|
|
|
|
2025-05-30 05:30:54 +00:00
|
|
|
this.editorsService.newActiveEditor$().pipe(
|
2025-05-28 02:10:45 +00:00
|
|
|
takeUntil(this.unsubscribe)
|
2025-05-30 05:30:54 +00:00
|
|
|
).subscribe((uuid: string) => {
|
2025-06-12 04:02:45 +00:00
|
|
|
this.editors.get(this.activeEditor).instance.removeActiveStyling();
|
2025-05-30 05:30:54 +00:00
|
|
|
this.activeEditor = uuid;
|
2025-06-12 04:02:45 +00:00
|
|
|
this.editors.get(this.activeEditor).instance.addActiveStyling();
|
2025-05-28 02:10:45 +00:00
|
|
|
});
|
2025-06-02 05:10:56 +00:00
|
|
|
|
|
|
|
this.editorsService.loadTabToEditor$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((path: string) => {
|
2025-06-14 18:03:00 +00:00
|
|
|
let file = this.filesService.get(path);
|
2025-06-02 05:10:56 +00:00
|
|
|
let editorComponent = this.getActiveEditorComponent();
|
|
|
|
let editor = editorComponent.editor;
|
|
|
|
|
|
|
|
editorComponent.activeFile = file;
|
|
|
|
editor.setSession(file.session);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editorsService.closeTabRequested$().pipe(
|
|
|
|
takeUntil(this.unsubscribe)
|
|
|
|
).subscribe((path: string) => {
|
2025-06-14 18:03:00 +00:00
|
|
|
let file = this.filesService.get(path);
|
2025-06-02 05:10:56 +00:00
|
|
|
let editors = [...this.editors.values()];
|
|
|
|
|
|
|
|
for (let i = 0; i < editors.length; i++) {
|
|
|
|
let editorComponent = editors[i].instance;
|
|
|
|
if (editorComponent.editor.session == file.session) {
|
|
|
|
editorComponent.newBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-14 18:03:00 +00:00
|
|
|
this.filesService.delete(file);
|
2025-06-02 05:10:56 +00:00
|
|
|
});
|
2025-06-14 03:36:01 +00:00
|
|
|
|
2025-05-28 02:10:45 +00:00
|
|
|
}
|
|
|
|
|
2025-06-01 05:49:30 +00:00
|
|
|
loadMainSubscribers() {
|
|
|
|
window.fs.onLoadFiles(async (paths: []) => {
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
|
|
let file = new File([], "") as NewtonFile;
|
|
|
|
|
2025-06-14 18:03:00 +00:00
|
|
|
await this.filesService.addFile(paths[i], file);
|
|
|
|
this.filesService.addTab(file);
|
2025-06-01 05:49:30 +00:00
|
|
|
}
|
|
|
|
|
2025-06-14 18:03:00 +00:00
|
|
|
let path = paths[ paths.length - 1 ];
|
|
|
|
let file = this.filesService.get(path);
|
2025-06-01 18:49:18 +00:00
|
|
|
this.setSession(file);
|
2025-06-01 05:49:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window.main.onMenuActions(async (action: string) => {
|
|
|
|
let editorComponent = this.getActiveEditorComponent();
|
|
|
|
let editor = editorComponent.editor;
|
|
|
|
|
|
|
|
switch ( action ) {
|
|
|
|
case "new-file":
|
|
|
|
break;
|
2025-06-01 18:49:18 +00:00
|
|
|
case "open-files":
|
|
|
|
editorComponent.openFiles();
|
|
|
|
break;
|
2025-06-01 05:49:30 +00:00
|
|
|
case "save-file":
|
|
|
|
editorComponent.saveFile();
|
|
|
|
break;
|
|
|
|
case "save-file-as":
|
|
|
|
editorComponent.saveFileAs();
|
|
|
|
break;
|
|
|
|
case "cut":
|
|
|
|
editorComponent.cutText();
|
|
|
|
break;
|
|
|
|
case "copy":
|
|
|
|
editorComponent.copyText();
|
|
|
|
break;
|
|
|
|
case "paste":
|
|
|
|
editorComponent.pasteText();
|
|
|
|
break;
|
|
|
|
case "zoom-in":
|
|
|
|
editorComponent.zoomIn()
|
|
|
|
break;
|
|
|
|
case "zoom-out":
|
|
|
|
editorComponent.zoomOut()
|
|
|
|
break;
|
2025-06-13 01:31:08 +00:00
|
|
|
case "open-settings":
|
|
|
|
editor.showSettingsMenu();
|
2025-06-01 05:49:30 +00:00
|
|
|
case "show-about":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
editor.execCommand(action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-05-28 02:10:45 +00:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.unsubscribe.next();
|
|
|
|
this.unsubscribe.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private createEditor() {
|
2025-06-13 01:31:08 +00:00
|
|
|
const component = this.containerRef.createComponent(NewtonEditorComponent);
|
2025-05-28 02:10:45 +00:00
|
|
|
component.instance.editorSettings = this.editorSettings;
|
|
|
|
this.editors.set(component.instance.uuid, component)
|
|
|
|
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2025-06-01 05:49:30 +00:00
|
|
|
protected onFileDropped(files: any) {
|
2025-06-14 18:03:00 +00:00
|
|
|
this.filesService.loadFilesList(files).then((file: NewtonFile | undefined | null) => {
|
2025-06-01 18:49:18 +00:00
|
|
|
this.setSession(file);
|
2025-05-28 02:10:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
private async setSession(file: NewtonFile | undefined | null) {
|
|
|
|
if ( !file ) return;
|
|
|
|
let editorComponent = this.getActiveEditorComponent();
|
|
|
|
let editor = editorComponent.editor;
|
2025-06-01 05:49:30 +00:00
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
editorComponent.activeFile = file;
|
|
|
|
editor.setSession(file.session);
|
2025-06-01 05:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getSession() {
|
|
|
|
let editorComponent = this.editors.get(this.activeEditor)?.instance;
|
|
|
|
let editor = editorComponent.editor;
|
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
return editor.getSession();
|
2025-06-01 05:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getActiveEditorComponent(): any {
|
|
|
|
return this.editors.get(this.activeEditor)?.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getActiveEditor(): any {
|
|
|
|
let editorComponent = this.editors.get(this.activeEditor)?.instance;
|
|
|
|
let editor = editorComponent.editor;
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2025-05-28 02:10:45 +00:00
|
|
|
}
|