boldediting.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module basic-styles/bold/boldediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import AttributeCommand from '../attributecommand';
  10. const BOLD = 'bold';
  11. /**
  12. * The bold editing feature.
  13. *
  14. * It registers the `'bold'` command and introduces the `bold` attribute in the model which renders to the view
  15. * as a `<strong>` element.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class BoldEditing extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. static get pluginName() {
  24. return 'BoldEditing';
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. init() {
  30. const editor = this.editor;
  31. // Allow bold attribute on text nodes.
  32. editor.model.schema.extend( '$text', { allowAttributes: BOLD } );
  33. editor.model.schema.setAttributeProperties( BOLD, {
  34. isFormatting: true,
  35. copyOnEnter: true
  36. } );
  37. // Build converter from model to view for data and editing pipelines.
  38. editor.conversion.attributeToElement( {
  39. model: BOLD,
  40. view: 'strong',
  41. upcastAlso: [
  42. 'b',
  43. viewElement => {
  44. const fontWeight = viewElement.getStyle( 'font-weight' );
  45. if ( !fontWeight ) {
  46. return null;
  47. }
  48. // Value of the `font-weight` attribute can be defined as a string or a number.
  49. if ( fontWeight == 'bold' || Number( fontWeight ) >= 600 ) {
  50. return {
  51. name: true,
  52. styles: [ 'font-weight' ]
  53. };
  54. }
  55. }
  56. ]
  57. } );
  58. // Create bold command.
  59. editor.commands.add( BOLD, new AttributeCommand( editor, BOLD ) );
  60. // Set the Ctrl+B keystroke.
  61. editor.keystrokes.set( 'CTRL+B', BOLD );
  62. }
  63. }