tablesediting.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module tables/tablesediting
  7. */
  8. import Plugin from '../../ckeditor5-core/src/plugin';
  9. import { upcastElementToElement } from '../../ckeditor5-engine/src/conversion/upcast-converters';
  10. import { createTableCell, createTableRow, downcastTableCell, downcastTable } from './converters';
  11. export default class TablesEditing extends Plugin {
  12. /**
  13. * @inheritDoc
  14. */
  15. init() {
  16. const editor = this.editor;
  17. const schema = editor.model.schema;
  18. const conversion = editor.conversion;
  19. schema.register( 'table', {
  20. allowWhere: '$block',
  21. allowAttributes: [],
  22. isBlock: true,
  23. isObject: true
  24. } );
  25. schema.register( 'tableRow', {
  26. allowIn: 'table',
  27. allowAttributes: [ 'isHeading', 'isFooter' ],
  28. isBlock: true,
  29. isLimit: true
  30. } );
  31. schema.register( 'tableCell', {
  32. allowIn: 'tableRow',
  33. allowContentOf: '$block',
  34. allowAttributes: [ 'isHeading', 'colspan', 'rowspan' ],
  35. isBlock: true,
  36. isLimit: true
  37. } );
  38. // Table conversion.
  39. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'table', view: 'table' } ) );
  40. conversion.for( 'downcast' ).add( downcastTable );
  41. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  42. conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableRow, view: 'tr' } ) );
  43. // Table cell conversion.
  44. conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableCell, view: 'td' } ) );
  45. conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableCell, view: 'th' } ) );
  46. conversion.for( 'downcast' ).add( downcastTableCell );
  47. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  48. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  49. }
  50. }