8
0

tableselection.js 1.8 KB

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