Skip to content

Commit

Permalink
Merge pull request #140 from ckeditor/t/139
Browse files Browse the repository at this point in the history
Fix: The `<ckeditor>` component won't call the CVA `registerOnChange()` when the change comes from the CVA. This will fix an issue with changing data in Reactive Forms. Closes #139.
  • Loading branch information
pomek committed Oct 15, 2019
2 parents 2633347 + dde33ea commit 4fd431e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/app/demo-form/demo-form.component.spec.ts
Expand Up @@ -55,7 +55,7 @@ describe( 'DemoFormComponent', () => {

fixture.detectChanges();

expect( component.formDataPreview ).toEqual( '{"name":null,"surname":null,"description":""}' );
expect( component.formDataPreview ).toEqual( '{"name":null,"surname":null,"description":null}' );

done();
} );
Expand Down
Expand Up @@ -55,7 +55,7 @@ describe( 'DemoReactiveFormComponent', () => {

fixture.detectChanges();

expect( component.formDataPreview ).toEqual( '{"name":null,"surname":null,"description":""}' );
expect( component.formDataPreview ).toEqual( '{"name":null,"surname":null,"description":null}' );

done();
} );
Expand Down
13 changes: 13 additions & 0 deletions src/ckeditor/ckeditor.component.spec.ts
Expand Up @@ -285,6 +285,19 @@ describe( 'CKEditorComponent', () => {
expect( spy ).toHaveBeenCalledWith( '<p>foo</p>' );
} );
} );

it( 'onChange callback should not be called when the change is coming from outside of the editor', () => {
fixture.detectChanges();

return wait().then( () => {
const spy = jasmine.createSpy();
component.registerOnChange( spy );

component.writeValue( 'foo' );

expect( spy ).not.toHaveBeenCalled();
} );
} );
} );
} );

Expand Down
11 changes: 10 additions & 1 deletion src/ckeditor/ckeditor.component.ts
Expand Up @@ -150,6 +150,11 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
*/
private editorElement?: HTMLElement;

/**
* A lock flag preventing from calling the `cvaOnChange()` during setting editor data.
*/
private isEditorSettingData = false;

public constructor( elementRef: ElementRef, ngZone: NgZone ) {
this.ngZone = ngZone;
this.elementRef = elementRef;
Expand Down Expand Up @@ -180,7 +185,11 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue

// If already initialized.
if ( this.editorInstance ) {
// The lock mechanism prevents from calling `cvaOnChange()` during changing
// the editor state. See #139
this.isEditorSettingData = true;
this.editorInstance.setData( value );
this.isEditorSettingData = false;
}
// If not, wait for it to be ready; store the data.
else {
Expand Down Expand Up @@ -265,7 +274,7 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue

modelDocument.on( 'change:data', ( evt: CKEditor5.EventInfo<'change:data'> ) => {
this.ngZone.run( () => {
if ( this.cvaOnChange ) {
if ( this.cvaOnChange && !this.isEditorSettingData ) {
const data = editor.getData();

this.cvaOnChange( data );
Expand Down

0 comments on commit 4fd431e

Please sign in to comment.