-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
menubarview.ts
428 lines (359 loc) · 13.2 KB
/
menubarview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/menubar/menubarview
*/
import {
logWarning,
type BaseEvent,
type Locale,
type ObservableChangeEvent
} from '@ckeditor/ckeditor5-utils';
import { type FocusableView } from '../focuscycler.js';
import View from '../view.js';
import { isObject } from 'lodash-es';
import ListItemView from '../list/listitemview.js';
import ListSeparatorView from '../list/listseparatorview.js';
import type ViewCollection from '../viewcollection.js';
import type ComponentFactory from '../componentfactory.js';
import MenuBarMenuView from './menubarmenuview.js';
import MenuBarMenuListView from './menubarmenulistview.js';
import MenuBarMenuListItemView from './menubarmenulistitemview.js';
import MenuBarMenuListItemButtonView from './menubarmenulistitembuttonview.js';
import MenuBarMenuListItemFileDialogButtonView from './menubarmenulistitemfiledialogbuttonview.js';
import {
MenuBarBehaviors,
processMenuBarConfig
} from './utils.js';
const EVENT_NAME_DELEGATES = [ 'mouseenter', 'arrowleft', 'arrowright', 'change:isOpen' ] as const;
import '../../theme/components/menubar/menubar.css';
/**
* The application menu bar component. It brings a set of top-level menus (and sub-menus) that can be used
* to organize and access a large number of buttons.
*/
export default class MenuBarView extends View implements FocusableView {
/**
* Collection of the child views inside the {@link #element}.
*/
public children: ViewCollection<MenuBarMenuView>;
/**
* Indicates whether any of top-level menus are open in the menu bar. To close
* the menu bar use the {@link #close} method.
*
* @observable
*/
declare public isOpen: boolean;
/**
* A list of {@link module:ui/menubar/menubarmenuview~MenuBarMenuView} instances registered in the menu bar.
*
* @observable
*/
public menus: Array<MenuBarMenuView> = [];
/**
* Creates an instance of the menu bar view.
*
* @param locale The localization services instance.
*/
constructor( locale: Locale ) {
super( locale );
const t = locale.t;
this.set( 'isOpen', false );
this._setupIsOpenUpdater();
this.children = this.createCollection();
// @if CK_DEBUG_MENU_BAR // // Logs events in the main event bus of the component.
// @if CK_DEBUG_MENU_BAR // this.on( 'menu', ( evt, data ) => {
// @if CK_DEBUG_MENU_BAR // console.log( `MenuBarView:${ evt.name }`, evt.path.map( view => view.element ) );
// @if CK_DEBUG_MENU_BAR // } );
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-menu-bar'
],
'aria-label': t( 'Editor menu bar' ),
role: 'menubar'
},
children: this.children
} );
}
/**
* A utility that expands a plain menu bar configuration into a structure of menus (also: sub-menus)
* and items using a given {@link module:ui/componentfactory~ComponentFactory component factory}.
*
* See the {@link module:core/editor/editorconfig~EditorConfig#menuBar menu bar} in the editor
* configuration reference to learn how to configure the menu bar.
*/
public fillFromConfig( config: NormalizedMenuBarConfigObject, componentFactory: ComponentFactory ): void {
const locale = this.locale!;
const processedConfig = processMenuBarConfig( {
normalizedConfig: config,
locale,
componentFactory
} );
const topLevelCategoryMenuViews = processedConfig.items.map( menuDefinition => this._createMenu( {
componentFactory,
menuDefinition
} ) );
this.children.addMany( topLevelCategoryMenuViews );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
MenuBarBehaviors.toggleMenusAndFocusItemsOnHover( this );
MenuBarBehaviors.closeMenusWhenTheBarCloses( this );
MenuBarBehaviors.closeMenuWhenAnotherOnTheSameLevelOpens( this );
MenuBarBehaviors.focusCycleMenusOnArrows( this );
MenuBarBehaviors.closeOnClickOutside( this );
}
/**
* Focuses the menu bar.
*/
public focus(): void {
if ( this.children.first ) {
this.children.first.focus();
}
}
/**
* Closes all menus in the bar.
*/
public close(): void {
for ( const topLevelCategoryMenuView of this.children ) {
topLevelCategoryMenuView.isOpen = false;
}
}
/**
* Registers a menu view in the menu bar. Every {@link module:ui/menubar/menubarmenuview~MenuBarMenuView} instance must be registered
* in the menu bar to be properly managed.
*/
public registerMenu( menuView: MenuBarMenuView, parentMenuView: MenuBarMenuView | null = null ): void {
if ( parentMenuView ) {
menuView.delegate( ...EVENT_NAME_DELEGATES ).to( parentMenuView );
menuView.parentMenuView = parentMenuView;
} else {
menuView.delegate( ...EVENT_NAME_DELEGATES ).to( this, name => 'menu:' + name );
}
menuView._attachBehaviors();
this.menus.push( menuView );
}
/**
* Creates a {@link module:ui/menubar/menubarmenuview~MenuBarMenuView} based on the given definition.
*/
private _createMenu( { componentFactory, menuDefinition, parentMenuView }: {
componentFactory: ComponentFactory;
menuDefinition: MenuBarMenuDefinition;
parentMenuView?: MenuBarMenuView;
} ) {
const locale = this.locale!;
const menuView = new MenuBarMenuView( locale );
this.registerMenu( menuView, parentMenuView );
menuView.buttonView.set( {
label: menuDefinition.label
} );
// Defer the creation of the menu structure until it gets open. This is a performance optimization
// that shortens the time needed to create the editor.
menuView.once<ObservableChangeEvent<boolean>>( 'change:isOpen', () => {
const listView = new MenuBarMenuListView( locale );
listView.ariaLabel = menuDefinition.label;
menuView.panelView.children.add( listView );
listView.items.addMany( this._createMenuItems( { menuDefinition, parentMenuView: menuView, componentFactory } ) );
} );
return menuView;
}
/**
* Creates a {@link module:ui/menubar/menubarmenuview~MenuBarMenuView} items based on the given definition.
*/
private _createMenuItems( { menuDefinition, parentMenuView, componentFactory }: {
menuDefinition: MenuBarMenuDefinition;
componentFactory: ComponentFactory;
parentMenuView: MenuBarMenuView;
} ): Array<MenuBarMenuListItemView | ListSeparatorView> {
const locale = this.locale!;
const items = [];
for ( const menuGroupDefinition of menuDefinition.groups ) {
for ( const itemDefinition of menuGroupDefinition.items ) {
const menuItemView = new MenuBarMenuListItemView( locale, parentMenuView );
if ( isObject( itemDefinition ) ) {
menuItemView.children.add( this._createMenu( {
componentFactory,
menuDefinition: itemDefinition,
parentMenuView
} ) );
} else {
const componentView = this._createMenuItemContentFromFactory( {
componentName: itemDefinition,
componentFactory,
parentMenuView
} );
if ( !componentView ) {
continue;
}
menuItemView.children.add( componentView );
}
items.push( menuItemView );
}
// Separate groups with a separator.
if ( menuGroupDefinition !== menuDefinition.groups[ menuDefinition.groups.length - 1 ] ) {
items.push( new ListSeparatorView( locale ) );
}
}
return items;
}
/**
* Uses the component factory to create a content of the menu item (a button or a sub-menu).
*/
private _createMenuItemContentFromFactory( { componentName, parentMenuView, componentFactory }: {
componentName: string;
componentFactory: ComponentFactory;
parentMenuView: MenuBarMenuView;
} ): MenuBarMenuView | MenuBarMenuListItemButtonView | null {
const componentView = componentFactory.create( componentName );
if ( !(
componentView instanceof MenuBarMenuView ||
componentView instanceof MenuBarMenuListItemButtonView ||
componentView instanceof MenuBarMenuListItemFileDialogButtonView
) ) {
/**
* Adding unsupported components to the {@link module:ui/menubar/menubarview~MenuBarView} is not possible.
*
* A component should be either a {@link module:ui/menubar/menubarmenuview~MenuBarMenuView} (sub-menu) or a
* {@link module:ui/menubar/menubarmenulistitembuttonview~MenuBarMenuListItemButtonView} (button).
*
* @error menu-bar-component-unsupported
* @param componentName A name of the unsupported component used in the configuration.
* @param componentView An unsupported component view.
*/
logWarning( 'menu-bar-component-unsupported', {
componentName,
componentView
} );
return null;
}
this._registerMenuTree( componentView, parentMenuView );
// Close the whole menu bar when a component is executed.
componentView.on( 'execute', () => {
this.close();
} );
return componentView;
}
/**
* Checks component and its children recursively and calls {@link #registerMenu}
* for each item that is {@link module:ui/menubar/menubarmenuview~MenuBarMenuView}.
*
* @internal
*/
private _registerMenuTree( componentView: MenuBarMenuView | MenuBarMenuListItemButtonView, parentMenuView: MenuBarMenuView ) {
if ( !( componentView instanceof MenuBarMenuView ) ) {
componentView.delegate( 'mouseenter' ).to( parentMenuView );
return;
}
this.registerMenu( componentView, parentMenuView );
const menuBarItemsList = componentView.panelView.children
.filter( child => child instanceof MenuBarMenuListView )[ 0 ] as MenuBarMenuListView | undefined;
if ( !menuBarItemsList ) {
componentView.delegate( 'mouseenter' ).to( parentMenuView );
return;
}
const nonSeparatorItems = menuBarItemsList.items.filter( item => item instanceof ListItemView ) as Array<ListItemView>;
for ( const item of nonSeparatorItems ) {
this._registerMenuTree(
item.children.get( 0 ) as MenuBarMenuView | MenuBarMenuListItemButtonView,
componentView
);
}
}
/**
* Manages the state of the {@link #isOpen} property of the menu bar. Because the state is a sum of individual
* top-level menus' states, it's necessary to listen to their changes and update the state accordingly.
*
* Additionally, it prevents from unnecessary changes of `isOpen` when one top-level menu opens and another closes
* (regardless of in which order), maintaining a stable `isOpen === true` in that situation.
*/
private _setupIsOpenUpdater() {
let closeTimeout: ReturnType<typeof setTimeout>;
// TODO: This is not the prettiest approach but at least it's simple.
this.on<MenuBarMenuChangeIsOpenEvent>( 'menu:change:isOpen', ( evt, name, isOpen ) => {
clearTimeout( closeTimeout );
if ( isOpen ) {
this.isOpen = true;
} else {
closeTimeout = setTimeout( () => {
this.isOpen = Array.from( this.children ).some( menuView => menuView.isOpen );
}, 0 );
}
} );
}
}
export type MenuBarConfig = MenuBarConfigObject;
export type MenuBarConfigObject = {
items?: Array<MenuBarMenuDefinition>;
removeItems?: Array<string>;
addItems?: Array<MenuBarConfigAddedItem | MenuBarConfigAddedGroup | MenuBarConfigAddedMenu>;
isVisible?: boolean;
};
export type NormalizedMenuBarConfigObject = Required<MenuBarConfigObject> & {
isUsingDefaultConfig: boolean;
};
export type MenuBarMenuGroupDefinition = {
groupId: string;
items: Array<MenuBarMenuDefinition | string>;
};
export type MenuBarMenuDefinition = {
menuId: string;
label: string;
groups: Array<MenuBarMenuGroupDefinition>;
};
export type MenuBarConfigAddedPosition =
`start:${ string }` | `end:${ string }` | 'start' | 'end' | `after:${ string }` | `before:${ string }`;
export type MenuBarConfigAddedItem = {
item: string;
position: MenuBarConfigAddedPosition;
};
export type MenuBarConfigAddedGroup = {
group: MenuBarMenuGroupDefinition;
position: MenuBarConfigAddedPosition;
};
export type MenuBarConfigAddedMenu = {
menu: MenuBarMenuDefinition;
position: MenuBarConfigAddedPosition;
};
/**
* Any namespaced event fired by menu a {@link module:ui/menubar/menubarview~MenuBarView#menus menu view instance} of the
* {@link module:ui/menubar/menubarview~MenuBarView menu bar}.
*/
interface MenuBarMenuEvent extends BaseEvent {
name: `menu:${ string }` | `menu:change:${ string }`;
}
/**
* A `mouseenter` event originating from a {@link module:ui/menubar/menubarview~MenuBarView#menus menu view instance} of the
* {@link module:ui/menubar/menubarview~MenuBarView menu bar}.
*/
export interface MenuBarMenuMouseEnterEvent extends MenuBarMenuEvent {
name: 'menu:mouseenter';
}
/**
* An `arrowleft` event originating from a {@link module:ui/menubar/menubarview~MenuBarView#menus menu view instance} of the
* {@link module:ui/menubar/menubarview~MenuBarView menu bar}.
*/
export interface MenuBarMenuArrowLeftEvent extends MenuBarMenuEvent {
name: 'menu:arrowleft';
}
/**
* An `arrowright` event originating from a {@link module:ui/menubar/menubarview~MenuBarView#menus menu view instance} of the
* {@link module:ui/menubar/menubarview~MenuBarView menu bar}.
*/
export interface MenuBarMenuArrowRightEvent extends MenuBarMenuEvent {
name: 'menu:arrowright';
}
/**
* A `change:isOpen` event originating from a {@link module:ui/menubar/menubarview~MenuBarView#menus menu view instance} of the
* {@link module:ui/menubar/menubarview~MenuBarView menu bar}.
*/
export interface MenuBarMenuChangeIsOpenEvent extends MenuBarMenuEvent {
name: 'menu:change:isOpen';
args: [ name: string, value: boolean, oldValue: boolean ];
}