Skip to content

Commit

Permalink
Changed editor initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Feb 28, 2019
1 parent cc264cd commit 3575939
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/ckeditor/ckeditor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
*/
private cvaOnTouched?: () => void;

/**
* Reference to the source element used by the editor.
*/
private editorElement?: HTMLElement;

constructor( elementRef: ElementRef, ngZone: NgZone ) {
this.ngZone = ngZone;
this.elementRef = elementRef;
Expand Down Expand Up @@ -179,6 +184,12 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
// If not, wait for it to be ready; store the data.
else {
this.data = value;

// If the editor element is already available, then update its content.
// If the ngModel is used then the editor element should be updated directly here.
if ( this.editorElement ) {
this.editorElement.innerHTML = this.data;
}
}
}

Expand Down Expand Up @@ -210,24 +221,18 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
*/
private createEditor(): Promise<any> {
const element = document.createElement( this.tagName );
this.editorElement = element;

// Do not use the `editor.setData()` here because of the issue in the collaboration mode (#6).
// Instead set data to the element on which the editor will be later initialized.
element.innerHTML = this.data;

const oldData = this.data;

this.elementRef.nativeElement.appendChild( element );

return this.editor!.create( element, this.config )
.then( editor => {
this.editorInstance = editor;

// Update data if it has changed in the meantime.
if ( this.data !== oldData ) {
this.editorInstance.setData( this.data );
}

if ( this.initialIsDisabled ) {
editor.isReadOnly = this.initialIsDisabled;
}
Expand Down

0 comments on commit 3575939

Please sign in to comment.