2025-05-31 01:10:55 -05:00
|
|
|
import { Directive, ElementRef, Input, ViewChild } from '@angular/core';
|
2025-06-07 22:20:17 -05:00
|
|
|
import * as uuid from 'uuid';
|
2025-05-31 01:10:55 -05:00
|
|
|
|
|
|
|
|
import { EditorSettings } from "../../common/configs/editor.config";
|
2025-06-01 13:49:18 -05:00
|
|
|
import { NewtonFile } from '../../common/types/file.type';
|
2025-05-31 01:10:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
|
export class AceEditorBase {
|
|
|
|
|
@ViewChild('editor') editorElm!: ElementRef;
|
|
|
|
|
@Input() editorSettings!: typeof EditorSettings;
|
|
|
|
|
editor!: any;
|
|
|
|
|
uuid!: string;
|
|
|
|
|
cutBuffer: string = "";
|
|
|
|
|
timerId: number = -1;
|
2025-06-01 13:49:18 -05:00
|
|
|
activeFile!: NewtonFile;
|
2025-05-31 01:10:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
2025-06-07 22:20:17 -05:00
|
|
|
) {
|
|
|
|
|
this.uuid = uuid.v4();
|
|
|
|
|
}
|
2025-05-31 01:10:55 -05:00
|
|
|
|
|
|
|
|
|
2025-06-01 00:49:30 -05:00
|
|
|
protected search() {
|
|
|
|
|
console.log(this.editor.getSession()["$modeId"])
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-01 13:49:18 -05:00
|
|
|
protected openFiles() {
|
|
|
|
|
let startDir = "";
|
|
|
|
|
if (this.activeFile) {
|
|
|
|
|
let pathParts = this.activeFile.path.split("/");
|
|
|
|
|
pathParts.pop();
|
|
|
|
|
startDir = pathParts.join( '/' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.fs.openFiles(startDir);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-01 00:49:30 -05:00
|
|
|
protected saveFile() {
|
2025-06-01 13:49:18 -05:00
|
|
|
if (!this.activeFile) {
|
|
|
|
|
this.saveFileAs();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const text = this.activeFile.session.getValue();
|
|
|
|
|
window.fs.saveFile(this.activeFile.path, text);
|
2025-06-01 00:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected saveFileAs() {
|
|
|
|
|
const text = this.editor.session.getValue();
|
|
|
|
|
window.fs.saveFileAs(text);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 01:10:55 -05:00
|
|
|
protected zoomIn() {
|
2025-06-01 00:49:30 -05:00
|
|
|
this.editor.setFontSize(
|
|
|
|
|
parseInt(this.editor.getFontSize()) + 1
|
|
|
|
|
)
|
2025-05-31 01:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected zoomOut() {
|
2025-06-01 00:49:30 -05:00
|
|
|
this.editor.setFontSize(
|
|
|
|
|
parseInt(this.editor.getFontSize()) - 1
|
|
|
|
|
)
|
2025-05-31 01:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-01 00:49:30 -05:00
|
|
|
protected cutText() {
|
|
|
|
|
let cutText = this.editor.getSelectedText();
|
|
|
|
|
this.editor.remove();
|
|
|
|
|
navigator.clipboard.writeText(cutText).catch(() => {
|
|
|
|
|
console.error("Unable to cut text...");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected copyText() {
|
|
|
|
|
let copyText = this.editor.getSelectedText();
|
|
|
|
|
navigator.clipboard.writeText(copyText).catch(() => {
|
|
|
|
|
console.error("Unable to copy text...");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected pasteText() {
|
|
|
|
|
navigator.clipboard.readText().then((pasteText) => {
|
|
|
|
|
this.editor.insert(pasteText, true);
|
|
|
|
|
});
|
2025-05-31 01:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected movelinesUp() {
|
|
|
|
|
this.editor.execCommand("movelinesup");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected movelinesDown() {
|
|
|
|
|
this.editor.execCommand("movelinesdown");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected duplicateLines() {
|
|
|
|
|
this.editor.execCommand("copylinesdown");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected cutToBuffer() {
|
|
|
|
|
if (this.timerId) { clearTimeout(this.timerId); }
|
|
|
|
|
|
|
|
|
|
const cursorPosition = this.editor.getCursorPosition();
|
|
|
|
|
let lineText = this.editor.session.getLine(cursorPosition.row);
|
|
|
|
|
this.cutBuffer += `${lineText}\n`;
|
|
|
|
|
|
2025-06-01 00:49:30 -05:00
|
|
|
this.editor.session.removeFullLines(cursorPosition.row, cursorPosition.row)
|
2025-05-31 01:10:55 -05:00
|
|
|
this.setBufferClearTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected pasteCutBuffer() {
|
|
|
|
|
if (this.timerId) { clearTimeout(this.timerId); }
|
|
|
|
|
|
2025-06-01 00:49:30 -05:00
|
|
|
this.editor.insert(this.cutBuffer, true);
|
2025-05-31 01:10:55 -05:00
|
|
|
this.setBufferClearTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setBufferClearTimeout(timeout: number = 5000) {
|
|
|
|
|
this.timerId = setTimeout(() => {
|
|
|
|
|
this.cutBuffer = "";
|
|
|
|
|
this.timerId = -1;
|
|
|
|
|
}, timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|