boldengine.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/boldengine
  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 ToggleAttributeCommand from '@ckeditor/ckeditor5-core/src/command/toggleattributecommand';
  12. const BOLD = 'bold';
  13. /**
  14. * The bold engine feature.
  15. *
  16. * It registers the `bold` command and introduces the `bold` attribute in the model which renders to the view
  17. * as a `<strong>` element.
  18. *
  19. * @extends module:core/plugin~Plugin
  20. */
  21. export default class BoldEngine 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 bold attribute on all inline nodes.
  30. editor.document.schema.allow( { name: '$inline', attributes: [ BOLD ] } );
  31. // Build converter from model to view for data and editing pipelines.
  32. buildModelConverter().for( data.modelToView, editing.modelToView )
  33. .fromAttribute( BOLD )
  34. .toElement( 'strong' );
  35. // Build converter from view to model for data pipeline.
  36. buildViewConverter().for( data.viewToModel )
  37. .fromElement( 'strong' )
  38. .fromElement( 'b' )
  39. .fromAttribute( 'style', { 'font-weight': 'bold' } )
  40. .toAttribute( BOLD, true );
  41. // Create bold command.
  42. editor.commands.set( BOLD, new ToggleAttributeCommand( editor, BOLD ) );
  43. }
  44. }