8
0

tableselection.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, CKEditorInspector */
  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. import TableClipboard from '../../src/tableclipboard';
  13. import TableProperties from '../../src/tableproperties';
  14. import TableCellProperties from '../../src/tablecellproperties';
  15. window.editors = {};
  16. createEditor( '#editor-content', 'content' );
  17. createEditor( '#editor-geometry', 'geometry' );
  18. function createEditor( target, inspectorName ) {
  19. ClassicEditor
  20. .create( document.querySelector( target ), {
  21. plugins: [ ArticlePluginSet, Table, TableToolbar, TableSelection, TableClipboard, TableProperties, TableCellProperties ],
  22. toolbar: [
  23. 'heading', '|',
  24. 'insertTable', '|',
  25. 'bold', 'italic', 'link', '|',
  26. 'bulletedList', 'numberedList', 'blockQuote', '|',
  27. 'undo', 'redo'
  28. ],
  29. image: {
  30. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  31. },
  32. table: {
  33. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells', 'tableProperties', 'tableCellProperties' ]
  34. }
  35. } )
  36. .then( editor => {
  37. window.editors[ inspectorName ] = editor;
  38. CKEditorInspector.attach( { [ inspectorName ]: editor } );
  39. editor.model.document.on( 'change', () => {
  40. printModelContents( editor );
  41. } );
  42. } )
  43. .catch( err => {
  44. console.error( err.stack );
  45. } );
  46. }
  47. const modelDiv = global.document.querySelector( '#model' );
  48. function printModelContents( editor ) {
  49. modelDiv.innerHTML = formatTable( getData( editor.model ) )
  50. .replace( /</g, '&lt;' )
  51. .replace( />/g, '&gt;' )
  52. .replace( /\n/g, '<br>' )
  53. .replace( /\[/g, '<span class="print-selected">[' )
  54. .replace( /]/g, ']</span>' );
  55. }
  56. function formatTable( tableString ) {
  57. return tableString
  58. .replace( /<table>/g, '\n<table' )
  59. .replace( /<table /g, '\n<table ' )
  60. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  61. .replace( /<thead>/g, '\n<thead>\n ' )
  62. .replace( /<tbody>/g, '\n<tbody>\n ' )
  63. .replace( /<tr>/g, '\n<tr>\n ' )
  64. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  65. .replace( /<\/thead>/g, '\n</thead>' )
  66. .replace( /<\/tbody>/g, '\n</tbody>' )
  67. .replace( /<\/tr>/g, '\n</tr>' )
  68. .replace( /<\/table>/g, '\n</table>' )
  69. .replace( /tableCell/g, 'cell' )
  70. .replace( /tableRow/g, 'row' )
  71. .replace( /paragraph/g, 'p' );
  72. }