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

Commit 717608c

Browse files
authored
Merge pull request #234 from ckeditor/i/6197
Other: Convert `alignment` table property to a `<figure>` element. Closes ckeditor/ckeditor5#6197. Closes ckeditor/ckeditor5#6179.
2 parents 37f3796 + ea06fc7 commit 717608c

File tree

12 files changed

+492
-120
lines changed

12 files changed

+492
-120
lines changed

src/converters/tableproperties.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ export function downcastTableAttribute( conversion, modelAttribute, styleName )
105105
const { item, attributeNewValue } = data;
106106
const { mapper, writer } = conversionApi;
107107

108+
if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
109+
return;
110+
}
111+
108112
const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );
109113

110114
if ( attributeNewValue ) {

src/tableproperties/tablepropertiesediting.js

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import TableWidthCommand from './commands/tablewidthcommand';
2626
import TableHeightCommand from './commands/tableheightcommand';
2727
import TableAlignmentCommand from './commands/tablealignmentcommand';
2828

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

3331
/**
3432
* The table properties editing feature.
@@ -116,31 +114,32 @@ function enableAlignmentProperty( schema, conversion ) {
116114
schema.extend( 'table', {
117115
allowAttributes: [ 'alignment' ]
118116
} );
119-
conversion.for( 'upcast' )
117+
118+
conversion
120119
.attributeToAttribute( {
121-
view: {
122-
styles: {
123-
'margin-right': MARGIN_REG_EXP,
124-
'margin-left': MARGIN_REG_EXP
125-
}
126-
},
127120
model: {
128121
name: 'table',
129122
key: 'alignment',
130-
value: viewElement => {
131-
// At this point we only have auto or 0 value (with a unit).
132-
if ( viewElement.getStyle( 'margin-right' ) != 'auto' ) {
133-
return 'right';
123+
values: [ 'left', 'right' ]
124+
},
125+
view: {
126+
left: {
127+
key: 'style',
128+
value: {
129+
float: 'left'
134130
}
135-
136-
if ( viewElement.getStyle( 'margin-left' ) != 'auto' ) {
137-
return 'left';
131+
},
132+
right: {
133+
key: 'style',
134+
value: {
135+
float: 'right'
138136
}
139-
140-
return 'center';
141137
}
142-
}
143-
} )
138+
},
139+
converterPriority: 'high'
140+
} );
141+
142+
conversion.for( 'upcast' )
144143
// Support for backwards compatibility and pasting from other sources.
145144
.attributeToAttribute( {
146145
view: {
@@ -154,35 +153,6 @@ function enableAlignmentProperty( schema, conversion ) {
154153
value: viewElement => viewElement.getAttribute( 'align' )
155154
}
156155
} );
157-
158-
conversion.for( 'downcast' ).add( dispatcher => dispatcher.on( 'attribute:alignment:table', ( evt, data, conversionApi ) => {
159-
const { item, attributeNewValue } = data;
160-
const { mapper, writer } = conversionApi;
161-
162-
const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );
163-
164-
if ( !attributeNewValue ) {
165-
writer.removeStyle( 'margin-left', table );
166-
writer.removeStyle( 'margin-right', table );
167-
168-
return;
169-
}
170-
171-
const styles = {
172-
'margin-right': 'auto',
173-
'margin-left': 'auto'
174-
};
175-
176-
if ( attributeNewValue == 'left' ) {
177-
styles[ 'margin-left' ] = '0';
178-
}
179-
180-
if ( attributeNewValue == 'right' ) {
181-
styles[ 'margin-right' ] = '0';
182-
}
183-
184-
writer.setStyle( styles, table );
185-
} ) );
186156
}
187157

188158
// Enables conversion for an attribute for simple view-model mappings.

src/tableproperties/tablepropertiesui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import { debounce } from 'lodash-es';
2626

2727
const DEFAULT_BORDER_STYLE = 'none';
28-
const DEFAULT_ALIGNMENT = 'center';
28+
const DEFAULT_ALIGNMENT = '';
2929
const ERROR_TEXT_TIMEOUT = 500;
3030

3131
/**

src/tableproperties/ui/tablepropertiesview.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ export default class TablePropertiesView extends View {
141141
* The value of the table alignment style.
142142
*
143143
* @observable
144-
* @default 'center'
144+
* @default ''
145145
* @member #alignment
146146
*/
147-
alignment: 'center',
147+
alignment: ''
148148
} );
149149

150150
/**
@@ -574,7 +574,10 @@ export default class TablePropertiesView extends View {
574574
icons: ALIGNMENT_ICONS,
575575
toolbar: alignmentToolbar,
576576
labels: this._alignmentLabels,
577-
propertyName: 'alignment'
577+
propertyName: 'alignment',
578+
nameToValue: name => {
579+
return name === 'center' ? '' : name;
580+
}
578581
} );
579582

580583
return {

src/ui/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,23 @@ export function getBorderStyleDefinitions( view ) {
212212
* @param {module:ui/toolbar/toolbarview~ToolbarView} options.toolbar
213213
* @param {Object.<String,String>} labels
214214
* @param {String} propertyName
215+
* @param {Function} [nameToValue] Optional function that maps button name to value. By default names are the same as values.
215216
*/
216-
export function fillToolbar( { view, icons, toolbar, labels, propertyName } ) {
217+
export function fillToolbar( { view, icons, toolbar, labels, propertyName, nameToValue = name => name } ) {
217218
for ( const name in labels ) {
218219
const button = new ButtonView( view.locale );
219220

220221
button.set( {
221222
label: labels[ name ],
222-
icon: icons[ name ],
223+
icon: icons[ name ]
223224
} );
224225

225226
button.bind( 'isOn' ).to( view, propertyName, value => {
226-
return value === name;
227+
return value === nameToValue( name );
227228
} );
228229

229230
button.on( 'execute', () => {
230-
view[ propertyName ] = name;
231+
view[ propertyName ] = nameToValue( name );
231232
} );
232233

233234
toolbar.items.add( button );

0 commit comments

Comments
 (0)