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

140 lines
4.4 KiB
TypeScript
Raw Normal View History

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
import * as ace from 'ace-builds/src-noconflict/ace';
import 'ace-builds/src-noconflict/theme-one_dark';
2025-05-28 05:52:55 +00:00
import "ace-builds/src-noconflict/ext-language_tools";
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(
private editorsService: EditorsService,
private lspService: LSPService
) {
super();
}
2025-05-28 02:10:45 +00:00
public ngAfterViewInit(): void {
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();
},
readOnly: true
}, {
name: "movelinesDown",
bindKey: {win: "ctrl-down", mac: "ctrl-down"},
exec: () => {
this.movelinesDown();
},
readOnly: true
},
{
name: "duplicateLines",
bindKey: {win: "ctrl-d", mac: "ctrl-d"},
exec: () => {
this.duplicateLines();
},
readOnly: true
}, {
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: true
}, {
name: "pasteCutBuffer",
bindKey: {win: "ctrl-u", mac: "ctrl-u"},
exec: () => {
this.pasteCutBuffer();
},
readOnly: true
}, {
name: "destroySession",
bindKey: {win: "ctrl-w", mac: "ctrl-w"},
exec: () => {
this.editor.session.destroy();
},
readOnly: true
2025-06-01 05:49:30 +00:00
}, {
name: "saveFile",
bindKey: {win: "ctrl-s", mac: "ctrl-s"},
exec: () => {
this.saveFile();
},
readOnly: true
}, {
name: "saveFileAs",
bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
exec: () => {
this.saveFileAs();
},
readOnly: true
}
]);
2025-05-28 02:10:45 +00:00
// Note: https://github.com/mkslanc/ace-linters/blob/c286d85c558530aa1b0597d02108bc782abd4736/packages/ace-linters/src/language-provider.ts#L277
// found on focus ^ might have other signals we can watch like session being set, etc.
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-05-28 02:10:45 +00:00
}
}