ckeditor5-elements-menu 0.0.0-stage → 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.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -2
  3. package/dist/augmentation.d.ts +30 -0
  4. package/dist/augmentation.d.ts.map +1 -0
  5. package/dist/augmentation.js +2 -0
  6. package/dist/augmentation.js.map +1 -0
  7. package/dist/breadcrumbs/breadcrumbItemView.d.ts +47 -0
  8. package/dist/breadcrumbs/breadcrumbItemView.d.ts.map +1 -0
  9. package/dist/breadcrumbs/breadcrumbItemView.js +30 -0
  10. package/dist/breadcrumbs/breadcrumbItemView.js.map +1 -0
  11. package/dist/breadcrumbs/breadcrumbLabelView.d.ts +29 -0
  12. package/dist/breadcrumbs/breadcrumbLabelView.d.ts.map +1 -0
  13. package/dist/breadcrumbs/breadcrumbLabelView.js +39 -0
  14. package/dist/breadcrumbs/breadcrumbLabelView.js.map +1 -0
  15. package/dist/breadcrumbs/breadcrumbs.d.ts +72 -0
  16. package/dist/breadcrumbs/breadcrumbs.d.ts.map +1 -0
  17. package/dist/breadcrumbs/breadcrumbs.js +224 -0
  18. package/dist/breadcrumbs/breadcrumbs.js.map +1 -0
  19. package/dist/breadcrumbs/breadcrumbsView.d.ts +68 -0
  20. package/dist/breadcrumbs/breadcrumbsView.d.ts.map +1 -0
  21. package/dist/breadcrumbs/breadcrumbsView.js +134 -0
  22. package/dist/breadcrumbs/breadcrumbsView.js.map +1 -0
  23. package/dist/contextMenu/contextMenuObserver.d.ts +19 -0
  24. package/dist/contextMenu/contextMenuObserver.d.ts.map +1 -0
  25. package/dist/contextMenu/contextMenuObserver.js +18 -0
  26. package/dist/contextMenu/contextMenuObserver.js.map +1 -0
  27. package/dist/contextMenu/elementContextMenu.d.ts +34 -0
  28. package/dist/contextMenu/elementContextMenu.d.ts.map +1 -0
  29. package/dist/contextMenu/elementContextMenu.js +98 -0
  30. package/dist/contextMenu/elementContextMenu.js.map +1 -0
  31. package/dist/elementMenu.d.ts +93 -0
  32. package/dist/elementMenu.d.ts.map +1 -0
  33. package/dist/elementMenu.js +162 -0
  34. package/dist/elementMenu.js.map +1 -0
  35. package/dist/elementMenuView.d.ts +35 -0
  36. package/dist/elementMenuView.d.ts.map +1 -0
  37. package/dist/elementMenuView.js +117 -0
  38. package/dist/elementMenuView.js.map +1 -0
  39. package/dist/index.d.ts +24 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +29 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/quickInsert/icons.d.ts +16 -0
  44. package/dist/quickInsert/icons.d.ts.map +1 -0
  45. package/dist/quickInsert/icons.js +21 -0
  46. package/dist/quickInsert/icons.js.map +1 -0
  47. package/dist/quickInsert/quickInsert.d.ts +57 -0
  48. package/dist/quickInsert/quickInsert.d.ts.map +1 -0
  49. package/dist/quickInsert/quickInsert.js +106 -0
  50. package/dist/quickInsert/quickInsert.js.map +1 -0
  51. package/dist/quickInsert/quickInsertCommand.d.ts +38 -0
  52. package/dist/quickInsert/quickInsertCommand.d.ts.map +1 -0
  53. package/dist/quickInsert/quickInsertCommand.js +70 -0
  54. package/dist/quickInsert/quickInsertCommand.js.map +1 -0
  55. package/dist/quickInsert/quickInsertListView.d.ts +29 -0
  56. package/dist/quickInsert/quickInsertListView.d.ts.map +1 -0
  57. package/dist/quickInsert/quickInsertListView.js +44 -0
  58. package/dist/quickInsert/quickInsertListView.js.map +1 -0
  59. package/dist/quickInsert/quickInsertPositionCommand.d.ts +28 -0
  60. package/dist/quickInsert/quickInsertPositionCommand.d.ts.map +1 -0
  61. package/dist/quickInsert/quickInsertPositionCommand.js +54 -0
  62. package/dist/quickInsert/quickInsertPositionCommand.js.map +1 -0
  63. package/dist/quickInsert/quickInsertView.d.ts +20 -0
  64. package/dist/quickInsert/quickInsertView.d.ts.map +1 -0
  65. package/dist/quickInsert/quickInsertView.js +52 -0
  66. package/dist/quickInsert/quickInsertView.js.map +1 -0
  67. package/package.json +49 -3
  68. package/src/augmentation.ts +44 -0
  69. package/src/breadcrumbs/breadcrumbItemView.ts +74 -0
  70. package/src/breadcrumbs/breadcrumbLabelView.ts +55 -0
  71. package/src/breadcrumbs/breadcrumbs.ts +322 -0
  72. package/src/breadcrumbs/breadcrumbsView.ts +176 -0
  73. package/src/contextMenu/contextMenuObserver.ts +27 -0
  74. package/src/contextMenu/elementContextMenu.ts +116 -0
  75. package/src/elementMenu.ts +274 -0
  76. package/src/elementMenuView.ts +163 -0
  77. package/src/index.ts +54 -0
  78. package/src/quickInsert/icons.ts +26 -0
  79. package/src/quickInsert/quickInsert.ts +149 -0
  80. package/src/quickInsert/quickInsertCommand.ts +85 -0
  81. package/src/quickInsert/quickInsertListView.ts +71 -0
  82. package/src/quickInsert/quickInsertPositionCommand.ts +72 -0
  83. package/src/quickInsert/quickInsertView.ts +64 -0
  84. package/theme/menu.css +131 -0
