tableediting.js 8.2 KB

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