tableediting.js 9.2 KB

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