Getting and saving data

CKEditor 5 allows you to retrieve the data from and save it to your server (or to your system in general) in various ways. In this guide you can learn about the available options along with their pros and cons.

Automatic integration with HTML forms

This is the classic way of integrating the editor. It is typically used in simpler CMSes, forums, comment sections, etc.

This approach is only available in the Classic editor and only if the editor was used to replace a <textarea> element:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CKEditor 5 - Classic editor</title>
  6. <script src="https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js"></script>
  7. </head>
  8. <body>
  9. <h1>Classic editor</h1>
  10. <form action="[URL]" method="post">
  11. <textarea name="content" id="editor">
  12. &lt;p&gt;This is some sample content.&lt;/p&gt;
  13. </textarea>
  14. <p><input type="submit" value="Submit"></p>
  15. </form>
  16. <script>
  17. ClassicEditor
  18. .create( document.querySelector( '#editor' ) )
  19. .catch( error => {
  20. console.error( error );
  21. } );
  22. </script>
  23. </body>
  24. </html>

Classic editor will automatically update the value of the <textarea> element once the user submits the form. You do not need any additional JavaScript code to send the editor data to the server.

In your HTTP server, you can now read the editor data from the content variable of the POST request. For instance, in PHP, you can get it in this way:

  1. <?php
  2. $editor_data = $_POST[ 'content' ];
  3. ?>

Please note that the replaced <textarea> element is updated automatically by CKEditor straight before the submission. If you need to access the <textarea> value programatically with JavaScript (e.g. in the onsubmit handler to validate the entered data), there is a chance that the <textarea> element would still store the original data. In order to update the value of the replaced <textarea>, use the editor.updateSourceElement() method.

If you need to get the actual data from CKEditor at any moment using JavaScript, use the editor.getData() method as described in the next section.

When you print the data from the database to a <textarea> element in an HTML page, you need to encode it correctly. For instance, if you use PHP then a minimal solution would look like this:

  1. <?php
  2. $data = str_replace( '&', '&amp;', $data );
  3. ?>
  4. <textarea name=“content” id=“editor”><?= $data ?></textarea>

Thanks to that, the <textarea> will be printed out like this:

  1. <textarea>&lt;p>This is some sample content.&lt;/p></textarea>

Instead of being printed like this:

  1. <textarea><p>This is some sample content.</p></textarea>

While simple content like mentioned above does not itself require to be encoded, encoding the data will prevent losing text like “<” or “<img>”.

Manually retrieving the data

When you:

  • Use Ajax requests instead of the classic integration with HTML forms,
  • Implement a single-page application,
  • Use a different editor type than the Classic editor (and hence, cannot use the previous method), you can retrieve the data from the editor by using the editor.getData() method.

For that, you need to store the reference to the editor because — unlike in CKEditor 4 — there is no global CKEDITOR.instances property. You can do that in multiple ways, for example by assigning the editor to a variable defined outside the then()'s callback:

  1. let editor;
  2. ClassicEditor
  3. .create( document.querySelector( '#editor' ) )
  4. .then( newEditor => {
  5. editor = newEditor;
  6. } )
  7. .catch( error => {
  8. console.error( error );
  9. } );
  10. // Assuming there is a <button id="submit">Submit</button> in your application.
  11. document.querySelector( '#submit' ).addEventListener( 'click', () => {
  12. const editorData = editor.getData();
  13. // ...
  14. } );

Autosave feature

The Autosave feature allows you to automatically save the data (e.g. send it to the server) when needed (when the user changed the content).

This plugin is unavailable in any of the builds by default so you need to install it.

Assuming that you implemented a saveData() function that sends the data to your server and returns a promise which is resolved once the data is successfully saved, configuring the autosave feature is as simple as:

  1. ClassicEditor
  2. .create( document.querySelector( '#editor' ), {
  3. plugins: [
  4. Autosave,
  5. // ... other plugins
  6. ],
  7. autosave: {
  8. save( editor ) {
  9. return saveData( editor.getData() );
  10. }
  11. },
  12. // ... other configuration options
  13. } );

