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

Commit

Permalink
Merge branch 'master' into i/6232
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/tableproperties/tablepropertiesui.js
  • Loading branch information
jodator committed Feb 12, 2020
2 parents a7bf62f + 717608c commit a47f564
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 107 deletions.
4 changes: 4 additions & 0 deletions src/converters/tableproperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export function downcastTableAttribute( conversion, modelAttribute, styleName )
const { item, attributeNewValue } = data;
const { mapper, writer } = conversionApi;

if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
return;
}

const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );

if ( attributeNewValue ) {
Expand Down
70 changes: 20 additions & 50 deletions src/tableproperties/tablepropertiesediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import TableWidthCommand from './commands/tablewidthcommand';
import TableHeightCommand from './commands/tableheightcommand';
import TableAlignmentCommand from './commands/tablealignmentcommand';

// RegExp used for matching margin style in converters.
const MARGIN_REG_EXP = /^(auto|0(%|[a-z]{2,4})?)$/;
const ALIGN_VALUES_REG_EXP = /^(left|right|center)$/;
const ALIGN_VALUES_REG_EXP = /^(left|right)$/;

/**
* The table properties editing feature.
Expand Down Expand Up @@ -116,31 +114,32 @@ function enableAlignmentProperty( schema, conversion ) {
schema.extend( 'table', {
allowAttributes: [ 'alignment' ]
} );
conversion.for( 'upcast' )

conversion
.attributeToAttribute( {
view: {
styles: {
'margin-right': MARGIN_REG_EXP,
'margin-left': MARGIN_REG_EXP
}
},
model: {
name: 'table',
key: 'alignment',
value: viewElement => {
// At this point we only have auto or 0 value (with a unit).
if ( viewElement.getStyle( 'margin-right' ) != 'auto' ) {
return 'right';
values: [ 'left', 'right' ]
},
view: {
left: {
key: 'style',
value: {
float: 'left'
}

if ( viewElement.getStyle( 'margin-left' ) != 'auto' ) {
return 'left';
},
right: {
key: 'style',
value: {
float: 'right'
}

return 'center';
}
}
} )
},
converterPriority: 'high'
} );

conversion.for( 'upcast' )
// Support for backwards compatibility and pasting from other sources.
.attributeToAttribute( {
view: {
Expand All @@ -154,35 +153,6 @@ function enableAlignmentProperty( schema, conversion ) {
value: viewElement => viewElement.getAttribute( 'align' )
}
} );

conversion.for( 'downcast' ).add( dispatcher => dispatcher.on( 'attribute:alignment:table', ( evt, data, conversionApi ) => {
const { item, attributeNewValue } = data;
const { mapper, writer } = conversionApi;

const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );

if ( !attributeNewValue ) {
writer.removeStyle( 'margin-left', table );
writer.removeStyle( 'margin-right', table );

return;
}

const styles = {
'margin-right': 'auto',
'margin-left': 'auto'
};

if ( attributeNewValue == 'left' ) {
styles[ 'margin-left' ] = '0';
}

if ( attributeNewValue == 'right' ) {
styles[ 'margin-right' ] = '0';
}

writer.setStyle( styles, table );
} ) );
}

// Enables conversion for an attribute for simple view-model mappings.
Expand Down
2 changes: 1 addition & 1 deletion src/tableproperties/tablepropertiesui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../ui/utils';
import { debounce } from 'lodash-es';

const DEFAULT_ALIGNMENT = 'center';
const DEFAULT_ALIGNMENT = '';
const ERROR_TEXT_TIMEOUT = 500;

/**
Expand Down
9 changes: 6 additions & 3 deletions src/tableproperties/ui/tablepropertiesview.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ export default class TablePropertiesView extends View {
* The value of the table alignment style.
*
* @observable
* @default 'center'
* @default ''
* @member #alignment
*/
alignment: 'center',
alignment: ''
} );

/**
Expand Down Expand Up @@ -574,7 +574,10 @@ export default class TablePropertiesView extends View {
icons: ALIGNMENT_ICONS,
toolbar: alignmentToolbar,
labels: this._alignmentLabels,
propertyName: 'alignment'
propertyName: 'alignment',
nameToValue: name => {
return name === 'center' ? '' : name;
}
} );

return {
Expand Down
180 changes: 180 additions & 0 deletions tests/tablecellproperties/tablecellpropertiesediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item borderColor attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderColor:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'borderColor', '#f00', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderColor:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'borderColor', '#f00', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
model.change( writer => writer.setAttribute( 'borderColor', {
top: '#f00',
Expand Down Expand Up @@ -294,6 +314,26 @@ describe( 'table cell properties', () => {
);
} );

it( 'should consume converted item borderStyle attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderStyle:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'borderStyle', 'ridge', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderStyle:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'borderStyle', 'ridge', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
model.change( writer => writer.setAttribute( 'borderStyle', {
top: 'solid',
Expand All @@ -316,6 +356,26 @@ describe( 'table cell properties', () => {
assertTableCellStyle( editor, 'border-bottom:dotted;border-left:dashed;border-right:ridge;border-top:solid;' );
} );

it( 'should consume converted item borderWidth attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderWidth:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'borderWidth', '2px', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:borderWidth:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'borderWidth', '2px', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
model.change( writer => writer.setAttribute( 'borderWidth', {
top: '42px',
Expand Down Expand Up @@ -559,6 +619,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item backgroundColor attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:backgroundColor:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:backgroundColor:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast backgroundColor', () => {
model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );

Expand Down Expand Up @@ -637,6 +717,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item horizontalAlignment attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast horizontalAlignment=left', () => {
model.change( writer => writer.setAttribute( 'horizontalAlignment', 'left', tableCell ) );

Expand Down Expand Up @@ -694,6 +794,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item verticalAlignment attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:verticalAlignment:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:verticalAlignment:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast verticalAlignment', () => {
model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );

Expand Down Expand Up @@ -733,6 +853,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item borderColor attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:padding:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'padding', '1px', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:padding:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'padding', '1px', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast padding (same top, right, bottom, left)', () => {
model.change( writer => writer.setAttribute( 'padding', {
top: '2px',
Expand Down Expand Up @@ -807,6 +947,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item width attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:width:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'width', '40px', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:width:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'width', '40px', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast width attribute', () => {
model.change( writer => writer.setAttribute( 'width', '20px', tableCell ) );

Expand Down Expand Up @@ -868,6 +1028,26 @@ describe( 'table cell properties', () => {
tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
} );

it( 'should consume converted item height attribute', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:height:tableCell', ( evt, data, conversionApi ) => {
expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
} ) );

model.change( writer => writer.setAttribute( 'height', '40px', tableCell ) );
} );

it( 'should be overridable', () => {
editor.conversion.for( 'downcast' )
.add( dispatcher => dispatcher.on( 'attribute:height:tableCell', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.item, evt.name );
}, { priority: 'high' } ) );

model.change( writer => writer.setAttribute( 'height', '40px', tableCell ) );

assertTableCellStyle( editor, '' );
} );

it( 'should downcast height attribute', () => {
model.change( writer => writer.setAttribute( 'height', '20px', tableCell ) );

Expand Down
Loading

0 comments on commit a47f564

Please sign in to comment.