tableediting.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/tableediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import upcastTable, { upcastTableCell } from './converters/upcasttable';
  10. import {
  11. downcastInsertCell,
  12. downcastInsertRow,
  13. downcastInsertTable,
  14. downcastRemoveRow,
  15. downcastTableHeadingColumnsChange,
  16. downcastTableHeadingRowsChange
  17. } from './converters/downcast';
  18. import InsertTableCommand from './commands/inserttablecommand';
  19. import InsertRowCommand from './commands/insertrowcommand';
  20. import InsertColumnCommand from './commands/insertcolumncommand';
  21. import SplitCellCommand from './commands/splitcellcommand';
  22. import MergeCellCommand from './commands/mergecellcommand';
  23. import RemoveRowCommand from './commands/removerowcommand';
  24. import RemoveColumnCommand from './commands/removecolumncommand';
  25. import SetHeaderRowCommand from './commands/setheaderrowcommand';
  26. import SetHeaderColumnCommand from './commands/setheadercolumncommand';
  27. import { findAncestor } from './commands/utils';
  28. import TableUtils from '../src/tableutils';
  29. import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
  30. import injectTableCellContentsPostFixer from './converters/table-cell-content-post-fixer';
  31. import injectTableCellPostFixer from './converters/tablecell-post-fixer';
  32. import '../theme/tableediting.css';
  33. /**
  34. * The table editing feature.
  35. *
  36. * @extends module:core/plugin~Plugin
  37. */
  38. export default class TableEditing extends Plugin {
  39. /**
  40. * @inheritDoc
  41. */
  42. init() {
  43. const editor = this.editor;
  44. const model = editor.model;
  45. const schema = model.schema;
  46. const conversion = editor.conversion;
  47. schema.register( 'table', {
  48. allowWhere: '$block',
  49. allowAttributes: [ 'headingRows', 'headingColumns' ],
  50. isLimit: true,
  51. isObject: true,
  52. isBlock: true
  53. } );
  54. schema.register( 'tableRow', {
  55. allowIn: 'table',
  56. isLimit: true
  57. } );
  58. schema.register( 'tableCell', {
  59. allowIn: 'tableRow',
  60. allowAttributes: [ 'colspan', 'rowspan' ],
  61. isLimit: true
  62. } );
  63. // Allow all $block content inside table cell.
  64. schema.extend( '$block', { allowIn: 'tableCell' } );
  65. // Disallow table in table.
  66. schema.addChildCheck( ( context, childDefinition ) => {
  67. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  68. return false;
  69. }
  70. } );
  71. // Disallow media in table cell.
  72. schema.addChildCheck( ( context, childDefinition ) => {
  73. if ( !Array.from( context.getNames() ).includes( 'table' ) ) {
  74. return;
  75. }
  76. if ( childDefinition.name == 'media' ) {
  77. return false;
  78. }
  79. } );
  80. // Table conversion.
  81. conversion.for( 'upcast' ).add( upcastTable() );
  82. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  83. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  84. // Table row conversion.
  85. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  86. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  87. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  88. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  89. // Table cell conversion.
  90. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  91. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  92. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  93. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  94. // Table attributes conversion.
  95. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  96. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  97. // Table heading rows and cols conversion.
  98. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  99. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  100. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  101. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  102. injectTableCellPostFixer( editor.model, editor.editing );
  103. // Define all the commands.
  104. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  105. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  106. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  107. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  108. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  109. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  110. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  111. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  112. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  113. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  114. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  115. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  116. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  117. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  118. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  119. injectTableLayoutPostFixer( model );
  120. injectTableCellContentsPostFixer( model );
  121. // Handle tab key navigation.
  122. this.editor.keystrokes.set( 'Tab', ( ...args ) => this._handleTabOnSelectedTable( ...args ), { priority: 'low' } );
  123. this.editor.keystrokes.set( 'Tab', this._getTabHandler( true ), { priority: 'low' } );
  124. this.editor.keystrokes.set( 'Shift+Tab', this._getTabHandler( false ), { priority: 'low' } );
  125. }
  126. /**
  127. * @inheritDoc
  128. */
  129. static get requires() {
  130. return [ TableUtils ];
  131. }
  132. /**
  133. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  134. * when the table widget is selected.
  135. *
  136. * @private
  137. * @param {module:utils/eventinfo~EventInfo} eventInfo
  138. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  139. */
  140. _handleTabOnSelectedTable( domEventData, cancel ) {
  141. const editor = this.editor;
  142. const selection = editor.model.document.selection;
  143. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  144. const selectedElement = selection.getSelectedElement();
  145. if ( !selectedElement || !selectedElement.is( 'table' ) ) {
  146. return;
  147. }
  148. cancel();
  149. editor.model.change( writer => {
  150. writer.setSelection( writer.createRangeIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  151. } );
  152. }
  153. }
  154. /**
  155. * Returns a handler for {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  156. * inside table cell.
  157. *
  158. * @private
  159. * @param {Boolean} isForward Whether this handler will move selection to the next cell or previous.
  160. */
  161. _getTabHandler( isForward ) {
  162. const editor = this.editor;
  163. return ( domEventData, cancel ) => {
  164. const selection = editor.model.document.selection;
  165. const firstPosition = selection.getFirstPosition();
  166. const tableCell = findAncestor( 'tableCell', firstPosition );
  167. if ( !tableCell ) {
  168. return;
  169. }
  170. cancel();
  171. const tableRow = tableCell.parent;
  172. const table = tableRow.parent;
  173. const currentRowIndex = table.getChildIndex( tableRow );
  174. const currentCellIndex = tableRow.getChildIndex( tableCell );
  175. const isFirstCellInRow = currentCellIndex === 0;
  176. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  177. // It's the first cell of a table - don't do anything (stay in current position).
  178. return;
  179. }
  180. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  181. const isLastRow = currentRowIndex === table.childCount - 1;
  182. if ( isForward && isLastRow && isLastCellInRow ) {
  183. editor.plugins.get( 'TableUtils' ).insertRows( table, { at: table.childCount } );
  184. }
  185. let cellToFocus;
  186. // Move to first cell in next row.
  187. if ( isForward && isLastCellInRow ) {
  188. const nextRow = table.getChild( currentRowIndex + 1 );
  189. cellToFocus = nextRow.getChild( 0 );
  190. }
  191. // Move to last cell in a previous row.
  192. else if ( !isForward && isFirstCellInRow ) {
  193. const previousRow = table.getChild( currentRowIndex - 1 );
  194. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  195. }
  196. // Move to next/previous cell.
  197. else {
  198. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  199. }
  200. editor.model.change( writer => {
  201. writer.setSelection( writer.createRangeIn( cellToFocus ) );
  202. } );
  203. };
  204. }
  205. }