tableediting.js 6.0 KB

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