The autosave feature listens to the editor.model.document#change:data event, throttles it and executes the config.autosave.save() function.

It also listens to the native window#beforeunload event and blocks it in the following cases:

  • The data has not been saved yet (the save() function did not resolve its promise or it was not called yet due to throttling).
  • Or any of the editor features registered a “pending action” (e.g. that an image is being uploaded). This automatically secures you from the user leaving the page before the content is saved or some ongoing actions like image upload did not finish.

The minimum time period between two save actions can be configured using the config.waitingTime property to not overload the backend. 1 second is the default waiting time before the next save action if nothing has changed in the meantime after the editor data has changed.

  1. ClassicEditor
  2. .create( document.querySelector( '#editor' ), {
  3. autosave: {
  4. waitingTime: 5000, // in ms
  5. save( editor ) {}
  6. },
  7. // ... other configuration options
  8. } );

Demo

This demo shows a simple integration of the editor with a fake HTTP server (which needs 1000ms to save the content).

  1. ClassicEditor
  2. .create( document.querySelector( '#editor' ), {
  3. plugins: [
  4. Autosave,
  5. // ... other plugins
  6. ],
  7. autosave: {
  8. save( editor ) {
  9. return saveData( editor.getData() );
  10. }
  11. }
  12. } )
  13. .then( editor => {
  14. window.editor = editor;
  15. displayStatus( editor );
  16. } )
  17. .catch( err => {
  18. console.error( err.stack );
  19. } );
  20. // Save the data to a fake HTTP server (emulated here with a setTimeout()).
  21. function saveData( data ) {
  22. return new Promise( resolve => {
  23. setTimeout( () => {
  24. console.log( 'Saved', data );
  25. resolve();
  26. }, HTTP_SERVER_LAG );
  27. } );
  28. }
  29. // Update the "Status: Saving..." info.
  30. function displayStatus( editor ) {
  31. const pendingActions = editor.plugins.get( 'PendingActions' );
  32. const statusIndicator = document.querySelector( '#editor-status' );
  33. pendingActions.on( 'change:hasAny', ( evt, propertyName, newValue ) => {
  34. if ( newValue ) {
  35. statusIndicator.classList.add( 'busy' );
  36. } else {
  37. statusIndicator.classList.remove( 'busy' );
  38. }
  39. } );
  40. }

