autosave.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, window, console */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import Autosave from '../../src/autosave';
  9. ClassicEditor
  10. .create( document.querySelector( '#editor' ), {
  11. plugins: [ ArticlePluginSet, Autosave ],
  12. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo' ],
  13. image: {
  14. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ],
  15. }
  16. } )
  17. .then( editor => {
  18. window.editor = editor;
  19. const destroyButton = document.getElementById( 'destroy-editor-button' );
  20. destroyButton.addEventListener( 'click', () => editor.destroy() );
  21. const autosave = editor.plugins.get( Autosave );
  22. autosave.provider = {
  23. save() {
  24. const data = editor.getData();
  25. return saveEditorContentToDatabase( data );
  26. }
  27. };
  28. } );
  29. function saveEditorContentToDatabase( data ) {
  30. return new Promise( res => {
  31. window.setTimeout( () => {
  32. console.log( data );
  33. res();
  34. }, 1000 );
  35. } );
  36. }