tableediting.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 upcastTable from './converters/upcasttable';
  11. import { downcastInsertCell, downcastInsertRow, downcastInsertTable } from './converters/downcast';
  12. import InsertTableCommand from './inserttablecommand';
  13. import InsertRowCommand from './insertrowcommand';
  14. import InsertColumnCommand from './insertcolumncommand';
  15. /**
  16. * The table editing feature.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class TablesEditing extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. init() {
  25. const editor = this.editor;
  26. const schema = editor.model.schema;
  27. const conversion = editor.conversion;
  28. schema.register( 'table', {
  29. allowWhere: '$block',
  30. allowAttributes: [ 'headingRows', 'headingColumns' ],
  31. isBlock: true,
  32. isObject: true
  33. } );
  34. schema.register( 'tableRow', {
  35. allowIn: 'table',
  36. allowAttributes: [],
  37. isBlock: true,
  38. isLimit: true
  39. } );
  40. schema.register( 'tableCell', {
  41. allowIn: 'tableRow',
  42. allowContentOf: '$block',
  43. allowAttributes: [ 'colspan', 'rowspan' ],
  44. isBlock: true,
  45. isLimit: true
  46. } );
  47. // Table conversion.
  48. conversion.for( 'upcast' ).add( upcastTable() );
  49. conversion.for( 'downcast' ).add( downcastInsertTable() );
  50. // Insert conversion
  51. conversion.for( 'downcast' ).add( downcastInsertRow() );
  52. conversion.for( 'downcast' ).add( downcastInsertCell() );
  53. // Table cell conversion.
  54. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  55. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  56. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  57. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  58. editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
  59. editor.commands.add( 'insertRow', new InsertRowCommand( editor ) );
  60. editor.commands.add( 'insertColumn', new InsertColumnCommand( editor ) );
  61. }
  62. }