ckeditor5-elements-core 49.0.0-alpha.2 → 49.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,275 +0,0 @@
1
- import {
2
- ContextualBalloon,
3
- DropdownMenuRootListView,
4
- KeystrokeHandler,
5
- Notification,
6
- Plugin,
7
- clickOutsideHandler,
8
- type Editor,
9
- type FocusableView,
10
- type ModelElement,
11
- type RectSource
12
- } from 'ckeditor5';
13
- import ElementMenuView from './elementMenuView.js';
14
- import ElementOrPositionCommand from './elementOrPositionCommand.js';
15
- import { isInDocument } from './utils.js';
16
-
17
- /**
18
- * A submenu of an element menu. Missing items are skipped, as in the menu.
19
- */
20
- export interface ElementSubmenu {
21
- title: string;
22
-
23
- /**
24
- * The icon (SVG string) shown next to the title.
25
- */
26
- icon?: string;
27
-
28
- items: (ElementMenuItem | null | undefined)[];
29
- }
30
-
31
- /**
32
- * An item of an element menu:
33
- *
34
- * * a command, shown as a button with its `title` and `icon`, enabled and "on" according to its
35
- * {@link ElementOrPositionCommand#getState state} for the element,
36
- * * a submenu,
37
- * * `'|'`, a separator. Separators at the start or the end of a menu, and repeated ones, are not shown, so a group of
38
- * items can start with one.
39
- */
40
- export type ElementMenuItem = ElementOrPositionCommand | ElementSubmenu | '|';
41
-
42
- /**
43
- * Returns the items of the menu of the given element, e.g. as the `elementContextMenu` or `breadcrumbs.menu` editor
44
- * config option. Missing items (e.g. commands that are not registered) are skipped. No items means no menu.
45
- */
46
- export type ElementMenuConfig = (element: ModelElement, editor: Editor) => (ElementMenuItem | null | undefined)[];
47
-
48
- /**
49
- * The options of {@link ElementMenu#open} and {@link ElementMenu#show}.
50
- */
51
- export interface ElementMenuShowOptions {
52
- /**
53
- * The accessible name of the menu of an element (see {@link ElementMenu#open}). Defaults to "Element menu".
54
- */
55
- label?: string;
56
-
57
- /**
58
- * Called when the document data changes while the menu is open. When it returns `true`, the menu closes, e.g.
59
- * because the element it is for was removed.
60
- */
61
- isObsolete?: () => boolean;
62
-
63
- /**
64
- * What gets the focus when the menu is closed with Esc, e.g. the button that opened the menu. Defaults to the editor.
65
- */
66
- returnFocusTo?: { focus(): void };
67
-
68
- /**
69
- * Called when the menu closes, for any reason: Esc, a click outside, an executed item, or another menu replacing it.
70
- */
71
- onHide?: () => void;
72
- }
73
-
74
- /**
75
- * Shows menus in a balloon: the menu of an element, built from commands, submenus and separators (see {@link open}),
76
- * or any view, e.g. the quick insert search (see {@link show}).
77
- *
78
- * The element context menu, the breadcrumbs and quick insert show their menus with it. The menu of an element is
79
- * CKEditor's dropdown menu (see {@link ElementMenuView}).
80
- *
81
- * Executing an item closes the menu. Esc and clicking outside close it too; Esc returns the focus to the editor, or to
82
- * {@link ElementMenuShowOptions.returnFocusTo}.
83
- */
84
- export default class ElementMenu extends Plugin {
85
- /**
86
- * The view in the open menu, if any.
87
- */
88
- public view: FocusableView | null = null;
89
-
90
- private _keystrokes: KeystrokeHandler | null = null;
91
- private _isObsolete: (() => boolean) | null = null;
92
- private _onHide: (() => void) | null = null;
93
-
94
- public static get requires(): readonly [typeof ContextualBalloon, typeof Notification] {
95
- // The buttons of the menu report failed commands with notifications.
96
- return [ContextualBalloon, Notification] as const;
97
- }
98
-
99
- public static get pluginName(): 'ElementMenu' {
100
- return 'ElementMenu' as const;
101
- }
102
-
103
- public init(): void {
104
- this.listenTo(this.editor.model.document, 'change:data', () => {
105
- if (this._isObsolete?.()) {
106
- this.hide();
107
- }
108
- });
109
- }
110
-
111
- /**
112
- * Opens the menu of the element next to the target (e.g. a button, or a `DOMRect` in viewport coordinates), with
113
- * the items from the config callback. The menu closes when the element is removed.
114
- *
115
- * Returns `false` when there are no items.
116
- */
117
- public open(
118
- element: ModelElement,
119
- target: RectSource,
120
- config: ElementMenuConfig,
121
- { label = this.editor.t('Element menu'), isObsolete, ...options }: ElementMenuShowOptions = {}
122
- ): boolean {
123
- const items = cleanItems(config(element, this.editor));
124
-
125
- if (!items.length) {
126
- return false;
127
- }
128
-
129
- const view = new ElementMenuView(this.editor.locale, this.editor.ui.view.body, items, element);
130
-
131
- view.ariaLabel = label;
132
-
133
- this.show(view, target, {
134
- ...options,
135
- isObsolete: () => !isInDocument(element) || !!isObsolete?.()
136
- });
137
-
138
- return true;
139
- }
140
-
141
- /**
142
- * Shows the view (e.g. a menu) next to the target (e.g. a button, or a `DOMRect` in viewport coordinates), and
143
- * focuses it. The menu closes when the view, or an item of CKEditor's dropdown menu in it, fires `execute`.
144
- */
145
- public show(
146
- view: FocusableView,
147
- target: RectSource,
148
- { isObsolete, returnFocusTo, onHide }: ElementMenuShowOptions = {}
149
- ): void {
150
- this.hide();
151
-
152
- const close = (): void => {
153
- // The item may have opened another menu already (e.g. "Insert before" opens quick insert).
154
- if (this.view === view) {
155
- this.hide();
156
- }
157
- };
158
-
159
- this.listenTo(view, 'execute', close);
160
- this.listenTo(view, 'menu:execute', close);
161
-
162
- clickOutsideHandler({
163
- emitter: view,
164
- activator: () => this._balloon.visibleView === view,
165
- // The open nested menus are in panels outside the balloon.
166
- contextElements: () => [this._balloon.view.element, ...getOpenPanels(view)].filter(element => !!element),
167
- callback: () => {
168
- this.hide();
169
- }
170
- });
171
-
172
- this._balloon.add({
173
- view,
174
- position: { target },
175
- withArrow: false,
176
- singleViewMode: true,
177
- // Views that other plugins add to the balloon meanwhile (e.g. an element toolbar, when the selection moves
178
- // into its element) go to their own stack, so they do not cover the menu.
179
- stackId: 'elementMenu'
180
- });
181
-
182
- const keystrokes = new KeystrokeHandler();
183
-
184
- // Esc in a nested menu closes only that menu.
185
- keystrokes.set('Esc', (data, cancel) => {
186
- this.hide();
187
- (returnFocusTo ?? this.editor.editing.view).focus();
188
- cancel();
189
- });
190
-
191
- if (view.element) {
192
- keystrokes.listenTo(view.element);
193
- }
194
-
195
- this.view = view;
196
- this._keystrokes = keystrokes;
197
- this._isObsolete = isObsolete ?? null;
198
- this._onHide = onHide ?? null;
199
- view.focus();
200
- }
201
-
202
- /**
203
- * Hides the open menu, if any.
204
- */
205
- public hide(): void {
206
- const view = this.view;
207
- const onHide = this._onHide;
208
-
209
- if (!view) {
210
- return;
211
- }
212
-
213
- this._keystrokes?.destroy();
214
- this.view = null;
215
- this._keystrokes = null;
216
- this._isObsolete = null;
217
- this._onHide = null;
218
- this.stopListening(view);
219
-
220
- if (this._balloon.hasView(view)) {
221
- this._balloon.remove(view);
222
- }
223
-
224
- view.destroy();
225
- onHide?.();
226
- }
227
-
228
- public override destroy(): void {
229
- this.hide();
230
- super.destroy();
231
- }
232
-
233
- private get _balloon(): ContextualBalloon {
234
- return this.editor.plugins.get(ContextualBalloon);
235
- }
236
- }
237
-
238
- /**
239
- * Returns the items without missing ones, submenus without items, and separators at the start or the end, or
240
- * repeated ones.
241
- */
242
- function cleanItems(items: readonly (ElementMenuItem | null | undefined)[]): ElementMenuItem[] {
243
- const result: ElementMenuItem[] = [];
244
-
245
- for (const item of items) {
246
- const cleaned = item === '|' || item instanceof ElementOrPositionCommand || !item ? item : cleanSubmenu(item);
247
-
248
- if (!cleaned || (cleaned === '|' && (!result.length || result.at(-1) === '|'))) {
249
- continue;
250
- }
251
-
252
- result.push(cleaned);
253
- }
254
-
255
- if (result.at(-1) === '|') {
256
- result.pop();
257
- }
258
-
259
- return result;
260
- }
261
-
262
- function cleanSubmenu(submenu: ElementSubmenu): ElementSubmenu | null {
263
- const items = cleanItems(submenu.items);
264
-
265
- return items.length ? { ...submenu, items } : null;
266
- }
267
-
268
- /**
269
- * Returns the panels of the open nested menus of CKEditor's dropdown menu.
270
- */
271
- function getOpenPanels(view: FocusableView): (HTMLElement | null)[] {
272
- return view instanceof DropdownMenuRootListView
273
- ? view.menus.filter(menu => menu.isOpen).map(menu => menu.panelView.element)
274
- : [];
275
- }
@@ -1,164 +0,0 @@
1
- import {
2
- DropdownMenuRootListView,
3
- ListSeparatorView,
4
- type BodyCollection,
5
- type DropdownMenuDefinition,
6
- type ListView,
7
- type Locale
8
- } from 'ckeditor5';
9
- import { type ElementMenuItem, type ElementSubmenu } from './elementMenu.js';
10
- import { bindButtonToCommand } from './elementOrPositionButtonView.js';
11
- import ElementOrPositionCommand, { type ElementOrPosition } from './elementOrPositionCommand.js';
12
-
13
- /**
14
- * The menu of an element: CKEditor's dropdown menu (`DropdownMenuRootListView`), built from commands, submenus and
15
- * separators.
16
- *
17
- * CKEditor builds the menu and handles it: the nested menus (opened on click, hover, or the right arrow; placed where
18
- * they fit), the keyboard, and right-to-left languages. This view only connects the buttons to the commands: they
19
- * reflect the command title, icon and {@link ElementOrPositionCommand#getState state} for the target, and execute the
20
- * command for it. It also adds the separators, which CKEditor's menu definitions do not have.
21
- *
22
- * Submenus are enabled when one of their items is. Right-clicking the menu does not open the native context menu.
23
- */
24
- export default class ElementMenuView extends DropdownMenuRootListView {
25
- /**
26
- * The element or position the commands are executed for.
27
- */
28
- public readonly target: ElementOrPosition;
29
-
30
- private readonly _commands: Map<string, ElementOrPositionCommand>;
31
- private readonly _submenus: Map<string, ElementSubmenu>;
32
-
33
- /**
34
- * The positions of the separators in each list, by the id of the submenu (`''` for the menu itself).
35
- */
36
- private readonly _separators: Map<string, number[]>;
37
-
38
- constructor(
39
- locale: Locale,
40
- bodyCollection: BodyCollection,
41
- items: readonly (ElementMenuItem | null | undefined)[],
42
- target: ElementOrPosition
43
- ) {
44
- const maps: DefinitionMaps = { commands: new Map(), submenus: new Map(), separators: new Map() };
45
-
46
- super(locale, bodyCollection, createDefinitions(items, '', maps));
47
-
48
- this.target = target;
49
- this._commands = maps.commands;
50
- this._submenus = maps.submenus;
51
- this._separators = maps.separators;
52
- this.menuPanelClass = 'ck-elements-menu__panel';
53
-
54
- this.extendTemplate({
55
- attributes: { class: 'ck-elements-menu' },
56
- on: { contextmenu: this.bindTemplate.to(preventDefault) }
57
- });
58
- }
59
-
60
- /**
61
- * Renders the menu: CKEditor builds it from the definitions; then the buttons are connected to the commands, and the
62
- * separators are added.
63
- */
64
- public override render(): void {
65
- super.render();
66
-
67
- for (const button of this.buttons) {
68
- const command = this._commands.get(button.id);
69
-
70
- if (command) {
71
- bindButtonToCommand(button, command, { withText: true, target: this.target });
72
-
73
- // A toggleable button with a checkable role tells assistive technology whether it is "on" with `aria-checked`.
74
- button.role = command.isToggleable ? 'menuitemcheckbox' : 'menuitem';
75
- }
76
- }
77
-
78
- for (const menu of this.menus) {
79
- const submenu = this._submenus.get(menu.id);
80
-
81
- if (submenu) {
82
- menu.buttonView.icon = submenu.icon;
83
- menu.isEnabled = this._isEnabled(submenu);
84
- }
85
-
86
- // The panels of nested menus are not inside the menu.
87
- if (menu.panelView.element) {
88
- this.listenTo(menu.panelView.element, 'contextmenu', (evt, domEvent: Event) => {
89
- preventDefault(domEvent);
90
- });
91
- }
92
- }
93
-
94
- this._addSeparators('', this);
95
-
96
- for (const menu of this.menus) {
97
- this._addSeparators(menu.id, menu.listView);
98
- }
99
- }
100
-
101
- private _addSeparators(id: string, list: ListView): void {
102
- for (const position of this._separators.get(id) ?? []) {
103
- const separator = new ListSeparatorView(this.locale);
104
-
105
- separator.extendTemplate({ attributes: { role: 'separator' } });
106
- list.items.add(separator, position);
107
- }
108
- }
109
-
110
- private _isEnabled(item: ElementMenuItem): boolean {
111
- if (item === '|') {
112
- return false;
113
- }
114
-
115
- return item instanceof ElementOrPositionCommand
116
- ? item.getState(this.target).isEnabled
117
- : item.items.some(child => !!child && this._isEnabled(child));
118
- }
119
- }
120
-
121
- interface DefinitionMaps {
122
- commands: Map<string, ElementOrPositionCommand>;
123
- submenus: Map<string, ElementSubmenu>;
124
- separators: Map<string, number[]>;
125
- }
126
-
127
- /**
128
- * Creates CKEditor's menu definitions for the items. The ids are paths of positions (e.g. `'2.0'`). Collects the
129
- * commands and submenus by id, and the positions of the separators by the id of their submenu.
130
- */
131
- function createDefinitions(
132
- items: readonly (ElementMenuItem | null | undefined)[],
133
- parentId: string,
134
- maps: DefinitionMaps
135
- ): DropdownMenuDefinition {
136
- const definitions: DropdownMenuDefinition = [];
137
- let position = 0;
138
-
139
- for (const item of items) {
140
- const id = parentId ? `${parentId}.${String(position)}` : String(position);
141
-
142
- if (!item) {
143
- continue;
144
- }
145
-
146
- if (item === '|') {
147
- maps.separators.set(parentId, [...(maps.separators.get(parentId) ?? []), position]);
148
- } else if (item instanceof ElementOrPositionCommand) {
149
- maps.commands.set(id, item);
150
- definitions.push({ id, label: item.title });
151
- } else {
152
- maps.submenus.set(id, item);
153
- definitions.push({ id, menu: item.title, children: createDefinitions(item.items, id, maps) });
154
- }
155
-
156
- position++;
157
- }
158
-
159
- return definitions;
160
- }
161
-
162
- function preventDefault(domEvent: Event): void {
163
- domEvent.preventDefault();
164
- }
@@ -1,14 +0,0 @@
1
- /*
2
- * The menu of an element is CKEditor's dropdown menu, so CKEditor styles it. Only the alignment of labels is added.
3
- */
4
-
5
- /* In a menu with icons (or a nested menu of it), items without an icon get the space of one, so that all labels line up. */
6
- :is(.ck.ck-elements-menu, .ck.ck-elements-menu__panel > .ck-dropdown-menu-list):has(
7
- > .ck-list__item > :is(.ck-button, .ck-dropdown-menu-list__nested-menu > .ck-button) > .ck-button__icon
8
- )
9
- > .ck-list__item
10
- > :is(.ck-button, .ck-dropdown-menu-list__nested-menu > .ck-button):not(:has(> .ck-button__icon))
11
- > .ck-button__label {
12
- /* The width of an icon, and the gap after it. */
13
- margin-inline-start: calc(var(--ck-icon-size) + var(--ck-button-icon-gap));
14
- }