italicengine.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 AttributeCommand from '../command/attributecommand.js';
  10. const ITALIC = 'italic';
  11. export default class ItalicEngine extends Feature {
  12. init() {
  13. const editor = this.editor;
  14. const data = editor.data;
  15. const editing = editor.editing;
  16. // Allow italic attribute on all inline nodes.
  17. editor.document.schema.allow( { name: '$inline', attributes: [ ITALIC ] } );
  18. // Build converter from model to view for data and editing pipelines.
  19. BuildModelConverterFor( data.modelToView, editing.modelToView )
  20. .fromAttribute( ITALIC )
  21. .toElement( 'em' );
  22. // Build converter from view to model for data pipeline.
  23. BuildViewConverterFor( data.viewToModel )
  24. .fromElement( 'em' )
  25. .fromElement( 'i' )
  26. .fromAttribute( 'style', { 'font-style': 'italic' } )
  27. .toAttribute( ITALIC, true );
  28. // Create italic command.
  29. editor.commands.set( ITALIC, new AttributeCommand( editor, ITALIC ) );
  30. }
  31. }