headingui.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module heading/headingui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import Model from '@ckeditor/ckeditor5-ui/src/model';
  10. import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  11. import { getLocalizedOptions } from './utils';
  12. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  13. import '../theme/heading.css';
  14. /**
  15. * The headings UI feature. It introduces the `headings` dropdown.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class HeadingUI extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. init() {
  24. const editor = this.editor;
  25. const t = editor.t;
  26. const options = getLocalizedOptions( editor );
  27. const defaultTitle = t( 'Choose heading' );
  28. const dropdownTooltip = t( 'Heading' );
  29. // Register UI component.
  30. editor.ui.componentFactory.add( 'heading', locale => {
  31. const titles = {};
  32. const itemDefinitions = new Collection();
  33. const headingCommand = editor.commands.get( 'heading' );
  34. const paragraphCommand = editor.commands.get( 'paragraph' );
  35. const commands = [ headingCommand ];
  36. for ( const option of options ) {
  37. const def = {
  38. type: 'button',
  39. model: new Model( {
  40. label: option.title,
  41. class: option.class,
  42. withText: true
  43. } )
  44. };
  45. if ( option.model === 'paragraph' ) {
  46. def.model.bind( 'isOn' ).to( paragraphCommand, 'value' );
  47. def.model.set( 'commandName', 'paragraph' );
  48. commands.push( paragraphCommand );
  49. } else {
  50. def.model.bind( 'isOn' ).to( headingCommand, 'value', value => value === option.model );
  51. def.model.set( {
  52. commandName: 'heading',
  53. commandValue: option.model
  54. } );
  55. }
  56. // Add the option to the collection.
  57. itemDefinitions.add( def );
  58. titles[ option.model ] = option.title;
  59. }
  60. const dropdownView = createDropdown( locale );
  61. addListToDropdown( dropdownView, itemDefinitions );
  62. dropdownView.buttonView.set( {
  63. isOn: false,
  64. withText: true,
  65. tooltip: dropdownTooltip
  66. } );
  67. dropdownView.extendTemplate( {
  68. attributes: {
  69. class: [
  70. 'ck-heading-dropdown'
  71. ]
  72. }
  73. } );
  74. dropdownView.bind( 'isEnabled' ).toMany( commands, 'isEnabled', ( ...areEnabled ) => {
  75. return areEnabled.some( isEnabled => isEnabled );
  76. } );
  77. dropdownView.buttonView.bind( 'label' ).to( headingCommand, 'value', paragraphCommand, 'value', ( value, para ) => {
  78. const whichModel = value || para && 'paragraph';
  79. // If none of the commands is active, display default title.
  80. return titles[ whichModel ] ? titles[ whichModel ] : defaultTitle;
  81. } );
  82. // Execute command when an item from the dropdown is selected.
  83. this.listenTo( dropdownView, 'execute', evt => {
  84. editor.execute( evt.source.commandName, evt.source.commandValue ? { value: evt.source.commandValue } : undefined );
  85. editor.editing.view.focus();
  86. } );
  87. return dropdownView;
  88. } );
  89. }
  90. }