8
0
Просмотр исходного кода

Refactor IndentCommand behaviors.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
a4b5d823e9

+ 6 - 6
packages/ckeditor5-indent/src/indentblock.js

@@ -11,8 +11,8 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Indent from '@ckeditor/ckeditor5-core/src/indent';
 
 import IndentBlockCommand from './indentblockcommand';
-import UsingOffset from './usingoffset';
-import UsingClasses from './usingclasses';
+import IndentUsingOffset from './indentcommandbehavior/indentusingoffset';
+import IndentUsingClasses from './indentcommandbehavior/indentusingclasses';
 
 /**
  * The block indentation feature.
@@ -78,12 +78,12 @@ export default class IndentBlock extends Plugin {
 
 		if ( useOffsetConfig ) {
 			this._setupConversionUsingOffset( conversion );
-			editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, new UsingOffset( indentConfig ) ) );
-			editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, new UsingOffset( indentConfig ) ) );
+			editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, new IndentUsingOffset( indentConfig ) ) );
+			editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, new IndentUsingOffset( indentConfig ) ) );
 		} else {
 			this._setupConversionUsingClasses( configuration.classes, editor );
-			editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, new UsingClasses( indentConfig ) ) );
-			editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, new UsingClasses( outdentConfig ) ) );
+			editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, new IndentUsingClasses( indentConfig ) ) );
+			editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, new IndentUsingClasses( outdentConfig ) ) );
 		}
 
 		const getCommandExecuter = commandName => {

+ 48 - 7
packages/ckeditor5-indent/src/indentblockcommand.js

@@ -11,7 +11,18 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
- * The block indentation feature.
+ * The indent block command.
+ *
+ * The command is registered by the {@link module:indent-block/indentblock~IndentBlock} as `'indentBlock'` - for indenting blocks and
+ * `'outdentBlock'` - for outdenting blocks.
+ *
+ * To increase block indentation at current selection execute the command:
+ *
+ *		editor.execute( 'indentBlock' );
+ *
+ * To decrease block indentation at current selection execute the command:
+ *
+ *		editor.execute( 'outdentBlock' );
  *
  * @extends module:core/plugin~Plugin
  */
