Skip to content

Commit 35a34e4

Browse files
authored
Merge pull request #13263 from ckeditor/ck/13262-table-property-window
Fix (table): Table and cell property commands were executed when the UI dialog opened.
2 parents 97377f5 + e72fc57 commit 35a34e4

File tree

4 files changed

+89
-25
lines changed

4 files changed

+89
-25
lines changed

packages/ckeditor5-table/src/tablecellproperties/tablecellpropertiesui.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ export default class TableCellPropertiesUI extends Plugin {
124124
*/
125125
this._undoStepBatch = null;
126126

127+
/**
128+
* Flag used to indicate whether view is ready to execute update commands
129+
* (it finished loading initial data).
130+
*
131+
* @private
132+
* @member {Boolean}
133+
*/
134+
this._isReady = false;
135+
127136
editor.ui.componentFactory.add( 'tableCellProperties', locale => {
128137
const view = new ButtonView( locale );
129138

@@ -312,6 +321,8 @@ export default class TableCellPropertiesUI extends Plugin {
312321

313322
this.view.set( property, value );
314323
} );
324+
325+
this._isReady = true;
315326
}
316327

317328
/**
@@ -359,6 +370,8 @@ export default class TableCellPropertiesUI extends Plugin {
359370

360371
this.stopListening( editor.ui, 'update' );
361372

373+
this._isReady = false;
374+
362375
// Blur any input element before removing it from DOM to prevent issues in some browsers.
363376
// See https://github.com/ckeditor/ckeditor5/issues/1501.
364377
this.view.saveButtonView.focus();
@@ -412,14 +425,12 @@ export default class TableCellPropertiesUI extends Plugin {
412425
*
413426
* @private
414427
* @param {String} commandName
415-
* @param {String} defaultValue The default value of the command.
416428
* @returns {Function}
417429
*/
418-
_getPropertyChangeCallback( commandName, defaultValue ) {
419-
return ( evt, propertyName, newValue, oldValue ) => {
420-
// If the "oldValue" is missing and "newValue" is set to the default value, do not execute the command.
421-
// It is an initial call (when opening the table properties view).
422-
if ( !oldValue && defaultValue === newValue ) {
430+
_getPropertyChangeCallback( commandName ) {
431+
return ( evt, propertyName, newValue ) => {
432+
// Do not execute the command on initial call (opening the table properties view).
433+
if ( !this._isReady ) {
423434
return;
424435
}
425436

@@ -441,21 +452,18 @@ export default class TableCellPropertiesUI extends Plugin {
441452
* @param {module:ui/view~View} options.viewField
442453
* @param {Function} options.validator
443454
* @param {String} options.errorText
444-
* @param {String} options.defaultValue
445455
* @returns {Function}
446456
*/
447457
_getValidatedPropertyChangeCallback( options ) {
448-
const { commandName, viewField, validator, errorText, defaultValue } = options;
458+
const { commandName, viewField, validator, errorText } = options;
449459
const setErrorTextDebounced = debounce( () => {
450460
viewField.errorText = errorText;
451461
}, ERROR_TEXT_TIMEOUT );
452462

453-
return ( evt, propertyName, newValue, oldValue ) => {
463+
return ( evt, propertyName, newValue ) => {
454464
setErrorTextDebounced.cancel();
455-
456-
// If the "oldValue" is missing and "newValue" is set to the default value, do not execute the command.
457-
// It is an initial call (when opening the table properties view).
458-
if ( !oldValue && defaultValue === newValue ) {
465+
// Do not execute the command on initial call (opening the table properties view).
466+
if ( !this._isReady ) {
459467
return;
460468
}
461469

packages/ckeditor5-table/src/tableproperties/tablepropertiesui.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ export default class TablePropertiesUI extends Plugin {
116116
*/
117117
this._undoStepBatch = null;
118118

119+
/**
120+
* Flag used to indicate whether view is ready to execute update commands
121+
* (it finished loading initial data).
122+
*
123+
* @private
124+
* @member {Boolean}
125+
*/
126+
this._isReady = false;
127+
119128
editor.ui.componentFactory.add( 'tableProperties', locale => {
120129
const view = new ButtonView( locale );
121130

@@ -292,6 +301,8 @@ export default class TablePropertiesUI extends Plugin {
292301

293302
this.view.set( property, value );
294303
} );
304+
305+
this._isReady = true;
295306
}
296307

297308
/**
@@ -339,6 +350,8 @@ export default class TablePropertiesUI extends Plugin {
339350

340351
this.stopListening( editor.ui, 'update' );
341352

353+
this._isReady = false;
354+
342355
// Blur any input element before removing it from DOM to prevent issues in some browsers.
343356
// See https://github.com/ckeditor/ckeditor5/issues/1501.
344357
this.view.saveButtonView.focus();
@@ -394,14 +407,12 @@ export default class TablePropertiesUI extends Plugin {
394407
*
395408
* @private
396409
* @param {String} commandName The command that will be executed.
397-
* @param {String} defaultValue The default value of the command.
398410
* @returns {Function}
399411
*/
400-
_getPropertyChangeCallback( commandName, defaultValue ) {
401-
return ( evt, propertyName, newValue, oldValue ) => {
402-
// If the "oldValue" is missing and "newValue" is set to the default value, do not execute the command.
403-
// It is an initial call (when opening the table properties view).
404-
if ( !oldValue && defaultValue === newValue ) {
412+
_getPropertyChangeCallback( commandName ) {
413+
return ( evt, propertyName, newValue ) => {
414+
// Do not execute the command on initial call (opening the table properties view).
415+
if ( !this._isReady ) {
405416
return;
406417
}
407418

@@ -423,21 +434,19 @@ export default class TablePropertiesUI extends Plugin {
423434
* @param {module:ui/view~View} options.viewField
424435
* @param {Function} options.validator
425436
* @param {String} options.errorText
426-
* @param {String} options.defaultValue
427437
* @returns {Function}
428438
*/
429439
_getValidatedPropertyChangeCallback( options ) {
430-
const { commandName, viewField, validator, errorText, defaultValue } = options;
440+
const { commandName, viewField, validator, errorText } = options;
431441
const setErrorTextDebounced = debounce( () => {
432442
viewField.errorText = errorText;
433443
}, ERROR_TEXT_TIMEOUT );
434444

435-
return ( evt, propertyName, newValue, oldValue ) => {
445+
return ( evt, propertyName, newValue ) => {
436446
setErrorTextDebounced.cancel();
437447

438-
// If the "oldValue" is missing and "newValue" is set to the default value, do not execute the command.
439-
// It is an initial call (when opening the table properties view).
440-
if ( !oldValue && defaultValue === newValue ) {
448+
// Do not execute the command on initial call (opening the table properties view).
449+
if ( !this._isReady ) {
441450
return;
442451
}
443452

packages/ckeditor5-table/tests/tablecellproperties/tablecellpropertiesui.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ describe( 'table cell properties', () => {
295295
expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
296296
} );
297297

298+
it( 'should not reposition if view is not visible', () => {
299+
const spy = sinon.spy( contextualBalloon, 'updatePosition' );
300+
301+
tableCellPropertiesButton.fire( 'execute' );
302+
tableCellPropertiesUI.view = false;
303+
editor.ui.fire( 'update' );
304+
305+
expect( spy.called ).to.be.false;
306+
} );
307+
298308
it( 'should hide if clicked outside the balloon', () => {
299309
tableCellPropertiesButton.fire( 'execute' );
300310
tableCellPropertiesView = tableCellPropertiesUI.view;
@@ -605,6 +615,20 @@ describe( 'table cell properties', () => {
605615
} );
606616

607617
describe( 'initial data', () => {
618+
it( 'should not execute commands before changing the data', () => {
619+
const tableCellBackgroundCommand = editor.commands.get( 'tableCellBackgroundColor' );
620+
const spy = sinon.spy( tableCellBackgroundCommand, 'execute' );
621+
622+
tableCellPropertiesUI._showView();
623+
tableCellPropertiesView = tableCellPropertiesUI.view;
624+
625+
expect( spy.called ).to.be.false;
626+
627+
tableCellPropertiesView.backgroundColor = 'red';
628+
629+
expect( spy.called ).to.be.true;
630+
} );
631+
608632
it( 'should be set before adding the form to the the balloon to avoid unnecessary input animations', () => {
609633
// Trigger lazy init.
610634
tableCellPropertiesUI._showView();

packages/ckeditor5-table/tests/tableproperties/tablepropertiesui.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ describe( 'table properties', () => {
569569
} );
570570

571571
describe( 'initial data', () => {
572+
it( 'should not execute commands before changing the data', () => {
573+
const tableBackgroundCommand = editor.commands.get( 'tableBackgroundColor' );
574+
const spy = sinon.spy( tableBackgroundCommand, 'execute' );
575+
576+
tablePropertiesUI._showView();
577+
tablePropertiesView = tablePropertiesUI.view;
578+
579+
expect( spy.called ).to.be.false;
580+
581+
tablePropertiesView.backgroundColor = 'red';
582+
583+
expect( spy.called ).to.be.true;
584+
} );
585+
572586
it( 'should be set before adding the form to the the balloon to avoid unnecessary input animations', () => {
573587
// Trigger lazy init.
574588
tablePropertiesUI._showView();
@@ -707,6 +721,15 @@ describe( 'table properties', () => {
707721
sinon.assert.calledOnce( spy );
708722
} );
709723

724+
it( 'should not reposition the baloon if view is not visible', () => {
725+
const spy = sinon.spy( contextualBalloon, 'updatePosition' );
726+
727+
tablePropertiesUI.view = false;
728+
editor.ui.fire( 'update' );
729+
730+
expect( spy.called ).to.be.false;
731+
} );
732+
710733
it( 'should hide the view and not reposition the balloon if table is no longer selected', () => {
711734
const positionSpy = sinon.spy( contextualBalloon, 'updatePosition' );
712735
const hideSpy = sinon.spy( tablePropertiesUI, '_hideView' );

0 commit comments

Comments
 (0)