formatsengine.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 FormatsCommand from './formatscommand.js';
  11. const formats = [
  12. { id: 'paragraph', viewElement: 'p', label: 'Paragraph', default: true },
  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 FormatsEngine extends Feature {
  18. static get requires() {
  19. return [ Paragraph ];
  20. }
  21. init() {
  22. const editor = this.editor;
  23. const document = editor.document;
  24. const schema = document.schema;
  25. const data = editor.data;
  26. const editing = editor.editing;
  27. for ( let format of formats ) {
  28. // Skip paragraph - it is defined in required Paragraph feature.
  29. if ( format.id !== 'paragraph' ) {
  30. // Schema.
  31. schema.registerItem( format.id, '$block' );
  32. // Build converter from model to view for data and editing pipelines.
  33. BuildModelConverterFor( data.modelToView, editing.modelToView )
  34. .fromElement( format.id )
  35. .toElement( format.viewElement );
  36. // Build converter from view to model for data pipeline.
  37. BuildViewConverterFor( data.viewToModel )
  38. .fromElement( format.viewElement )
  39. .toElement( format.id );
  40. }
  41. }
  42. // Register command.
  43. const command = new FormatsCommand( editor, formats );
  44. editor.commands.set( 'format', command );
  45. }
  46. }