Files
Newton-Editor/src/app/editor/code-view/view.component.ts

237 lines
7.6 KiB
TypeScript
Raw Normal View History

2025-06-12 02:45:17 -05:00
import { Component } from "@angular/core";
2025-05-27 21:10:45 -05:00
// Import Ace and its modes/themes so that `ace` global is defined
import * as ace from "ace-builds/src-min-noconflict/ace";
2025-07-13 14:41:46 -05:00
// Note: https://github.com/mkslanc/ace-linters/blob/c286d85c558530aa1b0597d02108bc782abd4736/packages/demo/file-api-websockets/client.ts#L27
// import { AceLayout, Box, TabManager, Button, dom, AceTreeWrapper, FileSystemWeb, Pane, AceEditor, Tab } from "ace-layout";
import "ace-builds/src-min-noconflict/ext-settings_menu";
import "ace-builds/src-min-noconflict/ext-keybinding_menu";
import "ace-builds/src-min-noconflict/ext-command_bar";
import "ace-builds/src-min-noconflict/ext-prompt";
import "ace-builds/src-min-noconflict/ext-code_lens";
// import "ace-builds/src-min-noconflict/ext-searchbox";
import "ace-builds/src-min-noconflict/ext-language_tools";
// import "ace-builds/src-min-noconflict/theme-one_dark";
// import "ace-builds/src-min-noconflict/theme-penguins_in_space";
import "ace-builds/src-min-noconflict/theme-gruvbox";
2025-05-28 00:52:55 -05:00
// https://www.npmjs.com/package/web-tree-sitter
// import { Language, Parser } from 'web-tree-sitter';
import { CodeViewBase } from './view.base';
import { NewtonFile } from '../../common/types/file.type';
import { EditorType } from '../../common/types/editor.type';
import { ServiceMessage } from '../../common/types/service-message.type';
2025-05-27 21:10:45 -05:00
@Component({
selector: 'code-view',
2025-05-27 21:10:45 -05:00
standalone: true,
imports: [
],
templateUrl: './view.component.html',
styleUrl: './view.component.css',
2025-05-27 21:10:45 -05:00
host: {
'class': 'col zero-margin-padding scroller'
2025-05-27 21:10:45 -05:00
}
})
export class CodeViewComponent extends CodeViewBase {
2025-05-27 21:10:45 -05:00
constructor() {
super();
// const { Parser } = window.TreeSitter;
// const { Parser } = TreeSitter;
// console.log(treeSitter);
// treeSitter.Parser.init().then(() => {
// console.log("Parser ready...");
// });
// const parser = new Parser();
// const JavaScript = await Language.load('/path/to/tree-sitter-javascript.wasm');
// Language.load('resources/wasm/tree-sitter-javascript.wasm').then((language) => {
// console.log(language);
// });
this.aceApi = ace;
}
2025-05-27 21:10:45 -05:00
private ngAfterViewInit(): void {
2025-06-30 20:01:00 -05:00
this.loadAce();
}
private loadAce(): void {
this.configAceAndBindToElement()
this.setupRequestedMode();
}
private configAceAndBindToElement(): void {
this.editorSettings = this.editorsService.editorSettings;
2025-05-27 21:10:45 -05:00
ace.config.set('basePath', this.editorSettings.BASE_PATH);
this.editor = ace.edit( this.editorElm.nativeElement );
this.editor.setOptions( this.editorSettings.CONFIG );
2025-07-13 14:41:46 -05:00
this.editorsService.set(this.uuid, this);
if (this.isDefault) {
this.editorsService.setActiveEditor(this.uuid);
this.addActiveStyling();
this.editor.focus();
}
}
private setupRequestedMode() {
switch(this.mode) {
case EditorType.Standalone:
// Note: Ace editor without any additional Newton setup...
break;
case EditorType.MiniMap:
this.setAsMiniMapView();
break;
case EditorType.ReadOnly:
this.setAsReadOnly();
break;
default:
this.loadNewtonKeyBindings();
this.loadNewtonEventBindings();
break;
}
}
private loadNewtonKeyBindings(): void {
2025-06-17 21:11:09 -05:00
let keyBindings = [];
for (let i = 0; i < this.editorSettings.KEYBINDINGS.length; i++) {
let keyBinding = this.editorSettings.KEYBINDINGS[i];
keyBindings.push(
{
name: keyBinding.name,
bindKey: keyBinding.bindKey,
exec: (keyBinding.name && keyBinding?.service) ?
() => (
this[keyBinding?.service][keyBinding.name]()
)
:
(this[keyBinding.name]) ?
() => (
this[keyBinding.name]()
)
:
() => (
console.log(
`Name: ${keyBinding.name}, is not mapping to a method OR mapping to a Service: ${keyBinding?.service} and Name: ${keyBinding.name}.`
)
)
,
readOnly: keyBinding.readOnly
}
);
}
this.editor.commands.addCommands( keyBindings );
}
private loadNewtonEventBindings(): void {
2025-06-12 02:45:17 -05:00
// Note: https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.EditorEvents.html
this.editor.on("focus", (e) => {
let message = new ServiceMessage();
message.action = "set-active-editor";
message.editorUUID = this.uuid;
message.rawData = this.editor;
this.editorsService.sendMessage(message);
this.searchReplaceService.sendMessage(message);
2025-07-04 21:59:09 -05:00
message = new ServiceMessage();
message.action = "set-active-editor";
message.rawData = this;
2025-07-13 14:41:46 -05:00
this.lspManagerService.sendMessage(message);
2025-07-04 21:59:09 -05:00
this.markdownPreviewService.sendMessage(message);
this.updateInfoBar();
});
2025-06-12 02:45:17 -05:00
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-06-20 00:50:43 -05:00
this.editor.on("change", () => {
if (this.debounceId) { clearTimeout(this.debounceId); }
this.setDebounceTimeout();
if (!this.activeFile) return;
2025-06-20 00:50:43 -05:00
let message = new ServiceMessage();
message.action = "file-changed";
message.filePath = this.activeFile.path;
this.tabsService.sendMessage(message);
2025-06-20 00:50:43 -05:00
});
this.editor.on("changeSession", (session) => {
2025-07-13 14:41:46 -05:00
let message = new ServiceMessage();
message.action = "editor-update";
message.rawData = this;
this.lspManagerService.sendMessage(message);
2025-06-12 02:45:17 -05:00
this.updateInfoBar();
});
2025-05-27 21:10:45 -05:00
}
2025-06-12 02:45:17 -05:00
public assignSession(file: NewtonFile) {
if (!file) return;
this.activeFile = file;
this.editor.setSession(file.session);
}
public cloneSession(file: NewtonFile) {
if (!file) return;
this.activeFile = file;
let session = this.aceApi.createEditSession(file.session.getValue());
session.setMode( file.session.getMode()["$id"] );
this.editor.setSession(session);
}
private setDebounceTimeout(timeout: number = null) {
this.debounceId = setTimeout(() => {
this.editorsService.miniMapView.editor.session.setValue(
this.editor.session.getValue()
);
this.debounceId = -1;
}, (timeout) ? timeout : this.debounceWait);
}
2025-05-27 21:10:45 -05:00
}