tableediting.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/tableediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import upcastTable, { upcastTableCell } from './converters/upcasttable';
  10. import {
  11. downcastInsertCell,
  12. downcastInsertRow,
  13. downcastInsertTable,
  14. downcastRemoveRow,
  15. downcastTableHeadingColumnsChange,
  16. downcastTableHeadingRowsChange
  17. } from './converters/downcast';
  18. import InsertTableCommand from './commands/inserttablecommand';
  19. import InsertRowCommand from './commands/insertrowcommand';
  20. import InsertColumnCommand from './commands/insertcolumncommand';
  21. import SplitCellCommand from './commands/splitcellcommand';
  22. import MergeCellCommand from './commands/mergecellcommand';
  23. import RemoveRowCommand from './commands/removerowcommand';
  24. import RemoveColumnCommand from './commands/removecolumncommand';
  25. import SetHeaderRowCommand from './commands/setheaderrowcommand';
  26. import SetHeaderColumnCommand from './commands/setheadercolumncommand';
  27. import { findAncestor } from './commands/utils';
  28. import TableUtils from '../src/tableutils';
  29. import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
  30. import injectTableCellParagraphPostFixer from './converters/table-cell-paragraph-post-fixer';
  31. import injectTableCellRefreshPostFixer from './converters/table-cell-refresh-post-fixer';
  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. static get pluginName() {
  43. return 'TableEditing';
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. init() {
  49. const editor = this.editor;
  50. const model = editor.model;
  51. const schema = model.schema;
  52. const conversion = editor.conversion;
  53. schema.register( 'table', {
  54. allowWhere: '$block',
  55. allowAttributes: [ 'headingRows', 'headingColumns' ],
  56. isLimit: true,
  57. isObject: true,
  58. isBlock: true
  59. } );
  60. schema.register( 'tableRow', {
  61. allowIn: 'table',
  62. isLimit: true
  63. } );
  64. schema.register( 'tableCell', {
  65. allowIn: 'tableRow',
  66. allowAttributes: [ 'colspan', 'rowspan' ],
  67. isObject: true
  68. } );
  69. // Allow all $block content inside table cell.
  70. schema.extend( '$block', { allowIn: 'tableCell' } );
  71. // Disallow table in table.
  72. schema.addChildCheck( ( context, childDefinition ) => {
  73. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  74. return false;
  75. }
  76. } );
  77. // Table conversion.
  78. conversion.for( 'upcast' ).add( upcastTable() );
  79. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  80. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  81. // Table row conversion.
  82. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  83. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  84. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  85. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  86. // Table cell conversion.
  87. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  88. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  89. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  90. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  91. // Table attributes conversion.
  92. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  93. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  94. // Table heading rows and columns conversion.
  95. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  96. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  97. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  98. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  99. // Define all the commands.
  100. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  101. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  102. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  103. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  104. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  105. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  106. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  107. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  108. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  109. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  110. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  111. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  112. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  113. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  114. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  115. injectTableLayoutPostFixer( model );
  116. injectTableCellRefreshPostFixer( model );
  117. injectTableCellParagraphPostFixer( model );
  118. // Handle Tab key navigation.
  119. this.editor.keystrokes.set( 'Tab', ( ...args ) => this._handleTabOnSelectedTable( ...args ), { priority: 'low' } );
  120. this.editor.keystrokes.set( 'Tab', this._getTabHandler( true ), { priority: 'low' } );
  121. this.editor.keystrokes.set( 'Shift+Tab', this._getTabHandler( false ), { priority: 'low' } );
  122. }
  123. /**
  124. * @inheritDoc
  125. */
  126. static get requires() {
  127. return [ TableUtils ];
  128. }
  129. /**
  130. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  131. * when the table widget is selected.
  132. *
  133. * @private
  134. * @param {module:utils/eventinfo~EventInfo} eventInfo
  135. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  136. */
  137. _handleTabOnSelectedTable( domEventData, cancel ) {
  138. const editor = this.editor;
  139. const selection = editor.model.document.selection;
  140. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  141. const selectedElement = selection.getSelectedElement();
  142. if ( !selectedElement || !selectedElement.is( 'table' ) ) {
  143. return;
  144. }
  145. cancel();
  146. editor.model.change( writer => {
  147. writer.setSelection( writer.createRangeIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  148. } );
  149. }
  150. }
  151. /**
  152. * Returns a handler for {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  153. * inside table cell.
  154. *
  155. * @private
  156. * @param {Boolean} isForward Whether this handler will move the selection to the next or the previous cell.
  157. */
  158. _getTabHandler( isForward ) {
  159. const editor = this.editor;
  160. return ( domEventData, cancel ) => {
  161. const selection = editor.model.document.selection;
  162. const firstPosition = selection.getFirstPosition();
  163. const tableCell = findAncestor( 'tableCell', firstPosition );
  164. if ( !tableCell ) {
  165. return;
  166. }
  167. cancel();
  168. const tableRow = tableCell.parent;
  169. const table = tableRow.parent;
  170. const currentRowIndex = table.getChildIndex( tableRow );
  171. const currentCellIndex = tableRow.getChildIndex( tableCell );
  172. const isFirstCellInRow = currentCellIndex === 0;
  173. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  174. // It's the first cell of the table - don't do anything (stay in the current position).
  175. return;
  176. }
  177. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  178. const isLastRow = currentRowIndex === table.childCount - 1;
  179. if ( isForward && isLastRow && isLastCellInRow ) {
  180. editor.execute( 'insertTableRowBelow' );
  181. // Check if the command actually added a row. If `insertTableRowBelow` execution didn't add a row (because it was disabled
  182. // or it got overwritten) do not change the selection.
  183. if ( currentRowIndex === table.childCount - 1 ) {
  184. return;
  185. }
  186. }
  187. let cellToFocus;
  188. // Move to first cell in next row.
  189. if ( isForward && isLastCellInRow ) {
  190. const nextRow = table.getChild( currentRowIndex + 1 );
  191. cellToFocus = nextRow.getChild( 0 );
  192. }
  193. // Move to last cell in a previous row.
  194. else if ( !isForward && isFirstCellInRow ) {
  195. const previousRow = table.getChild( currentRowIndex - 1 );
  196. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  197. }
  198. // Move to next/previous cell.
  199. else {
  200. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  201. }
  202. editor.model.change( writer => {
  203. writer.setSelection( writer.createRangeIn( cellToFocus ) );
  204. } );
  205. };
  206. }
  207. }