/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /** * @module font/fontcommand */ import Command from '@ckeditor/ckeditor5-core/src/command'; /** * The base font command. * * @extends module:core/command~Command */ export default class FontCommand extends Command { /** * Creates an instance of the command. * * @param {module:core/editor/editor~Editor} editor Editor instance. * @param {String} attributeKey Name of an model attribute on which this command operates. */ constructor( editor, attributeKey ) { super( editor ); /** * If is set it means that selection has `attributeKey` set to that value. * * @observable * @readonly * @member {Boolean} module:font/fontcommand~FontCommand#value */ /** * A model attribute on which this command operates. * * @readonly * @member {Boolean} module:font/fontcommand~FontCommand#attributeKey */ this.attributeKey = attributeKey; } /** * @inheritDoc */ refresh() { const model = this.editor.model; const doc = model.document; this.value = doc.selection.getAttribute( this.attributeKey ); this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, this.attributeKey ); } /** * Executes the command. Applies the the attribute `value` to a selection. If no value is passed it removes attribute from selection. * * @protected * @param {Object} [options] Options for the executed command. * @param {String} [options.value] a value to apply. * * @fires execute */ execute( options = {} ) { const model = this.editor.model; const document = model.document; const selection = document.selection; const value = options.value; model.change( writer => { if ( selection.isCollapsed ) { if ( value ) { selection.setAttribute( this.attributeKey, value ); } else { selection.removeAttribute( this.attributeKey ); } } else { const ranges = model.schema.getValidRanges( selection.getRanges(), this.attributeKey ); for ( const range of ranges ) { if ( value ) { writer.setAttribute( this.attributeKey, value, range ); } else { writer.removeAttribute( this.attributeKey, range ); } } } } ); } }