8
0

underlineengine.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 underline attribute on all inline nodes.
  30. editor.model.schema.allow( { name: '$inline', attributes: UNDERLINE, inside: '$block' } );
  31. // Temporary workaround. See https://github.com/ckeditor/ckeditor5/issues/477.
  32. editor.model.schema.allow( { name: '$inline', attributes: UNDERLINE, inside: '$clipboardHolder' } );
  33. // Build converter from model to view for data and editing pipelines.
  34. buildModelConverter().for( data.modelToView, editing.modelToView )
  35. .fromAttribute( UNDERLINE )
  36. .toElement( 'u' );
  37. // Build converter from view to model for data pipeline.
  38. buildViewConverter().for( data.viewToModel )
  39. .fromElement( 'u' )
  40. .fromAttribute( 'style', { 'text-decoration': 'underline' } )
  41. .toAttribute( UNDERLINE, true );
  42. // Create underline command.
  43. editor.commands.add( UNDERLINE, new AttributeCommand( editor, UNDERLINE ) );
  44. }
  45. }