8
0

tableediting.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, skipEmptyTableRow } 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 MergeCellsCommand from './commands/mergecellscommand';
  28. import SelectRowCommand from './commands/selectrowcommand';
  29. import SelectColumnCommand from './commands/selectcolumncommand';
  30. import TableUtils from '../src/tableutils';
  31. import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
  32. import injectTableCellParagraphPostFixer from './converters/table-cell-paragraph-post-fixer';
  33. import injectTableCellRefreshPostFixer from './converters/table-cell-refresh-post-fixer';
  34. import '../theme/tableediting.css';
  35. /**
  36. * The table editing feature.
  37. *
  38. * @extends module:core/plugin~Plugin
  39. */
  40. export default class TableEditing extends Plugin {
  41. /**
  42. * @inheritDoc
  43. */
  44. static get pluginName() {
  45. return 'TableEditing';
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. init() {
  51. const editor = this.editor;
  52. const model = editor.model;
  53. const schema = model.schema;
  54. const conversion = editor.conversion;
  55. schema.register( 'table', {
  56. allowWhere: '$block',
  57. allowAttributes: [ 'headingRows', 'headingColumns' ],
  58. isLimit: true,
  59. isObject: true,
  60. isBlock: true
  61. } );
  62. schema.register( 'tableRow', {
  63. allowIn: 'table',
  64. isLimit: true
  65. } );
  66. schema.register( 'tableCell', {
  67. allowIn: 'tableRow',
  68. allowAttributes: [ 'colspan', 'rowspan' ],
  69. isObject: true
  70. } );
  71. // Allow all $block content inside table cell.
  72. schema.extend( '$block', { allowIn: 'tableCell' } );
  73. // Disallow table in table.
  74. schema.addChildCheck( ( context, childDefinition ) => {
  75. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  76. return false;
  77. }
  78. } );
  79. // Table conversion.
  80. conversion.for( 'upcast' ).add( upcastTable() );
  81. conversion.for( 'editingDowncast' ).add( downcastInsertTable( { asWidget: true } ) );
  82. conversion.for( 'dataDowncast' ).add( downcastInsertTable() );
  83. // Table row conversion.
  84. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  85. conversion.for( 'upcast' ).add( skipEmptyTableRow() );
  86. conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
  87. conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
  88. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  89. // Table cell conversion.
  90. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  91. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  92. conversion.for( 'editingDowncast' ).add( downcastInsertCell( { asWidget: true } ) );
  93. conversion.for( 'dataDowncast' ).add( downcastInsertCell() );
  94. // Table attributes conversion.
  95. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  96. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  97. // Table heading rows and columns conversion.
  98. conversion.for( 'editingDowncast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  99. conversion.for( 'dataDowncast' ).add( downcastTableHeadingColumnsChange() );
  100. conversion.for( 'editingDowncast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  101. conversion.for( 'dataDowncast' ).add( downcastTableHeadingRowsChange() );
  102. // Define all the commands.
  103. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  104. editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
  105. editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
  106. editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
  107. editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
  108. editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
  109. editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
  110. editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
  111. editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
  112. editor.commands.add( 'mergeTableCells', new MergeCellsCommand( editor ) );
  113. editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
  114. editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
  115. editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
  116. editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
  117. editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
  118. editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
  119. editor.commands.add( 'selectTableRow', new SelectRowCommand( editor ) );
  120. editor.commands.add( 'selectTableColumn', new SelectColumnCommand( editor ) );
  121. injectTableLayoutPostFixer( model );
  122. injectTableCellRefreshPostFixer( model );
  123. injectTableCellParagraphPostFixer( model );
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. static get requires() {
  129. return [ TableUtils ];
  130. }
  131. }