tableselection.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. table: {
  30. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells', 'tableProperties', 'tableCellProperties' ]
  31. }
  32. } )
  33. .then( editor => {
  34. window.editors[ inspectorName ] = editor;
  35. CKEditorInspector.attach( inspectorName, editor );
  36. editor.model.document.on( 'change', () => {
  37. printModelContents( editor );
  38. } );
  39. } )
  40. .catch( err => {
  41. console.error( err.stack );
  42. } );
  43. }
  44. const modelDiv = global.document.querySelector( '#model' );
  45. function printModelContents( editor ) {
  46. modelDiv.innerHTML = formatTable( getData( editor.model ) )
  47. .replace( /</g, '&lt;' )
  48. .replace( />/g, '&gt;' )
  49. .replace( /\n/g, '<br>' )
  50. .replace( /\[/g, '<span class="print-selected">[' )
  51. .replace( /]/g, ']</span>' );
  52. }
  53. function formatTable( tableString ) {
  54. return tableString
  55. .replace( /<table>/g, '\n<table' )
  56. .replace( /<table /g, '\n<table ' )
  57. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  58. .replace( /<thead>/g, '\n<thead>\n ' )
  59. .replace( /<tbody>/g, '\n<tbody>\n ' )
  60. .replace( /<tr>/g, '\n<tr>\n ' )
  61. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  62. .replace( /<\/thead>/g, '\n</thead>' )
  63. .replace( /<\/tbody>/g, '\n</tbody>' )
  64. .replace( /<\/tr>/g, '\n</tr>' )
  65. .replace( /<\/table>/g, '\n</table>' )
  66. .replace( /tableCell/g, 'cell' )
  67. .replace( /tableRow/g, 'row' )
  68. .replace( /paragraph/g, 'p' );
  69. }