tableediting.js 6.4 KB

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