tableediting.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 './tableutils';
  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 schema = editor.model.schema;
  45. const conversion = editor.conversion;
  46. schema.register( 'table', {
  47. allowWhere: '$block',
  48. allowAttributes: [ 'headingRows', 'headingColumns' ],
  49. isLimit: true,
  50. isObject: true
  51. } );
  52. schema.register( 'tableRow', {
  53. allowIn: 'table',
  54. isLimit: true
  55. } );
  56. schema.register( 'tableCell', {
  57. allowIn: 'tableRow',
  58. allowContentOf: '$block',
  59. allowAttributes: [ 'colspan', 'rowspan' ],
  60. isLimit: true
  61. } );
  62. // Allow all $block content inside table cell.
  63. schema.extend( '$block', { allowIn: 'tableCell' } );
  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. // Handle tab key navigation.
  103. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabOnSelectedTable( ...args ) );
  104. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabInsideTable( ...args ) );
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. static get requires() {
  110. return [ TableUtils ];
  111. }
  112. /**
  113. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  114. * when the table widget is selected.
  115. *
  116. * @private
  117. * @param {module:utils/eventinfo~EventInfo} eventInfo
  118. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  119. */
  120. _handleTabOnSelectedTable( eventInfo, domEventData ) {
  121. const tabPressed = domEventData.keyCode == keyCodes.tab;
  122. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  123. if ( !tabPressed || domEventData.ctrlKey ) {
  124. return;
  125. }
  126. const editor = this.editor;
  127. const selection = editor.model.document.selection;
  128. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  129. const selectedElement = selection.getSelectedElement();
  130. if ( !selectedElement || selectedElement.name != 'table' ) {
  131. return;
  132. }
  133. eventInfo.stop();
  134. domEventData.preventDefault();
  135. domEventData.stopPropagation();
  136. editor.model.change( writer => {
  137. writer.setSelection( Range.createIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  138. } );
  139. }
  140. }
  141. /**
  142. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed inside table
  143. * cell.
  144. *
  145. * @private
  146. * @param {module:utils/eventinfo~EventInfo} eventInfo
  147. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  148. */
  149. _handleTabInsideTable( eventInfo, domEventData ) {
  150. const tabPressed = domEventData.keyCode == keyCodes.tab;
  151. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  152. if ( !tabPressed || domEventData.ctrlKey ) {
  153. return;
  154. }
  155. const editor = this.editor;
  156. const selection = editor.model.document.selection;
  157. const table = getParentTable( selection.getFirstPosition() );
  158. if ( !table ) {
  159. return;
  160. }
  161. domEventData.preventDefault();
  162. domEventData.stopPropagation();
  163. const tableCell = selection.focus.parent;
  164. const tableRow = tableCell.parent;
  165. const currentRowIndex = table.getChildIndex( tableRow );
  166. const currentCellIndex = tableRow.getChildIndex( tableCell );
  167. const isForward = !domEventData.shiftKey;
  168. const isFirstCellInRow = currentCellIndex === 0;
  169. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  170. // It's the first cell of a table - don't do anything (stay in current position).
  171. return;
  172. }
  173. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  174. const isLastRow = currentRowIndex === table.childCount - 1;
  175. if ( isForward && isLastRow && isLastCellInRow ) {
  176. editor.plugins.get( TableUtils ).insertRows( table, { at: table.childCount } );
  177. }
  178. let cellToFocus;
  179. // Move to first cell in next row.
  180. if ( isForward && isLastCellInRow ) {
  181. const nextRow = table.getChild( currentRowIndex + 1 );
  182. cellToFocus = nextRow.getChild( 0 );
  183. }
  184. // Move to last cell in a previous row.
  185. else if ( !isForward && isFirstCellInRow ) {
  186. const previousRow = table.getChild( currentRowIndex - 1 );
  187. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  188. }
  189. // Move to next/previous cell.
  190. else {
  191. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  192. }
  193. editor.model.change( writer => {
  194. writer.setSelection( Range.createIn( cellToFocus ) );
  195. } );
  196. }
  197. }