tableediting.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /**
  6. * @module table/tableediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import upcastTable, { ensureParagraphInTableCell, skipEmptyTableRow } from './converters/upcasttable';
  10. import {
  11. downcastInsertCell,
  12. downcastInsertRow,
  13. downcastInsertTable,
  14. downcastRemoveRow,
  15. downcastTableHeadingColumnsChange
  16. } from './converters/downcast';
  17. import InsertTableCommand from './commands/inserttablecommand';
  18. import InsertRowCommand from './commands/insertrowcommand';
  19. import InsertColumnCommand from './commands/insertcolumncommand';
  20. import SplitCellCommand from './commands/splitcellcommand';
  21. import MergeCellCommand from './commands/mergecellcommand';
  22. import RemoveRowCommand from './commands/removerowcommand';
  23. import RemoveColumnCommand from './commands/removecolumncommand';
  24. import SetHeaderRowCommand from './commands/setheaderrowcommand';
  25. import SetHeaderColumnCommand from './commands/setheadercolumncommand';
  26. import MergeCellsCommand from './commands/mergecellscommand';
  27. import SelectRowCommand from './commands/selectrowcommand';
  28. import SelectColumnCommand from './commands/selectcolumncommand';
  29. import TableUtils from '../src/tableutils';
  30. import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
  31. import injectTableCellParagraphPostFixer from './converters/table-cell-paragraph-post-fixer';
  32. import injectTableCellRefreshPostFixer from './converters/table-cell-refresh-post-fixer';
  33. import '../theme/tableediting.css';
  34. /**
  35. * The table editing feature.
  36. *
  37. * @extends module:core/plugin~Plugin
  38. */
  39. export default class TableEditing extends Plugin {
  40. /**
  41. * @inheritDoc
  42. */
  43. static get pluginName() {
  44. return 'TableEditing';
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. init() {
  50. const editor = this.editor;
  51. const model = editor.model;
  52. const schema = model.schema;
  53. const conversion = editor.conversion;
  54. schema.register( 'table', {
  55. allowWhere: '$block',
  56. allowAttributes: [ 'headingRows', 'headingColumns' ],
  57. isObject: true,
  58. isBlock: true
  59. } );
  60. schema.register( 'tableRow', {
  61. allowIn: 'table',
  62. isLimit: true
  63. } );
  64. schema.register( 'tableCell', {
  65. allowIn: 'tableRow',
  66. allowAttributes: [ 'colspan', 'rowspan' ],
  67. isLimit: true,
  68. isSelectable: true
  69. } );
  70. // Allow all $block content inside table cell.
  71. schema.extend( '$block', { allowIn: 'tableCell' } );
  72. // Disallow table in table.
  73. schema.addChildCheck( ( context, childDefinition ) => {
  74. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  75. return false;
  76. }
  77. } );
  78. // Table conversion.
  79. conversion.for( 'upcast' ).add( upcastTable() );
  80. conversion.for( 'editingDowncast' ).elementToElement( {
  81. model: 'table',
  82. view: downcastInsertTable( { asWidget: true } ),
  83. triggerBy: [
  84. 'attribute:headingRows:table'
  85. ]
  86. } );
  87. conversion.for( 'dataDowncast' ).elementToElement( {
  88. model: 'table',
  89. view: downcastInsertTable()
  90. } );
  91. // Table row conversion.
  92. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  93. conversion.for( 'upcast' ).add( skipEmptyTableRow() );
  94. conversion.for( 'editingDowncast' ).add( downcastInsertRow() );
  95. conversion.for( 'editingDowncast' ).add( downcastRemoveRow() );
  96. // Table cell conversion.
  97. conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'td' } );
  98. conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'th' } );
  99. conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'td' ) );
  100. conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'th' ) );
  101. conversion.for( 'editingDowncast' ).add( downcastInsertCell() );
  102. // Table attributes conversion.
  103. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  104. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  105. // Table heading columns conversion (change of heading rows requires reconversion of the whole table).
  106. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange() );
  107. // Define all the commands.
  108. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  109. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  110. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  111. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  112. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  113. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  114. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  115. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  116. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  117. editor.commands.add( 'mergeTableCells', new MergeCellsCommand( editor ) );
  118. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  119. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  120. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  121. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  122. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  123. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  124. editor.commands.add( 'selectTableRow', new SelectRowCommand( editor ) );
  125. editor.commands.add( 'selectTableColumn', new SelectColumnCommand( editor ) );
  126. // injectTableHeadingRowsRefreshPostFixer( model );
  127. injectTableLayoutPostFixer( model );
  128. injectTableCellRefreshPostFixer( model );
  129. injectTableCellParagraphPostFixer( model );
  130. }
  131. /**
  132. * @inheritDoc
  133. */
  134. static get requires() {
  135. return [ TableUtils ];
  136. }
  137. }