Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Used UI#update event instead of View#render to attach table UI. #78

Merged
merged 3 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/tabletoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class TableToolbar extends Plugin {
this._toolbar.fillFromConfig( toolbarConfig, editor.ui.componentFactory );

// Show balloon panel each time table widget is selected.
this.listenTo( editor.editing.view, 'render', () => {
this.listenTo( editor.ui, 'update', () => {
this._checkIsVisible();
} );

Expand All @@ -108,17 +108,12 @@ export default class TableToolbar extends Plugin {
*/
_checkIsVisible() {
const editor = this.editor;
const viewSelection = editor.editing.view.document.selection;

if ( !editor.ui.focusTracker.isFocused ) {
if ( !editor.ui.focusTracker.isFocused || !isTableContentSelected( viewSelection ) ) {
this._hideToolbar();
} else {
const viewSelection = editor.editing.view.document.selection;

if ( isTableContentSelected( viewSelection ) ) {
this._showToolbar();
} else {
this._hideToolbar();
}
this._showToolbar();
}
}

Expand All @@ -132,14 +127,12 @@ export default class TableToolbar extends Plugin {

if ( this._isVisible ) {
repositionContextualBalloon( editor );
} else {
if ( !this._balloon.hasView( this._toolbar ) ) {
this._balloon.add( {
view: this._toolbar,
position: getBalloonPositionData( editor ),
balloonClassName
} );
}
} else if ( !this._balloon.hasView( this._toolbar ) ) {
this._balloon.add( {
view: this._toolbar,
position: getBalloonPositionData( editor ),
balloonClassName
} );
}
}

Expand Down
20 changes: 11 additions & 9 deletions tests/tabletoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

describe( 'TableToolbar', () => {
let editor, model, doc, editingView, plugin, toolbar, balloon, editorElement;
let editor, model, doc, plugin, toolbar, balloon, editorElement;

beforeEach( () => {
editorElement = global.document.createElement( 'div' );
Expand All @@ -36,7 +36,6 @@ describe( 'TableToolbar', () => {
doc = model.document;
plugin = editor.plugins.get( TableToolbar );
toolbar = plugin._toolbar;
editingView = editor.editing.view;
balloon = editor.plugins.get( 'ContextualBalloon' );
} );
} );
Expand Down Expand Up @@ -112,23 +111,24 @@ describe( 'TableToolbar', () => {
} );
} );

describe( 'integration with the editor selection (#change event)', () => {
describe( 'integration with the editor selection (ui#update event)', () => {
beforeEach( () => {
editor.ui.focusTracker.isFocused = true;
} );

it( 'should not show the toolbar on render when the table is selected', () => {
it( 'should not show the toolbar on ui#update when the table is selected', () => {
setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );

expect( balloon.visibleView ).to.be.null;
} );

it( 'should show the toolbar on render when the table content is selected', () => {
it( 'should show the toolbar on ui#update when the table content is selected', () => {
setData( model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );

expect( balloon.visibleView ).to.be.null;

editingView.change( () => {} );
editor.ui.fire( 'update' );

expect( balloon.visibleView ).to.be.null;

model.change( writer => {
Expand All @@ -142,12 +142,13 @@ describe( 'TableToolbar', () => {

// Make sure successive change does not throw, e.g. attempting
// to insert the toolbar twice.
editingView.change( () => {} );
editor.ui.fire( 'update' );
expect( balloon.visibleView ).to.equal( toolbar );
} );

it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
setData( model, '<table><tableRow><tableCell>x[y]z</tableCell></tableRow></table>' );

expect( balloon.visibleView ).to.equal( toolbar );

// Put anything on top of the ContextualBalloon stack above the table toolbar.
Expand All @@ -163,7 +164,8 @@ describe( 'TableToolbar', () => {

expect( balloon.visibleView ).to.equal( lastView );

editingView.change( () => {} );
editor.ui.fire( 'update' );

expect( balloon.visibleView ).to.equal( lastView );
} );

Expand All @@ -183,7 +185,7 @@ describe( 'TableToolbar', () => {

// Make sure successive change does not throw, e.g. attempting
// to remove the toolbar twice.
editingView.change( () => {} );
editor.ui.fire( 'update' );
expect( balloon.visibleView ).to.be.null;
} );
} );
Expand Down