headingbuttonsui.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 heading/headingbuttonsui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import { getLocalizedOptions } from './utils';
  11. import iconHeading1 from '../theme/icons/heading1.svg';
  12. import iconHeading2 from '../theme/icons/heading2.svg';
  13. import iconHeading3 from '../theme/icons/heading3.svg';
  14. import iconHeading4 from '../theme/icons/heading4.svg';
  15. import iconHeading5 from '../theme/icons/heading5.svg';
  16. import iconHeading6 from '../theme/icons/heading6.svg';
  17. const defaultIcons = {
  18. heading1: iconHeading1,
  19. heading2: iconHeading2,
  20. heading3: iconHeading3,
  21. heading4: iconHeading4,
  22. heading5: iconHeading5,
  23. heading6: iconHeading6
  24. };
  25. /**
  26. * The `HeadingButtonsUI` plugin defines a set of UI buttons that can be used instead of the
  27. * standard drop down component.
  28. *
  29. * This feature is not enabled by default by the {@link module:heading/heading~Heading} plugin and needs to be
  30. * installed manually to the editor configuration.
  31. *
  32. * Plugin introduces button UI elements, which names are same as `model` property from {@link module:heading/heading~HeadingOption}.
  33. *
  34. * ClassicEditor
  35. * .create( {
  36. * plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
  37. * heading: {
  38. * options: [
  39. * { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  40. * { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  41. * { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  42. * { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
  43. * ]
  44. * },
  45. * toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
  46. * } )
  47. * .then( ... )
  48. * .catch( ... );
  49. *
  50. * NOTE: The `'paragraph'` button is defined in by the {@link module:paragraph/paragraphbuttonui~ParagraphButtonUI} plugin
  51. * which needs to be loaded manually as well.
  52. *
  53. * It is possible to use custom icons by providing `icon` config option in {@link module:heading/heading~HeadingOption}.
  54. * For the default configuration standard icons are used.
  55. *
  56. * @extends module:core/plugin~Plugin
  57. */
  58. export default class HeadingButtonsUI extends Plugin {
  59. /**
  60. * @inheritDoc
  61. */
  62. init() {
  63. const options = getLocalizedOptions( this.editor );
  64. options
  65. .filter( item => item.model !== 'paragraph' )
  66. .map( item => this._createButton( item ) );
  67. }
  68. /**
  69. * Creates single button view from provided configuration option.
  70. *
  71. * @private
  72. * @param {Object} option
  73. */
  74. _createButton( option ) {
  75. const editor = this.editor;
  76. editor.ui.componentFactory.add( option.model, locale => {
  77. const view = new ButtonView( locale );
  78. const command = editor.commands.get( 'heading' );
  79. view.label = option.title;
  80. view.icon = option.icon || defaultIcons[ option.model ];
  81. view.tooltip = true;
  82. view.isToggleable = true;
  83. view.bind( 'isEnabled' ).to( command );
  84. view.bind( 'isOn' ).to( command, 'value', value => value == option.model );
  85. view.on( 'execute', () => {
  86. editor.execute( 'heading', { value: option.model } );
  87. editor.editing.view.focus();
  88. } );
  89. return view;
  90. } );
  91. }
  92. }