8
0

tableediting.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  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', ( evt, data ) => {
  68. const tabPressed = data.keyCode == keyCodes.tab;
  69. // Act only on TAB & SHIFT-TAB - Do not override native CTRL+TAB handler.
  70. if ( !tabPressed || data.ctrlKey ) {
  71. return;
  72. }
  73. const selection = editor.model.document.selection;
  74. const table = getParentTable( selection.getFirstPosition() );
  75. if ( !table ) {
  76. return;
  77. }
  78. data.preventDefault();
  79. data.stopPropagation();
  80. const tableCell = selection.focus.parent;
  81. const tableRow = tableCell.parent;
  82. const rowIndex = table.getChildIndex( tableRow );
  83. const cellIndex = tableRow.getChildIndex( tableCell );
  84. const isForward = !data.shiftKey;
  85. if ( !isForward && cellIndex === 0 && rowIndex === 0 ) {
  86. // It's the first cell of a table - don't do anything (stay in current position).
  87. return;
  88. }
  89. const indexOfLastCellInRow = tableRow.childCount - 1;
  90. if ( isForward && rowIndex === table.childCount - 1 && cellIndex === indexOfLastCellInRow ) {
  91. // It's a last table cell in a table - so create a new row at table's end.
  92. editor.execute( 'insertRow', { at: table.childCount } );
  93. }
  94. let moveToCell;
  95. if ( isForward && cellIndex === indexOfLastCellInRow ) {
  96. // Move to first cell in next row.
  97. const nextRow = table.getChild( rowIndex + 1 );
  98. moveToCell = nextRow.getChild( 0 );
  99. } else if ( !isForward && cellIndex === 0 ) {
  100. // Move to last cell in a previous row.
  101. const prevRow = table.getChild( rowIndex - 1 );
  102. moveToCell = prevRow.getChild( prevRow.childCount - 1 );
  103. } else {
  104. // Move to next/previous cell otherwise.
  105. moveToCell = tableRow.getChild( cellIndex + ( isForward ? 1 : -1 ) );
  106. }
  107. editor.model.change( writer => {
  108. writer.setSelection( Range.createIn( moveToCell ) );
  109. } );
  110. } );
  111. }
  112. }