@@ -0,0 +1,274 @@
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 { ElementOrPositionCommand, isInDocument } from 'ckeditor5-elements-core';
14
+ import ElementMenuView from './elementMenuView.js';
15
+
16
+ /**
17
+ * A submenu of an element menu. Missing items are skipped, as in the menu.
18
+ */
19
+ export interface ElementSubmenu {
20
+ title: string;
21
+
22
+ /**
23
+ * The icon (SVG string) shown next to the title.
24
+ */
25
+ icon?: string;
26
+
27
+ items: (ElementMenuItem | null | undefined)[];
28
+ }
29
+
30
+ /**
31
+ * An item of an element menu:
32
+ *
33
+ * * a command, shown as a button with its `title` and `icon`, enabled and "on" according to its
34
+ * {@link ElementOrPositionCommand#getState state} for the element,
35
+ * * a submenu,
36
+ * * `'|'`, a separator. Separators at the start or the end of a menu, and repeated ones, are not shown, so a group of
37
+ * items can start with one.
38
+ */
39
+ export type ElementMenuItem = ElementOrPositionCommand | ElementSubmenu | '|';
40
+
41
+ /**
42
+ * Returns the items of the menu of the given element, e.g. as the `elementContextMenu` or `breadcrumbs.menu` editor
43
+ * config option. Missing items (e.g. commands that are not registered) are skipped. No items means no menu.
44
+ */
45
+ export type ElementMenuConfig = (element: ModelElement, editor: Editor) => (ElementMenuItem | null | undefined)[];
46
+
47
+ /**
48
+ * The options of {@link ElementMenu#open} and {@link ElementMenu#show}.
49
+ */
50
+ export interface ElementMenuShowOptions {
51
+ /**
52
+ * The accessible name of the menu of an element (see {@link ElementMenu#open}). Defaults to "Element menu".
53
+ */
54
+ label?: string;
55
+
56
+ /**
57
+ * Called when the document data changes while the menu is open. When it returns `true`, the menu closes, e.g.
58
+ * because the element it is for was removed.
59
+ */
60
+ isObsolete?: () => boolean;
61
+
62
+ /**
63
+ * What gets the focus when the menu is closed with Esc, e.g. the button that opened the menu. Defaults to the editor.
64
+ */
65
+ returnFocusTo?: { focus(): void };
66
+
67
+ /**
68
+ * Called when the menu closes, for any reason: Esc, a click outside, an executed item, or another menu replacing it.
69
+ */
70
+ onHide?: () => void;
71
+ }
72
+
73
+ /**
74
+ * Shows menus in a balloon: the menu of an element, built from commands, submenus and separators (see {@link open}),
75
+ * or any view, e.g. the quick insert search (see {@link show}).
76
+ *
77
+ * The element context menu, the breadcrumbs and quick insert show their menus with it. The menu of an element is
78
+ * CKEditor's dropdown menu (see {@link ElementMenuView}).
79
+ *
80
+ * Executing an item closes the menu. Esc and clicking outside close it too; Esc returns the focus to the editor, or to
81
+ * {@link ElementMenuShowOptions.returnFocusTo}.
82
+ */
83
+ export default class ElementMenu extends Plugin {
84
+ /**
85
+ * The view in the open menu, if any.
86
+ */
87
+ public view: FocusableView | null = null;
88
+
89
+ private _keystrokes: KeystrokeHandler | null = null;
90
+ private _isObsolete: (() => boolean) | null = null;
91
+ private _onHide: (() => void) | null = null;
92
+
93
+ public static get requires(): readonly [typeof ContextualBalloon, typeof Notification] {
94
+ // The buttons of the menu report failed commands with notifications.
95
+ return [ContextualBalloon, Notification] as const;
96
+ }
97
+
98
+ public static get pluginName(): 'ElementMenu' {
99
+ return 'ElementMenu' as const;
100
+ }
101
+
102
+ public init(): void {
103
+ this.listenTo(this.editor.model.document, 'change:data', () => {
104
+ if (this._isObsolete?.()) {
105
+ this.hide();
106
+ }
107
+ });
108
+ }
109
+
110
+ /**
111
+ * Opens the menu of the element next to the target (e.g. a button, or a `DOMRect` in viewport coordinates), with
112
+ * the items from the config callback. The menu closes when the element is removed.
113
+ *
114
+ * Returns `false` when there are no items.
115
+ */
116
+ public open(
117
+ element: ModelElement,
118
+ target: RectSource,
119
+ config: ElementMenuConfig,
120
+ { label = this.editor.t('Element menu'), isObsolete, ...options }: ElementMenuShowOptions = {}
121
+ ): boolean {
122
+ const items = cleanItems(config(element, this.editor));
123
+
124
+ if (!items.length) {
125
+ return false;
126
+ }
127
+
128
+ const view = new ElementMenuView(this.editor.locale, this.editor.ui.view.body, items, element);
129
+
130
+ view.ariaLabel = label;
131
+
132
+ this.show(view, target, {
133
+ ...options,
134
+ isObsolete: () => !isInDocument(element) || !!isObsolete?.()
135
+ });
136
+
137
+ return true;
138
+ }
139
+
140
+ /**
141
+ * Shows the view (e.g. a menu) next to the target (e.g. a button, or a `DOMRect` in viewport coordinates), and
142
+ * focuses it. The menu closes when the view, or an item of CKEditor's dropdown menu in it, fires `execute`.
143
+ */
144
+ public show(
145
+ view: FocusableView,
146
+ target: RectSource,
147
+ { isObsolete, returnFocusTo, onHide }: ElementMenuShowOptions = {}
148
+ ): void {
149
+ this.hide();
150
+
151
+ const close = (): void => {
152
+ // The item may have opened another menu already (e.g. "Insert before" opens quick insert).
153
+ if (this.view === view) {
154
+ this.hide();
155
+ }
156
+ };
157
+
158
+ this.listenTo(view, 'execute', close);
159
+ this.listenTo(view, 'menu:execute', close);
160
+
161
+ clickOutsideHandler({
162
+ emitter: view,
163
+ activator: () => this._balloon.visibleView === view,
164
+ // The open nested menus are in panels outside the balloon.
165
+ contextElements: () => [this._balloon.view.element, ...getOpenPanels(view)].filter(element => !!element),
166
+ callback: () => {
167
+ this.hide();
168
+ }
169
+ });
170
+
171
+ this._balloon.add({
172
+ view,
173
+ position: { target },
174
+ withArrow: false,
175
+ singleViewMode: true,
176
+ // Views that other plugins add to the balloon meanwhile (e.g. an element toolbar, when the selection moves
177
+ // into its element) go to their own stack, so they do not cover the menu.
178
+ stackId: 'elementMenu'
179
+ });
180
+
181
+ const keystrokes = new KeystrokeHandler();
182
+
183
+ // Esc in a nested menu closes only that menu.
184
+ keystrokes.set('Esc', (data, cancel) => {
185
+ this.hide();
186
+ (returnFocusTo ?? this.editor.editing.view).focus();
187
+ cancel();
188
+ });
189
+
190
+ if (view.element) {
191
+ keystrokes.listenTo(view.element);
192
+ }
193
+
194
+ this.view = view;
195
+ this._keystrokes = keystrokes;
196
+ this._isObsolete = isObsolete ?? null;
197
+ this._onHide = onHide ?? null;
198
+ view.focus();
199
+ }
200
+
201
+ /**
202
+ * Hides the open menu, if any.
203
+ */
204
+ public hide(): void {
205
+ const view = this.view;
206
+ const onHide = this._onHide;
207
+
208
+ if (!view) {
209
+ return;
210
+ }
211
+
212
+ this._keystrokes?.destroy();
213
+ this.view = null;
214
+ this._keystrokes = null;
215
+ this._isObsolete = null;
216
+ this._onHide = null;
217
+ this.stopListening(view);
218
+
219
+ if (this._balloon.hasView(view)) {
220
+ this._balloon.remove(view);
221
+ }
222
+
223
+ view.destroy();
224
+ onHide?.();
225
+ }
226
+
227
+ public override destroy(): void {
228
+ this.hide();
229
+ super.destroy();
230
+ }
231
+
232
+ private get _balloon(): ContextualBalloon {
233
+ return this.editor.plugins.get(ContextualBalloon);
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Returns the items without missing ones, submenus without items, and separators at the start or the end, or
239
+ * repeated ones.
240
+ */
241
+ function cleanItems(items: readonly (ElementMenuItem | null | undefined)[]): ElementMenuItem[] {
242
+ const result: ElementMenuItem[] = [];
243
+
244
+ for (const item of items) {
245
+ const cleaned = item === '|' || item instanceof ElementOrPositionCommand || !item ? item : cleanSubmenu(item);
246
+
247
+ if (!cleaned || (cleaned === '|' && (!result.length || result.at(-1) === '|'))) {
248
+ continue;
249
+ }
250
+
251
+ result.push(cleaned);
252
+ }
253
+
254
+ if (result.at(-1) === '|') {
255
+ result.pop();
256
+ }
257
+
258
+ return result;
259
+ }
260
+
261
+ function cleanSubmenu(submenu: ElementSubmenu): ElementSubmenu | null {
262
+ const items = cleanItems(submenu.items);
263
+
264
+ return items.length ? { ...submenu, items } : null;
265
+ }
266
+
267
+ /**
268
+ * Returns the panels of the open nested menus of CKEditor's dropdown menu.
269
+ */
270
+ function getOpenPanels(view: FocusableView): (HTMLElement | null)[] {
271
+ return view instanceof DropdownMenuRootListView
272
+ ? view.menus.filter(menu => menu.isOpen).map(menu => menu.panelView.element)
273
+ : [];
274
+ }
@@ -0,0 +1,163 @@
1
+ import {
2
+ DropdownMenuRootListView,
3
+ ListSeparatorView,
4
+ type BodyCollection,
5
+ type DropdownMenuDefinition,
6
+ type ListView,
7
+ type Locale
8
+ } from 'ckeditor5';
9
+ import { ElementOrPositionCommand, bindButtonToCommand, type ElementOrPosition } from 'ckeditor5-elements-core';
10
+ import { type ElementMenuItem, type ElementSubmenu } from './elementMenu.js';
11
+
12
+ /**
13
+ * The menu of an element: CKEditor's dropdown menu (`DropdownMenuRootListView`), built from commands, submenus and
14
+ * separators.
15
+ *
16
+ * CKEditor builds the menu and handles it: the nested menus (opened on click, hover, or the right arrow; placed where
17
+ * they fit), the keyboard, and right-to-left languages. This view only connects the buttons to the commands: they
18
+ * reflect the command title, icon and {@link ElementOrPositionCommand#getState state} for the target, and execute the
19
+ * command for it. It also adds the separators, which CKEditor's menu definitions do not have.
20
+ *
21
+ * Submenus are enabled when one of their items is. Right-clicking the menu does not open the native context menu.
22
+ */
23
+ export default class ElementMenuView extends DropdownMenuRootListView {
24
+ /**
25
+ * The element or position the commands are executed for.
26
+ */
27
+ public readonly target: ElementOrPosition;
28
+
29
+ private readonly _commands: Map<string, ElementOrPositionCommand>;
30
+ private readonly _submenus: Map<string, ElementSubmenu>;
31
+
32
+ /**
33
+ * The positions of the separators in each list, by the id of the submenu (`''` for the menu itself).
34
+ */
35
+ private readonly _separators: Map<string, number[]>;
36
+
37
+ constructor(
38
+ locale: Locale,
39
+ bodyCollection: BodyCollection,
40
+ items: readonly (ElementMenuItem | null | undefined)[],
41
+ target: ElementOrPosition
42
+ ) {
43
+ const maps: DefinitionMaps = { commands: new Map(), submenus: new Map(), separators: new Map() };
44
+
45
+ super(locale, bodyCollection, createDefinitions(items, '', maps));
46
+
47
+ this.target = target;
48
+ this._commands = maps.commands;
49
+ this._submenus = maps.submenus;
50
+ this._separators = maps.separators;
51
+ this.menuPanelClass = 'ck-elements-menu__panel';
52
+
53
+ this.extendTemplate({
54
+ attributes: { class: 'ck-elements-menu' },
55
+ on: { contextmenu: this.bindTemplate.to(preventDefault) }
56
+ });
57
+ }
58
+
59
+ /**
60
+ * Renders the menu: CKEditor builds it from the definitions; then the buttons are connected to the commands, and the
61
+ * separators are added.
62
+ */
63
+ public override render(): void {
64
+ super.render();
65
+
66
+ for (const button of this.buttons) {
67
+ const command = this._commands.get(button.id);
68
+
69
+ if (command) {
70
+ bindButtonToCommand(button, command, { withText: true, target: this.target });
71
+
72
+ // A toggleable button with a checkable role tells assistive technology whether it is "on" with `aria-checked`.
73
+ button.role = command.isToggleable ? 'menuitemcheckbox' : 'menuitem';
74
+ }
75
+ }
76
+
77
+ for (const menu of this.menus) {
78
+ const submenu = this._submenus.get(menu.id);
79
+
80
+ if (submenu) {
81
+ menu.buttonView.icon = submenu.icon;
82
+ menu.isEnabled = this._isEnabled(submenu);
83
+ }
84
+
85
+ // The panels of nested menus are not inside the menu.
86
+ if (menu.panelView.element) {
87
+ this.listenTo(menu.panelView.element, 'contextmenu', (evt, domEvent: Event) => {
88
+ preventDefault(domEvent);
89
+ });
90
+ }
91
+ }
92
+
93
+ this._addSeparators('', this);
94
+
95
+ for (const menu of this.menus) {
96
+ this._addSeparators(menu.id, menu.listView);
97
+ }
98
+ }
99
+
100
+ private _addSeparators(id: string, list: ListView): void {
101
+ for (const position of this._separators.get(id) ?? []) {
102
+ const separator = new ListSeparatorView(this.locale);
103
+
104
+ separator.extendTemplate({ attributes: { role: 'separator' } });
105
+ list.items.add(separator, position);
106
+ }
107
+ }
108
+
109
+ private _isEnabled(item: ElementMenuItem): boolean {
110
+ if (item === '|') {
111
+ return false;
112
+ }
113
+
114
+ return item instanceof ElementOrPositionCommand
115
+ ? item.getState(this.target).isEnabled
116
+ : item.items.some(child => !!child && this._isEnabled(child));
117
+ }
118
+ }
119
+
120
+ interface DefinitionMaps {
121
+ commands: Map<string, ElementOrPositionCommand>;
122
+ submenus: Map<string, ElementSubmenu>;
123
+ separators: Map<string, number[]>;
124
+ }
125
+
126
+ /**
127
+ * Creates CKEditor's menu definitions for the items. The ids are paths of positions (e.g. `'2.0'`). Collects the
128
+ * commands and submenus by id, and the positions of the separators by the id of their submenu.
129
+ */
130
+ function createDefinitions(
131
+ items: readonly (ElementMenuItem | null | undefined)[],
132
+ parentId: string,
133
+ maps: DefinitionMaps
134
+ ): DropdownMenuDefinition {
135
+ const definitions: DropdownMenuDefinition = [];
136
+ let position = 0;
137
+
138
+ for (const item of items) {
139
+ const id = parentId ? `${parentId}.${String(position)}` : String(position);
140
+
141
+ if (!item) {
142
+ continue;
143
+ }
144
+
145
+ if (item === '|') {
146
+ maps.separators.set(parentId, [...(maps.separators.get(parentId) ?? []), position]);
147
+ } else if (item instanceof ElementOrPositionCommand) {
148
+ maps.commands.set(id, item);
149
+ definitions.push({ id, label: item.title });
150
+ } else {
151
+ maps.submenus.set(id, item);
152
+ definitions.push({ id, menu: item.title, children: createDefinitions(item.items, id, maps) });
153
+ }
154
+
155
+ position++;
156
+ }
157
+
158
+ return definitions;
159
+ }
160
+
161
+ function preventDefault(domEvent: Event): void {
162
+ domEvent.preventDefault();
163
+ }
package/src/index.ts ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Element menus: `ElementMenu` shows the menu of an element, built from commands, submenus and separators, and the
3
+ * features that open it: the element context menu (a right-click, Shift+F10 or the menu key), the breadcrumbs (the
4
+ * element path at the selection, clickable with a menu) and quick insert (search and insert the elements allowed at a
5
+ * position).
6
+ *
7
+ * @module ckeditor5-elements-menu
8
+ */
9
+
10
+ // Plugins.
11
+ export {
12
+ default as ElementMenu,
13
+ type ElementMenuConfig,
14
+ type ElementMenuItem,
15
+ type ElementMenuShowOptions,
16
+ type ElementSubmenu
17
+ } from './elementMenu.js';
18
+ export { default as ElementContextMenu } from './contextMenu/elementContextMenu.js';
19
+ export { default as Breadcrumbs, type BreadcrumbsConfig } from './breadcrumbs/breadcrumbs.js';
20
+ export { default as QuickInsert, type QuickInsertConfig } from './quickInsert/quickInsert.js';
21
+
22
+ // Commands.
23
+ export { default as QuickInsertCommand } from './quickInsert/quickInsertCommand.js';
24
+ export {
25
+ default as QuickInsertPositionCommand,
26
+ type QuickInsertPlacement
27
+ } from './quickInsert/quickInsertPositionCommand.js';
28
+
29
+ // Views.
30
+ export { default as ElementMenuView } from './elementMenuView.js';
31
+ export {
32
+ default as BreadcrumbsView,
33
+ type BreadcrumbsViewExecuteEvent,
34
+ type BreadcrumbsViewOptions
35
+ } from './breadcrumbs/breadcrumbsView.js';
36
+ export {
37
+ default as BreadcrumbItemView,
38
+ type BreadcrumbItem,
39
+ type BreadcrumbItemViewOptions
40
+ } from './breadcrumbs/breadcrumbItemView.js';
41
+ export { default as BreadcrumbLabelView } from './breadcrumbs/breadcrumbLabelView.js';
42
+ export { default as QuickInsertView } from './quickInsert/quickInsertView.js';
43
+ export { default as QuickInsertListView, type QuickInsertItem } from './quickInsert/quickInsertListView.js';
44
+
45
+ // Observers.
46
+ export {
47
+ default as ContextMenuObserver,
48
+ type ViewDocumentContextMenuEvent
49
+ } from './contextMenu/contextMenuObserver.js';
50
+
51
+ // Icons.
52
+ export { insertAfterIcon, insertBeforeIcon, insertIcon } from './quickInsert/icons.js';
53
+
54
+ import './augmentation.js';
@@ -0,0 +1,26 @@
1
+ import { IconPlus } from 'ckeditor5';
2
+
3
+ /**
4
+ * The icons of the quick insert commands, in the style of the CKEditor 5 icons (20×20).
5
+ */
6
+
7
+ /**
8
+ * Insert element: CKEditor's own plus icon.
9
+ */
10
+ export const insertIcon = IconPlus;
11
+
12
+ /**
13
+ * Insert before: a plus above a block.
14
+ */
15
+ export const insertBeforeIcon =
16
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill-rule="evenodd" d="' +
17
+ 'M9.25 1.5h1.5v2.75h2.75v1.5h-2.75V8.5h-1.5V5.75H6.5v-1.5h2.75zM3.5 11h13a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1h-13a1 1 0 0 ' +
18
+ '1-1-1v-5a1 1 0 0 1 1-1zm.5 1.5v4h12v-4z"/></svg>';
19
+
20
+ /**
21
+ * Insert after: a block above a plus.
22
+ */
23
+ export const insertAfterIcon =
24
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill-rule="evenodd" d="' +
25
+ 'M3.5 2h13a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1h-13a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1zm.5 1.5v4h12v-4zM9.25 11h1.5v2.75h2.75v1.5' +
26
+ 'h-2.75V18h-1.5v-2.75H6.5v-1.5h2.75z"/></svg>';
@@ -0,0 +1,149 @@
1
+ import { Plugin, type Editor, type FilteredViewExecuteEvent, type ModelElement, type RectSource } from 'ckeditor5';
2
+ import type { ElementOrPosition } from 'ckeditor5-elements-core';
3
+ import ElementMenu from '../elementMenu.js';
4
+ import QuickInsertCommand from './quickInsertCommand.js';
5
+ import QuickInsertPositionCommand from './quickInsertPositionCommand.js';
6
+ import { type QuickInsertItem } from './quickInsertListView.js';
7
+ import QuickInsertView from './quickInsertView.js';
8
+
9
+ /**
10
+ * The configuration of the {@link QuickInsert} plugin.
11
+ */
12
+ export interface QuickInsertConfig {
13
+ /**
14
+ * Labels of the elements, by model name. Elements without a label are listed by their model name.
15
+ */
16
+ labels?: Record<string, string>;
17
+
18
+ /**
19
+ * Model names of the elements that are never listed.
20
+ */
21
+ exclude?: string[];
22
+
23
+ /**
24
+ * The keystroke that opens the menu at the selection, e.g. `'Ctrl+Space'`. Defaults to `'Alt+Enter'`
25
+ * (Option+Enter on macOS). `false` registers no keystroke.
26
+ */
27
+ keystroke?: string | false;
28
+ }
29
+
30
+ /**
31
+ * Opens a menu with a search field and the elements allowed at the selection, on Alt+Enter (Option+Enter on macOS; see
32
+ * {@link QuickInsertConfig.keystroke}). Choosing an element inserts it with the `quickInsert` command.
33
+ *
34
+ * The menu is shown by the `ElementMenu` plugin. The `quickInsertBefore` and `quickInsertAfter` commands (see
35
+ * {@link QuickInsertPositionCommand}) open it for the position before or after an element; list them in the
36
+ * `elementContextMenu` config, e.g. as an "Insert" submenu.
37
+ *
38
+ * The menu closes when the document changes while it is open, so it never inserts at an outdated position.
39
+ */
40
+ export default class QuickInsert extends Plugin {
41
+ public static get requires(): readonly [typeof ElementMenu] {
42
+ return [ElementMenu] as const;
43
+ }
44
+
45
+ public static get pluginName(): 'QuickInsert' {
46
+ return 'QuickInsert' as const;
47
+ }
48
+
49
+ constructor(editor: Editor) {
50
+ super(editor);
51
+
52
+ editor.config.define('quickInsert', { labels: {}, exclude: [], keystroke: 'Alt+Enter' });
53
+ }
54
+
55
+ public init(): void {
56
+ const editor = this.editor;
57
+ const keystroke = editor.config.get('quickInsert')?.keystroke;
58
+
59
+ editor.commands.add('quickInsert', new QuickInsertCommand(editor));
60
+ editor.commands.add('quickInsertBefore', new QuickInsertPositionCommand(editor, 'before'));
61
+ editor.commands.add('quickInsertAfter', new QuickInsertPositionCommand(editor, 'after'));
62
+
63
+ if (keystroke) {
64
+ editor.keystrokes.set(keystroke, (data, cancel) => {
65
+ if (this.open()) {
66
+ cancel();
67
+ }
68
+ });
69
+
70
+ editor.accessibility.addKeystrokeInfos({
71
+ keystrokes: [{ label: editor.t('Insert an element at the cursor'), keystroke }]
72
+ });
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Returns the elements that can be inserted at the target (a position, the position before an element, or the
78
+ * selection), with their labels.
79
+ */
80
+ public getItems(elementOrPosition?: ElementOrPosition): QuickInsertItem[] {
81
+ const config = this.editor.config.get('quickInsert');
82
+ const labels = config?.labels ?? {};
83
+ const command = this.editor.commands.get('quickInsert');
84
+
85
+ return (command?.getAllowedElements(elementOrPosition, config?.exclude) ?? []).map(name => ({
86
+ name,
87
+ label: labels[name] ?? name
88
+ }));
89
+ }
90
+
91
+ /**
92
+ * Opens the menu for inserting at the target (a position, the position before an element, or the selection), next
93
+ * to the given DOM target. By default, the menu is next to the element, or the selection.
94
+ *
95
+ * Returns `false` when nothing can be inserted at the target because it cannot be edited, or there is nothing to
96
+ * show the menu next to.
97
+ */
98
+ public open(elementOrPosition?: ElementOrPosition, domTarget?: RectSource): boolean {
99
+ const editor = this.editor;
100
+ const command = editor.commands.get('quickInsert');
101
+ const position = command?.getInsertPosition(elementOrPosition);
102
+ const menuTarget = domTarget ?? this._getDomTarget(elementOrPosition);
103
+
104
+ if (!command || !position || !menuTarget || !command.getState(position).isEnabled) {
105
+ return false;
106
+ }
107
+
108
+ const view = new QuickInsertView(editor.locale, this.getItems(position));
109
+
110
+ view.on<FilteredViewExecuteEvent>('execute', (evt, { value }) => {
111
+ // Focus first: focusing reads the DOM selection, which would override the selection set by the command.
112
+ editor.editing.view.focus();
113
+ command.executeFor(position, value);
114
+ });
115
+
116
+ // The search field is labelled "Insert element".
117
+ editor.plugins.get(ElementMenu).show(view, menuTarget, {
118
+ // The position would be outdated after any change, e.g. by another user.
119
+ isObsolete: () => true
120
+ });
121
+
122
+ return true;
123
+ }
124
+
125
+ /**
126
+ * Returns the DOM element of the element, or the DOM range of the selection.
127
+ */
128
+ private _getDomTarget(elementOrPosition?: ElementOrPosition): RectSource | null {
129
+ const editing = this.editor.editing;
130
+
131
+ if (elementOrPosition && !elementOrPosition.is('position')) {
132
+ return this._getDomElement(elementOrPosition);
133
+ }
134
+
135
+ const viewRange = elementOrPosition
136
+ ? editing.view.createRange(editing.mapper.toViewPosition(elementOrPosition))
137
+ : editing.view.document.selection.getFirstRange();
138
+
139
+ return viewRange && editing.view.domConverter.viewRangeToDom(viewRange);
140
+ }
141
+
142
+ private _getDomElement(element: ModelElement): HTMLElement | null {
143
+ const editing = this.editor.editing;
144
+ const viewElement = editing.mapper.toViewElement(element);
145
+ const domElement = viewElement && editing.view.domConverter.mapViewToDom(viewElement);
146
+
147
+ return domElement instanceof HTMLElement ? domElement : null;
148
+ }
149
+ }