UNPKG

10.6 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module table/tablecellproperties/tablecellpropertiesediting
7 */
8import { Plugin } from 'ckeditor5/src/core';
9import { addBorderRules, addPaddingRules, addBackgroundRules } from 'ckeditor5/src/engine';
10import { downcastAttributeToStyle, upcastBorderStyles } from './../converters/tableproperties';
11import TableEditing from './../tableediting';
12import TableCellWidthEditing from '../tablecellwidth/tablecellwidthediting';
13import TableCellPaddingCommand from './commands/tablecellpaddingcommand';
14import TableCellHeightCommand from './commands/tablecellheightcommand';
15import TableCellBackgroundColorCommand from './commands/tablecellbackgroundcolorcommand';
16import TableCellVerticalAlignmentCommand from './commands/tablecellverticalalignmentcommand';
17import TableCellHorizontalAlignmentCommand from './commands/tablecellhorizontalalignmentcommand';
18import TableCellBorderStyleCommand from './commands/tablecellborderstylecommand';
19import TableCellBorderColorCommand from './commands/tablecellbordercolorcommand';
20import TableCellBorderWidthCommand from './commands/tablecellborderwidthcommand';
21import { getNormalizedDefaultProperties } from '../utils/table-properties';
22import { enableProperty } from '../utils/common';
23const VALIGN_VALUES_REG_EXP = /^(top|middle|bottom)$/;
24const ALIGN_VALUES_REG_EXP = /^(left|center|right|justify)$/;
25/**
26 * The table cell properties editing feature.
27 *
28 * Introduces table cell model attributes and their conversion:
29 *
30 * - border: `tableCellBorderStyle`, `tableCellBorderColor` and `tableCellBorderWidth`
31 * - background color: `tableCellBackgroundColor`
32 * - cell padding: `tableCellPadding`
33 * - horizontal and vertical alignment: `tableCellHorizontalAlignment`, `tableCellVerticalAlignment`
34 * - cell width and height: `tableCellWidth`, `tableCellHeight`
35 *
36 * It also registers commands used to manipulate the above attributes:
37 *
38 * - border: the `'tableCellBorderStyle'`, `'tableCellBorderColor'` and `'tableCellBorderWidth'` commands
39 * - background color: the `'tableCellBackgroundColor'` command
40 * - cell padding: the `'tableCellPadding'` command
41 * - horizontal and vertical alignment: the `'tableCellHorizontalAlignment'` and `'tableCellVerticalAlignment'` commands
42 * - width and height: the `'tableCellWidth'` and `'tableCellHeight'` commands
43 */
44export default class TableCellPropertiesEditing extends Plugin {
45 /**
46 * @inheritDoc
47 */
48 static get pluginName() {
49 return 'TableCellPropertiesEditing';
50 }
51 /**
52 * @inheritDoc
53 */
54 static get requires() {
55 return [TableEditing, TableCellWidthEditing];
56 }
57 /**
58 * @inheritDoc
59 */
60 init() {
61 const editor = this.editor;
62 const schema = editor.model.schema;
63 const conversion = editor.conversion;
64 editor.config.define('table.tableCellProperties.defaultProperties', {});
65 const defaultTableCellProperties = getNormalizedDefaultProperties(editor.config.get('table.tableCellProperties.defaultProperties'), {
66 includeVerticalAlignmentProperty: true,
67 includeHorizontalAlignmentProperty: true,
68 includePaddingProperty: true,
69 isRightToLeftContent: editor.locale.contentLanguageDirection === 'rtl'
70 });
71 editor.data.addStyleProcessorRules(addBorderRules);
72 enableBorderProperties(schema, conversion, {
73 color: defaultTableCellProperties.borderColor,
74 style: defaultTableCellProperties.borderStyle,
75 width: defaultTableCellProperties.borderWidth
76 });
77 editor.commands.add('tableCellBorderStyle', new TableCellBorderStyleCommand(editor, defaultTableCellProperties.borderStyle));
78 editor.commands.add('tableCellBorderColor', new TableCellBorderColorCommand(editor, defaultTableCellProperties.borderColor));
79 editor.commands.add('tableCellBorderWidth', new TableCellBorderWidthCommand(editor, defaultTableCellProperties.borderWidth));
80 enableProperty(schema, conversion, {
81 modelAttribute: 'tableCellHeight',
82 styleName: 'height',
83 defaultValue: defaultTableCellProperties.height
84 });
85 editor.commands.add('tableCellHeight', new TableCellHeightCommand(editor, defaultTableCellProperties.height));
86 editor.data.addStyleProcessorRules(addPaddingRules);
87 enableProperty(schema, conversion, {
88 modelAttribute: 'tableCellPadding',
89 styleName: 'padding',
90 reduceBoxSides: true,
91 defaultValue: defaultTableCellProperties.padding
92 });
93 editor.commands.add('tableCellPadding', new TableCellPaddingCommand(editor, defaultTableCellProperties.padding));
94 editor.data.addStyleProcessorRules(addBackgroundRules);
95 enableProperty(schema, conversion, {
96 modelAttribute: 'tableCellBackgroundColor',
97 styleName: 'background-color',
98 defaultValue: defaultTableCellProperties.backgroundColor
99 });
100 editor.commands.add('tableCellBackgroundColor', new TableCellBackgroundColorCommand(editor, defaultTableCellProperties.backgroundColor));
101 enableHorizontalAlignmentProperty(schema, conversion, defaultTableCellProperties.horizontalAlignment);
102 editor.commands.add('tableCellHorizontalAlignment', new TableCellHorizontalAlignmentCommand(editor, defaultTableCellProperties.horizontalAlignment));
103 enableVerticalAlignmentProperty(schema, conversion, defaultTableCellProperties.verticalAlignment);
104 editor.commands.add('tableCellVerticalAlignment', new TableCellVerticalAlignmentCommand(editor, defaultTableCellProperties.verticalAlignment));
105 }
106}
107/**
108 * Enables the `'tableCellBorderStyle'`, `'tableCellBorderColor'` and `'tableCellBorderWidth'` attributes for table cells.
109 *
110 * @param defaultBorder The default border values.
111 * @param defaultBorder.color The default `tableCellBorderColor` value.
112 * @param defaultBorder.style The default `tableCellBorderStyle` value.
113 * @param defaultBorder.width The default `tableCellBorderWidth` value.
114 */
115function enableBorderProperties(schema, conversion, defaultBorder) {
116 const modelAttributes = {
117 width: 'tableCellBorderWidth',
118 color: 'tableCellBorderColor',
119 style: 'tableCellBorderStyle'
120 };
121 schema.extend('tableCell', {
122 allowAttributes: Object.values(modelAttributes)
123 });
124 upcastBorderStyles(conversion, 'td', modelAttributes, defaultBorder);
125 upcastBorderStyles(conversion, 'th', modelAttributes, defaultBorder);
126 downcastAttributeToStyle(conversion, { modelElement: 'tableCell', modelAttribute: modelAttributes.style, styleName: 'border-style' });
127 downcastAttributeToStyle(conversion, { modelElement: 'tableCell', modelAttribute: modelAttributes.color, styleName: 'border-color' });
128 downcastAttributeToStyle(conversion, { modelElement: 'tableCell', modelAttribute: modelAttributes.width, styleName: 'border-width' });
129}
130/**
131 * Enables the `'tableCellHorizontalAlignment'` attribute for table cells.
132 *
133 * @param defaultValue The default horizontal alignment value.
134 */
135function enableHorizontalAlignmentProperty(schema, conversion, defaultValue) {
136 schema.extend('tableCell', {
137 allowAttributes: ['tableCellHorizontalAlignment']
138 });
139 conversion.for('downcast')
140 .attributeToAttribute({
141 model: {
142 name: 'tableCell',
143 key: 'tableCellHorizontalAlignment'
144 },
145 view: alignment => ({
146 key: 'style',
147 value: {
148 'text-align': alignment
149 }
150 })
151 });
152 conversion.for('upcast')
153 // Support for the `text-align:*;` CSS definition for the table cell alignment.
154 .attributeToAttribute({
155 view: {
156 name: /^(td|th)$/,
157 styles: {
158 'text-align': ALIGN_VALUES_REG_EXP
159 }
160 },
161 model: {
162 key: 'tableCellHorizontalAlignment',
163 value: (viewElement) => {
164 const align = viewElement.getStyle('text-align');
165 return align === defaultValue ? null : align;
166 }
167 }
168 })
169 // Support for the `align` attribute as the backward compatibility while pasting from other sources.
170 .attributeToAttribute({
171 view: {
172 name: /^(td|th)$/,
173 attributes: {
174 align: ALIGN_VALUES_REG_EXP
175 }
176 },
177 model: {
178 key: 'tableCellHorizontalAlignment',
179 value: (viewElement) => {
180 const align = viewElement.getAttribute('align');
181 return align === defaultValue ? null : align;
182 }
183 }
184 });
185}
186/**
187 * Enables the `'verticalAlignment'` attribute for table cells.
188 *
189 * @param defaultValue The default vertical alignment value.
190 */
191function enableVerticalAlignmentProperty(schema, conversion, defaultValue) {
192 schema.extend('tableCell', {
193 allowAttributes: ['tableCellVerticalAlignment']
194 });
195 conversion.for('downcast')
196 .attributeToAttribute({
197 model: {
198 name: 'tableCell',
199 key: 'tableCellVerticalAlignment'
200 },
201 view: alignment => ({
202 key: 'style',
203 value: {
204 'vertical-align': alignment
205 }
206 })
207 });
208 conversion.for('upcast')
209 // Support for the `vertical-align:*;` CSS definition for the table cell alignment.
210 .attributeToAttribute({
211 view: {
212 name: /^(td|th)$/,
213 styles: {
214 'vertical-align': VALIGN_VALUES_REG_EXP
215 }
216 },
217 model: {
218 key: 'tableCellVerticalAlignment',
219 value: (viewElement) => {
220 const align = viewElement.getStyle('vertical-align');
221 return align === defaultValue ? null : align;
222 }
223 }
224 })
225 // Support for the `align` attribute as the backward compatibility while pasting from other sources.
226 .attributeToAttribute({
227 view: {
228 name: /^(td|th)$/,
229 attributes: {
230 valign: VALIGN_VALUES_REG_EXP
231 }
232 },
233 model: {
234 key: 'tableCellVerticalAlignment',
235 value: (viewElement) => {
236 const valign = viewElement.getAttribute('valign');
237 return valign === defaultValue ? null : valign;
238 }
239 }
240 });
241}