8
0
Maciej Gołaszewski 8 лет назад
Родитель
Сommit
1f8cbebbb6

+ 0 - 1
packages/ckeditor5-alignment/src/alignment.js

@@ -15,7 +15,6 @@ import AlignmentUI from './alignmentui';
 /**
  * The alignment plugin.
  *
- *
  * It requires {@link module:alignment/alignmentediting~AlignmentEditing} and {@link module:alignment/alignmentui~AlignmentUI} plugins.
  *
  * @extends module:core/plugin~Plugin

+ 10 - 4
packages/ckeditor5-alignment/src/alignmentcommand.js

@@ -70,7 +70,8 @@ export default class AlignmentCommand extends Command {
 			const batch = options.batch || document.batch();
 			const blocks = Array.from( document.selection.getSelectedBlocks() );
 
-			// Executing command on already aligned text should remove alignment attribute
+			// Remove alignment attribute if current alignment is as selected or is default one.
+			// Default alignment should not be stored in model as it will bloat model data.
 			if ( this.value || this._isDefault() ) {
 				removeAlignmentFromSelection( blocks, batch );
 			} else {
@@ -86,6 +87,7 @@ export default class AlignmentCommand extends Command {
 	 * @returns {Boolean} Whether the command should be enabled.
 	 */
 	_isDefault() {
+		// Right now only LTR is supported so 'left' is always default one.
 		return this.type === 'left';
 	}
 
@@ -102,9 +104,11 @@ export default class AlignmentCommand extends Command {
 
 		const schema = this.editor.document.schema;
 
+		// Check if adding alignment attribute to selected block is allowed.
 		return schema.check( {
 			name: firstBlock.name,
-			attributes: [ ...firstBlock.getAttributeKeys(), 'alignment' ]
+			// Apparently I must pass current attributes as otherwise adding alignment on listItem will fail.
+			attributes: [ 'alignment' ]
 		} );
 	}
 
@@ -115,13 +119,15 @@ export default class AlignmentCommand extends Command {
 	 * @returns {Boolean} The current value.
 	 */
 	_getValue( firstBlock ) {
+		// The #_checkEnabled is checked as first so if command is disabled it's value is also false.
 		if ( !this.isEnabled || !firstBlock ) {
 			return false;
 		}
 
-		const currentAlignment = firstBlock.getAttribute( 'alignment' );
+		const selectionAlignment = firstBlock.getAttribute( 'alignment' );
 
-		return currentAlignment ? currentAlignment === this.type : this._isDefault();
+		// Command's value will be set when commands type is matched in selection or the selection is default one.
+		return selectionAlignment ? selectionAlignment === this.type : this._isDefault();
 	}
 }
 

+ 15 - 1
packages/ckeditor5-alignment/src/alignmentediting.js

@@ -37,7 +37,11 @@ export default class AlignmentEditing extends Plugin {
 	}
 
 	/**
-	 * Returns list of supported styles.
+	 * List of supported alignment styles:
+	 * - left
+	 * - right
+	 * - center
+	 * - justify
 	 *
 	 * @static
 	 * @readonly
@@ -73,11 +77,13 @@ export default class AlignmentEditing extends Plugin {
 		// Allow alignment attribute on all blocks.
 		schema.allow( { name: '$block', attributes: 'alignment' } );
 
+		// Convert model attribute alignment to `text-align` style property.
 		buildModelConverter()
 			.for( data.modelToView, editing.modelToView )
 			.fromAttribute( 'alignment' )
 			.toAttribute( value => ( { key: 'style', value: `text-align:${ value }` } ) );
 
+		// Convert `text-align` style property from element to model attribute alignment.
 		buildViewConverter()
 			.for( data.viewToModel )
 			.fromAttribute( 'style', /text-align/ )
@@ -92,6 +98,7 @@ export default class AlignmentEditing extends Plugin {
 				return { key: 'alignment', value: textAlign };
 			} );
 
+		// Add only enabled & supported commands.
 		enabledStyles
 			.filter( isSupported )
 			.forEach( style => editor.commands.add( AlignmentEditing.commandName( style ), new AlignmentCommand( editor, style ) ) );
@@ -110,6 +117,12 @@ export default class AlignmentEditing extends Plugin {
 	}
 }
 
+/**
+ * Checks whether passed style is supported by {@link module:alignment/alignmentediting~AlignmentEditing}.
+ *
+ * @param {String} style Style value to check.
+ * @returns {Boolean}
+ */
 export function isSupported( style ) {
 	return AlignmentEditing.supportedStyles.includes( style );
 }
@@ -117,6 +130,7 @@ export function isSupported( style ) {
 // Check whether alignment is default one.
 // @private
 function isDefault( textAlign ) {
+	// Right now only RTL is supported so 'left' value is always default one.
 	return textAlign === 'left';
 }