@ckeditor/ckeditor5-widget 36.0.0 → 37.0.0-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,23 +17,32 @@ import { isWidget } from './utils';
17
17
  *
18
18
  * The following example comes from the {@link module:image/imagetoolbar~ImageToolbar} plugin:
19
19
  *
20
- * class ImageToolbar extends Plugin {
21
- * static get requires() {
22
- * return [ WidgetToolbarRepository ];
23
- * }
20
+ * ```ts
21
+ * class ImageToolbar extends Plugin {
22
+ * static get requires() {
23
+ * return [ WidgetToolbarRepository ];
24
+ * }
24
25
  *
25
- * afterInit() {
26
- * const editor = this.editor;
27
- * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
26
+ * afterInit() {
27
+ * const editor = this.editor;
28
+ * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
28
29
  *
29
- * widgetToolbarRepository.register( 'image', {
30
- * items: editor.config.get( 'image.toolbar' ),
31
- * getRelatedElement: getClosestSelectedImageWidget
32
- * } );
33
- * }
34
- * }
30
+ * widgetToolbarRepository.register( 'image', {
31
+ * items: editor.config.get( 'image.toolbar' ),
32
+ * getRelatedElement: getClosestSelectedImageWidget
33
+ * } );
34
+ * }
35
+ * }
36
+ * ```
35
37
  */
36
38
  export default class WidgetToolbarRepository extends Plugin {
39
+ constructor() {
40
+ super(...arguments);
41
+ /**
42
+ * A map of toolbar definitions.
43
+ */
44
+ this._toolbarDefinitions = new Map();
45
+ }
37
46
  /**
38
47
  * @inheritDoc
39
48
  */
@@ -60,16 +69,6 @@ export default class WidgetToolbarRepository extends Plugin {
60
69
  }
61
70
  }, { priority: 'high' });
62
71
  }
63
- /**
64
- * A map of toolbar definitions.
65
- *
66
- * @protected
67
- * @member {Map.<String,module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition>} #_toolbarDefinitions
68
- */
69
- this._toolbarDefinitions = new Map();
70
- /**
71
- * @private
72
- */
73
72
  this._balloon = this.editor.plugins.get('ContextualBalloon');
