ckeditor5-elements-quick-insert 0.0.0-stage → 49.0.0-alpha.0

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 (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +29 -2
  3. package/dist/augmentation.d.ts +18 -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/icons.d.ts +16 -0
  8. package/dist/icons.d.ts.map +1 -0
  9. package/dist/icons.js +21 -0
  10. package/dist/icons.js.map +1 -0
  11. package/dist/index.d.ts +13 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +17 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/quickInsert.d.ts +56 -0
  16. package/dist/quickInsert.d.ts.map +1 -0
  17. package/dist/quickInsert.js +106 -0
  18. package/dist/quickInsert.js.map +1 -0
  19. package/dist/quickInsertCommand.d.ts +39 -0
  20. package/dist/quickInsertCommand.d.ts.map +1 -0
  21. package/dist/quickInsertCommand.js +88 -0
  22. package/dist/quickInsertCommand.js.map +1 -0
  23. package/dist/quickInsertListView.d.ts +29 -0
  24. package/dist/quickInsertListView.d.ts.map +1 -0
  25. package/dist/quickInsertListView.js +44 -0
  26. package/dist/quickInsertListView.js.map +1 -0
  27. package/dist/quickInsertPositionCommand.d.ts +28 -0
  28. package/dist/quickInsertPositionCommand.d.ts.map +1 -0
  29. package/dist/quickInsertPositionCommand.js +54 -0
  30. package/dist/quickInsertPositionCommand.js.map +1 -0
  31. package/dist/quickInsertView.d.ts +20 -0
  32. package/dist/quickInsertView.d.ts.map +1 -0
  33. package/dist/quickInsertView.js +52 -0
  34. package/dist/quickInsertView.js.map +1 -0
  35. package/package.json +49 -3
  36. package/src/augmentation.ts +20 -0
  37. package/src/icons.ts +26 -0
  38. package/src/index.ts +21 -0
  39. package/src/quickInsert.ts +148 -0
  40. package/src/quickInsertCommand.ts +103 -0
  41. package/src/quickInsertListView.ts +71 -0
  42. package/src/quickInsertPositionCommand.ts +72 -0
  43. package/src/quickInsertView.ts +64 -0
  44. package/theme/quickInsert.css +12 -0
@@ -0,0 +1,54 @@
1
+ import {} from 'ckeditor5';
2
+ import { ElementOrPositionCommand } from 'ckeditor5-elements-core';
3
+ import { insertAfterIcon, insertBeforeIcon } from './icons.js';
4
+ /**
5
+ * Opens the quick insert menu for the position before or after an element, next to that element.
6
+ *
7
+ * The element is the given one, the parent of the given position, or the selected element or the parent of the
8
+ * selection (see {@link ElementOrPositionCommand}). Enabled when something can be inserted at that position.
9
+ *
10
+ * Requires the `QuickInsert` plugin, which registers it as `quickInsertBefore` and `quickInsertAfter`.
11
+ */
12
+ export default class QuickInsertPositionCommand extends ElementOrPositionCommand {
13
+ /**
14
+ * Where the quick insert menu inserts, relative to the element.
15
+ */
16
+ placement;
17
+ constructor(editor, placement) {
18
+ super(editor);
19
+ this.placement = placement;
20
+ this.title = placement === 'before' ? editor.t('Insert before') : editor.t('Insert after');
21
+ this.icon = placement === 'before' ? insertBeforeIcon : insertAfterIcon;
22
+ }
23
+ /**
24
+ * Opens the quick insert menu for the position before or after the element, next to the element.
25
+ */
26
+ execute(elementOrPosition) {
27
+ const editor = this.editor;
28
+ const element = this._getElement(elementOrPosition);
29
+ const position = this._getInsertPosition(elementOrPosition);
30
+ if (!element || !position) {
31
+ return;
32
+ }
33
+ const viewElement = editor.editing.mapper.toViewElement(element);
34
+ const domElement = viewElement && editor.editing.view.domConverter.mapViewToDom(viewElement);
35
+ editor.plugins.get('QuickInsert').open(position, domElement ?? undefined);
36
+ }
37
+ _isEnabledFor(elementOrPosition) {
38
+ const position = this._getInsertPosition(elementOrPosition);
39
+ const plugins = this.editor.plugins;
40
+ return (!!position &&
41
+ this.editor.model.canEditAt(position) &&
42
+ plugins.has('QuickInsert') &&
43
+ plugins.get('QuickInsert').getItems(position).length > 0);
44
+ }
45
+ _getInsertPosition(elementOrPosition) {
46
+ const element = this._getElement(elementOrPosition);
47
+ const model = this.editor.model;
48
+ if (!element) {
49
+ return null;
50
+ }
51
+ return this.placement === 'before' ? model.createPositionBefore(element) : model.createPositionAfter(element);
52
+ }
53
+ }
54
+ //# sourceMappingURL=quickInsertPositionCommand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quickInsertPositionCommand.js","sourceRoot":"","sources":["../src/quickInsertPositionCommand.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,wBAAwB,EAA0B,MAAM,yBAAyB,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAO/D;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,0BAA2B,SAAQ,wBAAwB;IAC/E;;OAEG;IACa,SAAS,CAAuB;IAEhD,YAAY,MAAc,EAAE,SAA+B;QAC1D,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;QAC3F,IAAI,CAAC,IAAI,GAAG,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,eAAe,CAAC;IACzE,CAAC;IAED;;OAEG;IACa,OAAO,CAAC,iBAAqC;QAC5D,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;QACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,kBAAkB,CAAC,iBAAiB,CAAC,CAAC;QAE5D,IAAI,CAAC,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC3B,OAAO;QAC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or5';
2
+ import QuickInsertListView, { type QuickInsertItem } from './quickInsertListView.js';
3
+ /**
4
+ * A search field with a filtered list of insertable elements.
5
+ *
6
+ * In the search field, Enter chooses the first matching item and the down arrow moves the focus to the list.
7
+ * Fires `execute` with `{ value: name }` when an item is chosen.
8
+ */
9
+ export default class QuickInsertView extends SearchTextView {
10
+ /**
11
+ * The list of elements.
12
+ */
13
+ readonly listView: QuickInsertListView;
14
+ constructor(locale: Locale, items: readonly QuickInsertItem[]);
15
+ /**
16
+ * Renders the view and sets up Enter (choose the first match) and the down arrow (go to the list) in the search field.
17
+ */
18
+ render(): void;
19
+ }
20
+ //# sourceMappingURL=quickInsertView.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quickInsertView.d.ts","sourceRoot":"","sources":["../src/quickInsertView.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAiC,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AACvF,OAAO,mBAAmB,EAAE,EAAE,KAAK,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAErF;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,cAAc;IAC1D;;OAEG;IACH,SAAgB,QAAQ,EAAE,mBAAmB,CAAC;gBAElC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,eAAe,EAAE;IAoB7D;;OAEG;IACa,MAAM,IAAI,IAAI;CAyB9B"}
@@ -0,0 +1,52 @@
1
+ import { SearchTextView } from 'ckeditor5';
2
+ import QuickInsertListView, {} from './quickInsertListView.js';
3
+ /**
4
+ * A search field with a filtered list of insertable elements.
5
+ *
6
+ * In the search field, Enter chooses the first matching item and the down arrow moves the focus to the list.
7
+ * Fires `execute` with `{ value: name }` when an item is chosen.
8
+ */
9
+ export default class QuickInsertView extends SearchTextView {
10
+ /**
11
+ * The list of elements.
12
+ */
13
+ listView;
14
+ constructor(locale, items) {
15
+ const listView = new QuickInsertListView(locale, items);
16
+ const t = locale.t;
17
+ super(locale, {
18
+ queryView: { label: t('Insert element') },
19
+ filteredView: listView,
20
+ infoView: {
21
+ text: {
22
+ notFound: { primary: t('No matching elements') },
23
+ noSearchableItems: { primary: t('No elements can be inserted here') }
24
+ }
25
+ },
26
+ class: 'ck-elements-quick-insert'
27
+ });
28
+ this.listView = listView;
29
+ listView.delegate('execute').to(this);
30
+ }
31
+ /**
32
+ * Renders the view and sets up Enter (choose the first match) and the down arrow (go to the list) in the search field.
33
+ */
34
+ render() {
35
+ super.render();
36
+ const isInQueryField = () => this.focusTracker.focusedElement === this.queryView.element;
37
+ this.keystrokes.set('enter', (data, cancel) => {
38
+ const item = isInQueryField() ? this.listView.getFirstVisibleItem() : null;
39
+ if (item) {
40
+ this.fire('execute', { value: item.name });
41
+ cancel();
42
+ }
43
+ });
44
+ this.keystrokes.set('arrowdown', (data, cancel) => {
45
+ if (isInQueryField()) {
46
+ this.listView.focusFirst();
47
+ cancel();
48
+ }
49
+ }, { priority: 'high' });
50
+ }
51
+ }
52
+ //# sourceMappingURL=quickInsertView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quickInsertView.js","sourceRoot":"","sources":["../src/quickInsertView.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA8C,MAAM,WAAW,CAAC;AACvF,OAAO,mBAAmB,EAAE,EAAwB,MAAM,0BAA0B,CAAC;AAErF;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,cAAc;IAC1D;;OAEG;IACa,QAAQ,CAAsB;IAE9C,YAAY,MAAc,EAAE,KAAiC;QAC5D,MAAM,QAAQ,GAAG,IAAI,mBAAmB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACxD,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;QAEnB,KAAK,CAAC,MAAM,EAAE;YACb,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,gBAAgB,CAAC,EAAE;YACzC,YAAY,EAAE,QAAQ;YACt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}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "ckeditor5-elements-quick-insert",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "49.0.0-alpha.0",
4
+ "description": "Quick insert for CKEditor 5: search and insert the elements allowed at a position (Alt+Enter, or the context menu).",
5
+ "keywords": [
6
+ "ckeditor",
7
+ "ckeditor5",
8
+ "ckeditor 5",
9
+ "ckeditor5-feature",
10
+ "ckeditor5-plugin"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "Ivan Kildyushov",
14
+ "homepage": "https://ivankild.github.io/ckeditor5-elements/guide/quick-insert",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/ivankild/ckeditor5-elements.git",
18
+ "directory": "packages/ckeditor5-elements-quick-insert"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/ivankild/ckeditor5-elements/issues"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js"
30
+ },
31
+ "./index.css": "./theme/quickInsert.css",
32
+ "./package.json": "./package.json"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "src",
37
+ "theme",
38
+ "README.md"
39
+ ],
40
+ "sideEffects": [
41
+ "*.css"
42
+ ],
43
+ "dependencies": {
44
+ "ckeditor5-elements-core": "49.0.0-alpha.0"
45
+ },
46
+ "peerDependencies": {
47
+ "ckeditor5": ">=49.0.0"
48
+ },
49
+ "scripts": {
50
+ "build": "tsc --build"
51
+ }
6
52
  }
