boldengine.js 1.4 KB

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