tableediting.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. downcastAttributeChange,
  15. downcastInsertCell,
  16. downcastInsertRow,
  17. downcastInsertTable,
  18. downcastRemoveRow
  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 SetTableHeadersCommand from './commands/settableheaderscommand';
  28. import { getParentTable } from './commands/utils';
  29. import './../theme/table.css';
  30. /**
  31. * The table editing feature.
  32. *
  33. * @extends module:core/plugin~Plugin
  34. */
  35. export default class TablesEditing extends Plugin {
  36. /**
  37. * @inheritDoc
  38. */
  39. init() {
  40. const editor = this.editor;
  41. const schema = editor.model.schema;
  42. const conversion = editor.conversion;
  43. schema.register( 'table', {
  44. allowWhere: '$block',
  45. allowAttributes: [ 'headingRows', 'headingColumns' ],
  46. isBlock: true,
  47. isObject: true
  48. } );
  49. schema.register( 'tableRow', {
  50. allowIn: 'table',
  51. allowAttributes: [],
  52. isBlock: true,
  53. isLimit: true
  54. } );
  55. schema.register( 'tableCell', {
  56. allowIn: 'tableRow',
  57. allowContentOf: '$block',
  58. allowAttributes: [ 'colspan', 'rowspan' ],
  59. isBlock: true,
  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. // Insert row conversion.
  67. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  68. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  69. // Remove row conversion.
  70. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  71. // Table cell conversion.
  72. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  73. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  74. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  75. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  76. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  77. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  78. conversion.for( 'editingDowncast' ).add( downcastAttributeChange( { attribute: 'headingRows', asWidget: true } ) );
  79. conversion.for( 'dataDowncast' ).add( downcastAttributeChange( { attribute: 'headingRows' } ) );
  80. conversion.for( 'editingDowncast' ).add( downcastAttributeChange( { attribute: 'headingColumns', asWidget: true } ) );
  81. conversion.for( 'dataDowncast' ).add( downcastAttributeChange( { attribute: 'headingColumns' } ) );
  82. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  83. editor.commands.add( 'insertRow', new InsertRowCommand( editor ) );
  84. editor.commands.add( 'insertColumn', new InsertColumnCommand( editor ) );
  85. editor.commands.add( 'splitCell', new SplitCellCommand( editor ) );
  86. editor.commands.add( 'removeRow', new RemoveRowCommand( editor ) );
  87. editor.commands.add( 'removeColumn', new RemoveColumnCommand( editor ) );
  88. editor.commands.add( 'mergeRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  89. editor.commands.add( 'mergeLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  90. editor.commands.add( 'mergeDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  91. editor.commands.add( 'mergeUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  92. editor.commands.add( 'setTableHeaders', new SetTableHeadersCommand( editor ) );
  93. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabOnSelectedTable( ...args ) );
  94. this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabInsideTable( ...args ) );
  95. }
  96. /**
  97. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for 'Tab' key executed
  98. * when table widget is selected.
  99. *
  100. * @private
  101. * @param {module:utils/eventinfo~EventInfo} eventInfo
  102. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  103. */
  104. _handleTabOnSelectedTable( eventInfo, domEventData ) {
  105. const tabPressed = domEventData.keyCode == keyCodes.tab;
  106. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  107. if ( !tabPressed || domEventData.ctrlKey ) {
  108. return;
  109. }
  110. const editor = this.editor;
  111. const selection = editor.model.document.selection;
  112. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  113. const selectedElement = selection.getSelectedElement();
  114. if ( !selectedElement || selectedElement.name != 'table' ) {
  115. return;
  116. }
  117. eventInfo.stop();
  118. domEventData.preventDefault();
  119. domEventData.stopPropagation();
  120. editor.model.change( writer => {
  121. writer.setSelection( Range.createIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  122. } );
  123. }
  124. }
  125. /**
  126. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for 'Tab' key executed inside table cell.
  127. *
  128. * @private
  129. * @param {module:utils/eventinfo~EventInfo} eventInfo
  130. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  131. */
  132. _handleTabInsideTable( eventInfo, domEventData ) {
  133. const tabPressed = domEventData.keyCode == keyCodes.tab;
  134. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  135. if ( !tabPressed || domEventData.ctrlKey ) {
  136. return;
  137. }
  138. const editor = this.editor;
  139. const selection = editor.model.document.selection;
  140. const table = getParentTable( selection.getFirstPosition() );
  141. if ( !table ) {
  142. return;
  143. }
  144. domEventData.preventDefault();
  145. domEventData.stopPropagation();
  146. const tableCell = selection.focus.parent;
  147. const tableRow = tableCell.parent;
  148. const currentRow = table.getChildIndex( tableRow );
  149. const currentCellIndex = tableRow.getChildIndex( tableCell );
  150. const isForward = !domEventData.shiftKey;
  151. const isFirstCellInRow = currentCellIndex === 0;
  152. if ( !isForward && isFirstCellInRow && currentRow === 0 ) {
  153. // It's the first cell of a table - don't do anything (stay in current position).
  154. return;
  155. }
  156. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  157. const isLastRow = currentRow === table.childCount - 1;
  158. if ( isForward && isLastRow && isLastCellInRow ) {
  159. editor.execute( 'insertRow', { at: table.childCount } );
  160. }
  161. let moveToCell;
  162. // Move to first cell in next row.
  163. if ( isForward && isLastCellInRow ) {
  164. const nextRow = table.getChild( currentRow + 1 );
  165. moveToCell = nextRow.getChild( 0 );
  166. }
  167. // Move to last cell in a previous row.
  168. else if ( !isForward && isFirstCellInRow ) {
  169. const previousRow = table.getChild( currentRow - 1 );
  170. moveToCell = previousRow.getChild( previousRow.childCount - 1 );
  171. }
  172. // Move to next/previous cell.
  173. else {
  174. moveToCell = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  175. }
  176. editor.model.change( writer => {
  177. writer.setSelection( Range.createIn( moveToCell ) );
  178. } );
  179. }
  180. }