Add system try; refactored editor naming

This commit is contained in:
2025-06-12 20:31:08 -05:00
parent 0675985a5e
commit 9d3eb76960
10 changed files with 124 additions and 21 deletions

View File

@@ -0,0 +1,139 @@
import { Directive, ElementRef, Input, ViewChild } from '@angular/core';
import * as uuid from 'uuid';
import { EditorSettings } from "../../common/configs/editor.config";
import { NewtonFile } from '../../common/types/file.type';
@Directive()
export class NewtonEditorBase {
@ViewChild('editor') editorElm!: ElementRef;
@Input() editorSettings!: typeof EditorSettings;
editor!: any;
uuid!: string;
cutBuffer: string = "";
timerId: number = -1;
activeFile!: NewtonFile;
isDefault: boolean = false;
constructor(
) {
this.uuid = uuid.v4();
}
public addActiveStyling() {
this.editorElm.nativeElement.classList.add("active-editor")
}
public removeActiveStyling() {
this.editorElm.nativeElement.classList.remove("active-editor")
}
public commander() {
this.editor.execCommand("openCommandPalette");
}
protected search() {
console.log(this.editor.session.getMode()["$id"]);
}
protected openFiles() {
let startDir = "";
if (this.activeFile) {
let pathParts = this.activeFile.path.split("/");
pathParts.pop();
startDir = pathParts.join( '/' );
}
window.fs.openFiles(startDir);
}
protected saveFile() {
if (!this.activeFile) {
this.saveFileAs();
return;
}
const text = this.activeFile.session.getValue();
window.fs.saveFile(this.activeFile.path, text);
}
protected saveFileAs() {
const text = this.editor.session.getValue();
window.fs.saveFileAs(text);
}
protected zoomIn() {
this.editor.setFontSize(
parseInt(this.editor.getFontSize()) + 1
)
}
protected zoomOut() {
this.editor.setFontSize(
parseInt(this.editor.getFontSize()) - 1
)
}
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);
});
}
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`;
this.editor.session.removeFullLines(cursorPosition.row, cursorPosition.row)
this.setBufferClearTimeout();
}
protected pasteCutBuffer() {
if (this.timerId) { clearTimeout(this.timerId); }
this.editor.insert(this.cutBuffer, true);
this.setBufferClearTimeout();
}
private setBufferClearTimeout(timeout: number = 5000) {
this.timerId = setTimeout(() => {
this.cutBuffer = "";
this.timerId = -1;
}, timeout);
}
}

View File

@@ -0,0 +1,28 @@
/*
.editor {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.editor {
height: 100vh;
width: auto;
}
*/
.editor {
position: relative;
height: 100%;
width: 100%;
}
.active-editor {
border-style: solid;
border-width: thin;
border-color: rgba(124, 124, 124, 1);
}

View File

@@ -0,0 +1,2 @@
<div class="editor" #editor >
</div>

View File

@@ -0,0 +1,208 @@
import { Component } from "@angular/core";
// Import Ace and its modes/themes so that `ace` global is defined
import * as ace from "ace-builds/src-noconflict/ace";
import "ace-builds/src-noconflict/ext-settings_menu";
import "ace-builds/src-noconflict/ext-command_bar";
import "ace-builds/src-noconflict/ext-language_tools";
import "ace-builds/src-noconflict/theme-one_dark";
import "ace-builds/src-noconflict/theme-dracula";
import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service';
import { EditorsService } from '../../common/services/editor/editors.service';
import { LSPService } from '../../common/services/lsp.service';
import { NewtonEditorBase } from './newton-editor.base';
@Component({
selector: 'newton-editor',
standalone: true,
imports: [
],
templateUrl: './newton-editor.component.html',
styleUrl: './newton-editor.component.css',
host: {
'class': 'col'
}
})
export class NewtonEditorComponent extends NewtonEditorBase {
constructor(
private infoBarService: InfoBarService,
private editorsService: EditorsService,
private lspService: LSPService
) {
super();
}
public ngAfterViewInit(): void {
if (this.isDefault) {
this.addActiveStyling();
}
this.loadAce();
}
public loadAce(): void {
ace.config.set('basePath', this.editorSettings.BASE_PATH);
this.editor = ace.edit( this.editorElm.nativeElement );
this.editor.setOptions( this.editorSettings.CONFIG );
// this.editor.commands.addCommands( this.editorSettings.KEYBINDINGS );
this.editor.commands.addCommands([
{
name: "openCommandPalette2",
bindKey: {linux: "Command-Shift-/|F1", win: "Ctrl-Shift-/|F1"},
exec: () => {
this.commander();
},
readOnly: false
}, {
name: "search",
bindKey: {win: "ctrl-f", mac: "ctrl-f"},
exec: () => {
this.search();
},
readOnly: true
}, {
name: "movelinesUp",
bindKey: {win: "ctrl-up", mac: "ctrl-up"},
exec: () => {
this.movelinesUp();
},
readOnly: false
}, {
name: "movelinesDown",
bindKey: {win: "ctrl-down", mac: "ctrl-down"},
exec: () => {
this.movelinesDown();
},
readOnly: false
},
{
name: "duplicateLines",
bindKey: {win: "ctrl-d", mac: "ctrl-d"},
exec: () => {
this.duplicateLines();
},
readOnly: false
}, {
name: "zoomIn",
bindKey: {win: "ctrl-=", mac: "ctrl-="},
exec: () => {
this.zoomIn();
},
readOnly: true
}, {
name: "zoomOut",
bindKey: {win: "ctrl--", mac: "ctrl--"},
exec: () => {
this.zoomOut();
},
readOnly: true
}, {
name: "cutToBuffer",
bindKey: {win: "ctrl-k", mac: "ctrl-k"},
exec: () => {
this.cutToBuffer();
},
readOnly: false
}, {
name: "pasteCutBuffer",
bindKey: {win: "ctrl-u", mac: "ctrl-u"},
exec: () => {
this.pasteCutBuffer();
},
readOnly: false
}, {
name: "destroySession",
bindKey: {win: "ctrl-w", mac: "ctrl-w"},
exec: () => {
this.editor.session.destroy();
},
readOnly: false
}, {
name: "openFiles",
bindKey: {win: "ctrl-o", mac: "ctrl-o"},
exec: () => {
this.openFiles();
},
readOnly: false
}, {
name: "saveFile",
bindKey: {win: "ctrl-s", mac: "ctrl-s"},
exec: () => {
this.saveFile();
},
readOnly: false
}, {
name: "saveFileAs",
bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
exec: () => {
this.saveFileAs();
},
readOnly: false
}
]);
// Note: https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.EditorEvents.html
this.editor.on("changeStatus", (e) => {
console.log(e);
});
this.editor.on("click", () => {
this.updateInfoBar();
});
this.editor.on("input", () => {
this.updateInfoBar();
});
this.editor.on("keyboardActivity", (e) => {
switch(e.command.name) {
case "golineup":
case "golinedown":
case "gotoleft":
case "gotoright":
this.infoBarService.setInfoBarCursorPos(
this.editor.getCursorPosition()
);
break;
default:
break;
}
});
this.editor.on("focus", () => {
this.editorsService.setActiveEditor(this.uuid);
});
this.editor.on("changeSession", (session) => {
this.lspService.registerEditor(this.editor);
this.updateInfoBar();
});
}
public updateInfoBar() {
this.infoBarService.setInfoBarFPath(this.activeFile?.path)
this.infoBarService.setInfoBarCursorPos(
this.editor.getCursorPosition()
);
this.infoBarService.setInfoBarFType(
this.editor.session.getMode()["$id"]
);
}
public newBuffer() {
let buffer = ace.createEditSession([""]);
this.editor.setSession(buffer);
this.activeFile = null;
this.updateInfoBar();
}
}