8
0

pendingactions.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console, window, document, setTimeout */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '../_utils/articlepluginset';
  8. import PendingActions from '../../src/pendingactions';
  9. ClassicEditor
  10. .create( document.querySelector( '#editor' ), {
  11. plugins: [ ArticlePluginSet, PendingActions ],
  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 pendingActions = editor.plugins.get( PendingActions );
  20. const actionsEl = document.querySelector( '.pending-actions' );
  21. document.querySelector( '#add-action' ).addEventListener( 'click', () => {
  22. const action = pendingActions.add( 'Pending action 0%.' );
  23. wait( 1000 )
  24. .then( () => ( action.message = 'Pending action 0%.' ) )
  25. .then( () => wait( 500 ) )
  26. .then( () => ( action.message = 'Pending action 20%.' ) )
  27. .then( () => wait( 500 ) )
  28. .then( () => ( action.message = 'Pending action 40%.' ) )
  29. .then( () => wait( 500 ) )
  30. .then( () => ( action.message = 'Pending action 60%.' ) )
  31. .then( () => wait( 500 ) )
  32. .then( () => ( action.message = 'Pending action 80%.' ) )
  33. .then( () => wait( 500 ) )
  34. .then( () => ( action.message = 'Pending action 100%.' ) )
  35. .then( () => wait( 500 ) )
  36. .then( () => pendingActions.remove( action ) );
  37. } );
  38. window.addEventListener( 'beforeunload', evt => {
  39. if ( pendingActions.hasAny ) {
  40. evt.returnValue = pendingActions.first.message;
  41. }
  42. } );
  43. pendingActions.on( 'add', () => displayActions() );
  44. pendingActions.on( 'remove', () => displayActions() );
  45. function displayActions() {
  46. const frag = document.createDocumentFragment();
  47. for ( const action of pendingActions ) {
  48. const item = document.createElement( 'li' );
  49. item.textContent = action.message;
  50. action.on( 'change:message', () => {
  51. item.textContent = action.message;
  52. } );
  53. frag.appendChild( item );
  54. }
  55. actionsEl.innerHTML = '';
  56. actionsEl.appendChild( frag );
  57. }
  58. function wait( ms ) {
  59. return new Promise( resolve => setTimeout( resolve, ms ) );
  60. }
  61. } )
  62. .catch( err => {
  63. console.error( err.stack );
  64. } );