tableediting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Position from '@ckeditor/ckeditor5-engine/src/model/position';
  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. if ( !tabPressed ) {
  70. return;
  71. }
  72. const doc = editor.model.document;
  73. const selection = doc.selection;
  74. const table = getParentTable( selection.getFirstPosition() );
  75. if ( !table ) {
  76. return;
  77. }
  78. const tableCell = selection.focus.parent;
  79. const tableRow = tableCell.parent;
  80. const rowIndex = table.getChildIndex( tableRow );
  81. data.preventDefault();
  82. data.stopPropagation();
  83. const rowChildrenCount = tableRow.childCount;
  84. const isLastTableCell = tableCell === tableRow.getChild( rowChildrenCount - 1 );
  85. if ( rowIndex === table.childCount - 1 && isLastTableCell ) {
  86. // It's a last table cell in a table - create row
  87. editor.execute( 'insertRow', { at: rowChildrenCount - 1 } );
  88. }
  89. // go to next cell
  90. const cellIndex = tableRow.getChildIndex( tableCell );
  91. let moveTo;
  92. if ( cellIndex === rowChildrenCount - 1 ) {
  93. const nextRow = table.getChild( rowIndex + 1 );
  94. moveTo = Position.createAt( nextRow.getChild( 0 ) );
  95. } else {
  96. moveTo = Position.createAt( tableRow.getChild( cellIndex + 1 ) );
  97. }
  98. editor.model.change( writer => {
  99. writer.setSelection( moveTo );
  100. } );
  101. } );
  102. }
  103. }