8
0

tableediting.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 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 injectTableCellContentsPostFixer from './converters/table-cell-content-post-fixer';
  31. import injectTableCellPostFixer from './converters/tablecell-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. init() {
  43. const editor = this.editor;
  44. const model = editor.model;
  45. const schema = model.schema;
  46. const conversion = editor.conversion;
  47. schema.register( 'table', {
  48. allowWhere: '$block',
  49. allowAttributes: [ 'headingRows', 'headingColumns' ],
  50. isLimit: true,
  51. isObject: true,
  52. isBlock: true
  53. } );
  54. schema.register( 'tableRow', {
  55. allowIn: 'table',
  56. isLimit: true
  57. } );
  58. schema.register( 'tableCell', {
  59. allowIn: 'tableRow',
  60. allowAttributes: [ 'colspan', 'rowspan' ],
  61. isLimit: true
  62. } );
  63. // Allow all $block content inside table cell.
  64. schema.extend( '$block', { allowIn: 'tableCell' } );
  65. // Disallow table in table.
  66. schema.addChildCheck( ( context, childDefinition ) => {
  67. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  68. return false;
  69. }
  70. } );
  71. // Table conversion.
  72. conversion.for( 'upcast' ).add( upcastTable() );
  73. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  74. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  75. // Table row conversion.
  76. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  77. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  78. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  79. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  80. // Table cell conversion.
  81. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  82. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  83. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  84. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  85. // Table attributes conversion.
  86. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  87. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  88. // Table heading rows and cols conversion.
  89. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  90. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  91. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  92. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  93. injectTableCellPostFixer( editor.model, editor.editing );
  94. // Define all the commands.
  95. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  96. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  97. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  98. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  99. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  100. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  101. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  102. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  103. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  104. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  105. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  106. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  107. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  108. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  109. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  110. injectTableLayoutPostFixer( model );
  111. injectTableCellContentsPostFixer( model );
  112. // Handle tab key navigation.
  113. this.editor.keystrokes.set( 'Tab', ( ...args ) => this._handleTabOnSelectedTable( ...args ), { priority: 'low' } );
  114. this.editor.keystrokes.set( 'Tab', this._getTabHandler( true ), { priority: 'low' } );
  115. this.editor.keystrokes.set( 'Shift+Tab', this._getTabHandler( false ), { priority: 'low' } );
  116. }
  117. /**
  118. * @inheritDoc
  119. */
  120. static get requires() {
  121. return [ TableUtils ];
  122. }
  123. /**
  124. * Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  125. * when the table widget is selected.
  126. *
  127. * @private
  128. * @param {module:utils/eventinfo~EventInfo} eventInfo
  129. * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
  130. */
  131. _handleTabOnSelectedTable( domEventData, cancel ) {
  132. const editor = this.editor;
  133. const selection = editor.model.document.selection;
  134. if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
  135. const selectedElement = selection.getSelectedElement();
  136. if ( !selectedElement || !selectedElement.is( 'table' ) ) {
  137. return;
  138. }
  139. cancel();
  140. editor.model.change( writer => {
  141. writer.setSelection( writer.createRangeIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
  142. } );
  143. }
  144. }
  145. /**
  146. * Returns a handler for {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
  147. * inside table cell.
  148. *
  149. * @private
  150. * @param {Boolean} isForward Whether this handler will move selection to the next cell or previous.
  151. */
  152. _getTabHandler( isForward ) {
  153. const editor = this.editor;
  154. return ( domEventData, cancel ) => {
  155. const selection = editor.model.document.selection;
  156. const firstPosition = selection.getFirstPosition();
  157. const tableCell = findAncestor( 'tableCell', firstPosition );
  158. if ( !tableCell ) {
  159. return;
  160. }
  161. cancel();
  162. const tableRow = tableCell.parent;
  163. const table = tableRow.parent;
  164. const currentRowIndex = table.getChildIndex( tableRow );
  165. const currentCellIndex = tableRow.getChildIndex( tableCell );
  166. const isFirstCellInRow = currentCellIndex === 0;
  167. if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
  168. // It's the first cell of a table - don't do anything (stay in current position).
  169. return;
  170. }
  171. const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
  172. const isLastRow = currentRowIndex === table.childCount - 1;
  173. if ( isForward && isLastRow && isLastCellInRow ) {
  174. editor.plugins.get( 'TableUtils' ).insertRows( table, { at: table.childCount } );
  175. }
  176. let cellToFocus;
  177. // Move to first cell in next row.
  178. if ( isForward && isLastCellInRow ) {
  179. const nextRow = table.getChild( currentRowIndex + 1 );
  180. cellToFocus = nextRow.getChild( 0 );
  181. }
  182. // Move to last cell in a previous row.
  183. else if ( !isForward && isFirstCellInRow ) {
  184. const previousRow = table.getChild( currentRowIndex - 1 );
  185. cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
  186. }
  187. // Move to next/previous cell.
  188. else {
  189. cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
  190. }
  191. editor.model.change( writer => {
  192. writer.setSelection( writer.createRangeIn( cellToFocus ) );
  193. } );
  194. };
  195. }
  196. }