| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module heading/headingui
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import Model from '@ckeditor/ckeditor5-ui/src/model';
- import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
- import Collection from '@ckeditor/ckeditor5-utils/src/collection';
- import '../theme/heading.css';
- /**
- * The headings UI feature. It introduces the `headings` drop-down.
- *
- * @extends module:core/plugin~Plugin
- */
- export default class HeadingUI extends Plugin {
- /**
- * @inheritDoc
- */
- init() {
- const editor = this.editor;
- const t = editor.t;
- const options = this._getLocalizedOptions();
- const defaultTitle = t( 'Choose heading' );
- const dropdownTooltip = t( 'Heading' );
- // Register UI component.
- editor.ui.componentFactory.add( 'heading', locale => {
- const titles = {};
- const dropdownItems = new Collection();
- const headingCommand = editor.commands.get( 'heading' );
- const paragraphCommand = editor.commands.get( 'paragraph' );
- const commands = [ headingCommand ];
- for ( const option of options ) {
- const itemModel = new Model( {
- label: option.title,
- class: option.class
- } );
- if ( option.model === 'paragraph' ) {
- itemModel.bind( 'isActive' ).to( paragraphCommand, 'value' );
- itemModel.set( 'commandName', 'paragraph' );
- commands.push( paragraphCommand );
- } else {
- itemModel.bind( 'isActive' ).to( headingCommand, 'value', value => value === option.model );
- itemModel.set( {
- commandName: 'heading',
- commandValue: option.model
- } );
- }
- // Add the option to the collection.
- dropdownItems.add( itemModel );
- titles[ option.model ] = option.title;
- }
- const dropdownView = createDropdown( locale );
- addListToDropdown( dropdownView, dropdownItems );
- dropdownView.buttonView.set( {
- isOn: false,
- withText: true,
- tooltip: dropdownTooltip
- } );
- dropdownView.extendTemplate( {
- attributes: {
- class: [
- 'ck-heading-dropdown'
- ]
- }
- } );
- dropdownView.bind( 'isEnabled' ).toMany( commands, 'isEnabled', ( ...areEnabled ) => {
- return areEnabled.some( isEnabled => isEnabled );
- } );
- dropdownView.buttonView.bind( 'label' ).to( headingCommand, 'value', paragraphCommand, 'value', ( value, para ) => {
- const whichModel = value || para && 'paragraph';
- // If none of the commands is active, display default title.
- return titles[ whichModel ] ? titles[ whichModel ] : defaultTitle;
- } );
- // Execute command when an item from the dropdown is selected.
- this.listenTo( dropdownView, 'execute', evt => {
- editor.execute( evt.source.commandName );
- editor.editing.view.focus();
- } );
- return dropdownView;
- } );
- }
- /**
- * Returns heading options as defined in `config.heading.options` but processed to consider
- * editor localization, i.e. to display {@link module:heading/heading~HeadingOption}
- * in the correct language.
- *
- * Note: The reason behind this method is that there's no way to use {@link module:utils/locale~Locale#t}
- * when the user config is defined because the editor does not exist yet.
- *
- * @private
- * @returns {Array.<module:heading/heading~HeadingOption>}.
- */
- _getLocalizedOptions() {
- const editor = this.editor;
- const t = editor.t;
- const localizedTitles = {
- Paragraph: t( 'Paragraph' ),
- 'Heading 1': t( 'Heading 1' ),
- 'Heading 2': t( 'Heading 2' ),
- 'Heading 3': t( 'Heading 3' )
- };
- return editor.config.get( 'heading.options' ).map( option => {
- const title = localizedTitles[ option.title ];
- if ( title && title != option.title ) {
- // Clone the option to avoid altering the original `config.heading.options`.
- option = Object.assign( {}, option, { title } );
- }
- return option;
- } );
- }
- }
|