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

Commit e276e66

Browse files
author
Piotr Jasiun
authored
Merge pull request #115 from ckeditor/t/ckeditor5-ui/442
Other: Aligned `tableToolbar` to the widget toolbar repository. Closes #107.
2 parents 76857ba + 5086cfd commit e276e66

File tree

2 files changed

+12
-122
lines changed

2 files changed

+12
-122
lines changed

src/tabletoolbar.js

Lines changed: 9 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
*/
99

1010
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
11-
import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
12-
import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
13-
import { isTableWidgetSelected, isTableContentSelected } from './utils';
14-
import { repositionContextualBalloon, getBalloonPositionData } from './ui/utils';
15-
16-
const balloonClassName = 'ck-toolbar-container';
11+
import { isTableContentSelected } from './utils';
12+
import WidgetToolbarRepository from '@ckeditor/ckeditor5-widget/src/widgettoolbarrepository';
1713

1814
/**
19-
* The table toolbar class. It creates a table toolbar that shows up when the table widget is selected.
15+
* The table toolbar class. It creates a table toolbar that shows up when the table content is selected.
2016
*
21-
* Instanecs of toolbar components (e.g. buttons) are created using the editor's
17+
* Instances of toolbar components (e.g. buttons) are created using the editor's
2218
* {@link module:ui/componentfactory~ComponentFactory component factory}
2319
* based on the {@link module:table/table~TableConfig#toolbar `table.toolbar` configuration option}.
2420
*
@@ -31,7 +27,7 @@ export default class TableToolbar extends Plugin {
3127
* @inheritDoc
3228
*/
3329
static get requires() {
34-
return [ ContextualBalloon ];
30+
return [ WidgetToolbarRepository ];
3531
}
3632

3733
/**
@@ -41,123 +37,17 @@ export default class TableToolbar extends Plugin {
4137
return 'TableToolbar';
4238
}
4339

44-
/**
45-
* @inheritDoc
46-
*/
47-
init() {
48-
const editor = this.editor;
49-
const balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
50-
51-
// If the `BalloonToolbar` plugin is loaded, it should be disabled for tables
52-
// which have their own toolbar to avoid duplication.
53-
// https://github.com/ckeditor/ckeditor5-image/issues/110
54-
if ( balloonToolbar ) {
55-
this.listenTo( balloonToolbar, 'show', evt => {
56-
if ( isTableWidgetSelected( editor.editing.view.document.selection ) ) {
57-
evt.stop();
58-
}
59-
}, { priority: 'high' } );
60-
}
61-
}
62-
6340
/**
6441
* @inheritDoc
6542
*/
6643
afterInit() {
6744
const editor = this.editor;
68-
const toolbarConfig = editor.config.get( 'table.toolbar' );
69-
70-
// Don't add the toolbar if there is no configuration.
71-
if ( !toolbarConfig || !toolbarConfig.length ) {
72-
return;
73-
}
45+
const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
7446

75-
/**
76-
* A contextual balloon plugin instance.
77-
*
78-
* @private
79-
* @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
80-
*/
81-
this._balloon = this.editor.plugins.get( 'ContextualBalloon' );
82-
83-
/**
84-
* A toolbar view instance used to display the buttons specific for table editing.
85-
*
86-
* @protected
87-
* @type {module:ui/toolbar/toolbarview~ToolbarView}
88-
*/
89-
this._toolbar = new ToolbarView();
90-
91-
// Add buttons to the toolbar.
92-
this._toolbar.fillFromConfig( toolbarConfig, editor.ui.componentFactory );
93-
94-
// Show balloon panel each time table widget is selected.
95-
this.listenTo( editor.ui, 'update', () => {
96-
this._checkIsVisible();
47+
widgetToolbarRepository.register( 'table', {
48+
items: editor.config.get( 'table.toolbar' ) || [],
49+
visibleWhen: isTableContentSelected,
9750
} );
98-
99-
// There is no render method after focus is back in editor, we need to check if balloon panel should be visible.
100-
this.listenTo( editor.ui.focusTracker, 'change:isFocused', () => {
101-
this._checkIsVisible();
102-
}, { priority: 'low' } );
103-
}
104-
105-
/**
106-
* Checks whether the toolbar should show up or hide depending on the current selection.
107-
*
108-
* @private
109-
*/
110-
_checkIsVisible() {
111-
const editor = this.editor;
112-
const viewSelection = editor.editing.view.document.selection;
113-
114-
if ( !editor.ui.focusTracker.isFocused || !isTableContentSelected( viewSelection ) ) {
115-
this._hideToolbar();
116-
} else {
117-
this._showToolbar();
118-
}
119-
}
120-
121-
/**
122-
* Shows the {@link #_toolbar} in the {@link #_balloon}.
123-
*
124-
* @private
125-
*/
126-
_showToolbar() {
127-
const editor = this.editor;
128-
129-
if ( this._isVisible ) {
130-
repositionContextualBalloon( editor );
131-
} else if ( !this._balloon.hasView( this._toolbar ) ) {
132-
this._balloon.add( {
133-
view: this._toolbar,
134-
position: getBalloonPositionData( editor ),
135-
balloonClassName
136-
} );
137-
}
138-
}
139-
140-
/**
141-
* Removes the {@link #_toolbar} from the {@link #_balloon}.
142-
*
143-
* @private
144-
*/
145-
_hideToolbar() {
146-
if ( !this._isVisible ) {
147-
return;
148-
}
149-
150-
this._balloon.remove( this._toolbar );
151-
}
152-
153-
/**
154-
* Returns `true` when the {@link #_toolbar} is the visible view in the {@link #_balloon}.
155-
*
156-
* @private
157-
* @type {Boolean}
158-
*/
159-
get _isVisible() {
160-
return this._balloon.visibleView == this._toolbar;
16151
}
16252
}
16353

tests/tabletoolbar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
1717
import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
1818

1919
describe( 'TableToolbar', () => {
20-
let editor, model, doc, plugin, toolbar, balloon, editorElement;
20+
let editor, model, doc, widgetToolbarRepository, toolbar, balloon, editorElement;
2121

2222
beforeEach( () => {
2323
editorElement = global.document.createElement( 'div' );
@@ -34,8 +34,8 @@ describe( 'TableToolbar', () => {
3434
editor = newEditor;
3535
model = newEditor.model;
3636
doc = model.document;
37-
plugin = editor.plugins.get( TableToolbar );
38-
toolbar = plugin._toolbar;
37+
widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
38+
toolbar = widgetToolbarRepository._toolbars.get( 'table' ).view;
3939
balloon = editor.plugins.get( 'ContextualBalloon' );
4040
} );
4141
} );

0 commit comments

Comments
 (0)