/
tablepropertiesediting.js
183 lines (165 loc) · 5.87 KB
/
tablepropertiesediting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tableproperties/tablepropertiesediting
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import { addBorderRules } from '@ckeditor/ckeditor5-engine/src/view/styles/border';
import { addBackgroundRules } from '@ckeditor/ckeditor5-engine/src/view/styles/background';
import TableEditing from '../tableediting';
import {
downcastAttributeToStyle,
downcastTableAttribute,
upcastBorderStyles,
upcastStyleToAttribute
} from '../converters/tableproperties';
import TableBackgroundColorCommand from './commands/tablebackgroundcolorcommand';
import TableBorderColorCommand from './commands/tablebordercolorcommand';
import TableBorderStyleCommand from './commands/tableborderstylecommand';
import TableBorderWidthCommand from './commands/tableborderwidthcommand';
import TableWidthCommand from './commands/tablewidthcommand';
import TableHeightCommand from './commands/tableheightcommand';
import TableAlignmentCommand from './commands/tablealignmentcommand';
const ALIGN_VALUES_REG_EXP = /^(left|right)$/;
/**
* The table properties editing feature.
*
* Introduces table's model attributes and their conversion:
*
* - border: `borderStyle`, `borderColor` and `borderWidth`
* - background color: `backgroundColor`
* - horizontal alignment: `alignment`
* - width & height: `width` & `height`
*
* It also registers commands used to manipulate the above attributes:
*
* - border: `'tableBorderStyle'`, `'tableBorderColor'` and `'tableBorderWidth'` commands
* - background color: `'tableBackgroundColor'`
* - horizontal alignment: `'tableAlignment'`
* - width & height: `'tableWidth'` & `'tableHeight'`
*
* @extends module:core/plugin~Plugin
*/
export default class TablePropertiesEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'TablePropertiesEditing';
}
/**
* @inheritDoc
*/
static get requires() {
return [ TableEditing ];
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const schema = editor.model.schema;
const conversion = editor.conversion;
editor.data.addStyleProcessorRules( addBorderRules );
enableBorderProperties( schema, conversion );
editor.commands.add( 'tableBorderColor', new TableBorderColorCommand( editor ) );
editor.commands.add( 'tableBorderStyle', new TableBorderStyleCommand( editor ) );
editor.commands.add( 'tableBorderWidth', new TableBorderWidthCommand( editor ) );
enableAlignmentProperty( schema, conversion );
editor.commands.add( 'tableAlignment', new TableAlignmentCommand( editor ) );
enableTableToFigureProperty( schema, conversion, 'width', 'width' );
editor.commands.add( 'tableWidth', new TableWidthCommand( editor ) );
enableTableToFigureProperty( schema, conversion, 'height', 'height' );
editor.commands.add( 'tableHeight', new TableHeightCommand( editor ) );
editor.data.addStyleProcessorRules( addBackgroundRules );
enableProperty( schema, conversion, 'backgroundColor', 'background-color' );
editor.commands.add( 'tableBackgroundColor', new TableBackgroundColorCommand( editor ) );
}
}
// Enables `'borderStyle'`, `'borderColor'` and `'borderWidth'` attributes for table.
//
// @param {module:engine/model/schema~Schema} schema
// @param {module:engine/conversion/conversion~Conversion} conversion
function enableBorderProperties( schema, conversion ) {
schema.extend( 'table', {
allowAttributes: [ 'borderWidth', 'borderColor', 'borderStyle' ]
} );
upcastBorderStyles( conversion, 'table' );
downcastTableAttribute( conversion, 'borderColor', 'border-color' );
downcastTableAttribute( conversion, 'borderStyle', 'border-style' );
downcastTableAttribute( conversion, 'borderWidth', 'border-width' );
}
// Enables the `'alignment'` attribute for table.
//
// @param {module:engine/model/schema~Schema} schema
// @param {module:engine/conversion/conversion~Conversion} conversion
function enableAlignmentProperty( schema, conversion ) {
schema.extend( 'table', {
allowAttributes: [ 'alignment' ]
} );
conversion
.attributeToAttribute( {
model: {
name: 'table',
key: 'alignment',
values: [ 'left', 'right' ]
},
view: {
left: {
key: 'style',
value: {
float: 'left'
}
},
right: {
key: 'style',
value: {
float: 'right'
}
}
},
converterPriority: 'high'
} );
conversion.for( 'upcast' )
// Support for backwards compatibility and pasting from other sources.
.attributeToAttribute( {
view: {
attributes: {
align: ALIGN_VALUES_REG_EXP
}
},
model: {
name: 'table',
key: 'alignment',
value: viewElement => viewElement.getAttribute( 'align' )
}
} );
}
// Enables conversion for an attribute for simple view-model mappings.
//
// @param {String} modelAttribute
// @param {String} styleName
// @param {module:engine/model/schema~Schema} schema
// @param {module:engine/conversion/conversion~Conversion} conversion
function enableProperty( schema, conversion, modelAttribute, styleName ) {
schema.extend( 'table', {
allowAttributes: [ modelAttribute ]
} );
upcastStyleToAttribute( conversion, 'table', modelAttribute, styleName );
downcastTableAttribute( conversion, modelAttribute, styleName );
}
// Enables conversion for an attribute for simple view (figure) to model (table) mappings.
//
// @param {String} modelAttribute
// @param {String} styleName
// @param {module:engine/model/schema~Schema} schema
// @param {module:engine/conversion/conversion~Conversion} conversion
function enableTableToFigureProperty( schema, conversion, modelAttribute, styleName ) {
schema.extend( 'table', {
allowAttributes: [ modelAttribute ]
} );
upcastStyleToAttribute( conversion, 'table', modelAttribute, styleName );
downcastAttributeToStyle( conversion, 'table', modelAttribute, styleName );
}