tableselection.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, 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. import TableSelection from '../../src/tableselection';
  12. ClassicEditor
  13. .create( document.querySelector( '#editor' ), {
  14. plugins: [ ArticlePluginSet, Table, TableToolbar, TableSelection ],
  15. toolbar: [
  16. 'heading', '|', 'insertTable', '|', 'bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo'
  17. ],
  18. table: {
  19. contentToolbar: [ '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, '<span class="print-selected">[' )
  39. .replace( /]/g, ']</span>' );
  40. }
  41. function formatTable( tableString ) {
  42. return tableString
  43. .replace( /<table>/g, '\n<table' )
  44. .replace( /<table /g, '\n<table ' )
  45. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  46. .replace( /<thead>/g, '\n<thead>\n ' )
  47. .replace( /<tbody>/g, '\n<tbody>\n ' )
  48. .replace( /<tr>/g, '\n<tr>\n ' )
  49. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  50. .replace( /<\/thead>/g, '\n</thead>' )
  51. .replace( /<\/tbody>/g, '\n</tbody>' )
  52. .replace( /<\/tr>/g, '\n</tr>' )
  53. .replace( /<\/table>/g, '\n</table>' )
  54. .replace( /tableCell/g, 'cell' )
  55. .replace( /tableRow/g, 'row' )
  56. .replace( /paragraph/g, 'p' );
  57. }