setdata.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. image: {
  30. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  31. },
  32. table: {
  33. contentToolbar: [
  34. 'tableColumn',
  35. 'tableRow',
  36. 'mergeTableCells'
  37. ]
  38. }
  39. } )
  40. .then( editor => {
  41. window.editor = editor;
  42. } )
  43. .catch( err => {
  44. console.error( err.stack );
  45. } );
  46. const buttons = document.querySelectorAll( '#test-controls button' );
  47. const fileNames = Array.from( buttons ).map( button => button.getAttribute( 'data-file-name' ) );
  48. preloadData( fileNames.map( name => `_utils/${ name }.txt` ) )
  49. .then( fixtures => {
  50. console.log( 'fixtures' );
  51. console.log( fixtures );
  52. for ( const button of buttons ) {
  53. button.addEventListener( 'click', function() {
  54. const content = fixtures[ this.getAttribute( 'data-file-name' ) ];
  55. window.editor.setData( content );
  56. } );
  57. button.disabled = false;
  58. }
  59. } );
  60. function preloadData( urls ) {
  61. // @todo: simplify it - inline literals instead of looping over arrays.
  62. return Promise.all( urls.map( url => window.fetch( url ).then( resp => resp.text() ) ) )
  63. .then( responses => {
  64. return Object.fromEntries(
  65. Array.from( responses.keys() ).map( index => [ fileNames[ index ], responses[ index ] ] )
  66. );
  67. } );
  68. }