Skip to content

Commit

Permalink
Merge pull request #14963 from ckeditor/ck/14890-remove-temporarily-c…
Browse files Browse the repository at this point in the history
…kbox-focus-solution

Internal (ckbox): Remove temporarily ckbox focus solution.
  • Loading branch information
arkflpc authored Sep 11, 2023
2 parents 7f4f63e + 299be19 commit 8d8a573
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 109 deletions.
36 changes: 0 additions & 36 deletions packages/ckeditor5-ckbox/src/ckboxcommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ export default class CKBoxCommand extends Command {
document.body.appendChild( this._wrapper );

window.CKBox.mount( this._wrapper, this._prepareOptions() );

const MAX_NUMBER_OF_ATTEMPTS_TO_FOCUS = 50;

focusCKBoxItem( MAX_NUMBER_OF_ATTEMPTS_TO_FOCUS );
} );

// Handle closing of the CKBox dialog.
Expand Down Expand Up @@ -391,38 +387,6 @@ function getAssetUrl( asset: CKBoxRawAssetDefinition ) {
return url.toString();
}

/**
* Focuses the CKBox first item in gallery.
* This is a temporary fix. A permanent solution to this issue will be provided soon.
*
* @param limiter Max number of attempts to focus the ckbox item.
*/
function focusCKBoxItem( limiter: number ): void {
// Trying every 100 ms get access to the CKBox component until component will be loaded.
setTimeout( () => {
if ( limiter === 0 ) {
return;
}

const ckboxGalleryFirstItem = document.querySelector( '.ckbox-gallery .ckbox-gallery-item' );
// In case there is no items, "upload button" will be appeared in "div" with
// classname ".ckbox-empty-view".
const uploadButton = document.querySelector( '.ckbox-empty-view .ckbox-btn' );

// In case "upload button" is loaded in ".ckbox-empty-view" we focus actual button.
if ( uploadButton && uploadButton instanceof HTMLElement ) {
uploadButton.focus();
return;
}

if ( ckboxGalleryFirstItem && ckboxGalleryFirstItem instanceof HTMLElement ) {
ckboxGalleryFirstItem.focus();
} else {
focusCKBoxItem( limiter - 1 );
}
}, 100 );
}

/**
* Fired when the command is executed, the dialog is closed or the assets are chosen.
*
Expand Down
73 changes: 0 additions & 73 deletions packages/ckeditor5-ckbox/tests/ckboxcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,79 +167,6 @@ describe( 'CKBoxCommand', () => {
expect( document.body.appendChild.args[ 0 ][ 0 ] ).to.equal( wrapper );
} );

it( 'should focus ckbox gallery item after initialization', () => {
const ckboxGallery = document.createElement( 'div' );
const ckboxGalleryItem = document.createElement( 'div' );

ckboxGallery.setAttribute( 'class', 'ckbox-gallery' );
ckboxGalleryItem.setAttribute( 'class', 'ckbox-gallery-item' );

ckboxGallery.appendChild( ckboxGalleryItem );
document.body.append( ckboxGallery );

const spy = sinon.spy( ckboxGalleryItem, 'focus' );
command.execute();

sinon.clock.tick( 100 );
expect( spy.calledOnce ).to.be.true;
ckboxGallery.remove();
} );

it( 'should focus upload button item after initialization', () => {
const emptyView = document.createElement( 'div' );
const uploadButton = document.createElement( 'button' );

emptyView.setAttribute( 'class', 'ckbox-empty-view' );
uploadButton.setAttribute( 'class', 'ckbox-btn' );

emptyView.appendChild( uploadButton );
document.body.append( emptyView );

const spy = sinon.spy( uploadButton, 'focus' );
command.execute();

sinon.clock.tick( 100 );
expect( spy.calledOnce ).to.be.true;
emptyView.remove();
} );

it( 'should wait until ckbox will be loaded', () => {
const ckboxGallery = document.createElement( 'div' );
const ckboxGalleryItem = document.createElement( 'div' );

ckboxGallery.setAttribute( 'class', 'ckbox-gallery' );
ckboxGalleryItem.setAttribute( 'class', 'ckbox-gallery-item' );

const spy = sinon.spy( ckboxGalleryItem, 'focus' );
command.execute();

sinon.clock.tick( 100 );
expect( spy.notCalled ).to.be.true;

document.body.append( ckboxGallery );

sinon.clock.tick( 100 );
expect( spy.notCalled ).to.be.true;

ckboxGallery.appendChild( ckboxGalleryItem );
sinon.clock.tick( 100 );

expect( spy.calledOnce ).to.be.true;
ckboxGallery.remove();
} );

it( 'should giveup after 5 seconds', () => {
const ckboxGalleryItem = document.createElement( 'div' );

ckboxGalleryItem.setAttribute( 'class', 'ckbox-gallery-item' );

const spy = sinon.spy( ckboxGalleryItem, 'focus' );
command.execute();

sinon.clock.tick( 5100 );
expect( spy.notCalled ).to.be.true;
} );

it( 'should create and mount a wrapper only once', () => {
command.execute();

Expand Down

0 comments on commit 8d8a573

Please sign in to comment.