tableediting.js 8.5 KB

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