packages/ace/src/lib/ace.component.ts
Wraps ace editor as angular component. Implements ControlValueAccessor!
ControlValueAccessor
OnInit
OnChanges
providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => AceComponent), multi: true,
}
|
selector | ec-ace |
styleUrls | ace.component.scss |
templateUrl | ace.component.html |
Properties |
|
Methods |
Inputs |
mode | |
Type : string
|
|
Defined in packages/ace/src/lib/ace.component.ts:47
|
|
mode that should be used (e.g. javascript). Depending on your way of importing ace you have to make sure, the mode is availabe. |
theme | |
Type : string
|
|
Defined in packages/ace/src/lib/ace.component.ts:51
|
|
theme that should be used (e.g. javascript). Depending on your way of importing ace you have to make sure, the theme is availabe. |
init |
init()
|
Defined in packages/ace/src/lib/ace.component.ts:54
|
creates new ace instance if not present and sets mode and theme if given
Returns :
void
|
ngOnChanges |
ngOnChanges()
|
Defined in packages/ace/src/lib/ace.component.ts:97
|
Re-inits the editor
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in packages/ace/src/lib/ace.component.ts:93
|
Inits the editor
Returns :
void
|
registerOnChange | ||||
registerOnChange(fn)
|
||||
Defined in packages/ace/src/lib/ace.component.ts:115
|
||||
Registers change callback
Parameters :
Returns :
void
|
registerOnTouched |
registerOnTouched()
|
Defined in packages/ace/src/lib/ace.component.ts:119
|
Returns :
void
|
setMode | ||||||
setMode(mode: string)
|
||||||
Defined in packages/ace/src/lib/ace.component.ts:75
|
||||||
Sets the editor mode to the specified language (after ace/mode/)
Parameters :
Returns :
void
|
setTheme | ||||||
setTheme(theme: string)
|
||||||
Defined in packages/ace/src/lib/ace.component.ts:84
|
||||||
Sets the editor theme to the specified theme (after ace/theme/)
Parameters :
Returns :
void
|
writeValue | ||||||
writeValue(value: any)
|
||||||
Defined in packages/ace/src/lib/ace.component.ts:102
|
||||||
writes value to editor on outside model change.
Parameters :
Returns :
void
|
focus | ||||||
focus(focus)
|
||||||
Inherited from
DefaultInputComponent
|
||||||
Defined in
DefaultInputComponent:34
|
||||||
Parameters :
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:28
|
Returns :
void
|
container |
Type : ElementRef
|
Decorators :
@ViewChild('container', {static: true})
|
Defined in packages/ace/src/lib/ace.component.ts:43
|
container element for ace editor |
editor |
Type : any
|
Defined in packages/ace/src/lib/ace.component.ts:29
|
The ace editor instance |
input |
Type : InputComponent
|
Defined in packages/ace/src/lib/ace.component.ts:39
|
Form input component |
propagateChange |
Default value : () => {...}
|
Defined in packages/ace/src/lib/ace.component.ts:112
|
ready |
Type : Promise<any>
|
Defined in packages/ace/src/lib/ace.component.ts:37
|
Promise that resolves when the editor has been initialized. |
value |
Type : string
|
Default value : ''
|
Defined in packages/ace/src/lib/ace.component.ts:33
|
The current value of the editor |
Public control |
Type : FormControl
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:21
|
The form control that is used |
Public field |
Type : Field
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:15
|
The field for which the input is meant. |
focusEvent |
Type : EventEmitter<boolean>
|
Default value : new EventEmitter()
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:23
|
Emitter to focus the input field |
Public formService |
Type : FormService
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:26
|
Public group |
Type : FormGroup
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:19
|
The form group that is used |
Public item |
Type : Item<any>
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:17
|
The item that is targeted by the input |
Public keycommands |
Type : KeycommandsService
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:26
|
import { Component, OnInit, ElementRef, ViewChild, Input, OnChanges, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { DefaultInputComponent, InputComponent } from '@ec.components/ui';
import 'ace-builds/src-noconflict/ace';
import 'ace-builds/webpack-resolver';
/** Workaround that expects ace to be imported somewhere else... */
declare const ace: any;
/** Wraps ace editor as angular component. Implements ControlValueAccessor!
*
* <example-url>https://components.entrecode.de/misc/ace?e=1</example-url>
*/
@Component({
selector: 'ec-ace',
templateUrl: 'ace.component.html',
styleUrls: ['ace.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => AceComponent),
multi: true,
},
],
})
export class AceComponent extends DefaultInputComponent implements ControlValueAccessor, OnInit, OnChanges {
/** The ace editor instance */
editor: any;
/**
* The current value of the editor
*/
value = '';
/**
* Promise that resolves when the editor has been initialized.
*/
ready: Promise<any>;
/** Form input component */
input: InputComponent;
/**
* container element for ace editor
*/
@ViewChild('container', { static: true }) container: ElementRef;
/**
* mode that should be used (e.g. javascript). Depending on your way of importing ace you have to make sure, the mode is availabe.
*/
@Input() mode: string; // https://github.com/ajaxorg/ace-builds/blob/master/src/ext-modelist.js
/**
* theme that should be used (e.g. javascript). Depending on your way of importing ace you have to make sure, the theme is availabe.
*/
@Input() theme: string; // https://github.com/ajaxorg/ace-builds/blob/master/src/ext-themelist.js
/** creates new ace instance if not present and sets mode and theme if given */
init() {
if (!this.editor) {
this.editor = ace.edit(this.container.nativeElement);
}
this.ready = Promise.resolve(this.editor);
if (this.mode) {
this.setMode(this.mode);
}
if (this.theme) {
this.setTheme(this.theme);
}
if (this.value) {
this.editor.setValue(this.value, 1);
}
this.ready.then((editor) => {
editor.on('change', (e) => {
this.propagateChange(this.editor.getValue());
});
});
}
/** Sets the editor mode to the specified language (after ace/mode/) */
setMode(mode: string) {
this.mode = mode;
if (!this.editor) {
return;
}
this.editor.session.setMode('ace/mode/' + this.mode);
}
/** Sets the editor theme to the specified theme (after ace/theme/) */
setTheme(theme: string) {
this.theme = theme;
if (!this.editor) {
return;
}
this.editor.setTheme('ace/theme/' + this.theme);
}
/** Inits the editor */
ngOnInit() {
this.init();
}
/** Re-inits the editor */
ngOnChanges() {
this.init();
}
/** writes value to editor on outside model change. */
writeValue(value: any) {
this.value = value || '';
if (!this.ready) {
return;
}
this.ready.then((editor) => {
editor.setValue(this.value, 1);
});
}
propagateChange = (_: any) => {};
/** Registers change callback */
registerOnChange(fn) {
this.propagateChange = fn;
}
registerOnTouched() {}
}
<div class="ec-ace" #container></div>
ace.component.scss
.ec-ace {
min-height: 500px;
}