@@ -20,11 +31,18 @@ export default class IndentBlockCommand extends Command {
 	 * Creates an instance of the command.
 	 *
 	 * @param {module:core/editor/editor~Editor} editor Editor instance.
+	 * @param {module:indent-block/indentblockcommand~IndentBehavior} indentBehavior
 	 */
-	constructor( editor, strategy ) {
+	constructor( editor, indentBehavior ) {
 		super( editor );
 
-		this.strategy = strategy;
+		/**
+		 * Command's indentation behavior.
+		 *
+		 * @type {module:indent-block/indentblockcommand~IndentBehavior}
+		 * @private
+		 */
+		this._indentBehavior = indentBehavior;
 	}
 
 	/**
@@ -44,11 +62,12 @@ export default class IndentBlockCommand extends Command {
 			return;
 		}
 
-		const currentIndent = block.getAttribute( 'indent' );
-
-		this.isEnabled = this.strategy.checkEnabled( currentIndent );
+		this.isEnabled = this._indentBehavior.checkEnabled( block.getAttribute( 'indent' ) );
 	}
 
+	/**
+	 * @inheritDoc
+	 */
 	execute() {
 		const model = this.editor.model;
 		const doc = model.document;
@@ -59,7 +78,7 @@ export default class IndentBlockCommand extends Command {
 			for ( const item of itemsToChange ) {
 				const currentIndent = item.getAttribute( 'indent' );
 
-				const newIndent = this.strategy.getNewIndent( currentIndent );
+				const newIndent = this._indentBehavior.getNewIndent( currentIndent );
 
 				if ( newIndent ) {
 					writer.setAttribute( 'indent', newIndent, item );
@@ -70,3 +89,25 @@ export default class IndentBlockCommand extends Command {
 		} );
 	}
 }
+
+/**
+ * Provides indentation behavior to {@link module:indent-block/indentblockcommand~IndentBlockCommand}.
+ *
+ * @interface module:indent-block/indentblockcommand~IndentBehavior
+ */
+
+/**
+ * Performs check if command should be enabled.
+ *
+ * @method #checkEnabled
+ * @param {String} indentAttributeValue Current indent attribute value.
+ * @returns {Boolean}
+ */
+
+/**
+ * Returns new indent attribute value based on current indent.
+ *
+ * @method #getNewIndent
+ * @param {String} indentAttributeValue Current indent attribute value.
+ * @returns {String|undefined}
+ */

+ 61 - 0
packages/ckeditor5-indent/src/indentcommandbehavior/indentusingclasses.js

@@ -0,0 +1,61 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module indent-block/indentcommandbehavior/indentusingclasses
+ */
+
+/**
+ * The block indentation behavior that uses classes to set indentation.
+ *
+ * @implements module:indent-block/indentblockcommand~IndentBehavior
+ */
+export default class IndentUsingClasses {
+	/**
+	 * Creates an instance of the command.
+	 *
+	 * @param {Object} config
+	 * @param {String} config.direction The direction of indentation.
+	 * @param {Array.<String>} config.classes List of classes used for indentation.
+	 */
+	constructor( config ) {
+		/**
+		 * The direction of indentation.
+		 *
+		 * @type {Boolean}
+		 */
+		this.isForward = config.direction === 'forward';
+
+		/**
+		 * List of classes used for indentation.
+		 *
+		 * @type {Array<String>}
+		 */
+		this.classes = config.classes;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	checkEnabled( indentAttributeValue ) {
+		const currentIndex = this.classes.indexOf( indentAttributeValue );
+
+		if ( this.isForward ) {
+			return currentIndex < this.classes.length - 1;
+		} else {
+			return currentIndex > 0;
+		}
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	getNewIndent( indentAttributeValue ) {
+		const currentIndex = this.classes.indexOf( indentAttributeValue );
+		const indexStep = this.isForward ? 1 : -1;
+
+		return this.classes[ currentIndex + indexStep ];
+	}
+}

+ 72 - 0
packages/ckeditor5-indent/src/indentcommandbehavior/indentusingoffset.js

@@ -0,0 +1,72 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module indent-block/indentcommandbehavior/indentusingoffset
+ */
+
+/**
+ * @implements module:indent-block/indentblockcommand~IndentBehavior
+ */
+export default class IndentUsingOffset {
+	/**
+	 * Creates an instance of the behavior.
+	 *
+	 * @param {Object} config
+	 * @param {String} config.direction The direction of indentation.
+	 * @param {Number} config.offset Offset of next indentation step.
+	 * @param {String} config.unit Indentation unit.
+	 */
+	constructor( config ) {
+		/**
+		 * The direction of indentation.
+		 *
+		 * @type {Boolean}
+		 */
+		this.isForward = config.direction === 'forward';
+
+		/**
+		 * Offset of next indentation step.
+		 *
+		 * @type {Number}
+		 */
+		this.offset = config.offset;
+
+		/**
+		 * Indentation unit.
+		 *
+		 * @type {String}
+		 */
+		this.unit = config.unit;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	checkEnabled( indentAttributeValue ) {
+		const currentOffset = parseFloat( indentAttributeValue || 0 );
+
+		// The command is always enabled for forward indentation.
+		return this.isForward || currentOffset > 0;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	getNewIndent( indentAttributeValue ) {
+		const currentOffset = parseFloat( indentAttributeValue || 0 );
+		const isSameUnit = !indentAttributeValue || indentAttributeValue.endsWith( this.unit );
+
+		if ( !isSameUnit ) {
+			return this.isForward ? this.offset + this.unit : undefined;
+		}
+
+		const nextOffset = this.isForward ? this.offset : -this.offset;
+
+		const offsetToSet = currentOffset + nextOffset;
+
+		return offsetToSet > 0 ? offsetToSet + this.unit : undefined;
+	}
+}

+ 0 - 31
packages/ckeditor5-indent/src/usingclasses.js

@@ -1,31 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module indent-block/indentblockcommand/usingclasses
- */
-
-export default class UsingClasses {
-	constructor( config ) {
-		this.direction = config.direction == 'forward' ? 1 : -1;
-		this.classes = config.classes;
-	}
-
-	getNewIndent( currentIndent ) {
-		const currentIndex = this.classes.indexOf( currentIndent );
-
-		return this.classes[ currentIndex + this.direction ];
-	}
-
-	checkEnabled( currentIndent ) {
-		const currentIndex = this.classes.indexOf( currentIndent );
-
-		if ( this.direction > 0 ) {
-			return currentIndex < this.classes.length - 1;
-		} else {
-			return currentIndex >= 0 && currentIndex < this.classes.length;
-		}
-	}
-}

+ 0 - 51
packages/ckeditor5-indent/src/usingoffset.js

@@ -1,51 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module indent-block/indentblockcommand/usingoffset
- */
-
-/**
- * The block indentation feature.
- *
- * @extends module:core/plugin~Plugin
- */
-export default class UsingOffset {
-	/**
-	 * Creates an instance of the command.
-	 *
-	 * @param {module:core/editor/editor~Editor} editor Editor instance.
-	 * @param {Object} config.
-	 */
-	constructor( config ) {
-		this.direction = config.direction == 'forward' ? 1 : -1;
-		this.offset = config.offset;
-		this.unit = config.unit;
-	}
-
-	getNewIndent( currentIndent ) {
-		const currentOffset = parseFloat( currentIndent || 0 );
-		const isSameUnit = !currentIndent || currentIndent.endsWith( this.unit );
-
-		if ( !isSameUnit ) {
-			return this.direction > 0 ? this.offset + this.unit : undefined;
-		}
-
-		const offsetToSet = currentOffset + this.direction * this.offset;
-
-		return offsetToSet && offsetToSet > 0 ? offsetToSet + this.unit : undefined;
-	}
-
-	checkEnabled( currentIndent ) {
-		const currentOffset = parseFloat( currentIndent || 0 );
-
-		// is forward
-		if ( this.direction > 0 ) {
-			return true;
-		} else {
-			return currentOffset > 0;
-		}
-	}
-}

+ 7 - 7
packages/ckeditor5-indent/tests/indentblockcommand.js

@@ -7,8 +7,8 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import IndentBlockCommand from '../src/indentblockcommand';
-import UsingClasses from '../src/usingclasses';
-import UsingOffset from '../src/usingoffset';
+import IndentUsingClasses from '../src/indentcommandbehavior/indentusingclasses';
+import IndentUsingOffset from '../src/indentcommandbehavior/indentusingoffset';
 
 describe( 'IndentBlockCommand', () => {
 	let editor, command, model;
@@ -37,7 +37,7 @@ describe( 'IndentBlockCommand', () => {
 	describe( 'indent', () => {
 		describe( 'using classes', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, new UsingClasses( {
+				command = new IndentBlockCommand( editor, new IndentUsingClasses( {
 					classes: [
 						'indent-1',
 						'indent-2',
@@ -64,7 +64,7 @@ describe( 'IndentBlockCommand', () => {
 					expect( command.isEnabled ).to.be.true;
 				} );
 
-				it( 'should be true in indented block in last indentation class', () => {
+				it( 'should be false in indented block in last indentation class', () => {
 					setData( model, '<paragraph indent="indent-4">f[]oo</paragraph>' );
 					expect( command.isEnabled ).to.be.false;
 				} );
@@ -87,7 +87,7 @@ describe( 'IndentBlockCommand', () => {
 
 		describe( 'using offset', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, new UsingOffset( {
+				command = new IndentBlockCommand( editor, new IndentUsingOffset( {
 					offset: 50,
 					unit: 'px',
 					direction: 'forward'
@@ -147,7 +147,7 @@ describe( 'IndentBlockCommand', () => {
 	describe( 'outdent', () => {
 		describe( 'using classes', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, new UsingClasses( {
+				command = new IndentBlockCommand( editor, new IndentUsingClasses( {
 					classes: [
 						'indent-1',
 						'indent-2',
@@ -191,7 +191,7 @@ describe( 'IndentBlockCommand', () => {
 
 		describe( 'using offset', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, new UsingOffset( {
+				command = new IndentBlockCommand( editor, new IndentUsingOffset( {
 					offset: 50,
 					unit: 'px',
 					direction: 'backward'