File

packages/ace/src/lib/ace.component.ts

Description

Wraps ace editor as angular component. Implements ControlValueAccessor!

https://components.entrecode.de/misc/ace?e=1

Extends

DefaultInputComponent

Implements

ControlValueAccessor OnInit OnChanges

Metadata

providers { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => AceComponent), multi: true, }
selector ec-ace
styleUrls ace.component.scss
templateUrl ace.component.html

Index

Properties
Methods
Inputs

Inputs

mode
Type : string

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

theme that should be used (e.g. javascript). Depending on your way of importing ace you have to make sure, the theme is availabe.

Methods

init
init()

creates new ace instance if not present and sets mode and theme if given

Returns : void
ngOnChanges
ngOnChanges()

Re-inits the editor

Returns : void
ngOnInit
ngOnInit()

Inits the editor

Returns : void
registerOnChange
registerOnChange(fn)

Registers change callback

Parameters :
Name Optional
fn No
Returns : void
registerOnTouched
registerOnTouched()
Returns : void
setMode
setMode(mode: string)

Sets the editor mode to the specified language (after ace/mode/)

Parameters :
Name Type Optional
mode string No
Returns : void
setTheme
setTheme(theme: string)

Sets the editor theme to the specified theme (after ace/theme/)

Parameters :
Name Type Optional
theme string No
Returns : void
writeValue
writeValue(value: any)

writes value to editor on outside model change.

Parameters :
Name Type Optional
value any No
Returns : void
focus
focus(focus)
Inherited from DefaultInputComponent
Parameters :
Name Optional Default value
focus No true
Returns : void
ngAfterViewInit
ngAfterViewInit()
Inherited from DefaultInputComponent
Returns : void

Properties

container
Type : ElementRef
Decorators :
@ViewChild('container', {static: true})

container element for ace editor

editor
Type : any

The ace editor instance

input
Type : InputComponent

Form input component

propagateChange
Default value : () => {...}
ready
Type : Promise<any>

Promise that resolves when the editor has been initialized.

value
Type : string
Default value : ''

The current value of the editor

Public control
Type : FormControl
Inherited from DefaultInputComponent

The form control that is used

Public field
Type : Field
Inherited from DefaultInputComponent

The field for which the input is meant.

focusEvent
Type : EventEmitter<boolean>
Default value : new EventEmitter()
Inherited from DefaultInputComponent

Emitter to focus the input field

Public formService
Type : FormService
Inherited from DefaultInputComponent
Public group
Type : FormGroup
Inherited from DefaultInputComponent

The form group that is used

Public item
Type : Item<any>
Inherited from DefaultInputComponent

The item that is targeted by the input

Public keycommands
Type : KeycommandsService
Inherited from DefaultInputComponent
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;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""