tableediting.js 8.8 KB

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