tableediting.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. } );
  53. schema.register( 'tableRow', {
  54. allowIn: 'table',
  55. isLimit: true
  56. } );
  57. schema.register( 'tableCell', {
  58. allowIn: 'tableRow',
  59. allowAttributes: [ 'colspan', 'rowspan' ],
  60. isLimit: true
  61. } );
  62. // Allow all $block content inside table cell.
  63. schema.extend( '$block', { allowIn: 'tableCell' } );
  64. // Disallow table in table.
  65. schema.addChildCheck( ( context, childDefinition ) => {
  66. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  67. return false;
  68. }
  69. } );
  70. // Disallow media in table cell.
  71. schema.addChildCheck( ( context, childDefinition ) => {
  72. if ( !Array.from( context.getNames() ).includes( 'table' ) ) {
  73. return;
  74. }
  75. if ( childDefinition.name == 'media' ) {
  76. return false;
  77. }
  78. } );
  79. // Table conversion.
  80. conversion.for( 'upcast' ).add( upcastTable() );
  81. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  82. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  83. // Table row conversion.
  84. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  85. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  86. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  87. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  88. // Table cell conversion.
  89. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  90. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  91. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  92. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  93. // Table attributes conversion.
  94. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  95. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  96. // Table heading rows and cols conversion.
  97. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  98. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  99. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  100. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  101. injectTableCellPostFixer( editor.model, editor.editing );
  102. // Define all the commands.
  103. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  104. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  105. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  106. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  107. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  108. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  109. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  110. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  111. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  112. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  113. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  114. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  115. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  116. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  117. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  118. injectTableLayoutPostFixer( model );
  119. injectTableCellContentsPostFixer( model );
  120. // Handle tab key navigation.
  121. this.editor.keystrokes.set( 'Tab', ( ...args ) => this._handleTabOnSelectedTable( ...args ), { priority: 'low' } );
  122. this.editor.keystrokes.set( 'Tab', this._getTabHandler( true ), { priority: 'low' } );
  123. this.editor.keystrokes.set( 'Shift+Tab', this._getTabHandler( false ), { priority: 'low' } );
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. static get requires() {
  129. return [ TableUtils ];
  130. }
  131. /**
  132. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  133. * when the table widget is selected.
  134. *
  135. * @private
  136. * @param {module:utils/eventinfo~EventInfo} eventInfo
  137. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  138. */
  139. _handleTabOnSelectedTable( domEventData, cancel ) {
  140. const editor = this.editor;
  141. const selection = editor.model.document.selection;
  142. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  143. const selectedElement = selection.getSelectedElement();
  144. if ( !selectedElement || !selectedElement.is( 'table' ) ) {
  145. return;
  146. }
  147. cancel();
  148. editor.model.change( writer => {
  149. writer.setSelection( writer.createRangeIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  150. } );
  151. }
  152. }
  153. /**
  154. * Returns a handler for {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  155. * inside table cell.
  156. *
  157. * @private
  158. * @param {Boolean} isForward Whether this handler will move selection to the next cell or previous.
  159. */
  160. _getTabHandler( isForward ) {
  161. const editor = this.editor;
  162. return ( domEventData, cancel ) => {
  163. const selection = editor.model.document.selection;
  164. const firstPosition = selection.getFirstPosition();
  165. const tableCell = findAncestor( 'tableCell', firstPosition );
  166. if ( !tableCell ) {
  167. return;
  168. }
  169. cancel();
  170. const tableRow = tableCell.parent;
  171. const table = tableRow.parent;
  172. const currentRowIndex = table.getChildIndex( tableRow );
  173. const currentCellIndex = tableRow.getChildIndex( tableCell );
  174. const isFirstCellInRow = currentCellIndex === 0;
  175. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  176. // It's the first cell of a table - don't do anything (stay in current position).
  177. return;
  178. }
  179. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  180. const isLastRow = currentRowIndex === table.childCount - 1;
  181. if ( isForward && isLastRow && isLastCellInRow ) {
  182. editor.plugins.get( 'TableUtils' ).insertRows( table, { at: table.childCount } );
  183. }
  184. let cellToFocus;
  185. // Move to first cell in next row.
  186. if ( isForward && isLastCellInRow ) {
  187. const nextRow = table.getChild( currentRowIndex + 1 );
  188. cellToFocus = nextRow.getChild( 0 );
  189. }
  190. // Move to last cell in a previous row.
  191. else if ( !isForward && isFirstCellInRow ) {
  192. const previousRow = table.getChild( currentRowIndex - 1 );
  193. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  194. }
  195. // Move to next/previous cell.
  196. else {
  197. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  198. }
  199. editor.model.change( writer => {
  200. writer.setSelection( writer.createRangeIn( cellToFocus ) );
  201. } );
  202. };
  203. }
  204. }