tableediting.js 6.3 KB

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