1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin } from 'ckeditor5/src/core';
|
9 | import { addBorderRules, addPaddingRules, addBackgroundRules } from 'ckeditor5/src/engine';
|
10 | import { downcastAttributeToStyle, upcastBorderStyles } from './../converters/tableproperties';
|
11 | import TableEditing from './../tableediting';
|
12 | import TableCellWidthEditing from '../tablecellwidth/tablecellwidthediting';
|
13 | import TableCellPaddingCommand from './commands/tablecellpaddingcommand';
|
14 | import TableCellHeightCommand from './commands/tablecellheightcommand';
|
15 | import TableCellBackgroundColorCommand from './commands/tablecellbackgroundcolorcommand';
|
16 | import TableCellVerticalAlignmentCommand from './commands/tablecellverticalalignmentcommand';
|
17 | import TableCellHorizontalAlignmentCommand from './commands/tablecellhorizontalalignmentcommand';
|
18 | import TableCellBorderStyleCommand from './commands/tablecellborderstylecommand';
|
19 | import TableCellBorderColorCommand from './commands/tablecellbordercolorcommand';
|
20 | import TableCellBorderWidthCommand from './commands/tablecellborderwidthcommand';
|
21 | import { getNormalizedDefaultProperties } from '../utils/table-properties';
|
22 | import { enableProperty } from '../utils/common';
|
23 | const VALIGN_VALUES_REG_EXP = /^(top|middle|bottom)$/;
|
24 | const ALIGN_VALUES_REG_EXP = /^(left|center|right|justify)$/;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export default class TableCellPropertiesEditing extends Plugin {
|
45 | |
46 |
|
47 |
|
48 | static get pluginName() {
|
49 | return 'TableCellPropertiesEditing';
|
50 | }
|
51 | |
52 |
|
53 |
|
54 | static get requires() {
|
55 | return [TableEditing, TableCellWidthEditing];
|
56 | }
|
57 | |
58 |
|
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 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function 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 |
|
132 |
|
133 |
|
134 |
|
135 | function 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 |
|
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 |
|
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 |
|
188 |
|
189 |
|
190 |
|
191 | function 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 |
|
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 |
|
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 | }
|