|
|
@@ -8,7 +8,6 @@
|
|
|
*/
|
|
|
|
|
|
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
|
|
|
-import HeadingCommand from './headingcommand';
|
|
|
import HeadingEngine from './headingengine';
|
|
|
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
import Model from '@ckeditor/ckeditor5-ui/src/model';
|
|
|
@@ -34,21 +33,22 @@ export default class Heading extends Plugin {
|
|
|
*/
|
|
|
init() {
|
|
|
const editor = this.editor;
|
|
|
- const t = editor.t;
|
|
|
- const headingEngine = editor.plugins.get( HeadingEngine );
|
|
|
- const commands = headingEngine.commands;
|
|
|
const dropdownItems = new Collection();
|
|
|
- let defaultCommand;
|
|
|
+ const options = this._getLocalizedOptions();
|
|
|
+ const commands = [];
|
|
|
+ let defaultOption;
|
|
|
|
|
|
- for ( let command of commands ) {
|
|
|
+ for ( let option of options ) {
|
|
|
// Add the option to the collection.
|
|
|
dropdownItems.add( new Model( {
|
|
|
- modelElement: getCommandModelElement( command ),
|
|
|
- label: getCommandTitle( command, t )
|
|
|
+ modelElement: option.modelElement,
|
|
|
+ label: option.title
|
|
|
} ) );
|
|
|
|
|
|
- if ( !( command instanceof HeadingCommand ) ) {
|
|
|
- defaultCommand = command;
|
|
|
+ commands.push( editor.commands.get( option.modelElement ) );
|
|
|
+
|
|
|
+ if ( !defaultOption && option.modelElement == 'paragraph' ) {
|
|
|
+ defaultOption = option;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -73,7 +73,7 @@ export default class Heading extends Plugin {
|
|
|
const index = areActive.findIndex( value => value );
|
|
|
|
|
|
// If none of the commands is active, display the first one.
|
|
|
- return getCommandTitle( index > -1 ? commands.get( index ) : defaultCommand, t );
|
|
|
+ return ( options[ index ] || defaultOption ).title;
|
|
|
}
|
|
|
);
|
|
|
|
|
|
@@ -90,6 +90,45 @@ export default class Heading extends Plugin {
|
|
|
return dropdown;
|
|
|
} );
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns heading options as defined in `config.heading.options` but processed to consider
|
|
|
+ * editor localization, i.e. to display {@link module:heading/headingcommand~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/headingcommand~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 => {
|
|
|
+ let title;
|
|
|
+
|
|
|
+ if ( option.modelElement == 'paragraph' ) {
|
|
|
+ title = localizedTitles.Paragraph;
|
|
|
+ } else {
|
|
|
+ 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;
|
|
|
+ } );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Returns an array of binding components for
|
|
|
@@ -103,22 +142,3 @@ export default class Heading extends Plugin {
|
|
|
function getCommandsBindingTargets( commands, attribute ) {
|
|
|
return Array.prototype.concat( ...commands.map( c => [ c, attribute ] ) );
|
|
|
}
|
|
|
-
|
|
|
-// Returns the `modelElement` string for given command.
|
|
|
-//
|
|
|
-// @private
|
|
|
-// @param {module:core/command/command~Command} command
|
|
|
-// @returns {String}
|
|
|
-function getCommandModelElement( command ) {
|
|
|
- return command instanceof HeadingCommand ? command.modelElement : 'paragraph';
|
|
|
-}
|
|
|
-
|
|
|
-// Returns the `title` string for given command.
|
|
|
-//
|
|
|
-// @private
|
|
|
-// @param {module:core/command/command~Command} command
|
|
|
-// @param {module:utils/locale~Locale#t} t
|
|
|
-// @returns {String}
|
|
|
-function getCommandTitle( command, t ) {
|
|
|
- return command instanceof HeadingCommand ? command.title : t( 'Paragraph' );
|
|
|
-}
|