headingsengine.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Feature from '../feature.js';
  7. import BuildModelConverterFor from '../engine/conversion/model-converter-builder.js';
  8. import BuildViewConverterFor from '../engine/conversion/view-converter-builder.js';
  9. import Paragraph from '../paragraph/paragraph.js';
  10. import HeadingsCommand from './headingscommand.js';
  11. const formats = [
  12. { id: 'paragraph', viewElement: 'p', label: 'Paragraph' },
  13. { id: 'heading1', viewElement: 'h2', label: 'Heading 1' },
  14. { id: 'heading2', viewElement: 'h3', label: 'Heading 2' },
  15. { id: 'heading3', viewElement: 'h4', label: 'Heading 3' }
  16. ];
  17. export default class HeadingsEngine extends Feature {
  18. static get requires() {
  19. return [ Paragraph ];
  20. }
  21. init() {
  22. const editor = this.editor;
  23. const data = editor.data;
  24. const editing = editor.editing;
  25. for ( let format of formats ) {
  26. // Skip paragraph - it is defined in required Paragraph feature.
  27. if ( format.id !== 'paragraph' ) {
  28. // Schema.
  29. editor.document.schema.registerItem( format.id, '$block' );
  30. // Build converter from model to view for data and editing pipelines.
  31. BuildModelConverterFor( data.modelToView, editing.modelToView )
  32. .fromElement( format.id )
  33. .toElement( format.viewElement );
  34. // Build converter from view to model for data pipeline.
  35. BuildViewConverterFor( data.viewToModel )
  36. .fromElement( format.viewElement )
  37. .toElement( format.id );
  38. }
  39. }
  40. // Register command.
  41. const command = new HeadingsCommand( editor, formats );
  42. editor.commands.set( 'headings', command );
  43. }
  44. }