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

Commit d57085d

Browse files
authored
Merge pull request #224 from ckeditor/i/3287
Feature: Introduced the table and table cell properties plugins. Closes ckeditor/ckeditor5#6048.
2 parents ffbc6c9 + 3719705 commit d57085d

File tree

67 files changed

+6620
-370
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+6620
-370
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dependencies": {
1313
"@ckeditor/ckeditor5-core": "^16.0.0",
1414
"@ckeditor/ckeditor5-ui": "^16.0.0",
15-
"@ckeditor/ckeditor5-widget": "^16.0.0"
15+
"@ckeditor/ckeditor5-widget": "^16.0.0",
16+
"lodash-es": "^4.17.10"
1617
},
1718
"devDependencies": {
1819
"@ckeditor/ckeditor5-alignment": "^16.0.0",
@@ -21,6 +22,7 @@
2122
"@ckeditor/ckeditor5-editor-classic": "^16.0.0",
2223
"@ckeditor/ckeditor5-engine": "^16.0.0",
2324
"@ckeditor/ckeditor5-image": "^16.0.0",
25+
"@ckeditor/ckeditor5-indent": "^16.0.0",
2426
"@ckeditor/ckeditor5-list": "^16.0.0",
2527
"@ckeditor/ckeditor5-media-embed": "^16.0.0",
2628
"@ckeditor/ckeditor5-paragraph": "^16.0.0",

src/commands/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @module table/commands/utils
88
*/
99

10+
import { isObject } from 'lodash-es';
11+
1012
/**
1113
* Returns the parent element of given name. Returns undefined if position is not inside desired parent.
1214
*
@@ -55,3 +57,31 @@ export function createEmptyTableCell( writer, insertPosition, attributes = {} )
5557
writer.insertElement( 'paragraph', tableCell );
5658
writer.insert( tableCell, insertPosition );
5759
}
60+
61+
/**
62+
* Returns a string if all four values of box sides are equal.
63+
*
64+
* If a string is passed, it is treated as a single value (pass-through).
65+
*
66+
* // returns 'foo':
67+
* getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
68+
* getSingleValue( 'foo' );
69+
*
70+
* // Returns undefined:
71+
* getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
72+
* getSingleValue( { top: 'foo', right: 'foo' } );
73+
*
74+
* @param objectOrString
75+
* @returns {module:engine/view/stylesmap~BoxSides|String}
76+
*/
77+
export function getSingleValue( objectOrString ) {
78+
if ( !objectOrString || !isObject( objectOrString ) ) {
79+
return objectOrString;
80+
}
81+
82+
const { top, right, bottom, left } = objectOrString;
83+
84+
if ( top == right && right == bottom && bottom == left ) {
85+
return top;
86+
}
87+
}

src/converters/tableproperties.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/converters/tableproperites
8+
*/
9+
10+
/**
11+
* Conversion helper for upcasting attribute using normalized styles.
12+
*
13+
* @param {module:engine/conversion/conversion~Conversion} conversion
14+
* @param {String} modelElement
15+
* @param {String} modelAttribute
16+
* @param {String} styleName
17+
*/
18+
export function upcastStyleToAttribute( conversion, modelElement, modelAttribute, styleName ) {
19+
conversion.for( 'upcast' ).attributeToAttribute( {
20+
view: {
21+
styles: {
22+
[ styleName ]: /[\s\S]+/
23+
}
24+
},
25+
model: {
26+
name: modelElement,
27+
key: modelAttribute,
28+
value: viewElement => viewElement.getNormalizedStyle( styleName )
29+
}
30+
} );
31+
}
32+
33+
/**
34+
* Conversion helper for upcasting border styles for view element.
35+
*
36+
* @param {module:engine/conversion/conversion~Conversion} conversion
37+
* @param {String} viewElementName
38+
*/
39+
export function upcastBorderStyles( conversion, viewElementName ) {
40+
conversion.for( 'upcast' ).add( dispatcher => dispatcher.on( 'element:' + viewElementName, ( evt, data, conversionApi ) => {
41+
// TODO: this is counter-intuitive: ie.: if only `border-top` is defined then `hasStyle( 'border' )` also returns true.
42+
// TODO: this might needs to be fixed in styles normalizer.
43+
const stylesToConsume = [
44+
'border-top',
45+
'border-right',
46+
'border-bottom',
47+
'border-left'
48+
].filter( styleName => data.viewItem.hasStyle( styleName ) );
49+
50+
if ( !stylesToConsume.length ) {
51+
return;
52+
}
53+
54+
const matcherPattern = {
55+
styles: stylesToConsume
56+
};
57+
58+
// Try to consume appropriate values from consumable values list.
59+
if ( !conversionApi.consumable.test( data.viewItem, matcherPattern ) ) {
60+
return;
61+
}
62+
63+
const modelElement = [ ...data.modelRange.getItems( { shallow: true } ) ].pop();
64+
65+
conversionApi.consumable.consume( data.viewItem, matcherPattern );
66+
67+
conversionApi.writer.setAttribute( 'borderStyle', data.viewItem.getNormalizedStyle( 'border-style' ), modelElement );
68+
conversionApi.writer.setAttribute( 'borderColor', data.viewItem.getNormalizedStyle( 'border-color' ), modelElement );
69+
conversionApi.writer.setAttribute( 'borderWidth', data.viewItem.getNormalizedStyle( 'border-width' ), modelElement );
70+
} ) );
71+
}
72+
73+
/**
74+
* Conversion helper for downcasting attribute to a style.
75+
*
76+
* @param {module:engine/conversion/conversion~Conversion} conversion
77+
* @param {String} modelElement
78+
* @param {String} modelAttribute
79+
* @param {String} styleName
80+
*/
81+
export function downcastAttributeToStyle( conversion, modelElement, modelAttribute, styleName ) {
82+
conversion.for( 'downcast' ).attributeToAttribute( {
83+
model: {
84+
name: modelElement,
85+
key: modelAttribute
86+
},
87+
view: modelAttributeValue => ( {
88+
key: 'style',
89+
value: {
90+
[ styleName ]: modelAttributeValue
91+
}
92+
} )
93+
} );
94+
}
95+
96+
/**
97+
* Conversion helper for downcasting attributes from model's table to a view table (not to figure).
98+
*
99+
* @param {module:engine/conversion/conversion~Conversion} conversion
100+
* @param {String} modelAttribute
101+
* @param {String} styleName
102+
*/
103+
export function downcastTableAttribute( conversion, modelAttribute, styleName ) {
104+
conversion.for( 'downcast' ).add( dispatcher => dispatcher.on( `attribute:${ modelAttribute }:table`, ( evt, data, conversionApi ) => {
105+
const { item, attributeNewValue } = data;
106+
const { mapper, writer } = conversionApi;
107+
108+
const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );
109+
110+
if ( attributeNewValue ) {
111+
writer.setStyle( styleName, attributeNewValue, table );
112+
} else {
113+
writer.removeAttribute( styleName, table );
114+
}
115+
} ) );
116+
}

