ngx-editor-plus
Version:
WYSIWYG Editor for Angular Applications
206 lines (173 loc) • 5.3 kB
text/typescript
import {
Component, OnInit, Input, Output,
ViewChild, HostListener, ElementRef, EventEmitter,
Renderer2, forwardRef
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { CommandExecutorService } from './common/services/command-executor.service';
import { MessageService } from './common/services/message.service';
import { ngxEditorConfig } from './common/ngx-editor.defaults';
import * as Utils from './common/utils/ngx-editor.utils';
export class NgxEditorComponent implements OnInit, ControlValueAccessor {
editable: boolean;
spellcheck: boolean;
placeholder: string;
translate: string;
height: string;
minHeight: string;
width: string;
minWidth: string;
toolbar: any;
resizer = 'stack';
config = ngxEditorConfig;
showToolbar = true;
textArea: any;
enableToolbar = false;
Utils = Utils;
private lastViewModel: any = '';
private onChange: (value: string) => void;
private onTouched: () => void;
blur = new EventEmitter();
focus = new EventEmitter();
/**
*
* @param _elementRef api to access dom element
* @param _messageService service to send message to the editor message component
* @param _commandExecutor executes command from the toolbar
* @param _renderer access and manipulate the dom element
*/
constructor(
private _elementRef: ElementRef,
private _messageService: MessageService,
private _commandExecutor: CommandExecutorService,
private _renderer: Renderer2) { }
/**
* events
*/
onFocus(): void {
this.enableToolbar = true;
this.focus.emit();
return;
}
onDocumentClick(event) {
this.enableToolbar = !!this._elementRef.nativeElement.contains(event.target);
if(!this.enableToolbar) this.blur.emit();
}
/**
*
* @param html html string from contenteditable
*/
onContentChange(html: string): void {
if (typeof this.onChange === 'function') {
this.config.htmlEncodedOutput ? this.onChange(this.Utils.encodeHTML(html)) : this.onChange(html);
}
return;
}
onBlur(event:any): void {
if (typeof this.onTouched === 'function') {
this.onTouched();
}
return;
}
/**
* resizing text area
*
* @param offsetY vertical height of the eidtable portion of the editor
*/
resizeTextArea(offsetY: number): void {
let newHeight = parseInt(this.height, 10);
newHeight += offsetY;
this.height = newHeight + 'px';
this.textArea.nativeElement.style.height = this.height;
return;
}
/**
* editor actions, i.e., executes command from toolbar
*
* @param commandName name of the command to be executed
*/
executeCommand(commandName: string): void {
try {
this._commandExecutor.execute(commandName);
} catch (error) {
this._messageService.sendMessage(error.message);
}
return;
}
/**
* Write a new value to the element.
*
* @param value value to be executed when there is a change in contenteditable
*/
writeValue(value: any): void {
if (value === undefined) {
return;
}
this.refreshView(value);
}
/**
* Set the function to be called
* when the control receives a change event.
*
* @param fn a function
*/
registerOnChange(fn: any): void {
this.onChange = fn;
}
/**
* Set the function to be called
* when the control receives a touch event.
*
* @param fn a function
*/
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
/**
* refresh view/HTML of the editor
*
* @param value html string from the editor
*/
refreshView(value: string): void {
value = this.config.htmlEncodedOutput ? this.Utils.decodeHTML(value) : value;
const normalizedValue = value == null ? '' : value;
this._renderer.setProperty(this.textArea.nativeElement, 'innerHTML', normalizedValue);
return;
}
/**
* return a json containing input params
*/
getCollectiveParams(): any {
return {
editable: this.editable,
spellcheck: this.spellcheck,
placeholder: this.placeholder,
translate: this.translate,
height: this.height,
minHeight: this.minHeight,
width: this.width,
minWidth: this.minWidth,
toolbar: this.toolbar
};
}
ngOnInit() {
/**
* set configuartion
*/
this.config = this.Utils.getEditorConfiguration(this.config, ngxEditorConfig, this.getCollectiveParams());
this.height = this.height || this.textArea.nativeElement.offsetHeight;
this.executeCommand('enableObjectResizing');
}
}