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

Extract indent block command behavior.

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

+ 9 - 3
packages/ckeditor5-indent/src/indentblock.js

@@ -11,6 +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';
 
 /**
  * The block indentation feature.
@@ -71,15 +73,19 @@ export default class IndentBlock extends Plugin {
 
 		const useOffsetConfig = !configuration.classes || !configuration.classes.length;
 
+		const indentConfig = Object.assign( { direction: 'forward' }, configuration );
+		const outdentConfig = Object.assign( { direction: 'backward' }, configuration );
+
 		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 ) ) );
 		} 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, Object.assign( { direction: 'forward' }, configuration ) ) );
-		editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, Object.assign( { direction: 'backward' }, configuration ) ) );
-
 		const getCommandExecuter = commandName => {
 			return ( data, cancel ) => {
 				const command = this.editor.commands.get( commandName );

+ 20 - 83
packages/ckeditor5-indent/src/indentblockcommand.js

@@ -20,22 +20,33 @@ export default class IndentBlockCommand extends Command {
 	 * Creates an instance of the command.
 	 *
 	 * @param {module:core/editor/editor~Editor} editor Editor instance.
-	 * @param {Object} config.
 	 */
-	constructor( editor, { classes, offset, unit, direction } ) {
+	constructor( editor, strategy ) {
 		super( editor );
-		this.classes = classes;
-		this.offset = offset;
-		this.unit = unit;
-		this.direction = direction == 'forward' ? 1 : -1;
-		this.useClasses = !!( classes && classes.length );
+
+		this.strategy = strategy;
 	}
 
 	/**
 	 * @inheritDoc
 	 */
 	refresh() {
-		this.isEnabled = this._checkEnabled();
+		// Check whether any of position's ancestor is a list item.
+		const editor = this.editor;
+		const model = editor.model;
+
+		const block = first( model.document.selection.getSelectedBlocks() );
+
+		// If selection is not in a list item, the command is disabled.
+		if ( !block || !model.schema.checkAttribute( block, 'indent' ) ) {
+			this.isEnabled = false;
+
+			return;
+		}
+
+		const currentIndent = block.getAttribute( 'indent' );
+
+		this.isEnabled = this.strategy.checkEnabled( currentIndent );
 	}
 
 	execute() {
@@ -47,14 +58,8 @@ export default class IndentBlockCommand extends Command {
 		model.change( writer => {
 			for ( const item of itemsToChange ) {
 				const currentIndent = item.getAttribute( 'indent' );
-				let newIndent;
 
-				// TODO: split potential:
-				if ( this.useClasses ) {
-					newIndent = this._getIndentClasses( currentIndent );
-				} else {
-					newIndent = this._getIndentOffset( currentIndent );
-				}
+				const newIndent = this.strategy.getNewIndent( currentIndent );
 
 				if ( newIndent ) {
 					writer.setAttribute( 'indent', newIndent, item );
@@ -64,72 +69,4 @@ export default class IndentBlockCommand extends Command {
 			}
 		} );
 	}
-
-	_getIndentOffset( 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;
-	}
-
-	_getIndentClasses( currentIndent ) {
-		const currentIndex = this.classes.indexOf( currentIndent );
-
-		return this.classes[ currentIndex + this.direction ];
-	}
-
-	/**
-	 * Checks whether the command can be enabled in the current context.
-	 *
-	 * @private
-	 * @returns {Boolean} Whether the command should be enabled.
-	 */
-	_checkEnabled() {
-		// Check whether any of position's ancestor is a list item.
-		const editor = this.editor;
-		const model = editor.model;
-
-		const block = first( model.document.selection.getSelectedBlocks() );
-
-		// If selection is not in a list item, the command is disabled.
-		if ( !block || !model.schema.checkAttribute( block, 'indent' ) ) {
-			return false;
-		}
-
-		const currentIndent = block.getAttribute( 'indent' );
-
-		// TODO: split potential.
-		if ( this.useClasses ) {
-			return this._checkEnabledClasses( currentIndent );
-		} else {
-			return this._checkEnabledOffset( currentIndent );
-		}
-	}
-
-	_checkEnabledOffset( currentIndent ) {
-		const currentOffset = parseFloat( currentIndent || 0 );
-
-		// is forward
-		if ( this.direction > 0 ) {
-			return true;
-		} else {
-			return currentOffset > 0;
-		}
-	}
-
-	_checkEnabledClasses( 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;
-		}
-	}
 }

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

@@ -0,0 +1,31 @@
+/**
+ * @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;
+		}
+	}
+}

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

@@ -0,0 +1,51 @@
+/**
+ * @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;
+		}
+	}
+}

+ 10 - 8
packages/ckeditor5-indent/tests/indentblockcommand.js

@@ -7,6 +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';
 
 describe( 'IndentBlockCommand', () => {
 	let editor, command, model;
@@ -35,7 +37,7 @@ describe( 'IndentBlockCommand', () => {
 	describe( 'indent', () => {
 		describe( 'using classes', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, {
+				command = new IndentBlockCommand( editor, new UsingClasses( {
 					classes: [
 						'indent-1',
 						'indent-2',
@@ -43,7 +45,7 @@ describe( 'IndentBlockCommand', () => {
 						'indent-4'
 					],
 					direction: 'forward'
-				} );
+				} ) );
 			} );
 
 			describe( 'isEnabled', () => {
@@ -85,11 +87,11 @@ describe( 'IndentBlockCommand', () => {
 
 		describe( 'using offset', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, {
+				command = new IndentBlockCommand( editor, new UsingOffset( {
 					offset: 50,
 					unit: 'px',
 					direction: 'forward'
-				} );
+				} ) );
 			} );
 
 			describe( 'isEnabled', () => {
@@ -145,7 +147,7 @@ describe( 'IndentBlockCommand', () => {
 	describe( 'outdent', () => {
 		describe( 'using classes', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, {
+				command = new IndentBlockCommand( editor, new UsingClasses( {
 					classes: [
 						'indent-1',
 						'indent-2',
@@ -153,7 +155,7 @@ describe( 'IndentBlockCommand', () => {
 						'indent-4'
 					],
 					direction: 'backward'
-				} );
+				} ) );
 			} );
 
 			describe( 'isEnabled', () => {
@@ -189,11 +191,11 @@ describe( 'IndentBlockCommand', () => {
 
 		describe( 'using offset', () => {
 			beforeEach( () => {
-				command = new IndentBlockCommand( editor, {
+				command = new IndentBlockCommand( editor, new UsingOffset( {
 					offset: 50,
 					unit: 'px',
 					direction: 'backward'
-				} );
+				} ) );
 			} );
 
 			describe( 'isEnabled', () => {