-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
tablepropertiesediting.ts
261 lines (230 loc) · 8.13 KB
/
tablepropertiesediting.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tableproperties/tablepropertiesediting
*/
import { Plugin } from 'ckeditor5/src/core.js';
import { addBackgroundRules, addBorderRules, type ViewElement, type Conversion, type Schema } from 'ckeditor5/src/engine.js';
import TableEditing from '../tableediting.js';
import {
downcastAttributeToStyle,
downcastTableAttribute,
upcastBorderStyles,
upcastStyleToAttribute
} from '../converters/tableproperties.js';
import TableBackgroundColorCommand from './commands/tablebackgroundcolorcommand.js';
import TableBorderColorCommand from './commands/tablebordercolorcommand.js';
import TableBorderStyleCommand from './commands/tableborderstylecommand.js';
import TableBorderWidthCommand from './commands/tableborderwidthcommand.js';
import TableWidthCommand from './commands/tablewidthcommand.js';
import TableHeightCommand from './commands/tableheightcommand.js';
import TableAlignmentCommand from './commands/tablealignmentcommand.js';
import { getNormalizedDefaultProperties } from '../utils/table-properties.js';
const ALIGN_VALUES_REG_EXP = /^(left|center|right)$/;
const FLOAT_VALUES_REG_EXP = /^(left|none|right)$/;
/**
* The table properties editing feature.
*
* Introduces table's model attributes and their conversion:
*
* - border: `tableBorderStyle`, `tableBorderColor` and `tableBorderWidth`
* - background color: `tableBackgroundColor`
* - horizontal alignment: `tableAlignment`
* - width & height: `tableWidth` & `tableHeight`
*
* 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'`
*/
export default class TablePropertiesEditing extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'TablePropertiesEditing' as const;
}
/**
* @inheritDoc
*/
public static get requires() {
return [ TableEditing ] as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const schema = editor.model.schema;
const conversion = editor.conversion;
editor.config.define( 'table.tableProperties.defaultProperties', {} );
const defaultTableProperties = getNormalizedDefaultProperties( editor.config.get( 'table.tableProperties.defaultProperties' )!, {
includeAlignmentProperty: true
} );
editor.data.addStyleProcessorRules( addBorderRules );
enableBorderProperties( schema, conversion, {
color: defaultTableProperties.borderColor,
style: defaultTableProperties.borderStyle,
width: defaultTableProperties.borderWidth
} );
editor.commands.add( 'tableBorderColor', new TableBorderColorCommand( editor, defaultTableProperties.borderColor ) );
editor.commands.add( 'tableBorderStyle', new TableBorderStyleCommand( editor, defaultTableProperties.borderStyle ) );
editor.commands.add( 'tableBorderWidth', new TableBorderWidthCommand( editor, defaultTableProperties.borderWidth ) );
enableAlignmentProperty( schema, conversion, defaultTableProperties.alignment! );
editor.commands.add( 'tableAlignment', new TableAlignmentCommand( editor, defaultTableProperties.alignment! ) );
enableTableToFigureProperty( schema, conversion, {
modelAttribute: 'tableWidth',
styleName: 'width',
defaultValue: defaultTableProperties.width
} );
editor.commands.add( 'tableWidth', new TableWidthCommand( editor, defaultTableProperties.width ) );
enableTableToFigureProperty( schema, conversion, {
modelAttribute: 'tableHeight',
styleName: 'height',
defaultValue: defaultTableProperties.height
} );
editor.commands.add( 'tableHeight', new TableHeightCommand( editor, defaultTableProperties.height ) );
editor.data.addStyleProcessorRules( addBackgroundRules );
enableProperty( schema, conversion, {
modelAttribute: 'tableBackgroundColor',
styleName: 'background-color',
defaultValue: defaultTableProperties.backgroundColor
} );
editor.commands.add(
'tableBackgroundColor',
new TableBackgroundColorCommand( editor, defaultTableProperties.backgroundColor )
);
}
}
/**
* Enables `tableBorderStyle'`, `tableBorderColor'` and `tableBorderWidth'` attributes for table.
*
* @param defaultBorder The default border values.
* @param defaultBorder.color The default `tableBorderColor` value.
* @param defaultBorder.style The default `tableBorderStyle` value.
* @param defaultBorder.width The default `tableBorderWidth` value.
*/
function enableBorderProperties( schema: Schema, conversion: Conversion, defaultBorder: { color: string; style: string; width: string } ) {
const modelAttributes = {
width: 'tableBorderWidth',
color: 'tableBorderColor',
style: 'tableBorderStyle'
};
schema.extend( 'table', {
allowAttributes: Object.values( modelAttributes )
} );
upcastBorderStyles( conversion, 'table', modelAttributes, defaultBorder );
downcastTableAttribute( conversion, { modelAttribute: modelAttributes.color, styleName: 'border-color' } );
downcastTableAttribute( conversion, { modelAttribute: modelAttributes.style, styleName: 'border-style' } );
downcastTableAttribute( conversion, { modelAttribute: modelAttributes.width, styleName: 'border-width' } );
}
/**
* Enables the `'alignment'` attribute for table.
*
* @param defaultValue The default alignment value.
*/
function enableAlignmentProperty( schema: Schema, conversion: Conversion, defaultValue: string ) {
schema.extend( 'table', {
allowAttributes: [ 'tableAlignment' ]
} );
conversion.for( 'downcast' )
.attributeToAttribute( {
model: {
name: 'table',
key: 'tableAlignment'
},
view: alignment => ( {
key: 'style',
value: {
// Model: `alignment:center` => CSS: `float:none`.
float: alignment === 'center' ? 'none' : alignment
}
} ),
converterPriority: 'high'
} );
conversion.for( 'upcast' )
// Support for the `float:*;` CSS definition for the table alignment.
.attributeToAttribute( {
view: {
name: /^(table|figure)$/,
styles: {
float: FLOAT_VALUES_REG_EXP
}
},
model: {
key: 'tableAlignment',
value: ( viewElement: ViewElement ) => {
let align = viewElement.getStyle( 'float' );
// CSS: `float:none` => Model: `alignment:center`.
if ( align === 'none' ) {
align = 'center';
}
return align === defaultValue ? null : align;
}
}
} )
// Support for the `align` attribute as the backward compatibility while pasting from other sources.
.attributeToAttribute( {
view: {
attributes: {
align: ALIGN_VALUES_REG_EXP
}
},
model: {
name: 'table',
key: 'tableAlignment',
value: ( viewElement: ViewElement ) => {
const align = viewElement.getAttribute( 'align' );
return align === defaultValue ? null : align;
}
}
} );
}
/**
* Enables conversion for an attribute for simple view-model mappings.
*
* @param options.defaultValue The default value for the specified `modelAttribute`.
*/
function enableProperty(
schema: Schema,
conversion: Conversion,
options: {
modelAttribute: string;
styleName: string;
defaultValue: string;
}
) {
const { modelAttribute } = options;
schema.extend( 'table', {
allowAttributes: [ modelAttribute ]
} );
upcastStyleToAttribute( conversion, { viewElement: 'table', ...options } );
downcastTableAttribute( conversion, options );
}
/**
* Enables conversion for an attribute for simple view (figure) to model (table) mappings.
*/
function enableTableToFigureProperty(
schema: Schema,
conversion: Conversion,
options: {
modelAttribute: string;
styleName: string;
defaultValue: string;
}
) {
const { modelAttribute } = options;
schema.extend( 'table', {
allowAttributes: [ modelAttribute ]
} );
upcastStyleToAttribute( conversion, {
viewElement: /^(table|figure)$/,
shouldUpcast: ( element: ViewElement ) => !( element.name == 'table' && element.parent!.name == 'figure' ),
...options
} );
downcastAttributeToStyle( conversion, { modelElement: 'table', ...options } );
}