pendingactions.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'Static pending action.' );
  23. wait( 5000 ).then( () => pendingActions.remove( action ) );
  24. } );
  25. document.querySelector( '#add-action-progress' ).addEventListener( 'click', () => {
  26. const action = pendingActions.add( 'Dynamic pending action 0%.' );
  27. wait( 1000 )
  28. .then( () => ( action.message = 'Dynamic pending action 0%.' ) )
  29. .then( () => wait( 500 ) )
  30. .then( () => ( action.message = 'Dynamic pending action 20%.' ) )
  31. .then( () => wait( 500 ) )
  32. .then( () => ( action.message = 'Dynamic pending action 40%.' ) )
  33. .then( () => wait( 500 ) )
  34. .then( () => ( action.message = 'Dynamic pending action 60%.' ) )
  35. .then( () => wait( 500 ) )
  36. .then( () => ( action.message = 'Dynamic pending action 80%.' ) )
  37. .then( () => wait( 500 ) )
  38. .then( () => ( action.message = 'Dynamic pending action 1000%.' ) )
  39. .then( () => wait( 500 ) )
  40. .then( () => pendingActions.remove( action ) );
  41. } );
  42. pendingActions.on( 'add', () => displayActions() );
  43. pendingActions.on( 'remove', () => displayActions() );
  44. function displayActions() {
  45. const frag = document.createDocumentFragment();
  46. for ( const action of pendingActions ) {
  47. const item = document.createElement( 'li' );
  48. item.textContent = action.message;
  49. action.on( 'change:message', () => {
  50. item.textContent = action.message;
  51. } );
  52. frag.appendChild( item );
  53. }
  54. actionsEl.innerHTML = '';
  55. actionsEl.appendChild( frag );
  56. }
  57. function wait( ms ) {
  58. return new Promise( resolve => setTimeout( resolve, ms ) );
  59. }
  60. } )
  61. .catch( err => {
  62. console.error( err.stack );
  63. } );