setdata.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. ClassicEditor
  9. .create( document.querySelector( '#editor' ), {
  10. plugins: [ ArticlePluginSet ],
  11. toolbar: [
  12. 'heading',
  13. '|',
  14. 'bold',
  15. 'italic',
  16. 'link',
  17. 'bulletedList',
  18. 'numberedList',
  19. '|',
  20. 'outdent',
  21. 'indent',
  22. '|',
  23. 'blockQuote',
  24. 'insertTable',
  25. 'mediaEmbed',
  26. 'undo',
  27. 'redo'
  28. ]
  29. } )
  30. .then( editor => {
  31. window.editor = editor;
  32. } )
  33. .catch( err => {
  34. console.error( err.stack );
  35. } );
  36. const buttons = document.querySelectorAll( '#test-controls button' );
  37. const fileNames = Array.from( buttons ).map( button => button.getAttribute( 'data-file-name' ) );
  38. preloadData( fileNames.map( name => `_utils/${ name }.txt` ) )
  39. .then( fixtures => {
  40. for ( const button of buttons ) {
  41. button.addEventListener( 'click', function() {
  42. const content = fixtures[ this.getAttribute( 'data-file-name' ) ];
  43. window.editor.setData( content );
  44. } );
  45. button.disabled = false;
  46. }
  47. } );
  48. function preloadData( urls ) {
  49. // @todo: simplify it - inline literals instead of looping over arrays.
  50. return Promise.all( urls.map( url => window.fetch( url ).then( resp => resp.text() ) ) )
  51. .then( responses => {
  52. return Object.fromEntries(
  53. Array.from( responses.keys() ).map( index => [ fileNames[ index ], responses[ index ] ] )
  54. );
  55. } );
  56. }