Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the dataChangeEventDisabled property #143

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/demo-form/demo-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h3>User profile form</h3>
[(ngModel)]="model.description"
[editor]="Editor"
id="description"
[disableOnChange]="disabledOnChangeEvent"
name="description">
</ckeditor>

Expand Down
5 changes: 5 additions & 0 deletions src/app/demo-form/demo-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export class DemoFormComponent implements AfterViewInit {
@ViewChild( 'demoForm', { static: true } ) public demoForm?: NgForm;

public Editor = ClassicEditorBuild;

public model = {
name: 'John',
surname: 'Doe',
description: '<p>A <b>really</b> nice fellow.</p>'
};

public formDataPreview?: string;
public disabledOnChangeEvent: boolean = false;

public get description() {
return this.demoForm!.controls.description;
Expand All @@ -35,6 +37,9 @@ export class DemoFormComponent implements AfterViewInit {
}

public onSubmit() {
if ( this.disabledOnChangeEvent ) {
this.model.description = this.Editor.getData();
ma2ciek marked this conversation as resolved.
Show resolved Hide resolved
}
console.log( 'Form submit, model', this.model );
}

Expand Down
30 changes: 21 additions & 9 deletions src/ckeditor/ckeditor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
return this.initialIsDisabled;
}

/**
* True to disable on-change event which boosts performance with large texts, use Editor.getData() to get the latest data.
*/
@Input() public disableOnChange: boolean = false;

/**
* Fires when the editor is ready. It corresponds with the `editor#ready`
* https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html#event-ready
Expand Down Expand Up @@ -263,17 +268,18 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
const modelDocument = editor.model.document;
const viewDocument = editor.editing.view.document;

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

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

this.change.emit( { event: evt, editor } );
this.change.emit( { event: evt, editor } );
} );
} );
} );
}

viewDocument.on( 'focus', ( evt: CKEditor5.EventInfo<'focus'> ) => {
this.ngZone.run( () => {
Expand All @@ -287,6 +293,12 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue
this.cvaOnTouched();
}

// if we disable onChange event, then we have to push changes to ngModel when leaving the text editor
if ( this.disableOnChange && this.cvaOnChange ) {
const data = editor.getData();
this.cvaOnChange( data );
}

this.blur.emit( { event: evt, editor } );
} );
} );
Expand Down