src/tablecellproperties.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/tablecellproperties
8+
*/
9+
10+
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
11+
12+
import TableCellPropertiesEditing from './tablecellproperties/tablecellpropertiesediting';
13+
14+
/**
15+
* The table cell properties feature.
16+
*
17+
* This is a "glue" plugin which loads the
18+
* {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing table properties editing feature} and
19+
* table cell properties UI feature.
20+
*
21+
* @extends module:core/plugin~Plugin
22+
*/
23+
export default class TableCellProperties extends Plugin {
24+
/**
25+
* @inheritDoc
26+
*/
27+
static get pluginName() {
28+
return 'TableCellProperties';
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
static get requires() {
35+
return [ TableCellPropertiesEditing ];
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/tablecellproperties/commands/tablecellbackgroundcolorcommand
8+
*/
9+
10+
import TableCellPropertyCommand from './tablecellpropertycommand';
11+
12+
/**
13+
* The table cell background color command.
14+
*
15+
* The command is registered by the {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
16+
* `'tableCellBackgroundColor'` editor command.
17+
*
18+
* To change the background color of selected cells, execute the command:
19+
*
20+
* editor.execute( 'tableCellBackgroundColor', {
21+
* value: '#f00'
22+
* } );
23+
*
24+
* @extends module:core/command~Command
25+
*/
26+
export default class TableCellBackgroundColorCommand extends TableCellPropertyCommand {
27+
/**
28+
* Creates a new `TableCellBackgroundColorCommand` instance.
29+
*
30+
* @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
31+
*/
32+
constructor( editor ) {
33+
super( editor, 'backgroundColor' );
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/tablecellproperties/commands/tablecellbordercolorcommand
8+
*/
9+
10+
import TableCellPropertyCommand from './tablecellpropertycommand';
11+
import { getSingleValue } from '../../commands/utils';
12+
13+
/**
14+
* The table cell border color command.
15+
*
16+
* The command is registered by the {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
17+
* `'tableCellBorderColor'` editor command.
18+
*
19+
* To change the border color of selected cells, execute the command:
20+
*
21+
* editor.execute( 'tableCellBorderColor', {
22+
* value: '#f00'
23+
* } );
24+
*
25+
* @extends module:core/command~Command
26+
*/
27+
export default class TableCellBorderColorCommand extends TableCellPropertyCommand {
28+
/**
29+
* Creates a new `TableCellBorderWidthCommand` instance.
30+
*
31+
* @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
32+
*/
33+
constructor( editor ) {
34+
super( editor, 'borderColor' );
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
_getAttribute( tableCell ) {
41+
if ( !tableCell ) {
42+
return;
43+
}
44+
45+
return getSingleValue( tableCell.getAttribute( this.attributeName ) );
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/tablecellproperties/commands/tablecellborderstylecommand
8+
*/
9+
10+
import TableCellPropertyCommand from './tablecellpropertycommand';
11+
import { getSingleValue } from '../../commands/utils';
12+
13+
/**
14+
* The table cell border style command.
15+
*
16+
* The command is registered by the {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
17+
* `'tableCellBorderStyle'` editor command.
18+
*
19+
* To change the border style of selected cells, execute the command:
20+
*
21+
* editor.execute( 'tableCellBorderStyle', {
22+
* value: 'dashed'
23+
* } );
24+
*
25+
* @extends module:core/command~Command
26+
*/
27+
export default class TableCellBorderStyleCommand extends TableCellPropertyCommand {
28+
/**
29+
* Creates a new `TableCellBorderWidthCommand` instance.
30+
*
31+
* @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
32+
*/
33+
constructor( editor ) {
34+
super( editor, 'borderStyle' );
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
_getAttribute( tableCell ) {
41+
if ( !tableCell ) {
42+
return;
43+
}
44+
45+
return getSingleValue( tableCell.getAttribute( this.attributeName ) );
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module table/tablecellproperties/commands/tablecellborderwidthcommand
8+
*/
9+
10+
import TableCellPropertyCommand from './tablecellpropertycommand';
11+
import { getSingleValue } from '../../commands/utils';
12+
13+
/**
14+
* The table cell border width command.
15+
*
16+
* The command is registered by the {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
17+
* `'tableCellBorderWidth'` editor command.
18+
*
19+
* To change the border width of selected cells, execute the command:
20+
*
21+
* editor.execute( 'tableCellBorderWidth', {
22+
* value: '5px'
23+
* } );
24+
*
25+
* @extends module:core/command~Command
26+
*/
27+
export default class TableCellBorderWidthCommand extends TableCellPropertyCommand {
28+
/**
29+
* Creates a new `TableCellBorderWidthCommand` instance.
30+
*
31+
* @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
32+
*/
33+
constructor( editor ) {
34+
super( editor, 'borderWidth' );
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
_getAttribute( tableCell ) {
41+
if ( !tableCell ) {
42+
return;
43+
}
44+
45+
return getSingleValue( tableCell.getAttribute( this.attributeName ) );
46+
}
47+
}

0 commit comments

Comments
 (0)