8
8
*/
9
9
10
10
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' ;
17
13
18
14
/**
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.
20
16
*
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
22
18
* {@link module:ui/componentfactory~ComponentFactory component factory}
23
19
* based on the {@link module:table/table~TableConfig#toolbar `table.toolbar` configuration option}.
24
20
*
@@ -31,7 +27,7 @@ export default class TableToolbar extends Plugin {
31
27
* @inheritDoc
32
28
*/
33
29
static get requires ( ) {
34
- return [ ContextualBalloon ] ;
30
+ return [ WidgetToolbarRepository ] ;
35
31
}
36
32
37
33
/**
@@ -41,123 +37,17 @@ export default class TableToolbar extends Plugin {
41
37
return 'TableToolbar' ;
42
38
}
43
39
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
-
63
40
/**
64
41
* @inheritDoc
65
42
*/
66
43
afterInit ( ) {
67
44
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 ) ;
74
46
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 ,
97
50
} ) ;
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 ;
161
51
}
162
52
}
163
53
0 commit comments