Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed balloon block toolbar not closing on click #12254

Merged
merged 8 commits into from
Aug 30, 2022
19 changes: 19 additions & 0 deletions packages/ckeditor5-ui/src/toolbar/block/blocktoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import normalizeToolbarConfig from '../normalizetoolbarconfig';
import ResizeObserver from '@ckeditor/ckeditor5-utils/src/dom/resizeobserver';

import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
import env from '@ckeditor/ckeditor5-utils/src/env';

const toPx = toUnit( 'px' );

Expand Down Expand Up @@ -280,13 +281,31 @@ export default class BlockToolbar extends Plugin {
const editor = this.editor;
const t = editor.t;
const buttonView = new BlockButtonView( editor.locale );
const bind = buttonView.bindTemplate;

buttonView.set( {
label: t( 'Edit block' ),
icon: pilcrow,
withText: false
} );

// Note that this piece over here overrides the default mousedown logic in ButtonView
// to make it work with BlockToolbar. See the implementation of the ButtonView class to learn more.
buttonView.extendTemplate( {
on: {
mousedown: bind.to( evt => {
// On Safari we have to force the focus on a button on click as it's the only browser
// that doesn't do that automatically. See #12115.
if ( env.isSafari && this.panelView.isVisible ) {
this.toolbarView.focus();
mateuszzagorski marked this conversation as resolved.
Show resolved Hide resolved
}

// Workaround to #12184, see https://github.com/ckeditor/ckeditor5/issues/12184#issuecomment-1199147964.
evt.preventDefault();
} )
}
} );

// Bind the panelView observable properties to the buttonView.
buttonView.bind( 'isOn' ).to( this.panelView, 'isVisible' );
buttonView.bind( 'tooltip' ).to( this.panelView, 'isVisible', isVisible => !isVisible );
Expand Down
45 changes: 45 additions & 0 deletions packages/ckeditor5-ui/tests/toolbar/block/blocktoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
import env from '@ckeditor/ckeditor5-utils/src/env';

describe( 'BlockToolbar', () => {
let editor, element, blockToolbar;
Expand Down Expand Up @@ -328,6 +329,50 @@ describe( 'BlockToolbar', () => {
const blockToolbar = editor.plugins.get( BlockToolbar );
expect( blockToolbar.buttonView.isVisible ).to.be.false;
} );

describe( 'mousedown event', () => {
// https://github.com/ckeditor/ckeditor5/issues/12184
it( 'should call preventDefault to avoid stealing the focus', () => {
const ret = blockToolbar.buttonView.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );

expect( ret ).to.false;
} );

// https://github.com/ckeditor/ckeditor5/issues/12115
describe( 'in Safari', () => {
let view, stub, spy;

beforeEach( () => {
stub = testUtils.sinon.stub( env, 'isSafari' ).value( true );
view = blockToolbar.buttonView;
spy = sinon.spy( blockToolbar.toolbarView, 'focus' );
} );

afterEach( () => {
stub.resetBehavior();
} );

it( 'should focus the toolbar when it shows up', () => {
blockToolbar.panelView.isVisible = true;
view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );

expect( spy.callCount ).to.equal( 1 );
} );

it( 'should not focus the toolbar when it hides', () => {
blockToolbar.panelView.isVisible = false;
view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );

expect( spy.callCount ).to.equal( 0 );
} );

it( 'should also preventDefault the event', () => {
const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );

expect( ret ).to.false;
} );
} );
} );
} );
} );

Expand Down