libs/core/monaco-editor/src/lib/monaco-editor/editor.component.ts
| changeDetection | ChangeDetectionStrategy.OnPush |
| providers |
)
|
| selector | taly-monaco-editor |
| standalone | true |
| styles |
:host {
display: block;
height: 200px;
}
.editor-container {
width: 100%;
height: 98%;
}
|
| template | |
Properties |
Methods |
Inputs |
Outputs |
Accessors |
| model | |
Type : NgxEditorModel
|
|
| options | |
Type : any
|
|
| insideNg | |
Type : boolean
|
|
|
Inherited from
BaseEditor
|
|
| onInit | |
Type : EventEmitter
|
|
|
Inherited from
BaseEditor
|
|
| registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
|
Parameters :
Returns :
void
|
| registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
|
Parameters :
Returns :
void
|
| setDisabledState | ||||||
setDisabledState(disabled: boolean)
|
||||||
|
Parameters :
Returns :
void
|
| writeValue | ||||||
writeValue(value: any)
|
||||||
|
Parameters :
Returns :
void
|
| onTouched |
Type : unknown
|
Default value : () => {...}
|
| propagateChange |
Type : unknown
|
Default value : () => {...}
|
| _editorContainer |
Type : ElementRef
|
Decorators :
@ViewChild('editorContainer', {static: true})
|
|
Inherited from
BaseEditor
|
| config |
Type : unknown
|
Default value : inject<NgxMonacoEditorConfig>(NGX_MONACO_EDITOR_CONFIG)
|
|
Inherited from
BaseEditor
|
| options | ||||||
getoptions()
|
||||||
setoptions(options: any)
|
||||||
|
Parameters :
Returns :
void
|
| model | ||||||
setmodel(model: NgxEditorModel)
|
||||||
|
Parameters :
Returns :
void
|
import {
ChangeDetectionStrategy,
Component,
forwardRef,
inject,
Input,
NgZone
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { fromEvent } from 'rxjs';
import { BaseEditor } from './base-editor';
import { NgxEditorModel } from './types';
declare let monaco: any;
@Component({
standalone: true,
selector: 'taly-monaco-editor',
template: '<div class="editor-container" #editorContainer></div>',
styles: [
`
:host {
display: block;
height: 200px;
}
.editor-container {
width: 100%;
height: 98%;
}
`
],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EditorComponent),
multi: true
}
]
})
export class EditorComponent extends BaseEditor implements ControlValueAccessor {
private zone = inject(NgZone);
private _value = '';
propagateChange = (_: any) => {
/* empty */
};
onTouched = () => {
/* empty */
};
@Input()
set options(options: any) {
this._options = Object.assign({}, this.config.defaultOptions, options);
if (this._editor) {
this._editor.dispose();
this.initMonaco(this._options, this.insideNg);
}
}
get options(): any {
return this._options;
}
@Input()
set model(model: NgxEditorModel) {
this.options.model = model;
if (this._editor) {
this._editor.dispose();
this.initMonaco(this.options, this.insideNg);
}
}
writeValue(value: any): void {
this._value = value || '';
// Fix for value change while dispose in process.
setTimeout(() => {
if (this._editor && !this.options.model) {
this._editor.setValue(this._value);
}
});
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(disabled: boolean): void {
this.options.readOnly = disabled || this._options.readOnly;
}
protected initMonaco(options: any, insideNg: boolean): void {
const hasModel = !!options.model;
if (hasModel) {
const model = monaco.editor.getModel(options.model.uri || '');
if (model) {
options.model = model;
options.model.setValue(this._value);
} else {
options.model = monaco.editor.createModel(
options.model.value,
options.model.language,
options.model.uri
);
}
}
if (insideNg) {
this._editor = monaco.editor.create(this._editorContainer.nativeElement, options);
} else {
this.zone.runOutsideAngular(() => {
this._editor = monaco.editor.create(this._editorContainer.nativeElement, options);
});
}
if (!hasModel) {
this._editor.setValue(this._value);
}
this._editor.onDidChangeModelContent((e: any) => {
const value = this._editor.getValue();
// value is not propagated to parent when executing outside zone.
this.zone.run(() => {
this.propagateChange(value);
this._value = value;
});
});
this._editor.onDidBlurEditorWidget(() => {
this.onTouched();
});
this._editor.setTheme = (themeName: string): void => {
this.options.theme = themeName;
monaco.editor.setTheme(themeName);
};
// refresh layout on resize event.
if (this._windowResizeSubscription) {
this._windowResizeSubscription.unsubscribe();
}
this._windowResizeSubscription = fromEvent(window, 'resize').subscribe(() =>
this._editor.layout()
);
this.onInit.emit(this._editor);
}
}
:host {
display: block;
height: 200px;
}
.editor-container {
width: 100%;
height: 98%;
}