|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @module remove-format/removeformatcommand |
| 8 | + */ |
| 9 | + |
| 10 | +import Command from '@ckeditor/ckeditor5-core/src/command'; |
| 11 | +import first from '@ckeditor/ckeditor5-utils/src/first'; |
| 12 | + |
| 13 | +/** |
| 14 | + * The remove format command. |
| 15 | + * |
| 16 | + * It is used by the {@link module:remove-format/removeformat~RemoveFormat remove format feature} |
| 17 | + * to clear the formatting in the selection. |
| 18 | + * |
| 19 | + * editor.execute( 'removeFormat' ); |
| 20 | + * |
| 21 | + * @extends module:core/command~Command |
| 22 | + */ |
| 23 | +export default class RemoveFormatCommand extends Command { |
| 24 | + /** |
| 25 | + * @inheritDoc |
| 26 | + */ |
| 27 | + refresh() { |
| 28 | + const model = this.editor.model; |
| 29 | + |
| 30 | + this.isEnabled = !!first( this._getFormattingItems( model.document.selection, model.schema ) ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritDoc |
| 35 | + */ |
| 36 | + execute() { |
| 37 | + const model = this.editor.model; |
| 38 | + const schema = model.schema; |
| 39 | + |
| 40 | + model.change( writer => { |
| 41 | + for ( const item of this._getFormattingItems( model.document.selection, schema ) ) { |
| 42 | + if ( item.is( 'selection' ) ) { |
| 43 | + for ( const attributeName of this._getFormattingAttributes( item, schema ) ) { |
| 44 | + writer.removeSelectionAttribute( attributeName ); |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Workaround for items with multiple removable attributes. See |
| 48 | + // https://github.com/ckeditor/ckeditor5-remove-format/pull/1#pullrequestreview-220515609 |
| 49 | + const itemRange = writer.createRangeOn( item ); |
| 50 | + |
| 51 | + for ( const attributeName of this._getFormattingAttributes( item, schema ) ) { |
| 52 | + writer.removeAttribute( attributeName, itemRange ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns an iterable of items in a selection (including selection itself) that have formatting model |
| 61 | + * attributes to be removed by the feature. |
| 62 | + * |
| 63 | + * @protected |
| 64 | + * @param {module:engine/model/documentselection~DocumentSelection} selection |
| 65 | + * @param {module:engine/model/schema~Schema} schema Schema describing the item. |
| 66 | + * @returns {Iterable.<module:engine/model/item~Item>|Iterable.<module:engine/model/documentselection~DocumentSelection>} |
| 67 | + */ |
| 68 | + * _getFormattingItems( selection, schema ) { |
| 69 | + const itemHasRemovableFormatting = item => { |
| 70 | + return !!first( this._getFormattingAttributes( item, schema ) ); |
| 71 | + }; |
| 72 | + |
| 73 | + for ( const curRange of selection.getRanges() ) { |
| 74 | + for ( const item of curRange.getItems() ) { |
| 75 | + if ( itemHasRemovableFormatting( item ) ) { |
| 76 | + yield item; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Finally the selection might be formatted as well, so make sure to check it. |
| 82 | + if ( itemHasRemovableFormatting( selection ) ) { |
| 83 | + yield selection; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns an iterable of formatting attributes of a given model item. |
| 89 | + * |
| 90 | + * **Note:** Formatting items have the `isFormatting` property set `true`. |
| 91 | + * |
| 92 | + * @protected |
| 93 | + * @param {module:engine/model/item~Item|module:engine/model/documentselection~DocumentSelection} item |
| 94 | + * @param {module:engine/model/schema~Schema} schema Schema describing the item. |
| 95 | + * @returns {Iterable.<String>} Names of formatting attributes found in a given item. |
| 96 | + */ |
| 97 | + * _getFormattingAttributes( item, schema ) { |
| 98 | + for ( const [ attributeName ] of item.getAttributes() ) { |
| 99 | + const attributeProperties = schema.getAttributeProperties( attributeName ); |
| 100 | + |
| 101 | + if ( attributeProperties && attributeProperties.isFormatting ) { |
| 102 | + yield attributeName; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments