@ckeditor/ckeditor5-table 0.0.0-nightly-20251125.0 → 0.0.0-nightly-20251126.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/build/table.js +1 -1
  2. package/dist/index-content.css +30 -1
  3. package/dist/index-editor.css +181 -88
  4. package/dist/index.css +300 -158
  5. package/dist/index.css.map +1 -1
  6. package/dist/index.js +12442 -9941
  7. package/dist/index.js.map +1 -1
  8. package/lang/contexts.json +6 -0
  9. package/package.json +9 -9
  10. package/src/augmentation.d.ts +7 -0
  11. package/src/converters/downcast.d.ts +26 -1
  12. package/src/converters/downcast.js +129 -3
  13. package/src/converters/tableproperties.d.ts +43 -1
  14. package/src/converters/tableproperties.js +186 -0
  15. package/src/converters/upcasttable.js +2 -11
  16. package/src/index.d.ts +5 -1
  17. package/src/index.js +5 -0
  18. package/src/plaintableoutput.js +0 -91
  19. package/src/tablecellproperties/tablecellpropertiesuiexperimental.d.ts +128 -0
  20. package/src/tablecellproperties/tablecellpropertiesuiexperimental.js +386 -0
  21. package/src/tablecellproperties/ui/tablecellpropertiesviewexperimental.d.ts +237 -0
  22. package/src/tablecellproperties/ui/tablecellpropertiesviewexperimental.js +633 -0
  23. package/src/tableconfig.d.ts +187 -10
  24. package/src/tableediting.d.ts +5 -0
  25. package/src/tableediting.js +28 -1
  26. package/src/tableproperties/tablepropertiesediting.js +250 -5
  27. package/src/tableproperties/tablepropertiesuiexperimental.d.ts +136 -0
  28. package/src/tableproperties/tablepropertiesuiexperimental.js +375 -0
  29. package/src/tableproperties/ui/tablepropertiesviewexperimental.d.ts +216 -0
  30. package/src/tableproperties/ui/tablepropertiesviewexperimental.js +544 -0
  31. package/src/utils/structure.d.ts +5 -1
  32. package/src/utils/structure.js +10 -0
  33. package/src/utils/ui/table-propertiesexperimental.d.ts +215 -0
  34. package/src/utils/ui/table-propertiesexperimental.js +391 -0
  35. package/theme/formrow-experimental.css +15 -0
  36. package/theme/tableform-experimental.css +61 -0
  37. package/theme/tableproperties-experimental.css +78 -0
@@ -37,28 +37,6 @@ export class PlainTableOutput extends Plugin {
37
37
  */
38
38
  init() {
39
39
  const editor = this.editor;
40
- // Override default table data downcast converter.
41
- editor.conversion.for('dataDowncast').elementToStructure({
42
- model: 'table',
43
- view: downcastTableElement,
44
- converterPriority: 'high'
45
- });
46
- // Make sure table <caption> is downcasted into <caption> in the data pipeline when necessary.
47
- if (editor.plugins.has('TableCaption')) {
48
- editor.conversion.for('dataDowncast').elementToElement({
49
- model: 'caption',
50
- view: (modelElement, { writer }) => {
51
- if (modelElement.parent.name === 'table') {
52
- return writer.createContainerElement('caption');
53
- }
54
- },
55
- converterPriority: 'high'
56
- });
57
- }
58
- // Handle border-style, border-color, border-width and background-color table attributes.
59
- if (editor.plugins.has('TableProperties')) {
60
- downcastTableBorderAndBackgroundAttributes(editor);
61
- }
62
40
  editor.conversion.for('upcast').add(dispatcher => {
63
41
  dispatcher.on('element:table', (evt, data, conversionApi) => {
64
42
  // It's not necessary to upcast the `table` class. This class was only added in data downcast
@@ -69,72 +47,3 @@ export class PlainTableOutput extends Plugin {
69
47
  });
70
48
  }
71
49
  }
72
- /**
73
- * The plain table downcast converter callback.
74
- *
75
- * @param table Table model element.
76
- * @param conversionApi The conversion API object.
77
- * @returns Created element.
78
- */
79
- function downcastTableElement(table, { writer }) {
80
- const headingRows = table.getAttribute('headingRows') || 0;
81
- // Table head rows slot.
82
- const headRowsSlot = writer.createSlot((element) => element.is('element', 'tableRow') && element.index < headingRows);
83
- // Table body rows slot.
84
- const bodyRowsSlot = writer.createSlot((element) => element.is('element', 'tableRow') && element.index >= headingRows);
85
- // Table children slot.
86
- const childrenSlot = writer.createSlot((element) => !element.is('element', 'tableRow'));
87
- // Table <thead> element with all the heading rows.
88
- const theadElement = writer.createContainerElement('thead', null, headRowsSlot);
89
- // Table <tbody> element with all the body rows.
90
- const tbodyElement = writer.createContainerElement('tbody', null, bodyRowsSlot);
91
- // Table contents element containing <thead> and <tbody> when necessary.
92
- const tableContentElements = [];
93
- if (headingRows) {
94
- tableContentElements.push(theadElement);
95
- }
96
- if (headingRows < table.childCount) {
97
- tableContentElements.push(tbodyElement);
98
- }
99
- // Create table structure.
100
- //
101
- // <table>
102
- // {children-slot-like-caption}
103
- // <thead>
104
- // {table-head-rows-slot}
105
- // </thead>
106
- // <tbody>
107
- // {table-body-rows-slot}
108
- // </tbody>
109
- // </table>
110
- return writer.createContainerElement('table', { class: 'table' }, [childrenSlot, ...tableContentElements]);
111
- }
112
- /**
113
- * Register table border and background attributes converters.
114
- */
115
- function downcastTableBorderAndBackgroundAttributes(editor) {
116
- const modelAttributes = {
117
- 'border-width': 'tableBorderWidth',
118
- 'border-color': 'tableBorderColor',
119
- 'border-style': 'tableBorderStyle',
120
- 'background-color': 'tableBackgroundColor'
121
- };
122
- for (const [styleName, modelAttribute] of Object.entries(modelAttributes)) {
123
- editor.conversion.for('dataDowncast').add(dispatcher => {
124
- return dispatcher.on(`attribute:${modelAttribute}:table`, (evt, data, conversionApi) => {
125
- const { item, attributeNewValue } = data;
126
- const { mapper, writer } = conversionApi;
127
- if (!conversionApi.consumable.consume(item, evt.name)) {
128
- return;
129
- }
130
- const table = mapper.toViewElement(item);
131
- if (attributeNewValue) {
132
- writer.setStyle(styleName, attributeNewValue, table);
133
- }
134
- else {
135
- writer.removeStyle(styleName, table);
136
- }
137
- }, { priority: 'high' });
138
- });
139
- }
140
- }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
+ */
5
+ /**
6
+ * @module table/tablecellproperties/tablecellpropertiesuiexperimental
7
+ */
8
+ import { Plugin, type Editor } from 'ckeditor5/src/core.js';
9
+ import { ContextualBalloon } from 'ckeditor5/src/ui.js';
10
+ import { TableCellPropertiesViewExperimental } from './ui/tablecellpropertiesviewexperimental.js';
11
+ /**
12
+ * The table cell properties UI plugin. It introduces the `'tableCellProperties'` button
13
+ * that opens a form allowing to specify the visual styling of a table cell.
14
+ *
15
+ * It uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
16
+ */
17
+ export declare class TableCellPropertiesUIExperimental extends Plugin {
18
+ /**
19
+ * The default table cell properties.
20
+ */
21
+ private _defaultContentTableCellProperties;
22
+ /**
23
+ * The default layout table cell properties.
24
+ */
25
+ private _defaultLayoutTableCellProperties;
26
+ /**
27
+ * The contextual balloon plugin instance.
28
+ */
29
+ private _balloon?;
30
+ /**
31
+ * The cell properties form view displayed inside the balloon.
32
+ */
33
+ view?: TableCellPropertiesViewExperimental | null;
34
+ /**
35
+ * The cell properties form view displayed inside the balloon (content table).
36
+ */
37
+ private _viewWithContentTableDefaults?;
38
+ /**
39
+ * The cell properties form view displayed inside the balloon (layout table).
40
+ */
41
+ private _viewWithLayoutTableDefaults?;
42
+ /**
43
+ * The batch used to undo all changes made by the form (which are live, as the user types)
44
+ * when "Cancel" was pressed. Each time the view is shown, a new batch is created.
45
+ */
46
+ private _undoStepBatch?;
47
+ /**
48
+ * Flag used to indicate whether view is ready to execute update commands
49
+ * (it finished loading initial data).
50
+ */
51
+ private _isReady?;
52
+ /**
53
+ * @inheritDoc
54
+ */
55
+ static get requires(): readonly [typeof ContextualBalloon];
56
+ /**
57
+ * @inheritDoc
58
+ */
59
+ static get pluginName(): "TableCellPropertiesUIExperimental";
60
+ /**
61
+ * @inheritDoc
62
+ */
63
+ static get isOfficialPlugin(): true;
64
+ /**
65
+ * @inheritDoc
66
+ */
67
+ constructor(editor: Editor);
68
+ /**
69
+ * @inheritDoc
70
+ */
71
+ init(): void;
72
+ /**
73
+ * @inheritDoc
74
+ */
75
+ destroy(): void;
76
+ /**
77
+ * Creates the {@link module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView} instance.
78
+ *
79
+ * @returns The cell properties form view instance.
80
+ */
81
+ private _createPropertiesView;
82
+ /**
83
+ * In this method the "editor data -> UI" binding is happening.
84
+ *
85
+ * When executed, this method obtains selected cell property values from various table commands
86
+ * and passes them to the {@link #view}.
87
+ *
88
+ * This way, the UI stays up–to–date with the editor data.
89
+ */
90
+ private _fillViewFormFromCommandValues;
91
+ /**
92
+ * Shows the {@link #view} in the {@link #_balloon}.
93
+ *
94
+ * **Note**: Each time a view is shown, a new {@link #_undoStepBatch} is created. It contains
95
+ * all changes made to the document when the view is visible, allowing a single undo step
96
+ * for all of them.
97
+ */
98
+ protected _showView(): void;
99
+ /**
100
+ * Removes the {@link #view} from the {@link #_balloon}.
101
+ */
102
+ protected _hideView(): void;
103
+ /**
104
+ * Repositions the {@link #_balloon} or hides the {@link #view} if a table cell is no longer selected.
105
+ */
106
+ protected _updateView(): void;
107
+ /**
108
+ * Returns `true` when the {@link #view} is visible in the {@link #_balloon}.
109
+ */
110
+ private get _isViewVisible();
111
+ /**
112
+ * Returns `true` when the {@link #view} is in the {@link #_balloon}.
113
+ */
114
+ private get _isViewInBalloon();
115
+ /**
116
+ * Creates a callback that when executed upon the {@link #view view's} property change
117
+ * executes a related editor command with the new property value.
118
+ *
119
+ * @param commandName The default value of the command.
120
+ */
121
+ private _getPropertyChangeCallback;
122
+ /**
123
+ * Creates a callback that when executed upon the {@link #view view's} property change:
124
+ * * Executes a related editor command with the new property value if the value is valid,
125
+ * * Or sets the error text next to the invalid field, if the value did not pass the validation.
126
+ */
127
+ private _getValidatedPropertyChangeCallback;
128
+ }
@@ -0,0 +1,386 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
+ */
5
+ /**
6
+ * @module table/tablecellproperties/tablecellpropertiesuiexperimental
7
+ */
8
+ /* istanbul ignore file -- @preserve */
9
+ import { Plugin } from 'ckeditor5/src/core.js';
10
+ import { IconTableCellProperties } from 'ckeditor5/src/icons.js';
11
+ import { ButtonView, clickOutsideHandler, ContextualBalloon, getLocalizedColorOptions, normalizeColorOptions } from 'ckeditor5/src/ui.js';
12
+ import { TableCellPropertiesViewExperimental } from './ui/tablecellpropertiesviewexperimental.js';
13
+ import { colorFieldValidator, getLocalizedColorErrorText, getLocalizedLengthErrorText, defaultColors, lengthFieldValidator, lineWidthFieldValidator } from '../utils/ui/table-properties.js';
14
+ import { debounce } from 'es-toolkit/compat';
15
+ import { getSelectionAffectedTableWidget, getTableWidgetAncestor } from '../utils/ui/widget.js';
16
+ import { getBalloonCellPositionData, repositionContextualBalloon } from '../utils/ui/contextualballoon.js';
17
+ import { getNormalizedDefaultCellProperties, getNormalizedDefaultProperties } from '../utils/table-properties.js';
18
+ const ERROR_TEXT_TIMEOUT = 500;
19
+ // Map of view properties and related commands.
20
+ const propertyToCommandMap = {
21
+ borderStyle: 'tableCellBorderStyle',
22
+ borderColor: 'tableCellBorderColor',
23
+ borderWidth: 'tableCellBorderWidth',
24
+ height: 'tableCellHeight',
25
+ width: 'tableCellWidth',
26
+ padding: 'tableCellPadding',
27
+ backgroundColor: 'tableCellBackgroundColor',
28
+ horizontalAlignment: 'tableCellHorizontalAlignment',
29
+ verticalAlignment: 'tableCellVerticalAlignment'
30
+ };
31
+ /**
32
+ * The table cell properties UI plugin. It introduces the `'tableCellProperties'` button
33
+ * that opens a form allowing to specify the visual styling of a table cell.
34
+ *
35
+ * It uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
36
+ */
37
+ export class TableCellPropertiesUIExperimental extends Plugin {
38
+ /**
39
+ * The default table cell properties.
40
+ */
41
+ _defaultContentTableCellProperties;
42
+ /**
43
+ * The default layout table cell properties.
44
+ */
45
+ _defaultLayoutTableCellProperties;
46
+ /**
47
+ * The contextual balloon plugin instance.
48
+ */
49
+ _balloon;
50
+ /**
51
+ * The cell properties form view displayed inside the balloon.
52
+ */
53
+ view;
54
+ /**
55
+ * The cell properties form view displayed inside the balloon (content table).
56
+ */
57
+ _viewWithContentTableDefaults;
58
+ /**
59
+ * The cell properties form view displayed inside the balloon (layout table).
60
+ */
61
+ _viewWithLayoutTableDefaults;
62
+ /**
63
+ * The batch used to undo all changes made by the form (which are live, as the user types)
64
+ * when "Cancel" was pressed. Each time the view is shown, a new batch is created.
65
+ */
66
+ _undoStepBatch;
67
+ /**
68
+ * Flag used to indicate whether view is ready to execute update commands
69
+ * (it finished loading initial data).
70
+ */
71
+ _isReady;
72
+ /**
73
+ * @inheritDoc
74
+ */
75
+ static get requires() {
76
+ return [ContextualBalloon];
77
+ }
78
+ /**
79
+ * @inheritDoc
80
+ */
81
+ static get pluginName() {
82
+ return 'TableCellPropertiesUIExperimental';
83
+ }
84
+ /**
85
+ * @inheritDoc
86
+ */
87
+ static get isOfficialPlugin() {
88
+ return true;
89
+ }
90
+ /**
91
+ * @inheritDoc
92
+ */
93
+ constructor(editor) {
94
+ super(editor);
95
+ editor.config.define('table.tableCellProperties', {
96
+ borderColors: defaultColors,
97
+ backgroundColors: defaultColors
98
+ });
99
+ }
100
+ /**
101
+ * @inheritDoc
102
+ */
103
+ init() {
104
+ const editor = this.editor;
105
+ const t = editor.t;
106
+ this._defaultContentTableCellProperties = getNormalizedDefaultCellProperties(editor.config.get('table.tableCellProperties.defaultProperties'), {
107
+ includeVerticalAlignmentProperty: true,
108
+ includeHorizontalAlignmentProperty: true,
109
+ includePaddingProperty: true,
110
+ isRightToLeftContent: editor.locale.contentLanguageDirection === 'rtl'
111
+ });
112
+ this._defaultLayoutTableCellProperties = getNormalizedDefaultProperties(undefined, {
113
+ includeVerticalAlignmentProperty: true,
114
+ includeHorizontalAlignmentProperty: true,
115
+ isRightToLeftContent: editor.locale.contentLanguageDirection === 'rtl'
116
+ });
117
+ this._balloon = editor.plugins.get(ContextualBalloon);
118
+ this.view = null;
119
+ this._isReady = false;
120
+ editor.ui.componentFactory.add('tableCellProperties', locale => {
121
+ const view = new ButtonView(locale);
122
+ view.set({
123
+ label: t('Cell properties'),
124
+ icon: IconTableCellProperties,
125
+ tooltip: true
126
+ });
127
+ this.listenTo(view, 'execute', () => this._showView());
128
+ const commands = Object.values(propertyToCommandMap)
129
+ .map(commandName => editor.commands.get(commandName));
130
+ view.bind('isEnabled').toMany(commands, 'isEnabled', (...areEnabled) => (areEnabled.some(isCommandEnabled => isCommandEnabled)));
131
+ return view;
132
+ });
133
+ }
134
+ /**
135
+ * @inheritDoc
136
+ */
137
+ destroy() {
138
+ super.destroy();
139
+ // Destroy created UI components as they are not automatically destroyed.
140
+ // See https://github.com/ckeditor/ckeditor5/issues/1341.
141
+ if (this.view) {
142
+ this.view.destroy();
143
+ }
144
+ }
145
+ /**
146
+ * Creates the {@link module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView} instance.
147
+ *
148
+ * @returns The cell properties form view instance.
149
+ */
150
+ _createPropertiesView(defaultTableCellProperties) {
151
+ const editor = this.editor;
152
+ const config = editor.config.get('table.tableCellProperties');
153
+ const borderColorsConfig = normalizeColorOptions(config.borderColors);
154
+ const localizedBorderColors = getLocalizedColorOptions(editor.locale, borderColorsConfig);
155
+ const backgroundColorsConfig = normalizeColorOptions(config.backgroundColors);
156
+ const localizedBackgroundColors = getLocalizedColorOptions(editor.locale, backgroundColorsConfig);
157
+ const hasColorPicker = config.colorPicker !== false;
158
+ const view = new TableCellPropertiesViewExperimental(editor.locale, {
159
+ borderColors: localizedBorderColors,
160
+ backgroundColors: localizedBackgroundColors,
161
+ defaultTableCellProperties,
162
+ colorPickerConfig: hasColorPicker ? (config.colorPicker || {}) : false
163
+ });
164
+ const t = editor.t;
165
+ // Render the view so its #element is available for the clickOutsideHandler.
166
+ view.render();
167
+ this.listenTo(view, 'submit', () => {
168
+ this._hideView();
169
+ });
170
+ this.listenTo(view, 'cancel', () => {
171
+ // https://github.com/ckeditor/ckeditor5/issues/6180
172
+ if (this._undoStepBatch.operations.length) {
173
+ editor.execute('undo', this._undoStepBatch);
174
+ }
175
+ this._hideView();
176
+ });
177
+ // Close the balloon on Esc key press.
178
+ view.keystrokes.set('Esc', (data, cancel) => {
179
+ this._hideView();
180
+ cancel();
181
+ });
182
+ // Close on click outside of balloon panel element.
183
+ clickOutsideHandler({
184
+ emitter: view,
185
+ activator: () => this._isViewInBalloon,
186
+ contextElements: [this._balloon.view.element],
187
+ callback: () => this._hideView()
188
+ });
189
+ const colorErrorText = getLocalizedColorErrorText(t);
190
+ const lengthErrorText = getLocalizedLengthErrorText(t);
191
+ // Create the "UI -> editor data" binding.
192
+ // These listeners update the editor data (via table commands) when any observable
193
+ // property of the view has changed. They also validate the value and display errors in the UI
194
+ // when necessary. This makes the view live, which means the changes are
195
+ // visible in the editing as soon as the user types or changes fields' values.
196
+ view.on('change:borderStyle', this._getPropertyChangeCallback('tableCellBorderStyle'));
197
+ view.on('change:borderColor', this._getValidatedPropertyChangeCallback({
198
+ viewField: view.borderColorInput,
199
+ commandName: 'tableCellBorderColor',
200
+ errorText: colorErrorText,
201
+ validator: colorFieldValidator
202
+ }));
203
+ view.on('change:borderWidth', this._getValidatedPropertyChangeCallback({
204
+ viewField: view.borderWidthInput,
205
+ commandName: 'tableCellBorderWidth',
206
+ errorText: lengthErrorText,
207
+ validator: lineWidthFieldValidator
208
+ }));
209
+ view.on('change:padding', this._getValidatedPropertyChangeCallback({
210
+ viewField: view.paddingInput,
211
+ commandName: 'tableCellPadding',
212
+ errorText: lengthErrorText,
213
+ validator: lengthFieldValidator
214
+ }));
215
+ view.on('change:width', this._getValidatedPropertyChangeCallback({
216
+ viewField: view.widthInput,
217
+ commandName: 'tableCellWidth',
218
+ errorText: lengthErrorText,
219
+ validator: lengthFieldValidator
220
+ }));
221
+ view.on('change:height', this._getValidatedPropertyChangeCallback({
222
+ viewField: view.heightInput,
223
+ commandName: 'tableCellHeight',
224
+ errorText: lengthErrorText,
225
+ validator: lengthFieldValidator
226
+ }));
227
+ view.on('change:backgroundColor', this._getValidatedPropertyChangeCallback({
228
+ viewField: view.backgroundInput,
229
+ commandName: 'tableCellBackgroundColor',
230
+ errorText: colorErrorText,
231
+ validator: colorFieldValidator
232
+ }));
233
+ view.on('change:horizontalAlignment', this._getPropertyChangeCallback('tableCellHorizontalAlignment'));
234
+ view.on('change:verticalAlignment', this._getPropertyChangeCallback('tableCellVerticalAlignment'));
235
+ return view;
236
+ }
237
+ /**
238
+ * In this method the "editor data -> UI" binding is happening.
239
+ *
240
+ * When executed, this method obtains selected cell property values from various table commands
241
+ * and passes them to the {@link #view}.
242
+ *
243
+ * This way, the UI stays up–to–date with the editor data.
244
+ */
245
+ _fillViewFormFromCommandValues() {
246
+ const commands = this.editor.commands;
247
+ const borderStyleCommand = commands.get('tableCellBorderStyle');
248
+ Object.entries(propertyToCommandMap)
249
+ .map(([property, commandName]) => {
250
+ const propertyKey = property;
251
+ const defaultValue = this.view === this._viewWithContentTableDefaults ?
252
+ this._defaultContentTableCellProperties[propertyKey] || '' :
253
+ this._defaultLayoutTableCellProperties[propertyKey] || '';
254
+ return [
255
+ property,
256
+ commands.get(commandName).value || defaultValue
257
+ ];
258
+ })
259
+ .forEach(([property, value]) => {
260
+ // Do not set the `border-color` and `border-width` fields if `border-style:none`.
261
+ if ((property === 'borderColor' || property === 'borderWidth') && borderStyleCommand.value === 'none') {
262
+ return;
263
+ }
264
+ this.view.set(property, value);
265
+ });
266
+ this._isReady = true;
267
+ }
268
+ /**
269
+ * Shows the {@link #view} in the {@link #_balloon}.
270
+ *
271
+ * **Note**: Each time a view is shown, a new {@link #_undoStepBatch} is created. It contains
272
+ * all changes made to the document when the view is visible, allowing a single undo step
273
+ * for all of them.
274
+ */
275
+ _showView() {
276
+ const editor = this.editor;
277
+ const viewTable = getSelectionAffectedTableWidget(editor.editing.view.document.selection);
278
+ const modelTable = viewTable && editor.editing.mapper.toModelElement(viewTable);
279
+ const useDefaults = !modelTable || modelTable.getAttribute('tableType') !== 'layout';
280
+ if (useDefaults && !this._viewWithContentTableDefaults) {
281
+ this._viewWithContentTableDefaults = this._createPropertiesView(this._defaultContentTableCellProperties);
282
+ }
283
+ else if (!useDefaults && !this._viewWithLayoutTableDefaults) {
284
+ this._viewWithLayoutTableDefaults = this._createPropertiesView(this._defaultLayoutTableCellProperties);
285
+ }
286
+ this.view = useDefaults ? this._viewWithContentTableDefaults : this._viewWithLayoutTableDefaults;
287
+ this.listenTo(editor.ui, 'update', () => {
288
+ this._updateView();
289
+ });
290
+ // Update the view with the model values.
291
+ this._fillViewFormFromCommandValues();
292
+ this._balloon.add({
293
+ view: this.view,
294
+ position: getBalloonCellPositionData(editor)
295
+ });
296
+ // Create a new batch. Clicking "Cancel" will undo this batch.
297
+ this._undoStepBatch = editor.model.createBatch();
298
+ // Basic a11y.
299
+ this.view.focus();
300
+ }
301
+ /**
302
+ * Removes the {@link #view} from the {@link #_balloon}.
303
+ */
304
+ _hideView() {
305
+ const editor = this.editor;
306
+ this.stopListening(editor.ui, 'update');
307
+ this._isReady = false;
308
+ // Blur any input element before removing it from DOM to prevent issues in some browsers.
309
+ // See https://github.com/ckeditor/ckeditor5/issues/1501.
310
+ this.view.saveButtonView.focus();
311
+ this._balloon.remove(this.view);
312
+ // Make sure the focus is not lost in the process by putting it directly
313
+ // into the editing view.
314
+ this.editor.editing.view.focus();
315
+ }
316
+ /**
317
+ * Repositions the {@link #_balloon} or hides the {@link #view} if a table cell is no longer selected.
318
+ */
319
+ _updateView() {
320
+ const editor = this.editor;
321
+ const viewDocument = editor.editing.view.document;
322
+ if (!getTableWidgetAncestor(viewDocument.selection)) {
323
+ this._hideView();
324
+ }
325
+ else if (this._isViewVisible) {
326
+ repositionContextualBalloon(editor, 'cell');
327
+ }
328
+ }
329
+ /**
330
+ * Returns `true` when the {@link #view} is visible in the {@link #_balloon}.
331
+ */
332
+ get _isViewVisible() {
333
+ return !!this.view && this._balloon.visibleView === this.view;
334
+ }
335
+ /**
336
+ * Returns `true` when the {@link #view} is in the {@link #_balloon}.
337
+ */
338
+ get _isViewInBalloon() {
339
+ return !!this.view && this._balloon.hasView(this.view);
340
+ }
341
+ /**
342
+ * Creates a callback that when executed upon the {@link #view view's} property change
343
+ * executes a related editor command with the new property value.
344
+ *
345
+ * @param commandName The default value of the command.
346
+ */
347
+ _getPropertyChangeCallback(commandName) {
348
+ return (evt, propertyName, newValue) => {
349
+ if (!this._isReady) {
350
+ return;
351
+ }
352
+ this.editor.execute(commandName, {
353
+ value: newValue,
354
+ batch: this._undoStepBatch
355
+ });
356
+ };
357
+ }
358
+ /**
359
+ * Creates a callback that when executed upon the {@link #view view's} property change:
360
+ * * Executes a related editor command with the new property value if the value is valid,
361
+ * * Or sets the error text next to the invalid field, if the value did not pass the validation.
362
+ */
363
+ _getValidatedPropertyChangeCallback(options) {
364
+ const { commandName, viewField, validator, errorText } = options;
365
+ const setErrorTextDebounced = debounce(() => {
366
+ viewField.errorText = errorText;
367
+ }, ERROR_TEXT_TIMEOUT);
368
+ return (evt, propertyName, newValue) => {
369
+ setErrorTextDebounced.cancel();
370
+ // Do not execute the command on initial call (opening the table properties view).
371
+ if (!this._isReady) {
372
+ return;
373
+ }
374
+ if (validator(newValue)) {
375
+ this.editor.execute(commandName, {
376
+ value: newValue,
377
+ batch: this._undoStepBatch
378
+ });
379
+ viewField.errorText = null;
380
+ }
381
+ else {
382
+ setErrorTextDebounced();
383
+ }
384
+ };
385
+ }
386
+ }