table.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, global */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import Table from '../../src/table';
  9. import TableToolbar from '../../src/tabletoolbar';
  10. import TableSelection from '../../src/tableselection';
  11. import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. ClassicEditor
  13. .create( document.querySelector( '#editor' ), {
  14. plugins: [ ArticlePluginSet, Table, TableSelection, TableToolbar ],
  15. toolbar: [
  16. 'heading', '|', 'insertTable', '|', 'bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo'
  17. ],
  18. table: {
  19. toolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  20. }
  21. } )
  22. .then( editor => {
  23. window.editor = editor;
  24. editor.model.document.on( 'change', () => {
  25. printModelContents( editor );
  26. } );
  27. printModelContents( editor );
  28. } )
  29. .catch( err => {
  30. console.error( err.stack );
  31. } );
  32. const modelDiv = global.document.querySelector( '#model' );
  33. function printModelContents( editor ) {
  34. modelDiv.innerHTML = formatTable( getData( editor.model ) )
  35. .replace( /</g, '&lt;' )
  36. .replace( />/g, '&gt;' )
  37. .replace( /\n/g, '<br>' )
  38. .replace( /\[/g, '<strong>[' )
  39. .replace( /]/g, ']</strong>' );
  40. }
  41. function formatTable( tableString ) {
  42. return tableString
  43. .replace( /<table>/g, '\n<table>' )
  44. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  45. .replace( /<thead>/g, '\n<thead>\n ' )
  46. .replace( /<tbody>/g, '\n<tbody>\n ' )
  47. .replace( /<tr>/g, '\n<tr>\n ' )
  48. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  49. .replace( /<\/thead>/g, '\n</thead>' )
  50. .replace( /<\/tbody>/g, '\n</tbody>' )
  51. .replace( /<\/tr>/g, '\n</tr>' )
  52. .replace( /<\/table>/g, '\n</table>' );
  53. }