74
73
  this.on('change:isEnabled', () => {
75
74
  this._updateToolbarsVisibility();
@@ -96,12 +95,11 @@ export default class WidgetToolbarRepository extends Plugin {
96
95
  * Note: This method should be called in the {@link module:core/plugin~PluginInterface#afterInit `Plugin#afterInit()`}
97
96
  * callback (or later) to make sure that the given toolbar items were already registered by other plugins.
98
97
  *
99
- * @param {String} toolbarId An id for the toolbar. Used to
100
- * @param {Object} options
101
- * @param {String} [options.ariaLabel] Label used by assistive technologies to describe this toolbar element.
102
- * @param {Array.<String>} options.items Array of toolbar items.
103
- * @param {Function} options.getRelatedElement Callback which returns an element the toolbar should be attached to.
104
- * @param {String} [options.balloonClassName='ck-toolbar-container'] CSS class for the widget balloon.
98
+ * @param toolbarId An id for the toolbar. Used to
99
+ * @param options.ariaLabel Label used by assistive technologies to describe this toolbar element.
100
+ * @param options.items Array of toolbar items.
101
+ * @param options.getRelatedElement Callback which returns an element the toolbar should be attached to.
102
+ * @param options.balloonClassName CSS class for the widget balloon.
105
103
  */
106
104
  register(toolbarId, { ariaLabel, items, getRelatedElement, balloonClassName = 'ck-toolbar-container' }) {
107
105
  // Trying to register a toolbar without any item.
@@ -115,10 +113,10 @@ export default class WidgetToolbarRepository extends Plugin {
115
113
  * See for instance:
116
114
  *
117
115
  * * {@link module:table/table~TableConfig#contentToolbar `config.table.contentToolbar`}
118
- * * {@link module:image/image~ImageConfig#toolbar `config.image.toolbar`}
116
+ * * {@link module:image/imageconfig~ImageConfig#toolbar `config.image.toolbar`}
119
117
  *
120
118
  * @error widget-toolbar-no-items
121
- * @param {String} toolbarId The id of the toolbar that has not been configured correctly.
119
+ * @param toolbarId The id of the toolbar that has not been configured correctly.
122
120
  */
123
121
  logWarning('widget-toolbar-no-items', { toolbarId });
124
122
  return;
@@ -160,8 +158,6 @@ export default class WidgetToolbarRepository extends Plugin {
160
158
  }
161
159
  /**
162
160
  * Iterates over stored toolbars and makes them visible or hidden.
163
- *
164
- * @private
165
161
  */
166
162
  _updateToolbarsVisibility() {
167
163
  let maxRelatedElementDepth = 0;
@@ -198,9 +194,6 @@ export default class WidgetToolbarRepository extends Plugin {
198
194
  }
199
195
  /**
200
196
  * Hides the given toolbar.
201
- *
202
- * @private
203
- * @param {module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition} toolbarDefinition
204
197
  */
205
198
  _hideToolbar(toolbarDefinition) {
206
199
  this._balloon.remove(toolbarDefinition.view);
@@ -212,10 +205,6 @@ export default class WidgetToolbarRepository extends Plugin {
212
205
  *
213
206
  * It might happen here that the toolbar's view is under another view. Then do nothing as the other toolbar view
214
207
  * should be still visible after the {@link module:core/editor/editorui~EditorUI#event:update}.
215
- *
216
- * @private
217
- * @param {module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition} toolbarDefinition
218
- * @param {module:engine/view/element~Element} relatedElement
219
208
  */
220
209
  _showToolbar(toolbarDefinition, relatedElement) {
221
210
  if (this._isToolbarVisible(toolbarDefinition)) {
@@ -245,19 +234,9 @@ export default class WidgetToolbarRepository extends Plugin {
245
234
  });
246
235
  }
247
236
  }
248
- /**
249
- * @private
250
- * @param {Object} toolbar
251
- * @returns {Boolean}
252
- */
253
237
  _isToolbarVisible(toolbar) {
254
238
  return this._balloon.visibleView === toolbar.view;
255
239
  }
256
- /**
257
- * @private
258
- * @param {Object} toolbar
259
- * @returns {Boolean}
260
- */
261
240
  _isToolbarInBalloon(toolbar) {
262
241
  return this._balloon.hasView(toolbar.view);
263
242
  }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import type { DocumentSelection, DomConverter, Element, Schema, Selection, ViewElement } from '@ckeditor/ckeditor5-engine';
6
+ /**
7
+ * The name of the type around model selection attribute responsible for
8
+ * displaying a fake caret next to a selected widget.
9
+ */
10
+ export declare const TYPE_AROUND_SELECTION_ATTRIBUTE = "widget-type-around";
11
+ /**
12
+ * Checks if an element is a widget that qualifies to get the widget type around UI.
13
+ */
14
+ export declare function isTypeAroundWidget(viewElement: ViewElement | undefined, modelElement: Element, schema: Schema): boolean;
15
+ /**
16
+ * For the passed HTML element, this helper finds the closest widget type around button ancestor.
17
+ */
18
+ export declare function getClosestTypeAroundDomButton(domElement: HTMLElement): HTMLElement | null;
19
+ /**
20
+ * For the passed widget type around button element, this helper determines at which position
21
+ * the paragraph would be inserted into the content if, for instance, the button was
22
+ * clicked by the user.
23
+ *
24
+ * @returns The position of the button.
25
+ */
26
+ export declare function getTypeAroundButtonPosition(domElement: HTMLElement): 'before' | 'after';
27
+ /**
28
+ * For the passed HTML element, this helper returns the closest view widget ancestor.
29
+ */
30
+ export declare function getClosestWidgetViewElement(domElement: HTMLElement, domConverter: DomConverter): ViewElement;
31
+ /**
32
+ * For the passed selection instance, it returns the position of the fake caret displayed next to a widget.
33
+ *
34
+ * **Note**: If the fake caret is not currently displayed, `null` is returned.
35
+ *
36
+ * @returns The position of the fake caret or `null` when none is present.
37
+ */
38
+ export declare function getTypeAroundFakeCaretPosition(selection: Selection | DocumentSelection): 'before' | 'after' | null;
@@ -13,20 +13,12 @@ import { isWidget } from '../utils';
13
13
  export const TYPE_AROUND_SELECTION_ATTRIBUTE = 'widget-type-around';
14
14
  /**
15
15
  * Checks if an element is a widget that qualifies to get the widget type around UI.
16
- *
17
- * @param {module:engine/view/element~Element} viewElement
18
- * @param {module:engine/model/element~Element} modelElement
19
- * @param {module:engine/model/schema~Schema} schema
20
- * @returns {Boolean}
21
16
  */
22
17
  export function isTypeAroundWidget(viewElement, modelElement, schema) {
23
18
  return !!viewElement && isWidget(viewElement) && !schema.isInline(modelElement);
24
19
  }
25
20
  /**
26
21
  * For the passed HTML element, this helper finds the closest widget type around button ancestor.
27
- *
28
- * @param {HTMLElement} domElement
29
- * @returns {HTMLElement|null}
30
22
  */
31
23
  export function getClosestTypeAroundDomButton(domElement) {
32
24
  return domElement.closest('.ck-widget__type-around__button');
@@ -36,18 +28,13 @@ export function getClosestTypeAroundDomButton(domElement) {
36
28
  * the paragraph would be inserted into the content if, for instance, the button was
37
29
  * clicked by the user.
38
30
  *
39
- * @param {HTMLElement} domElement
40
- * @returns {'before'|'after'} The position of the button.
31
+ * @returns The position of the button.
41
32
  */
42
33
  export function getTypeAroundButtonPosition(domElement) {
43
34
  return domElement.classList.contains('ck-widget__type-around__button_before') ? 'before' : 'after';
44
35
  }
45
36
  /**
46
37
  * For the passed HTML element, this helper returns the closest view widget ancestor.
47
- *
48
- * @param {HTMLElement} domElement
49
- * @param {module:engine/view/domconverter~DomConverter} domConverter
50
- * @returns {module:engine/view/element~Element}
51
38
  */
52
39
  export function getClosestWidgetViewElement(domElement, domConverter) {
53
40
  const widgetDomElement = domElement.closest('.ck-widget');
@@ -58,8 +45,7 @@ export function getClosestWidgetViewElement(domElement, domConverter) {
58
45
  *
59
46
  * **Note**: If the fake caret is not currently displayed, `null` is returned.
60
47
  *
61
- * @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
62
- * @returns {'before'|'after'|null} The position of the fake caret or `null` when none is present.
48
+ * @returns The position of the fake caret or `null` when none is present.
63
49
  */
64
50
  export function getTypeAroundFakeCaretPosition(selection) {
65
51
  return selection.getAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
@@ -0,0 +1,232 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module widget/widgettypearound
7
+ */
8
+ import { Plugin, type PluginDependencies } from '@ckeditor/ckeditor5-core';
9
+ import '../../theme/widgettypearound.css';
10
+ /**
11
+ * A plugin that allows users to type around widgets where normally it is impossible to place the caret due
12
+ * to limitations of web browsers. These "tight spots" occur, for instance, before (or after) a widget being
13
+ * the first (or last) child of its parent or between two block widgets.
14
+ *
15
+ * This plugin extends the {@link module:widget/widget~Widget `Widget`} plugin and injects the user interface
16
+ * with two buttons into each widget instance in the editor. Each of the buttons can be clicked by the
17
+ * user if the widget is next to the "tight spot". Once clicked, a paragraph is created with the selection anchored
18
+ * in it so that users can type (or insert content, paste, etc.) straight away.
19
+ */
20
+ export default class WidgetTypeAround extends Plugin {
21
+ /**
22
+ * A reference to the model widget element that has the fake caret active
23
+ * on either side of it. It is later used to remove CSS classes associated with the fake caret
24
+ * when the widget no longer needs it.
25
+ */
26
+ private _currentFakeCaretModelElement;
27
+ /**
28
+ * @inheritDoc
29
+ */
30
+ static get pluginName(): 'WidgetTypeAround';
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ static get requires(): PluginDependencies;
35
+ /**
36
+ * @inheritDoc
37
+ */
38
+ init(): void;
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ destroy(): void;
43
+ /**
44
+ * Inserts a new paragraph next to a widget element with the selection anchored in it.
45
+ *
46
+ * **Note**: This method is heavily user-oriented and will both focus the editing view and scroll
47
+ * the viewport to the selection in the inserted paragraph.
48
+ *
49
+ * @param widgetModelElement The model widget element next to which a paragraph is inserted.
50
+ * @param position The position where the paragraph is inserted. Either `'before'` or `'after'` the widget.
51
+ */
52
+ private _insertParagraph;
53
+ /**
54
+ * A wrapper for the {@link module:utils/emittermixin~EmitterMixin#listenTo} method that executes the callbacks only
55
+ * when the plugin {@link #isEnabled is enabled}.
56
+ *
57
+ * @param emitter The object that fires the event.
58
+ * @param event The name of the event.
59
+ * @param callback The function to be called on event.
60
+ * @param options Additional options.
61
+ * @param options.priority The priority of this event callback. The higher the priority value the sooner
62
+ * the callback will be fired. Events having the same priority are called in the order they were added.
63
+ */
64
+ private _listenToIfEnabled;
65
+ /**
66
+ * Similar to {@link #_insertParagraph}, this method inserts a paragraph except that it
67
+ * does not expect a position. Instead, it performs the insertion next to a selected widget
68
+ * according to the `widget-type-around` model selection attribute value (fake caret position).
69
+ *
70
+ * Because this method requires the `widget-type-around` attribute to be set,
71
+ * the insertion can only happen when the widget's fake caret is active (e.g. activated
72
+ * using the keyboard).
73
+ *
74
+ * @returns Returns `true` when the paragraph was inserted (the attribute was present) and `false` otherwise.
75
+ */
76
+ private _insertParagraphAccordingToFakeCaretPosition;
77
+ /**
78
+ * Creates a listener in the editing conversion pipeline that injects the widget type around
79
+ * UI into every single widget instance created in the editor.
80
+ *
81
+ * The UI is delivered as a {@link module:engine/view/uielement~UIElement}
82
+ * wrapper which renders DOM buttons that users can use to insert paragraphs.
83
+ */
84
+ private _enableTypeAroundUIInjection;
85
+ /**
86
+ * Brings support for the fake caret that appears when either:
87
+ *
88
+ * * the selection moves to a widget from a position next to it using arrow keys,
89
+ * * the arrow key is pressed when the widget is already selected.
90
+ *
91
+ * The fake caret lets the user know that they can start typing or just press
92
+ * <kbd>Enter</kbd> to insert a paragraph at the position next to a widget as suggested by the fake caret.
93
+ *
94
+ * The fake caret disappears when the user changes the selection or the editor
95
+ * gets blurred.
96
+ *
97
+ * The whole idea is as follows:
98
+ *
99
+ * 1. A user does one of the 2 scenarios described at the beginning.
100
+ * 2. The "keydown" listener is executed and the decision is made whether to show or hide the fake caret.
101
+ * 3. If it should show up, the `widget-type-around` model selection attribute is set indicating
102
+ * on which side of the widget it should appear.
103
+ * 4. The selection dispatcher reacts to the selection attribute and sets CSS classes responsible for the
104
+ * fake caret on the view widget.
105
+ * 5. If the fake caret should disappear, the selection attribute is removed and the dispatcher
106
+ * does the CSS class clean-up in the view.
107
+ * 6. Additionally, `change:range` and `FocusTracker#isFocused` listeners also remove the selection
108
+ * attribute (the former also removes widget CSS classes).
109
+ */
110
+ private _enableTypeAroundFakeCaretActivationUsingKeyboardArrows;
111
+ /**
112
+ * A listener executed on each "keydown" in the view document, a part of
113
+ * {@link #_enableTypeAroundFakeCaretActivationUsingKeyboardArrows}.
114
+ *
115
+ * It decides whether the arrow keypress should activate the fake caret or not (also whether it should
116
+ * be deactivated).
117
+ *
118
+ * The fake caret activation is done by setting the `widget-type-around` model selection attribute
119
+ * in this listener, and stopping and preventing the event that would normally be handled by the widget
120
+ * plugin that is responsible for the regular keyboard navigation near/across all widgets (that
121
+ * includes inline widgets, which are ignored by the widget type around plugin).
122
+ */
123
+ private _handleArrowKeyPress;
124
+ /**
125
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected and activates or deactivates
126
+ * the fake caret for that widget, depending on the current value of the `widget-type-around` model
127
+ * selection attribute and the direction of the pressed arrow key.
128
+ *
129
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
130
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
131
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
132
+ * process the event any further. Returns `false` otherwise.
133
+ */
134
+ private _handleArrowKeyPressOnSelectedWidget;
135
+ /**
136
+ * Handles the keyboard navigation on "keydown" when **no** widget is selected but the selection is **directly** next
137
+ * to one and upon the fake caret should become active for this widget upon arrow keypress
138
+ * (AKA entering/selecting the widget).
139
+ *
140
+ * **Note**: This code mirrors the implementation from the widget plugin but also adds the selection attribute.
141
+ * Unfortunately, there is no safe way to let the widget plugin do the selection part first and then just set the
142
+ * selection attribute here in the widget type around plugin. This is why this code must duplicate some from the widget plugin.
143
+ *
144
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
145
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
146
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
147
+ * process the event any further. Returns `false` otherwise.
148
+ */
149
+ private _handleArrowKeyPressWhenSelectionNextToAWidget;
150
+ /**
151
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected (together with some other content)
152
+ * and the widget is the first or last element in the selection. It activates or deactivates the fake caret for that widget.
153
+ *
154
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
155
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
156
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
157
+ * process the event any further. Returns `false` otherwise.
158
+ */
159
+ private _handleArrowKeyPressWhenNonCollapsedSelection;
160
+ /**
161
+ * Registers a `mousedown` listener for the view document which intercepts events
162
+ * coming from the widget type around UI, which happens when a user clicks one of the buttons
163
+ * that insert a paragraph next to a widget.
164
+ */
165
+ private _enableInsertingParagraphsOnButtonClick;
166
+ /**
167
+ * Creates the <kbd>Enter</kbd> key listener on the view document that allows the user to insert a paragraph
168
+ * near the widget when either:
169
+ *
170
+ * * The fake caret was first activated using the arrow keys,
171
+ * * The entire widget is selected in the model.
172
+ *
173
+ * In the first case, the new paragraph is inserted according to the `widget-type-around` selection
174
+ * attribute (see {@link #_handleArrowKeyPress}).
175
+ *
176
+ * In the second case, the new paragraph is inserted based on whether a soft (<kbd>Shift</kbd>+<kbd>Enter</kbd>) keystroke
177
+ * was pressed or not.
178
+ */
179
+ private _enableInsertingParagraphsOnEnterKeypress;
180
+ /**
181
+ * Similar to the {@link #_enableInsertingParagraphsOnEnterKeypress}, it allows the user
182
+ * to insert a paragraph next to a widget when the fake caret was activated using arrow
183
+ * keys but it responds to typing instead of <kbd>Enter</kbd>.
184
+ *
185
+ * Listener enabled by this method will insert a new paragraph according to the `widget-type-around`
186
+ * model selection attribute as the user simply starts typing, which creates the impression that the fake caret
187
+ * behaves like a real one rendered by the browser (AKA your text appears where the caret was).
188
+ *
189
+ * **Note**: At the moment this listener creates 2 undo steps: one for the `insertParagraph` command
190
+ * and another one for actual typing. It is not a disaster but this may need to be fixed
191
+ * sooner or later.
192
+ */
193
+ private _enableInsertingParagraphsOnTypingKeystroke;
194
+ /**
195
+ * It creates a "delete" event listener on the view document to handle cases when the <kbd>Delete</kbd> or <kbd>Backspace</kbd>
196
+ * is pressed and the fake caret is currently active.
197
+ *
198
+ * The fake caret should create an illusion of a real browser caret so that when it appears before or after
199
+ * a widget, pressing <kbd>Delete</kbd> or <kbd>Backspace</kbd> should remove a widget or delete the content
200
+ * before or after a widget (depending on the content surrounding the widget).
201
+ */
202
+ private _enableDeleteIntegration;
203
+ /**
204
+ * Attaches the {@link module:engine/model/model~Model#event:insertContent} event listener that, for instance, allows the user to paste
205
+ * content near a widget when the fake caret is first activated using the arrow keys.
206
+ *
207
+ * The content is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
208
+ */
209
+ private _enableInsertContentIntegration;
210
+ /**
211
+ * Attaches the {@link module:engine/model/model~Model#event:insertObject} event listener that modifies the
212
+ * `options.findOptimalPosition`parameter to position of fake caret in relation to selected element
213
+ * to reflect user's intent of desired insertion position.
214
+ *
215
+ * The object is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
216
+ */
217
+ private _enableInsertObjectIntegration;
218
+ /**
219
+ * Attaches the {@link module:engine/model/model~Model#event:deleteContent} event listener to block the event when the fake
220
+ * caret is active.
221
+ *
222
+ * This is required for cases that trigger {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
223
+ * before calling {@link module:engine/model/model~Model#insertContent `model.insertContent()`} like, for instance,
224
+ * plain text pasting.
225
+ */
226
+ private _enableDeleteContentIntegration;
227
+ }
228
+ declare module '@ckeditor/ckeditor5-core' {
229
+ interface PluginsMap {
230
+ [WidgetTypeAround.pluginName]: WidgetTypeAround;
231
+ }
232
+ }