Newton-Editor/src/app/editor/ace/ace-editor.component.ts

195 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-06-12 07:45:17 +00:00
import { Component } from "@angular/core";
2025-05-28 02:10:45 +00:00
// Import Ace and its modes/themes so that `ace` global is defined
2025-06-12 07:45:17 +00:00
import * as ace from "ace-builds/src-noconflict/ace";
import "ace-builds/src-noconflict/theme-one_dark";
import "ace-builds/src-noconflict/theme-dracula";
2025-05-28 05:52:55 +00:00
import "ace-builds/src-noconflict/ext-language_tools";
2025-06-12 07:45:17 +00:00
import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service';
2025-05-28 02:10:45 +00:00
import { EditorsService } from '../../common/services/editor/editors.service';
2025-05-30 05:30:54 +00:00
import { LSPService } from '../../common/services/lsp.service';
2025-05-28 02:10:45 +00:00
import { AceEditorBase } from './ace-editor.base';
2025-05-28 02:10:45 +00:00
@Component({
selector: 'ace-editor',
standalone: true,
imports: [
],
templateUrl: './ace-editor.component.html',
styleUrl: './ace-editor.component.css',
host: {
2025-05-30 05:30:54 +00:00
'class': 'col'
2025-05-28 02:10:45 +00:00
}
})
export class AceEditorComponent extends AceEditorBase {
2025-05-28 02:10:45 +00:00
2025-05-30 05:30:54 +00:00
constructor(
2025-06-12 07:45:17 +00:00
private infoBarService: InfoBarService,
2025-05-30 05:30:54 +00:00
private editorsService: EditorsService,
private lspService: LSPService
) {
super();
}
2025-05-28 02:10:45 +00:00
public ngAfterViewInit(): void {
if (this.isDefault) {
this.addActiveStyling();
}
2025-05-28 02:10:45 +00:00
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: "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();
},
2025-06-12 07:45:17 +00:00
readOnly: false
}, {
name: "movelinesDown",
bindKey: {win: "ctrl-down", mac: "ctrl-down"},
exec: () => {
this.movelinesDown();
},
2025-06-12 07:45:17 +00:00
readOnly: false
},
{
name: "duplicateLines",
bindKey: {win: "ctrl-d", mac: "ctrl-d"},
exec: () => {
this.duplicateLines();
},
2025-06-12 07:45:17 +00:00
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();
},
2025-06-12 07:45:17 +00:00
readOnly: false
}, {
name: "pasteCutBuffer",
bindKey: {win: "ctrl-u", mac: "ctrl-u"},
exec: () => {
this.pasteCutBuffer();
},
2025-06-12 07:45:17 +00:00
readOnly: false
}, {
name: "destroySession",
bindKey: {win: "ctrl-w", mac: "ctrl-w"},
exec: () => {
this.editor.session.destroy();
},
2025-06-12 07:45:17 +00:00
readOnly: false
}, {
name: "openFiles",
bindKey: {win: "ctrl-o", mac: "ctrl-o"},
exec: () => {
this.openFiles();
},
2025-06-12 07:45:17 +00:00
readOnly: false
2025-06-01 05:49:30 +00:00
}, {
name: "saveFile",
bindKey: {win: "ctrl-s", mac: "ctrl-s"},
exec: () => {
this.saveFile();
},
2025-06-12 07:45:17 +00:00
readOnly: false
2025-06-01 05:49:30 +00:00
}, {
name: "saveFileAs",
bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
exec: () => {
this.saveFileAs();
},
2025-06-12 07:45:17 +00:00
readOnly: false
}
]);
2025-05-28 02:10:45 +00:00
2025-06-12 07:45:17 +00:00
// Note: https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.EditorEvents.html
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;
}
});
2025-05-30 05:30:54 +00:00
this.editor.on("focus", () => {
this.editorsService.setActiveEditor(this.uuid);
});
2025-05-28 02:10:45 +00:00
this.editor.on("changeSession", (session) => {
this.lspService.registerEditor(this.editor);
2025-06-12 07:45:17 +00:00
this.updateInfoBar();
});
2025-05-28 02:10:45 +00:00
}
2025-06-12 07:45:17 +00:00
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;
2025-06-12 07:45:17 +00:00
this.updateInfoBar();
}
2025-05-28 02:10:45 +00:00
}