| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module heading/headingengine
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import Collection from '@ckeditor/ckeditor5-utils/src/collection';
- import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
- import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import HeadingCommand from './headingcommand';
- const defaultOptionId = 'paragraph';
- /**
- * The headings engine feature. It handles switching between block formats – headings and paragraph.
- * This class represents the engine part of the heading feature. See also {@link module:heading/heading~Heading}.
- *
- * @extends modules:core/plugin~Plugin
- */
- export default class HeadingEngine extends Plugin {
- /**
- * @inheritDoc
- */
- constructor( editor ) {
- super( editor );
- /**
- * A collection of heading commands associated with heading engine.
- *
- * @readonly
- * @member {module:utils/collection~Collection.<module:heading/headingcommand~HeadingCommand>}
- */
- this.commands = new Collection();
- editor.config.define( 'heading', {
- options: [
- { id: 'paragraph', element: 'p', label: 'Paragraph' },
- { id: 'heading1', element: 'h2', label: 'Heading 1' },
- { id: 'heading2', element: 'h3', label: 'Heading 2' },
- { id: 'heading3', element: 'h4', label: 'Heading 3' }
- ]
- } );
- }
- /**
- * @inheritDoc
- */
- static get requires() {
- return [ Paragraph ];
- }
- /**
- * @inheritDoc
- */
- init() {
- const editor = this.editor;
- const data = editor.data;
- const editing = editor.editing;
- const options = this._getLocalizedOptions();
- for ( let option of options ) {
- // Skip paragraph - it is defined in required Paragraph feature.
- if ( option.id !== defaultOptionId ) {
- // Schema.
- editor.document.schema.registerItem( option.id, '$block' );
- // Build converter from model to view for data and editing pipelines.
- buildModelConverter().for( data.modelToView, editing.modelToView )
- .fromElement( option.id )
- .toElement( option.element );
- // Build converter from view to model for data pipeline.
- buildViewConverter().for( data.viewToModel )
- .fromElement( option.element )
- .toElement( option.id );
- }
- // Register the heading command for this option.
- const command = new HeadingCommand( editor, option );
- this.commands.add( command );
- editor.commands.set( command.name, command );
- }
- }
- /**
- * @inheritDoc
- */
- afterInit() {
- // If the enter command is added to the editor, alter its behavior.
- // Enter at the end of a heading element should create a paragraph.
- const editor = this.editor;
- const enterCommand = editor.commands.get( 'enter' );
- const options = this._getLocalizedOptions();
- if ( enterCommand ) {
- this.listenTo( enterCommand, 'afterExecute', ( evt, data ) => {
- const positionParent = editor.document.selection.getFirstPosition().parent;
- const batch = data.batch;
- const isHeading = options.some( option => option.id == positionParent.name );
- if ( isHeading && positionParent.name != defaultOptionId && positionParent.childCount === 0 ) {
- batch.rename( positionParent, defaultOptionId );
- }
- } );
- }
- }
- /**
- * Returns heading options as defined in `config.heading.options` but processed to consider
- * editor localization, i.e. to display {@link module:heading/headingcommand~HeadingOption#label}
- * in the correct language.
- *
- * Note: The reason behind this method is that there's no way to use {@link 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() {
- if ( this._cachedLocalizedOptions ) {
- return this._cachedLocalizedOptions;
- }
- const editor = this.editor;
- const t = editor.t;
- const localizedLabels = {
- Paragraph: t( 'Paragraph' ),
- 'Heading 1': t( 'Heading 1' ),
- 'Heading 2': t( 'Heading 2' ),
- 'Heading 3': t( 'Heading 3' )
- };
- /**
- * Cached localized version of `config.heading.options` generated by
- * {@link module:heading/headingengine~HeadingEngine#_localizedOptions}.
- *
- * @private
- * @readonly
- * @member {Array.<module:heading/headingcommand~HeadingOption>} #_cachedLocalizedOptions
- */
- this._cachedLocalizedOptions = editor.config.get( 'heading.options' )
- .map( option => {
- if ( localizedLabels[ option.label ] ) {
- // Clone the option to avoid altering the original `config.heading.options`.
- option = Object.assign( {}, option, {
- label: localizedLabels[ option.label ]
- } );
- }
- return option;
- } );
- return this._cachedLocalizedOptions;
- }
- }
|