JS func update

This commit is contained in:
mpetrov 2025-02-26 18:33:26 +02:00
parent 82b40dfad7
commit 0b44abe121
2 changed files with 67 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright © 2016-2024 The Thingsboard Authors * Copyright © 2016-2025 The Thingsboard Authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -37,7 +37,7 @@ import { ActionNotificationHide, ActionNotificationShow } from '@core/notificati
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state'; import { AppState } from '@core/core.state';
import { UtilsService } from '@core/services/utils.service'; import { UtilsService } from '@core/services/utils.service';
import { deepClone, guid, isUndefined, isUndefinedOrNull } from '@app/core/utils'; import { deepClone, guid, isEqual, isObject, isUndefined, isUndefinedOrNull } from '@app/core/utils';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CancelAnimationFrame, RafService } from '@core/services/raf.service'; import { CancelAnimationFrame, RafService } from '@core/services/raf.service';
import { TbEditorCompleter } from '@shared/models/ace/completion.models'; import { TbEditorCompleter } from '@shared/models/ace/completion.models';
@ -179,26 +179,12 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
private http: HttpClient) { private http: HttpClient) {
} }
ngOnChanges(changes: SimpleChanges): void {
if (changes.functionArgs) {
this.updateFunctionArgsString();
this.updateFunctionLabel();
}
if (changes.editorCompleter?.previousValue) {
this.updateCompleters();
}
if (changes.highlightRules?.previousValue) {
this.updateHighlightRules();
}
}
ngOnInit(): void { ngOnInit(): void {
if (this.functionTitle || this.label) {
this.hideBrackets = true;
}
if (!this.resultType || this.resultType.length === 0) { if (!this.resultType || this.resultType.length === 0) {
this.resultType = 'nocheck'; this.resultType = 'nocheck';
} }
this.updateFunctionArgsString()
this.updateFunctionLabel();
const editorElement = this.javascriptEditorElmRef.nativeElement; const editorElement = this.javascriptEditorElmRef.nativeElement;
let editorOptions: Partial<Ace.EditorOptions> = { let editorOptions: Partial<Ace.EditorOptions> = {
mode: 'ace/mode/javascript', mode: 'ace/mode/javascript',
@ -262,6 +248,16 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
); );
} }
ngOnChanges(changes: SimpleChanges): void {
for (const propName of Object.keys(changes)) {
const { firstChange, currentValue, previousValue } = changes[propName];
const isChanged = isObject(currentValue) ? isEqual(currentValue, previousValue) : currentValue !== previousValue;
if (!firstChange && isChanged) {
this.updateByChangesPropName(propName);
}
}
}
ngOnDestroy(): void { ngOnDestroy(): void {
if (this.editorResize$) { if (this.editorResize$) {
this.editorResize$.disconnect(); this.editorResize$.disconnect();
@ -271,24 +267,6 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
} }
} }
private updateHighlightRules(): void {
// @ts-ignore
if (!!this.highlightRules && !!this.jsEditor.session.$mode) {
// @ts-ignore
const newMode = new this.jsEditor.session.$mode.constructor();
newMode.$highlightRules = new newMode.HighlightRules();
for(const group in this.highlightRules) {
if(!!newMode.$highlightRules.$rules[group]) {
newMode.$highlightRules.$rules[group].unshift(...this.highlightRules[group]);
} else {
newMode.$highlightRules.$rules[group] = this.highlightRules[group];
}
}
// @ts-ignore
this.jsEditor.session.$onChangeMode(newMode);
}
}
private onAceEditorResize() { private onAceEditorResize() {
if (this.editorsResizeCaf) { if (this.editorsResizeCaf) {
this.editorsResizeCaf(); this.editorsResizeCaf();
@ -340,6 +318,9 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
} }
private updateFunctionLabel(): void { private updateFunctionLabel(): void {
if (this.functionTitle || this.label) {
this.hideBrackets = true;
}
if (this.functionTitle) { if (this.functionTitle) {
this.functionLabel = `${this.functionTitle}: f(${this.functionArgsString})`; this.functionLabel = `${this.functionTitle}: f(${this.functionArgsString})`;
} else if (this.label) { } else if (this.label) {
@ -351,6 +332,10 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
this.cd.markForCheck(); this.cd.markForCheck();
} }
private updatedScriptLanguage() {
this.jsEditor.session.setMode(`ace/mode/${ScriptLanguage.TBEL === this.scriptLanguage ? 'tbel' : 'javascript'}`);
}
validateOnSubmit(): Observable<void> { validateOnSubmit(): Observable<void> {
if (!this.disabled) { if (!this.disabled) {
this.cleanupJsErrors(); this.cleanupJsErrors();
@ -561,6 +546,52 @@ export class JsFuncComponent implements OnInit, OnChanges, OnDestroy, ControlVal
} }
} }
private updateByChangesPropName(propName: string): void {
switch (propName) {
case 'functionArgs':
this.updateFunctionArgsString()
this.updateFunctionLabel();
this.updateJsWorkerGlobals();
break;
case 'label':
case 'functionTitle':
case 'functionName':
this.updateFunctionLabel();
break;
case 'scriptLanguage':
this.updatedScriptLanguage();
break;
case 'disableUndefinedCheck':
case 'globalVariables':
this.updateJsWorkerGlobals();
break;
case 'editorCompleter':
this.updateCompleters();
break;
case 'highlightRules':
this.updateHighlightRules();
break;
}
}
private updateHighlightRules(): void {
// @ts-ignore
if (!!this.highlightRules && !!this.jsEditor.session.$mode) {
// @ts-ignore
const newMode = new this.jsEditor.session.$mode.constructor();
newMode.$highlightRules = new newMode.HighlightRules();
for(const group in this.highlightRules) {
if(!!newMode.$highlightRules.$rules[group]) {
newMode.$highlightRules.$rules[group].unshift(...this.highlightRules[group]);
} else {
newMode.$highlightRules.$rules[group] = this.highlightRules[group];
}
}
// @ts-ignore
this.jsEditor.session.$onChangeMode(newMode);
}
}
private updateJsWorkerGlobals() { private updateJsWorkerGlobals() {
// @ts-ignore // @ts-ignore
if (!!this.jsEditor.session.$worker) { if (!!this.jsEditor.session.$worker) {