italicengine.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Feature from '../core/feature.js';
  6. import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
  7. import buildViewConverter from '../engine/conversion/buildviewconverter.js';
  8. import AttributeCommand from '../core/command/attributecommand.js';
  9. const ITALIC = 'italic';
  10. /**
  11. * The italic engine feature.
  12. *
  13. * It registers the `italic` command and introduces the `italic` attribute in the model which renders to the view
  14. * as an `<em>` element.
  15. *
  16. * @memberOf basic-styles
  17. * @extends core.Feature
  18. */
  19. export default class ItalicEngine extends Feature {
  20. /**
  21. * @inheritDoc
  22. */
  23. init() {
  24. const editor = this.editor;
  25. const data = editor.data;
  26. const editing = editor.editing;
  27. // Allow italic attribute on all inline nodes.
  28. editor.document.schema.allow( { name: '$inline', attributes: [ ITALIC ] } );
  29. // Build converter from model to view for data and editing pipelines.
  30. buildModelConverter().for( data.modelToView, editing.modelToView )
  31. .fromAttribute( ITALIC )
  32. .toElement( 'em' );
  33. // Build converter from view to model for data pipeline.
  34. buildViewConverter().for( data.viewToModel )
  35. .fromElement( 'em' )
  36. .fromElement( 'i' )
  37. .fromAttribute( 'style', { 'font-style': 'italic' } )
  38. .toAttribute( ITALIC, true );
  39. // Create italic command.
  40. editor.commands.set( ITALIC, new AttributeCommand( editor, ITALIC ) );
  41. }
  42. }