소스 검색

Changed: Extract FontCommand.

Maciej Gołaszewski 8 년 전
부모
커밋
1e0cfdce6f

+ 81 - 0
packages/ckeditor5-font/src/fontcommand.js

@@ -0,0 +1,81 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module font/fontfamilycommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+/**
+ * The font family command. It is used by the {@link module:font/fontfamily/fontfamilyediting~FontFamilyEditing}
+ * to apply font family.
+ *
+ * TODO: those commands are duplicated here and there - maybe make them one?
+ *
+ * @extends module:core/command~Command
+ */
+export default class FontCommand extends Command {
+	/**
+	 * TODO: docs me
+	 * @param editor
+	 * @param attribute
+	 */
+	constructor( editor, attribute ) {
+		super( editor );
+
+		this.attribute = attribute;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const doc = model.document;
+
+		/**
+		 * A flag indicating whether the command is active, which means that the selection has fontFamily attribute set.
+		 *
+		 * @observable
+		 * @readonly
+		 * @member {Boolean} module:font/fontfamilycommand~FontFamilyCommand#value
+		 */
+		this.value = doc.selection.getAttribute( this.attribute );
+		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, this.attribute );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @protected
+	 * @param {Object} [options] Options for the executed command.
+	 * @param {String} [options.value] a value to apply.
+	 */
+	execute( options = {} ) {
+		const model = this.editor.model;
+		const document = model.document;
+		const selection = document.selection;
+
+		// Do not apply value on collapsed selection.
+		if ( selection.isCollapsed ) {
+			return;
+		}
+
+		const value = options.value;
+
+		model.change( writer => {
+			const ranges = model.schema.getValidRanges( selection.getRanges(), this.attribute );
+
+			for ( const range of ranges ) {
+				if ( value && value !== 'default' ) {
+					writer.setAttribute( this.attribute, value, range );
+				} else {
+					writer.removeAttribute( this.attribute, range );
+				}
+			}
+		} );
+	}
+}

+ 6 - 49
packages/ckeditor5-font/src/fontfamily/fontfamilycommand.js

@@ -7,63 +7,20 @@
  * @module font/fontfamilycommand
  */
 
-import Command from '@ckeditor/ckeditor5-core/src/command';
+import FontCommand from '../fontcommand';
 
 /**
  * The font family command. It is used by the {@link module:font/fontfamily/fontfamilyediting~FontFamilyEditing}
  * to apply font family.
  *
- * TODO: those commands are duplicated here and there - maybe make them one?
- *
  * @extends module:core/command~Command
  */
