tableediting.js 7.9 KB

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