Bladeren bron

Add conversion for classes.

Maciej Gołaszewski 6 jaren geleden
bovenliggende
commit
fb1010b70f
2 gewijzigde bestanden met toevoegingen van 47 en 24 verwijderingen
  1. 46 23
      packages/ckeditor5-indent/src/indentblock.js
  2. 1 1
      packages/ckeditor5-indent/src/indentblockcommand.js

+ 46 - 23
packages/ckeditor5-indent/src/indentblock.js

@@ -43,38 +43,61 @@ export default class IndentBlock extends Plugin {
 		const editor = this.editor;
 		const schema = editor.model.schema;
 		const conversion = editor.conversion;
+		const config = editor.config.get( 'indentBlock' );
 
 		// TODO: better features inclusion
 		schema.extend( 'paragraph', { allowAttributes: 'indent' } );
 		schema.extend( 'heading1', { allowAttributes: 'indent' } );
 
-		conversion.for( 'upcast' ).attributeToAttribute( {
-			view: {
-				styles: {
-					'margin-left': /[\s\S]+/
-				}
-			},
-			model: {
-				key: 'indent',
-				value: viewElement => {
-					return viewElement.getStyle( 'margin-left' );
-				}
-			}
-		} );
+		const classes = config.classes;
 
-		conversion.for( 'downcast' ).attributeToAttribute( {
-			model: 'indent',
-			view: modelAttributeValue => {
-				return {
-					key: 'style',
-					value: {
-						'margin-left': modelAttributeValue
-					}
+		const usingClasses = !!classes.length;
+
+		if ( usingClasses ) {
+			const definition = {
+				model: {
+					key: 'indent',
+					values: []
+				},
+				view: {}
+			};
+
+			for ( const className of classes ) {
+				definition.model.values.push( className );
+				definition.view[ className ] = {
+					key: 'class',
+					value: [ className ]
 				};
 			}
-		} );
 
-		const config = editor.config.get( 'indentBlock' );
+			editor.conversion.attributeToAttribute( definition );
+		} else {
+			conversion.for( 'upcast' ).attributeToAttribute( {
+				view: {
+					styles: {
+						'margin-left': /[\s\S]+/
+					}
+				},
+				model: {
+					key: 'indent',
+					value: viewElement => {
+						return viewElement.getStyle( 'margin-left' );
+					}
+				}
+			} );
+
+			conversion.for( 'downcast' ).attributeToAttribute( {
+				model: 'indent',
+				view: modelAttributeValue => {
+					return {
+						key: 'style',
+						value: {
+							'margin-left': modelAttributeValue
+						}
+					};
+				}
+			} );
+		}
 
 		editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, Object.assign( { direction: 'forward' }, config ) ) );
 		editor.commands.add( 'outdentBlock', new IndentBlockCommand( editor, Object.assign( { direction: 'backward' }, config ) ) );

+ 1 - 1
packages/ckeditor5-indent/src/indentblockcommand.js

@@ -103,7 +103,7 @@ export default class IndentBlockCommand extends Command {
 			if ( this.direction > 0 ) {
 				return currentIndex < this.classes.length - 1;
 			} else {
-				return currentIndex > 0 && currentIndex < this.classes.length;
+				return currentIndex >= 0 && currentIndex < this.classes.length;
 			}
 		} else {
 			const currentOffset = parseFloat( currentIndent || 0 );