-export default class FontFamilyCommand extends Command {
-	/**
-	 * @inheritDoc
-	 */
-	refresh() {
-		const doc = this.editor.model.document;
-
-		/**
-		 * A flag indicating whether the command is active, which means that the selection has fontFamily attribute set.
-		 *
-		 * @observable
-		 * @readonly
-		 * @member {Boolean} module:font/fontfamilycommand~FontFamilyCommand#value
-		 */
-		this.value = doc.selection.getAttribute( 'fontFamily' );
-		this.isEnabled = this.editor.model.schema.checkAttributeInSelection( doc.selection, 'fontFamily' );
-	}
-
+export default class FontFamilyCommand extends FontCommand {
 	/**
-	 * Executes the command.
-	 *
-	 * @protected
-	 * @param {Object} [options] Options for the executed command.
-	 * @param {String} [options.fontFamily] FontSize value to apply.
+	 * TODO: docs me
+	 * @param editor
 	 */
-	execute( options = {} ) {
-		const model = this.editor.model;
-		const document = model.document;
-		const selection = document.selection;
-
-		// Do not apply fontFamily on collapsed selection.
-		if ( selection.isCollapsed ) {
-			return;
-		}
-
-		const value = options.fontFamily;
-
-		model.change( writer => {
-			const ranges = model.schema.getValidRanges( selection.getRanges(), 'fontFamily' );
-
-			for ( const range of ranges ) {
-				if ( value && value !== 'default' ) {
-					writer.setAttribute( 'fontFamily', value, range );
-				} else {
-					writer.removeAttribute( 'fontFamily', range );
-				}
-			}
-		} );
+	constructor( editor ) {
+		super( editor, 'fontFamily' );
 	}
 }

+ 7 - 49
packages/ckeditor5-font/src/fontsize/fontsizecommand.js

@@ -7,62 +7,20 @@
  * @module font/fontsizecommand
  */
 
-import Command from '@ckeditor/ckeditor5-core/src/command';
+import FontCommand from '../fontcommand';
 
 /**
- * The font size command. It is used by the {@link module:font/fontsize/fontsizeediting~FontSizeEditing FontSizeEditing feature}
+ * The font size command. It is used by the {@link module:font/fontsize/fontsizeediting~FontSizeEditing}
  * to apply font size.
  *
  * @extends module:core/command~Command
  */
-export default class FontSizeCommand extends Command {
+export default class FontSizeCommand extends FontCommand {
 	/**
-	 * @inheritDoc
+	 * TODO: docs me
+	 * @param editor
 	 */
-	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
-
-		/**
-		 * A flag indicating whether the command is active, which means that the selection has fontSize attribute set.
-		 *
-		 * @observable
-		 * @readonly
-		 * @member {Boolean} module:font/fontsizecommand~FontSizeCommand#value
-		 */
-		this.value = doc.selection.getAttribute( 'fontSize' );
-		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'fontSize' );
-	}
-
-	/**
-	 * Executes the command.
-	 *
-	 * @protected
-	 * @param {Object} [options] Options for the executed command.
-	 * @param {String} [options.fontSize] FontSize value to apply.
-	 */
-	execute( options = {} ) {
-		const model = this.editor.model;
-		const document = model.document;
-		const selection = document.selection;
-
-		// Do not apply fontSize on collapsed selection.
-		if ( selection.isCollapsed ) {
-			return;
-		}
-
-		const value = options.fontSize;
-
-		model.change( writer => {
-			const ranges = model.schema.getValidRanges( selection.getRanges(), 'fontSize' );
-
-			for ( const range of ranges ) {
-				if ( value && value !== 'normal' ) {
-					writer.setAttribute( 'fontSize', value, range );
-				} else {
-					writer.removeAttribute( 'fontSize', range );
-				}
-			}
-		} );
+	constructor( editor ) {
+		super( editor, 'fontSize' );
 	}
 }

+ 4 - 4
packages/ckeditor5-font/tests/fontfamily/fontfamilycommand.js

@@ -63,7 +63,7 @@ describe( 'FontFamilyCommand', () => {
 
 			expect( command.value ).to.be.undefined;
 
-			command.execute( { fontFamily: 'arial' } );
+			command.execute( { value: 'arial' } );
 
 			expect( command.value ).to.equal( 'arial' );
 
@@ -78,7 +78,7 @@ describe( 'FontFamilyCommand', () => {
 				'<paragraph>barbar]bar</paragraph>'
 			);
 
-			command.execute( { fontFamily: 'arial' } );
+			command.execute( { value: 'arial' } );
 
 			expect( command.value ).to.equal( 'arial' );
 
@@ -97,7 +97,7 @@ describe( 'FontFamilyCommand', () => {
 				'<paragraph><$text fontFamily="text-small">bar]bar</$text>bar</paragraph>'
 			);
 
-			command.execute( { fontFamily: 'arial' } );
+			command.execute( { value: 'arial' } );
 
 			expect( command.value ).to.equal( 'arial' );
 
@@ -113,7 +113,7 @@ describe( 'FontFamilyCommand', () => {
 
 			expect( command.value ).to.equal( 'arial' );
 
-			command.execute( { fontFamily: 'arial' } );
+			command.execute( { value: 'arial' } );
 
 			expect( getData( model ) ).to.equal( '<paragraph>abc<$text fontFamily="arial">foo[]bar</$text>xyz</paragraph>' );
 

+ 3 - 3
packages/ckeditor5-font/tests/fontsize/fontsizecommand.js

@@ -63,7 +63,7 @@ describe( 'FontSizeCommand', () => {
 
 			expect( command.value ).to.be.undefined;
 
-			command.execute( { fontSize: 'huge' } );
+			command.execute( { value: 'huge' } );
 
 			expect( command.value ).to.equal( 'huge' );
 
@@ -78,7 +78,7 @@ describe( 'FontSizeCommand', () => {
 				'<paragraph>barbar]bar</paragraph>'
 			);
 
-			command.execute( { fontSize: 'huge' } );
+			command.execute( { value: 'huge' } );
 
 			expect( command.value ).to.equal( 'huge' );
 
@@ -97,7 +97,7 @@ describe( 'FontSizeCommand', () => {
 				'<paragraph><$text fontSize="text-small">bar]bar</$text>bar</paragraph>'
 			);
 
-			command.execute( { fontSize: 'huge' } );
+			command.execute( { value: 'huge' } );
 
 			expect( command.value ).to.equal( 'huge' );