|
@@ -31,7 +31,14 @@ export default class TableProperties extends Plugin {
|
|
|
const schema = editor.model.schema;
|
|
const schema = editor.model.schema;
|
|
|
const conversion = editor.conversion;
|
|
const conversion = editor.conversion;
|
|
|
|
|
|
|
|
- // Border
|
|
|
|
|
|
|
+ this.enableBorderProperty( schema, conversion );
|
|
|
|
|
+ this.enableBackgroundColorProperty( schema, conversion );
|
|
|
|
|
+ this.enableWidthProperty( schema, conversion );
|
|
|
|
|
+ this.enableHeightProperty( schema, conversion );
|
|
|
|
|
+ this.enableAlignmentProperty( schema, conversion );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ enableBorderProperty( schema, conversion ) {
|
|
|
schema.extend( 'table', {
|
|
schema.extend( 'table', {
|
|
|
allowAttributes: [ 'borderWidth', 'borderColor', 'borderStyle' ]
|
|
allowAttributes: [ 'borderWidth', 'borderColor', 'borderStyle' ]
|
|
|
} );
|
|
} );
|
|
@@ -39,26 +46,101 @@ export default class TableProperties extends Plugin {
|
|
|
downcastTableAttribute( conversion, 'borderColor', 'border-color' );
|
|
downcastTableAttribute( conversion, 'borderColor', 'border-color' );
|
|
|
downcastTableAttribute( conversion, 'borderStyle', 'border-style' );
|
|
downcastTableAttribute( conversion, 'borderStyle', 'border-style' );
|
|
|
downcastTableAttribute( conversion, 'borderWidth', 'border-width' );
|
|
downcastTableAttribute( conversion, 'borderWidth', 'border-width' );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Background
|
|
|
|
|
|
|
+ enableBackgroundColorProperty( schema, conversion ) {
|
|
|
schema.extend( 'table', {
|
|
schema.extend( 'table', {
|
|
|
allowAttributes: [ 'backgroundColor' ]
|
|
allowAttributes: [ 'backgroundColor' ]
|
|
|
} );
|
|
} );
|
|
|
upcastAttribute( conversion, 'table', 'backgroundColor', 'background-color' );
|
|
upcastAttribute( conversion, 'table', 'backgroundColor', 'background-color' );
|
|
|
downcastTableAttribute( conversion, 'backgroundColor', 'background-color' );
|
|
downcastTableAttribute( conversion, 'backgroundColor', 'background-color' );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Width
|
|
|
|
|
|
|
+ enableWidthProperty( schema, conversion ) {
|
|
|
schema.extend( 'table', {
|
|
schema.extend( 'table', {
|
|
|
allowAttributes: [ 'width' ]
|
|
allowAttributes: [ 'width' ]
|
|
|
} );
|
|
} );
|
|
|
upcastAttribute( conversion, 'table', 'width', 'width' );
|
|
upcastAttribute( conversion, 'table', 'width', 'width' );
|
|
|
downcastTableAttribute( conversion, 'width', 'width' );
|
|
downcastTableAttribute( conversion, 'width', 'width' );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Height
|
|
|
|
|
|
|
+ enableHeightProperty( schema, conversion ) {
|
|
|
schema.extend( 'table', {
|
|
schema.extend( 'table', {
|
|
|
allowAttributes: [ 'height' ]
|
|
allowAttributes: [ 'height' ]
|
|
|
} );
|
|
} );
|
|
|
upcastAttribute( conversion, 'table', 'height', 'height' );
|
|
upcastAttribute( conversion, 'table', 'height', 'height' );
|
|
|
downcastTableAttribute( conversion, 'height', 'height' );
|
|
downcastTableAttribute( conversion, 'height', 'height' );
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ enableAlignmentProperty( schema, conversion ) {
|
|
|
|
|
+ schema.extend( 'table', {
|
|
|
|
|
+ allowAttributes: [ 'alignment' ]
|
|
|
|
|
+ } );
|
|
|
|
|
+ conversion.for( 'upcast' )
|
|
|
|
|
+ .attributeToAttribute( {
|
|
|
|
|
+ view: {
|
|
|
|
|
+ styles: {
|
|
|
|
|
+ 'margin-right': /^(auto|0(%|[a-z]{2,4})?)$/,
|
|
|
|
|
+ 'margin-left': /^(auto|0)/
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ 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';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ( viewElement.getStyle( 'margin-left' ) != 'auto' ) {
|
|
|
|
|
+ return 'left';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 'center';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } )
|
|
|
|
|
+ // Support for backwards compatibility and pasting from other sources.
|
|
|
|
|
+ .attributeToAttribute( {
|
|
|
|
|
+ view: {
|
|
|
|
|
+ attributes: {
|
|
|
|
|
+ align: /^(left|right|center)$/
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ model: {
|
|
|
|
|
+ name: 'table',
|
|
|
|
|
+ key: 'alignment',
|
|
|
|
|
+ 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 );
|
|
|
|
|
+ } ) );
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|