This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from ckeditor/t/ckeditor5/488
Other: Align feature class naming to a new scheme.
- Loading branch information
Showing
7 changed files
with
424 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* @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( 'headings', locale => { | ||
const commands = []; | ||
const dropdownItems = new Collection(); | ||
|
||
for ( const option of options ) { | ||
const command = editor.commands.get( option.model ); | ||
const itemModel = new Model( { | ||
commandName: option.model, | ||
label: option.title, | ||
class: option.class | ||
} ); | ||
|
||
itemModel.bind( 'isActive' ).to( command, 'value' ); | ||
|
||
// Add the option to the collection. | ||
dropdownItems.add( itemModel ); | ||
|
||
commands.push( command ); | ||
} | ||
|
||
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' ).toMany( commands, 'value', ( ...areActive ) => { | ||
const index = areActive.findIndex( value => value ); | ||
|
||
// If none of the commands is active, display default title. | ||
return options[ index ] ? options[ index ].title : 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; | ||
} ); | ||
} | ||
} |
Oops, something went wrong.