How to understand this demo:

  • The status indicator shows when the editor has some unsaved content or pending actions.
    • If you drop a big image into this editor, you will see that it is busy during the entire period when the image is being uploaded.
    • The editor is also busy when saving the content is in progress (the save()'s promise was not resolved).
  • The autosave feature has a throttling mechanism which groups frequent changes (e.g. typing) are grouped in batches.
  • The autosave itself does not check whether the data has really changed. It bases on changes in the model which, in special cases, may not be “visible” in the data. You can add such a check yourself if you would like to avoid sending the same data to the server twice in a row.
  • You will be asked whether you want to leave the page if an image is being uploaded or the data has not been saved successfully yet. You can test that by dropping a big image into the editor or changing the “HTTP server lag” to a high value (e.g. 9000ms) and typing something. These actions will make the editor “busy” for a longer time — try leaving the page then.

Type some text to test the autosave feature.

Status:

HTTP server lag (ms):

Server data:

  1. Type some text to test the <a href="#autosave-feature">autosave</a> feature.

Handling users exiting the page

An additional concern when integrating the editor in your website is that the user may mistakenly leave before saving the data. This problem is automatically handled by the autosave feature described above, but if you do not use it and instead chose different integration methods, you should consider handling these two scenarios:

  • The user leaves the page before saving the data (e.g. mistakenly closes a tab or clicks some link).
  • The user saved the data, but there are some pending actions like an image upload. To handle the former situation you can listen to the native window#beforeunload event. The latter situation can be handled by using CKEditor 5 PendingActions plugin.

Demo

The example below shows how all these mechanisms can be used together to enable or disable a “Save” button and block the user from leaving the page without saving the data.

The PendingActions plugin is unavailable in any of the builds by default so you need to install it.

  1. // Note: We need to build the editor from source.
  2. // We cannot use existing builds in this case.
  3. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  4. import PendingActions from '@ckeditor/ckeditor5-core/src/pendingactions';
  5. let isDirty = false;
  6. ClassicEditor
  7. .create( document.querySelector( '#editor' ), {
  8. plugins: [
  9. PendingActions,
  10. // ... other plugins
  11. ]
  12. } )
  13. .then( editor => {
  14. window.editor = editor;
  15. handleStatusChanges( editor );
  16. handleSaveButton( editor );
  17. handleBeforeunload( editor );
  18. } )
  19. .catch( err => {
  20. console.error( err.stack );
  21. } );
  22. // Handle clicking the "Save" button by sending the data to a
  23. // fake HTTP server (emulated here with setTimeout()).
  24. function handleSaveButton( editor ) {
  25. const saveButton = document.querySelector( '#save' );
  26. const pendingActions = editor.plugins.get( 'PendingActions' );
  27. saveButton.addEventListener( 'click', evt => {
  28. const data = editor.getData();
  29. // Register the action of saving the data as a "pending action".
  30. // All asynchronous actions related to the editor are tracked like this,
  31. // so later on you only need to check `pendingActions.hasAny` to check
  32. // whether the editor is busy or not.
  33. const action = pendingActions.add( 'Saving changes' );
  34. evt.preventDefault();
  35. // Save the data to a fake HTTP server.
  36. setTimeout( () => {
  37. pendingActions.remove( action );
  38. // Reset isDirty only if the data did not change in the meantime.
  39. if ( data == editor.getData() ) {
  40. isDirty = false;
  41. }
  42. updateStatus( editor );
  43. }, HTTP_SERVER_LAG );
  44. } );
  45. }
  46. // Listen to new changes (to enable the "Save" button) and to
  47. // pending actions (to show the spinner animation when the editor is busy).
  48. function handleStatusChanges( editor ) {
  49. editor.plugins.get( 'PendingActions' ).on( 'change:hasAny', () => updateStatus( editor ) );
  50. editor.model.document.on( 'change:data', () => {
  51. isDirty = true;
  52. updateStatus( editor );
  53. } );
  54. }
  55. // If the user tries to leave the page before the data is saved, ask
  56. // them whether they are sure they want to proceed.
  57. function handleBeforeunload( editor ) {
  58. const pendingActions = editor.plugins.get( 'PendingActions' );
  59. window.addEventListener( 'beforeunload', evt => {
  60. if ( pendingActions.hasAny ) {
  61. evt.preventDefault();
  62. }
  63. } );
  64. }
  65. function updateStatus( editor ) {
  66. const saveButton = document.querySelector( '#save' );
  67. // Disables the "Save" button when the data on the server is up to date.
  68. if ( isDirty ) {
  69. saveButton.classList.add( 'active' );
  70. } else {
  71. saveButton.classList.remove( 'active' );
  72. }
  73. // Shows the spinner animation.
  74. if ( editor.plugins.get( 'PendingActions' ).hasAny ) {
  75. saveButton.classList.add( 'saving' );
  76. } else {
  77. saveButton.classList.remove( 'saving' );
  78. }
  79. }

How to understand this demo:

  • The button changes to “Saving…” when the data is being sent to the server or there are any other pending actions (e.g. an image being uploaded).
  • You will be asked whether you want to leave the page if an image is being uploaded or the data has not been saved successfully yet. You can test that by dropping a big image into the editor or changing the “HTTP server lag” to a high value (e.g. 9000ms) and clicking the “Save” button. These actions will make the editor “busy” for a longer time — try leaving the page then.

Change the content of this editor, then save it on the server.

HTTP server lag (ms):

Server data:

  1. <p>Change the content of this editor, then save it on the server.</p>