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
|
2025-07-05 00:06:12 -05:00
|
|
|
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";
|
|
|
|
|
2025-07-05 00:06:12 -05:00
|
|
|
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
|
|
|
|
2025-07-13 17:40:17 -05:00
|
|
|
// https://www.npmjs.com/package/web-tree-sitter
|
|
|
|
// import { Language, Parser } from 'web-tree-sitter';
|
|
|
|
|
2025-06-30 17:20:11 -05:00
|
|
|
import { CodeViewBase } from './view.base';
|
2025-05-30 01:45:26 -05:00
|
|
|
|
2025-06-20 23:52:22 -05:00
|
|
|
import { NewtonFile } from '../../common/types/file.type';
|
2025-07-06 15:05:12 -05:00
|
|
|
import { EditorType } from '../../common/types/editor.type';
|
2025-06-18 22:35:28 -05:00
|
|
|
import { ServiceMessage } from '../../common/types/service-message.type';
|
|
|
|
|
2025-05-27 21:10:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2025-06-30 17:20:11 -05:00
|
|
|
selector: 'code-view',
|
2025-05-27 21:10:45 -05:00
|
|
|
standalone: true,
|
|
|
|
imports: [
|
|
|
|
],
|
2025-06-30 17:20:11 -05:00
|
|
|
templateUrl: './view.component.html',
|
|
|
|
styleUrl: './view.component.css',
|
2025-05-27 21:10:45 -05:00
|
|
|
host: {
|
2025-07-01 01:10:14 -05:00
|
|
|
'class': 'col zero-margin-padding scroller'
|
2025-05-27 21:10:45 -05:00
|
|
|
}
|
|
|
|
})
|
2025-06-30 17:20:11 -05:00
|
|
|
export class CodeViewComponent extends CodeViewBase {
|
2025-05-27 21:10:45 -05:00
|
|
|
|
|
|
|
|
2025-06-20 23:52:22 -05:00
|
|
|
constructor() {
|
2025-05-31 01:10:55 -05:00
|
|
|
super();
|
2025-07-03 09:28:20 -05:00
|
|
|
|
2025-07-13 17:40:17 -05:00
|
|
|
// 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);
|
|
|
|
// });
|
|
|
|
|
2025-07-03 09:28:20 -05:00
|
|
|
this.aceApi = ace;
|
2025-05-31 01:10:55 -05:00
|
|
|
}
|
2025-05-27 21:10:45 -05:00
|
|
|
|
|
|
|
|
2025-06-21 19:49:58 -05:00
|
|
|
private ngAfterViewInit(): void {
|
2025-06-30 20:01:00 -05:00
|
|
|
this.loadAce();
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadAce(): void {
|
|
|
|
this.configAceAndBindToElement()
|
2025-07-06 15:05:12 -05:00
|
|
|
this.setupRequestedMode();
|
2025-06-27 23:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private configAceAndBindToElement(): void {
|
2025-06-30 00:50:32 -05:00
|
|
|
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-06 15:05:12 -05:00
|
|
|
|
2025-07-13 14:41:46 -05:00
|
|
|
this.editorsService.set(this.uuid, this);
|
2025-07-06 15:05:12 -05:00
|
|
|
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;
|
|
|
|
}
|
2025-06-21 19:49:58 -05:00
|
|
|
}
|
|
|
|
|
2025-07-06 15:05:12 -05:00
|
|
|
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 );
|
2025-06-21 19:49:58 -05:00
|
|
|
}
|
|
|
|
|
2025-07-06 15:05:12 -05:00
|
|
|
private loadNewtonEventBindings(): void {
|
2025-06-12 02:45:17 -05:00
|
|
|
|
|
|
|
// Note: https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.EditorEvents.html
|
2025-06-13 22:36:01 -05:00
|
|
|
this.editor.on("focus", (e) => {
|
2025-06-24 01:07:30 -05:00
|
|
|
let message = new ServiceMessage();
|
|
|
|
message.action = "set-active-editor";
|
|
|
|
message.editorUUID = this.uuid;
|
2025-07-03 22:46:19 -05:00
|
|
|
message.rawData = this.editor;
|
2025-06-24 01:07:30 -05:00
|
|
|
|
|
|
|
this.editorsService.sendMessage(message);
|
2025-07-03 22:46:19 -05:00
|
|
|
this.searchReplaceService.sendMessage(message);
|
2025-06-24 01:07:30 -05:00
|
|
|
|
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);
|
|
|
|
|
2025-06-13 22:36:01 -05:00
|
|
|
this.updateInfoBar();
|
2025-06-12 20:31:08 -05:00
|
|
|
});
|
|
|
|
|
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", () => {
|
2025-07-02 19:47:25 -05:00
|
|
|
if (this.debounceId) { clearTimeout(this.debounceId); }
|
2025-07-03 09:28:20 -05:00
|
|
|
this.setDebounceTimeout();
|
|
|
|
|
|
|
|
if (!this.activeFile) return;
|
2025-06-20 19:51:03 -05:00
|
|
|
|
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-07-02 19:47:25 -05:00
|
|
|
|
2025-06-20 00:50:43 -05:00
|
|
|
});
|
|
|
|
|
2025-05-31 01:10:55 -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-31 01:10:55 -05:00
|
|
|
});
|
2025-05-27 21:10:45 -05:00
|
|
|
}
|
|
|
|
|
2025-06-12 02:45:17 -05:00
|
|
|
|
2025-07-01 01:10:14 -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;
|
2025-07-03 09:28:20 -05:00
|
|
|
let session = this.aceApi.createEditSession(file.session.getValue());
|
2025-07-01 01:10:14 -05:00
|
|
|
|
|
|
|
session.setMode( file.session.getMode()["$id"] );
|
|
|
|
this.editor.setSession(session);
|
|
|
|
}
|
|
|
|
|
2025-07-02 19:47:25 -05:00
|
|
|
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
|
|
|
}
|