@@ -100,6 +100,7 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
100
100
private static get CSS ( ) : { [ name : string ] : string } {
101
101
return {
102
102
toolbox : 'ce-toolbox' ,
103
+ toolboxOpenedTop : 'ce-toolbox--opened-top' ,
103
104
} ;
104
105
}
105
106
@@ -198,8 +199,15 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
198
199
return ;
199
200
}
200
201
201
- this . popover . show ( ) ;
202
+ /**
203
+ * Open popover top if there is not enought available space below it
204
+ */
205
+ if ( ! this . shouldOpenPopoverBottom ) {
206
+ this . nodes . toolbox . style . setProperty ( '--popover-height' , this . popover . calculateHeight ( ) + 'px' ) ;
207
+ this . nodes . toolbox . classList . add ( Toolbox . CSS . toolboxOpenedTop ) ;
208
+ }
202
209
210
+ this . popover . show ( ) ;
203
211
this . opened = true ;
204
212
this . emit ( ToolboxEvent . Opened ) ;
205
213
}
@@ -209,8 +217,8 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
209
217
*/
210
218
public close ( ) : void {
211
219
this . popover . hide ( ) ;
212
-
213
220
this . opened = false ;
221
+ this . nodes . toolbox . classList . remove ( Toolbox . CSS . toolboxOpenedTop ) ;
214
222
this . emit ( ToolboxEvent . Closed ) ;
215
223
}
216
224
@@ -225,6 +233,21 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
225
233
}
226
234
}
227
235
236
+ /**
237
+ * Checks if there popover should be opened downwards.
238
+ * It happens in case there is enough space below or not enough space above
239
+ */
240
+ private get shouldOpenPopoverBottom ( ) : boolean {
241
+ const toolboxRect = this . nodes . toolbox . getBoundingClientRect ( ) ;
242
+ const editorElementRect = this . api . ui . nodes . redactor . getBoundingClientRect ( ) ;
243
+ const popoverHeight = this . popover . calculateHeight ( ) ;
244
+ const popoverPotentialBottomEdge = toolboxRect . top + popoverHeight ;
245
+ const popoverPotentialTopEdge = toolboxRect . top - popoverHeight ;
246
+ const bottomEdgeForComparison = Math . min ( window . innerHeight , editorElementRect . bottom ) ;
247
+
248
+ return popoverPotentialTopEdge < editorElementRect . top || popoverPotentialBottomEdge <= bottomEdgeForComparison ;
249
+ }
250
+
228
251
/**
229
252
* Handles overlay click
230
253
*/
0 commit comments