Преглед на файлове

Fix indent conversion for missing consumables names.

Maciej Gołaszewski преди 5 години
родител
ревизия
cdb872f2ba
променени са 1 файла, в които са добавени 29 реда и са изтрити 10 реда
  1. 29 10
      packages/ckeditor5-indent/src/indentblock.js

+ 29 - 10
packages/ckeditor5-indent/src/indentblock.js

@@ -12,6 +12,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import IndentBlockCommand from './indentblockcommand';
 import IndentUsingOffset from './indentcommandbehavior/indentusingoffset';
 import IndentUsingClasses from './indentcommandbehavior/indentusingclasses';
+import { addMarginRules } from '@ckeditor/ckeditor5-engine/src/view/styles/margin';
 
 /**
  * The block indentation feature.
@@ -56,6 +57,7 @@ export default class IndentBlock extends Plugin {
 		const outdentConfig = Object.assign( { direction: 'backward' }, configuration );
 
 		if ( useOffsetConfig ) {
+			editor.editing.view.document.addStyleProcessorRules( addMarginRules );
 			this._setupConversionUsingOffset( editor.conversion );
 
 			editor.commands.add( 'indentBlock', new IndentBlockCommand( editor, new IndentUsingOffset( indentConfig ) ) );
@@ -100,17 +102,34 @@ export default class IndentBlock extends Plugin {
 		const locale = this.editor.locale;
 		const marginProperty = locale.contentLanguageDirection === 'rtl' ? 'margin-right' : 'margin-left';
 
-		conversion.for( 'upcast' ).attributeToAttribute( {
-			view: {
-				styles: {
-					[ marginProperty ]: /[\s\S]+/
-				}
-			},
-			model: {
-				key: 'blockIndent',
-				value: viewElement => viewElement.getStyle( marginProperty )
+		conversion.for( 'upcast' ).add( dispatcher => dispatcher.on( 'element', ( evt, data, conversionApi ) => {
+			const element = data.viewItem;
+
+			if ( !element.hasStyle( marginProperty ) ) {
+				return;
 			}
-		} );
+
+			// Try to consume appropriate values from consumable values list.
+			if ( !testStyle( marginProperty ) && !testStyle( 'margin' ) ) {
+				return;
+			}
+
+			if ( !data.modelRange ) {
+				data = Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
+			}
+
+			const items = Array.from( data.modelRange.getItems() );
+			const node = items.shift();
+
+			if ( conversionApi.schema.checkAttribute( node, 'blockIndent' ) ) {
+				conversionApi.writer.setAttribute( 'blockIndent', element.getNormalizedStyle( marginProperty ), node );
+				conversionApi.consumable.consume( data.viewItem, { styles: marginProperty } );
+			}
+
+			function testStyle( styleName ) {
+				return conversionApi.consumable.test( data.viewItem, { styles: styleName } );
+			}
+		} ) );
 
 		conversion.for( 'downcast' ).attributeToAttribute( {
 			model: 'blockIndent',