8
0

underlineengine.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module basic-styles/underlineengine
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  10. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  11. import AttributeCommand from './attributecommand';
  12. const UNDERLINE = 'underline';
  13. /**
  14. * The underline engine feature.
  15. *
  16. * It registers the `underline` command and introduces the `underline` attribute in the model which renders to the view
  17. * as an `<u>` element.
  18. *
  19. * @extends module:core/plugin~Plugin
  20. */
  21. export default class UnderlineEngine extends Plugin {
  22. /**
  23. * @inheritDoc
  24. */
  25. init() {
  26. const editor = this.editor;
  27. const data = editor.data;
  28. const editing = editor.editing;
  29. // Allow strikethrough attribute on text nodes.
  30. editor.model.schema.extend( '$text', { allowAttributes: UNDERLINE } );
  31. // Build converter from model to view for data and editing pipelines.
  32. buildModelConverter().for( data.modelToView, editing.modelToView )
  33. .fromAttribute( UNDERLINE )
  34. .toElement( 'u' );
  35. // Build converter from view to model for data pipeline.
  36. buildViewConverter().for( data.viewToModel )
  37. .fromElement( 'u' )
  38. .fromAttribute( 'style', { 'text-decoration': 'underline' } )
  39. .toAttribute( UNDERLINE, true );
  40. // Create underline command.
  41. editor.commands.add( UNDERLINE, new AttributeCommand( editor, UNDERLINE ) );
  42. }
  43. }