tableediting.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 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 { getParentTable } from './commands/utils';
  31. import TableUtils from '../src/tableutils';
  32. import injectTablePostFixer from './converters/table-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. allowContentOf: '$block',
  61. allowAttributes: [ 'colspan', 'rowspan' ],
  62. isLimit: true
  63. } );
  64. // Table conversion.
  65. conversion.for( 'upcast' ).add( upcastTable() );
  66. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  67. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  68. // Table row conversion.
  69. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  70. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  71. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  72. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  73. // Table cell conversion.
  74. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  75. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  76. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  77. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  78. // Table attributes conversion.
  79. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  80. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  81. // Table heading rows and cols conversion.
  82. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  83. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  84. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  85. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  86. // Define all the commands.
  87. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  88. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  89. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  90. editor.commands.add( 'insertTableColumnBefore', new InsertColumnCommand( editor, { order: 'before' } ) );
  91. editor.commands.add( 'insertTableColumnAfter', new InsertColumnCommand( editor, { order: 'after' } ) );
  92. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  93. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  94. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  95. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  96. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  97. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  98. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  99. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  100. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  101. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  102. injectTablePostFixer( model, this.editor.plugins.get( TableUtils ) );
  103. // Handle tab key navigation.
  104. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabOnSelectedTable( ...args ) );
  105. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabInsideTable( ...args ) );
  106. }
  107. /**
  108. * @inheritDoc
  109. */
  110. static get requires() {
  111. return [ TableUtils ];
  112. }
  113. /**
  114. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  115. * when the table widget is selected.
  116. *
  117. * @private
  118. * @param {module:utils/eventinfo~EventInfo} eventInfo
  119. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  120. */
  121. _handleTabOnSelectedTable( eventInfo, domEventData ) {
  122. const tabPressed = domEventData.keyCode == keyCodes.tab;
  123. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  124. if ( !tabPressed || domEventData.ctrlKey ) {
  125. return;
  126. }
  127. const editor = this.editor;
  128. const selection = editor.model.document.selection;
  129. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  130. const selectedElement = selection.getSelectedElement();
  131. if ( !selectedElement || selectedElement.name != 'table' ) {
  132. return;
  133. }
  134. eventInfo.stop();
  135. domEventData.preventDefault();
  136. domEventData.stopPropagation();
  137. editor.model.change( writer => {
  138. writer.setSelection( Range.createIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  139. } );
  140. }
  141. }
  142. /**
  143. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed inside table
  144. * cell.
  145. *
  146. * @private
  147. * @param {module:utils/eventinfo~EventInfo} eventInfo
  148. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  149. */
  150. _handleTabInsideTable( eventInfo, domEventData ) {
  151. const tabPressed = domEventData.keyCode == keyCodes.tab;
  152. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  153. if ( !tabPressed || domEventData.ctrlKey ) {
  154. return;
  155. }
  156. const editor = this.editor;
  157. const selection = editor.model.document.selection;
  158. const table = getParentTable( selection.getFirstPosition() );
  159. if ( !table ) {
  160. return;
  161. }
  162. domEventData.preventDefault();
  163. domEventData.stopPropagation();
  164. const tableCell = selection.focus.parent;
  165. const tableRow = tableCell.parent;
  166. const currentRowIndex = table.getChildIndex( tableRow );
  167. const currentCellIndex = tableRow.getChildIndex( tableCell );
  168. const isForward = !domEventData.shiftKey;
  169. const isFirstCellInRow = currentCellIndex === 0;
  170. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  171. // It's the first cell of a table - don't do anything (stay in current position).
  172. return;
  173. }
  174. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  175. const isLastRow = currentRowIndex === table.childCount - 1;
  176. if ( isForward && isLastRow && isLastCellInRow ) {
  177. editor.plugins.get( TableUtils ).insertRows( table, { at: table.childCount } );
  178. }
  179. let cellToFocus;
  180. // Move to first cell in next row.
  181. if ( isForward && isLastCellInRow ) {
  182. const nextRow = table.getChild( currentRowIndex + 1 );
  183. cellToFocus = nextRow.getChild( 0 );
  184. }
  185. // Move to last cell in a previous row.
  186. else if ( !isForward && isFirstCellInRow ) {
  187. const previousRow = table.getChild( currentRowIndex - 1 );
  188. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  189. }
  190. // Move to next/previous cell.
  191. else {
  192. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  193. }
  194. editor.model.change( writer => {
  195. writer.setSelection( Range.createIn( cellToFocus ) );
  196. } );
  197. }
  198. }