@@ -0,0 +1,20 @@
1
+ import type { QuickInsert, QuickInsertCommand, QuickInsertConfig, QuickInsertPositionCommand } from './index.js';
2
+
3
+ declare module 'ckeditor5' {
4
+ interface PluginsMap {
5
+ [QuickInsert.pluginName]: QuickInsert;
6
+ }
7
+
8
+ interface CommandsMap {
9
+ quickInsert: QuickInsertCommand;
10
+ quickInsertBefore: QuickInsertPositionCommand;
11
+ quickInsertAfter: QuickInsertPositionCommand;
12
+ }
13
+
14
+ interface EditorConfig {
15
+ /**
16
+ * The configuration of the {@link QuickInsert} plugin.
17
+ */
18
+ quickInsert?: QuickInsertConfig;
19
+ }
20
+ }
package/src/icons.ts ADDED
@@ -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>';
package/src/index.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Quick insert: search and insert the elements allowed at a position.
3
+ *
4
+ * @module ckeditor5-elements-quick-insert
5
+ */
6
+
7
+ // Plugins.
8
+ export { default as QuickInsert, type QuickInsertConfig } from './quickInsert.js';
9
+
10
+ // Commands.
11
+ export { default as QuickInsertCommand } from './quickInsertCommand.js';
12
+ export { default as QuickInsertPositionCommand, type QuickInsertPlacement } from './quickInsertPositionCommand.js';
13
+
14
+ // Views.
15
+ export { default as QuickInsertView } from './quickInsertView.js';
16
+ export { default as QuickInsertListView, type QuickInsertItem } from './quickInsertListView.js';
17
+
18
+ // Icons.
19
+ export { insertAfterIcon, insertBeforeIcon, insertIcon } from './icons.js';
20
+
21
+ import './augmentation.js';
@@ -0,0 +1,148 @@
1
+ import { Plugin, type Editor, type FilteredViewExecuteEvent, type ModelElement, type RectSource } from 'ckeditor5';
2
+ import { ElementMenu, type ElementOrPosition } from 'ckeditor5-elements-core';
3
+ import QuickInsertCommand from './quickInsertCommand.js';
4
+ import QuickInsertPositionCommand from './quickInsertPositionCommand.js';
5
+ import { type QuickInsertItem } from './quickInsertListView.js';
6
+ import QuickInsertView from './quickInsertView.js';
7
+
8
+ /**
9
+ * The configuration of the {@link QuickInsert} plugin.
10
+ */
11
+ export interface QuickInsertConfig {
12
+ /**
13
+ * Labels of the elements, by model name. Elements without a label are listed by their model name.
14
+ */
15
+ labels?: Record<string, string>;
16
+
17
+ /**
18
+ * Model names of the elements that are never listed.
19
+ */
20
+ exclude?: string[];
21
+
22
+ /**
23
+ * The keystroke that opens the menu at the selection, e.g. `'Ctrl+Space'`. Defaults to `'Alt+Enter'`
24
+ * (Option+Enter on macOS). `false` registers no keystroke.
25
+ */
26
+ keystroke?: string | false;
27
+ }
28
+
29
+ /**
30
+ * Opens a menu with a search field and the elements allowed at the selection, on Alt+Enter (Option+Enter on macOS; see
31
+ * {@link QuickInsertConfig.keystroke}). Choosing an element inserts it with the `quickInsert` command.
32
+ *
33
+ * The menu is shown by the `ElementMenu` plugin. The `quickInsertBefore` and `quickInsertAfter` commands (see
34
+ * {@link QuickInsertPositionCommand}) open it for the position before or after an element; list them in the
35
+ * `elementContextMenu` config, e.g. as an "Insert" submenu.
36
+ *
37
+ * The menu closes when the document changes while it is open, so it never inserts at an outdated position.
38
+ */
39
+ export default class QuickInsert extends Plugin {
40
+ public static get requires(): readonly [typeof ElementMenu] {
41
+ return [ElementMenu] as const;
42
+ }
43
+
44
+ public static get pluginName(): 'QuickInsert' {
45
+ return 'QuickInsert' as const;
46
+ }
47
+
48
+ constructor(editor: Editor) {
49
+ super(editor);
50
+
51
+ editor.config.define('quickInsert', { labels: {}, exclude: [], keystroke: 'Alt+Enter' });
52
+ }
53
+
54
+ public init(): void {
55
+ const editor = this.editor;
56
+ const keystroke = editor.config.get('quickInsert')?.keystroke;
57
+
58
+ editor.commands.add('quickInsert', new QuickInsertCommand(editor));
59
+ editor.commands.add('quickInsertBefore', new QuickInsertPositionCommand(editor, 'before'));
60
+ editor.commands.add('quickInsertAfter', new QuickInsertPositionCommand(editor, 'after'));
61
+
62
+ if (keystroke) {
63
+ editor.keystrokes.set(keystroke, (data, cancel) => {
64
+ if (this.open()) {
65
+ cancel();
66
+ }
67
+ });
68
+
69
+ editor.accessibility.addKeystrokeInfos({
70
+ keystrokes: [{ label: editor.t('Insert an element at the cursor'), keystroke }]
71
+ });
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Returns the elements that can be inserted at the target (a position, the position before an element, or the
77
+ * selection), with their labels.
78
+ */
79
+ public getItems(elementOrPosition?: ElementOrPosition): QuickInsertItem[] {
80
+ const config = this.editor.config.get('quickInsert');
81
+ const labels = config?.labels ?? {};
82
+ const command = this.editor.commands.get('quickInsert');
83
+
84
+ return (command?.getAllowedElements(elementOrPosition, config?.exclude) ?? []).map(name => ({
85
+ name,
86
+ label: labels[name] ?? name
87
+ }));
88
+ }
89
+
90
+ /**
91
+ * Opens the menu for inserting at the target (a position, the position before an element, or the selection), next
92
+ * to the given DOM target. By default, the menu is next to the element, or the selection.
93
+ *
94
+ * Returns `false` when nothing can be inserted at the target because it cannot be edited, or there is nothing to
95
+ * show the menu next to.
96
+ */
97
+ public open(elementOrPosition?: ElementOrPosition, domTarget?: RectSource): boolean {
98
+ const editor = this.editor;
99
+ const command = editor.commands.get('quickInsert');
100
+ const position = command?.getInsertPosition(elementOrPosition);
101
+ const menuTarget = domTarget ?? this._getDomTarget(elementOrPosition);
102
+
103
+ if (!command || !position || !menuTarget || !command.getState(position).isEnabled) {
104
+ return false;
105
+ }
106
+
107
+ const view = new QuickInsertView(editor.locale, this.getItems(position));
108
+
109
+ view.on<FilteredViewExecuteEvent>('execute', (evt, { value }) => {
110
+ // Focus first: focusing reads the DOM selection, which would override the selection set by the command.
111
+ editor.editing.view.focus();
112
+ command.executeFor(position, value);
113
+ });
114
+
115
+ // The search field is labelled "Insert element".
116
+ editor.plugins.get(ElementMenu).show(view, menuTarget, {
117
+ // The position would be outdated after any change, e.g. by another user.
118
+ isObsolete: () => true
119
+ });
120
+
121
+ return true;
122
+ }
123
+
124
+ /**
125
+ * Returns the DOM element of the element, or the DOM range of the selection.
126
+ */
127
+ private _getDomTarget(elementOrPosition?: ElementOrPosition): RectSource | null {
128
+ const editing = this.editor.editing;
129
+
130
+ if (elementOrPosition && !elementOrPosition.is('position')) {
131
+ return this._getDomElement(elementOrPosition);
132
+ }
133
+
134
+ const viewRange = elementOrPosition
135
+ ? editing.view.createRange(editing.mapper.toViewPosition(elementOrPosition))
136
+ : editing.view.document.selection.getFirstRange();
137
+
138
+ return viewRange && editing.view.domConverter.viewRangeToDom(viewRange);
139
+ }
140
+
141
+ private _getDomElement(element: ModelElement): HTMLElement | null {
142
+ const editing = this.editor.editing;
143
+ const viewElement = editing.mapper.toViewElement(element);
144
+ const domElement = viewElement && editing.view.domConverter.mapViewToDom(viewElement);
145
+
146
+ return domElement instanceof HTMLElement ? domElement : null;
147
+ }
148
+ }
@@ -0,0 +1,103 @@
1
+ import { type Editor, type ModelElement, type ModelPosition, type ModelWriter } from 'ckeditor5';
2
+ import { ElementOrPositionCommand, type ElementOrPosition } from 'ckeditor5-elements-core';
3
+ import { insertIcon } from './icons.js';
4
+
5
+ /**
6
+ * Inserts an element by its model name at a position, or opens the quick insert menu for that position.
7
+ *
8
+ * The target comes first, as in every {@link ElementOrPositionCommand}: a position, an element (meaning the position
9
+ * before it), or none (the selection).
10
+ *
11
+ * ```ts
12
+ * editor.execute('quickInsert', position, 'heading1'); // Inserts a heading at the position.
13
+ * editor.execute('quickInsert'); // Opens the quick insert menu at the selection.
14
+ * ```
15
+ *
16
+ * Only elements that the schema allows at exactly that position are inserted (see {@link getAllowedElements}):
17
+ * nothing is split to make room, so e.g. a heading cannot be inserted inside a paragraph.
18
+ *
19
+ * After inserting, objects are selected; other elements get the selection inside them, in a new paragraph when they
20
+ * can hold blocks.
21
+ */
22
+ export default class QuickInsertCommand extends ElementOrPositionCommand {
23
+ constructor(editor: Editor) {
24
+ super(editor);
25
+
26
+ this.title = editor.t('Insert element');
27
+ this.icon = insertIcon;
28
+ }
29
+
30
+ /**
31
+ * Returns the names of the model elements allowed at exactly the position (or the selection), in the order they
32
+ * were registered. Generic items (such as `$text` or `$block`) and the excluded names are skipped.
33
+ */
34
+ public getAllowedElements(elementOrPosition?: ElementOrPosition, exclude: readonly string[] = []): string[] {
35
+ const target = this._getPosition(elementOrPosition);
36
+
37
+ if (!target) {
38
+ return [];
39
+ }
40
+
41
+ const schema = this.editor.model.schema;
42
+
43
+ return Object.keys(schema.getDefinitions()).filter(
44
+ name => !name.startsWith('$') && !exclude.includes(name) && schema.checkChild(target, name)
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Returns the position where elements are inserted for the target: the position, the position before the element,
50
+ * or the selection.
51
+ */
52
+ public getInsertPosition(elementOrPosition?: ElementOrPosition): ModelPosition | null {
53
+ return this._getPosition(elementOrPosition);
54
+ }
55
+
56
+ /**
57
+ * Inserts the element named `name` at the target, when the schema allows it there. Without a name, opens the quick
58
+ * insert menu for the target (requires the `QuickInsert` plugin).
59
+ */
60
+ public override execute(elementOrPosition?: ElementOrPosition, name?: string): void {
61
+ if (name === undefined) {
62
+ const plugins = this.editor.plugins;
63
+
64
+ if (plugins.has('QuickInsert')) {
65
+ plugins.get('QuickInsert').open(elementOrPosition);
66
+ }
67
+
68
+ return;
69
+ }
70
+
71
+ const model = this.editor.model;
72
+ const target = this._getPosition(elementOrPosition);
73
+
74
+ if (!target || !model.canEditAt(target) || !model.schema.checkChild(target, name)) {
75
+ return;
76
+ }
77
+
78
+ model.change(writer => {
79
+ const element = writer.createElement(name);
80
+
81
+ writer.insert(element, target);
82
+ this._setSelection(writer, element);
83
+ });
84
+ }
85
+
86
+ private _setSelection(writer: ModelWriter, element: ModelElement): void {
87
+ const schema = this.editor.model.schema;
88
+
89
+ if (schema.isObject(element)) {
90
+ writer.setSelection(element, 'on');
91
+ } else if (schema.checkChild(element, '$text')) {
92
+ writer.setSelection(element, 0);
93
+ } else if (schema.checkChild(element, 'paragraph')) {
94
+ const paragraph = writer.createElement('paragraph');
95
+
96
+ writer.append(paragraph, element);
97
+ writer.setSelection(paragraph, 0);
98
+ } else {
99
+ // Only objects can be selected as a whole. The selection post-fixer moves it to the closest valid place.
100
+ writer.setSelection(element, 'after');
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ ButtonView,
3
+ ListItemView,
4
+ ListView,
5
+ type FilteredView,
6
+ type FilteredViewExecuteEvent,
7
+ type Locale
8
+ } from 'ckeditor5';
9
+
10
+ /**
11
+ * An element that can be inserted: its model name and the label shown in the list.
12
+ */
13
+ export interface QuickInsertItem {
14
+ name: string;
15
+ label: string;
16
+ }
17
+
18
+ /**
19
+ * A list of insertable elements that can be filtered by a search query.
20
+ *
21
+ * Fires `execute` with `{ value: name }` when an item is chosen.
22
+ */
23
+ export default class QuickInsertListView extends ListView implements FilteredView {
24
+ private readonly _entries: { item: QuickInsertItem; view: ListItemView; button: ButtonView }[];
25
+
26
+ constructor(locale: Locale, items: readonly QuickInsertItem[]) {
27
+ super(locale);
28
+
29
+ this.role = 'listbox';
30
+
31
+ this._entries = items.map(item => {
32
+ const view = new ListItemView(locale);
33
+ const button = new ButtonView(locale);
34
+
35
+ button.set({ label: item.label, withText: true, role: 'option' });
36
+ button.on('execute', () => {
37
+ this.fire<FilteredViewExecuteEvent>('execute', { value: item.name });
38
+ });
39
+
40
+ view.children.add(button);
41
+
42
+ return { item, view, button };
43
+ });
44
+
45
+ this.items.addMany(this._entries.map(entry => entry.view));
46
+ }
47
+
48
+ /**
49
+ * Shows only the items whose label or name matches the expression. `null` shows all items.
50
+ */
51
+ public filter(regExp: RegExp | null): { resultsCount: number; totalItemsCount: number } {
52
+ let resultsCount = 0;
53
+
54
+ for (const { item, view } of this._entries) {
55
+ view.isVisible = !regExp || regExp.test(item.label) || regExp.test(item.name);
56
+
57
+ if (view.isVisible) {
58
+ resultsCount++;
59
+ }
60
+ }
61
+
62
+ return { resultsCount, totalItemsCount: this._entries.length };
63
+ }
64
+
65
+ /**
66
+ * Returns the first item that is not filtered out.
67
+ */
68
+ public getFirstVisibleItem(): QuickInsertItem | null {
69
+ return this._entries.find(entry => entry.view.isVisible)?.item ?? null;
70
+ }
71
+ }
@@ -0,0 +1,72 @@
1
+ import { type Editor, type ModelPosition } from 'ckeditor5';
2
+ import { ElementOrPositionCommand, type ElementOrPosition } from 'ckeditor5-elements-core';
3
+ import { insertAfterIcon, insertBeforeIcon } from './icons.js';
4
+
5
+ /**
6
+ * Where the quick insert menu inserts, relative to the element.
7
+ */
8
+ export type QuickInsertPlacement = 'before' | 'after';
9
+
10
+ /**
11
+ * Opens the quick insert menu for the position before or after an element, next to that element.
12
+ *
13
+ * The element is the given one, the parent of the given position, or the selected element or the parent of the
14
+ * selection (see {@link ElementOrPositionCommand}). Enabled when something can be inserted at that position.
15
+ *
16
+ * Requires the `QuickInsert` plugin, which registers it as `quickInsertBefore` and `quickInsertAfter`.
17
+ */
18
+ export default class QuickInsertPositionCommand extends ElementOrPositionCommand {
19
+ /**
20
+ * Where the quick insert menu inserts, relative to the element.
21
+ */
22
+ public readonly placement: QuickInsertPlacement;
23
+
24
+ constructor(editor: Editor, placement: QuickInsertPlacement) {
25
+ super(editor);
26
+
27
+ this.placement = placement;
28
+ this.title = placement === 'before' ? editor.t('Insert before') : editor.t('Insert after');
29
+ this.icon = placement === 'before' ? insertBeforeIcon : insertAfterIcon;
30
+ }
31
+
32
+ /**
33
+ * Opens the quick insert menu for the position before or after the element, next to the element.
34
+ */
35
+ public override execute(elementOrPosition?: ElementOrPosition): void {
36
+ const editor = this.editor;
37
+ const element = this._getElement(elementOrPosition);
38
+ const position = this._getInsertPosition(elementOrPosition);
39
+
40
+ if (!element || !position) {
41
+ return;
42
+ }
43
+
44
+ const viewElement = editor.editing.mapper.toViewElement(element);
45
+ const domElement = viewElement && editor.editing.view.domConverter.mapViewToDom(viewElement);
46
+
47
+ editor.plugins.get('QuickInsert').open(position, domElement ?? undefined);
48
+ }
49
+
50
+ protected override _isEnabledFor(elementOrPosition?: ElementOrPosition): boolean {
51
+ const position = this._getInsertPosition(elementOrPosition);
52
+ const plugins = this.editor.plugins;
53
+
54
+ return (
55
+ !!position &&
56
+ this.editor.model.canEditAt(position) &&
57
+ plugins.has('QuickInsert') &&
58
+ plugins.get('QuickInsert').getItems(position).length > 0
59
+ );
60
+ }
61
+
62
+ private _getInsertPosition(elementOrPosition?: ElementOrPosition): ModelPosition | null {
63
+ const element = this._getElement(elementOrPosition);
64
+ const model = this.editor.model;
65
+
66
+ if (!element) {
67
+ return null;
68
+ }
69
+
70
+ return this.placement === 'before' ? model.createPositionBefore(element) : model.createPositionAfter(element);
71
+ }
72
+ }