Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Fix indent conversion for missing consumables names.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Jan 16, 2020
1 parent 38e2948 commit 9f8d9d5
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/indentblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) ) );
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 9f8d9d5

Please sign in to comment.