@ckeditor/ckeditor5-widget 36.0.1 → 37.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.
package/src/widget.js CHANGED
@@ -25,10 +25,15 @@ import '../theme/widget.css';
25
25
  * {@link module:engine/view/selection~Selection#isFake fake}. Additionally, the `ck-widget_selected` CSS class
26
26
  * is added to indicate that widget has been selected.
27
27
  * * The mouse and keyboard events handling on and around widget elements.
28
- *
29
- * @extends module:core/plugin~Plugin
30
28
  */
31
29
  export default class Widget extends Plugin {
30
+ constructor() {
31
+ super(...arguments);
32
+ /**
33
+ * Holds previously selected widgets.
34
+ */
35
+ this._previouslySelected = new Set();
36
+ }
32
37
  /**
33
38
  * @inheritDoc
34
39
  */
@@ -48,13 +53,6 @@ export default class Widget extends Plugin {
48
53
  const editor = this.editor;
49
54
  const view = editor.editing.view;
50
55
  const viewDocument = view.document;
51
- /**
52
- * Holds previously selected widgets.
53
- *
54
- * @private
55
- * @type {Set.<module:engine/view/element~Element>}
56
- */
57
- this._previouslySelected = new Set();
58
56
  // Model to view selection converter.
59
57
  // Converts selection placed over widget element to fake selection.
60
58
  //
@@ -150,10 +148,6 @@ export default class Widget extends Plugin {
150
148
  }
151
149
  /**
152
150
  * Handles {@link module:engine/view/document~Document#event:mousedown mousedown} events on widget elements.
153
- *
154
- * @private
155
- * @param {module:utils/eventinfo~EventInfo} eventInfo
156
- * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
157
151
  */
158
152
  _onMousedown(eventInfo, domEventData) {
159
153
  const editor = this.editor;
@@ -205,10 +199,6 @@ export default class Widget extends Plugin {
205
199
  * * the selection is next to a widget and the widget should become selected upon the arrow key press.
206
200
  *
207
201
  * See {@link #_preventDefaultOnArrowKeyPress}.
208
- *
209
- * @private
210
- * @param {module:utils/eventinfo~EventInfo} eventInfo
211
- * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
212
202
  */
213
203
  _handleSelectionChangeOnArrowKeyPress(eventInfo, domEventData) {
214
204
  const keyCode = domEventData.keyCode;
@@ -270,10 +260,6 @@ export default class Widget extends Plugin {
270
260
  * container.
271
261
  *
272
262
  * See {@link #_handleSelectionChangeOnArrowKeyPress}.
273
- *
274
- * @private
275
- * @param {module:utils/eventinfo~EventInfo} eventInfo
276
- * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
277
263
  */
278
264
  _preventDefaultOnArrowKeyPress(eventInfo, domEventData) {
279
265
  const model = this.editor.model;
@@ -288,9 +274,8 @@ export default class Widget extends Plugin {
288
274
  /**
289
275
  * Handles delete keys: backspace and delete.
290
276
  *
291
- * @private
292
- * @param {Boolean} isForward Set to true if delete was performed in forward direction.
293
- * @returns {Boolean|undefined} Returns `true` if keys were handled correctly.
277
+ * @param isForward Set to true if delete was performed in forward direction.
278
+ * @returns Returns `true` if keys were handled correctly.
294
279
  */
295
280
  _handleDelete(isForward) {
296
281
  // Do nothing when the read only mode is enabled.
@@ -322,8 +307,6 @@ export default class Widget extends Plugin {
322
307
  * Sets {@link module:engine/model/selection~Selection document's selection} over given element.
323
308
  *
324
309
  * @internal
325
- * @protected
326
- * @param {module:engine/model/element~Element} element
327
310
  */
328
311
  _setSelectionOverElement(element) {
329
312
  this.editor.model.change(writer => {
@@ -336,9 +319,7 @@ export default class Widget extends Plugin {
336
319
  * {@link module:engine/model/schema~Schema schema} as `object`.
337
320
  *
338
321
  * @internal
339
- * @protected
340
- * @param {Boolean} forward Direction of checking.
341
- * @returns {module:engine/model/element~Element|null}
322
+ * @param forward Direction of checking.
342
323
  */
343
324
  _getObjectElementNextToSelection(forward) {
344
325
  const model = this.editor.model;
@@ -360,9 +341,6 @@ export default class Widget extends Plugin {
360
341
  }
361
342
  /**
362
343
  * Removes CSS class from previously selected widgets.
363
- *
364
- * @private
365
- * @param {module:engine/view/downcastwriter~DowncastWriter} writer
366
344
  */
367
345
  _clearPreviouslySelectedWidgets(writer) {
368
346
  for (const widget of this._previouslySelected) {
@@ -371,10 +349,9 @@ export default class Widget extends Plugin {
371
349
  this._previouslySelected.clear();
372
350
  }
373
351
  }
374
- // Returns `true` when element is a nested editable or is placed inside one.
375
- //
376
- // @param {module:engine/view/element~Element}
377
- // @returns {Boolean}
352
+ /**
353
+ * Returns `true` when element is a nested editable or is placed inside one.
354
+ */
378
355
  function isInsideNestedEditable(element) {
379
356
  let currentElement = element;
380
357
  while (currentElement) {
@@ -389,11 +366,12 @@ function isInsideNestedEditable(element) {
389
366
  }
390
367
  return false;
391
368
  }
392
- // Checks whether the specified `element` is a child of the `parent` element.
393
- //
394
- // @param {module:engine/view/element~Element} element An element to check.
395
- // @param {module:engine/view/element~Element|null} parent A parent for the element.
396
- // @returns {Boolean}
369
+ /**
370
+ * Checks whether the specified `element` is a child of the `parent` element.
371
+ *
372
+ * @param element An element to check.
373
+ * @param parent A parent for the element.
374
+ */
397
375
  function isChild(element, parent) {
398
376
  if (!parent) {
399
377
  return false;
@@ -0,0 +1,177 @@
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 { Rect, type DecoratedMethodEvent } from '@ckeditor/ckeditor5-utils';
6
+ import ResizeState from './resizerstate';
7
+ import type { ResizerOptions } from '../widgetresize';
8
+ declare const Resizer_base: {
9
+ new (): import("@ckeditor/ckeditor5-utils").Observable;
10
+ prototype: import("@ckeditor/ckeditor5-utils").Observable;
11
+ };
12
+ /**
13
+ * Represents a resizer for a single resizable object.
14
+ */
15
+ export default class Resizer extends Resizer_base {
16
+ /**
17
+ * Flag that indicates whether resizer can be used.
18
+ *
19
+ * @observable
20
+ */
21
+ isEnabled: boolean;
22
+ /**
23
+ * Flag that indicates that resizer is currently focused.
24
+ *
25
+ * @observable
26
+ */
27
+ isSelected: boolean;
28
+ /**
29
+ * Flag that indicates whether resizer is rendered (visible on the screen).
30
+ *
31
+ * @readonly
32
+ * @observable
33
+ */
34
+ isVisible: boolean;
35
+ /**
36
+ * Stores the state of the resizable host geometry, such as the original width, the currently proposed height, etc.
37
+ *
38
+ * Note that a new state is created for each resize transaction.
39
+ */
40
+ private _state;
41
+ /**
42
+ * A view displaying the proposed new element size during the resizing.
43
+ */
44
+ private _sizeView;
45
+ /**
46
+ * Options passed to the {@link #constructor}.
47
+ */
48
+ private _options;
49
+ /**
50
+ * A wrapper that is controlled by the resizer. This is usually a widget element.
51
+ */
52
+ private _viewResizerWrapper;
53
+ /**
54
+ * The width of the resized {@link module:widget/widgetresize~ResizerOptions#viewElement viewElement} before the resizing started.
55
+ */
56
+ private _initialViewWidth;
57
+ /**
58
+ * @param options Resizer options.
59
+ */
60
+ constructor(options: ResizerOptions);
61
+ /**
62
+ * Stores the state of the resizable host geometry, such as the original width, the currently proposed height, etc.
63
+ *
64
+ * Note that a new state is created for each resize transaction.
65
+ */
66
+ get state(): ResizeState;
67
+ /**
68
+ * Makes resizer visible in the UI.
69
+ */
70
+ show(): void;
71
+ /**
72
+ * Hides resizer in the UI.
73
+ */
74
+ hide(): void;
75
+ /**
76
+ * Attaches the resizer to the DOM.
77
+ */
78
+ attach(): void;
79
+ /**
80
+ * Starts the resizing process.
81
+ *
82
+ * Creates a new {@link #state} for the current process.
83
+ *
84
+ * @fires begin
85
+ * @param domResizeHandle Clicked handle.
86
+ */
87
+ begin(domResizeHandle: HTMLElement): void;
88
+ /**
89
+ * Updates the proposed size based on `domEventData`.
90
+ *
91
+ * @fires updateSize
92
+ */
93
+ updateSize(domEventData: MouseEvent): void;
94
+ /**
95
+ * Applies the geometry proposed with the resizer.
96
+ *
97
+ * @fires commit
98
+ */
99
+ commit(): void;
100
+ /**
101
+ * Cancels and rejects the proposed resize dimensions, hiding the UI.
102
+ *
103
+ * @fires cancel
104
+ */
105
+ cancel(): void;
106
+ /**
107
+ * Destroys the resizer.
108
+ */
109
+ destroy(): void;
110
+ /**
111
+ * Redraws the resizer.
112
+ *
113
+ * @param handleHostRect Handle host rectangle might be given to improve performance.
114
+ */
115
+ redraw(handleHostRect?: Rect): void;
116
+ containsHandle(domElement: HTMLElement): boolean;
117
+ static isResizeHandle(domElement: HTMLElement): boolean;
118
+ /**
119
+ * Cleans up the context state.
120
+ */
121
+ private _cleanup;
122
+ /**
123
+ * Calculates the proposed size as the resize handles are dragged.
124
+ *
125
+ * @param domEventData Event data that caused the size update request. It should be used to calculate the proposed size.
126
+ */
127
+ private _proposeNewSize;
128
+ /**
129
+ * Obtains the resize host.
130
+ *
131
+ * Resize host is an object that receives dimensions which are the result of resizing.
132
+ */
133
+ private _getResizeHost;
134
+ /**
135
+ * Obtains the handle host.
136
+ *
137
+ * Handle host is an object that the handles are aligned to.
138
+ *
139
+ * Handle host will not always be an entire widget itself. Take an image as an example. The image widget
140
+ * contains an image and a caption. Only the image should be surrounded with handles.
141
+ */
142
+ private _getHandleHost;
143
+ /**
144
+ * DOM container of the entire resize UI.
145
+ *
146
+ * Note that this property will have a value only after the element bound with the resizer is rendered
147
+ * (otherwise `null`).
148
+ */
149
+ private get _domResizerWrapper();
150
+ /**
151
+ * Renders the resize handles in the DOM.
152
+ *
153
+ * @param domElement The resizer wrapper.
154
+ */
155
+ private _appendHandles;
156
+ /**
157
+ * Sets up the {@link #_sizeView} property and adds it to the passed `domElement`.
158
+ */
159
+ private _appendSizeUI;
160
+ }
161
+ /**
162
+ * @eventName begin
163
+ */
164
+ export type ResizerBeginEvent = DecoratedMethodEvent<Resizer, 'begin'>;
165
+ /**
166
+ * @eventName cancel
167
+ */
168
+ export type ResizerCancelEvent = DecoratedMethodEvent<Resizer, 'cancel'>;
169
+ /**
170
+ * @eventName commit
171
+ */
172
+ export type ResizerCommitEvent = DecoratedMethodEvent<Resizer, 'commit'>;
173
+ /**
174
+ * @eventName updateSize
175
+ */
176
+ export type ResizerUpdateSizeEvent = DecoratedMethodEvent<Resizer, 'updateSize'>;
177
+ export {};
@@ -11,68 +11,20 @@ import ResizeState from './resizerstate';
11
11
  import SizeView from './sizeview';
12
12
  /**
13
13
  * Represents a resizer for a single resizable object.
14
- *
15
- * @mixes module:utils/observablemixin~ObservableMixin
16
14
  */
17
15
  export default class Resizer extends ObservableMixin() {
18
16
  /**
19
- * @param {module:widget/widgetresize~ResizerOptions} options Resizer options.
17
+ * @param options Resizer options.
20
18
  */
21
19
  constructor(options) {
22
20
  super();
23
- /**
24
- * Stores the state of the resizable host geometry, such as the original width, the currently proposed height, etc.
25
- *
26
- * Note that a new state is created for each resize transaction.
27
- *
28
- * @readonly
29
- * @member {module:widget/widgetresize/resizerstate~ResizerState} #state
30
- */
31
- /**
32
- * A view displaying the proposed new element size during the resizing.
33
- *
34
- * @protected
35
- * @readonly
36
- * @member {module:widget/widgetresize/sizeview~SizeView} #_sizeView
37
- */
38
- /**
39
- * Options passed to the {@link #constructor}.
40
- *
41
- * @private
42
- * @type {module:widget/widgetresize~ResizerOptions}
43
- */
44
- this._options = options;
45
21
  /**
46
22
  * A wrapper that is controlled by the resizer. This is usually a widget element.
47
- *
48
- * @private
49
- * @type {module:engine/view/element~Element|null}
50
23
  */
51
24
  this._viewResizerWrapper = null;
52
- /**
53
- * The width of the resized {@link module:widget/widgetresize~ResizerOptions#viewElement viewElement} before the resizing started.
54
- *
55
- * @private
56
- * @member {Number|String|undefined} #_initialViewWidth
57
- */
58
- /**
59
- * Flag that indicates whether resizer can be used.
60
- *
61
- * @observable
62
- */
25
+ this._options = options;
63
26
  this.set('isEnabled', true);
64
- /**
65
- * Flag that indicates that resizer is currently focused.
66
- *
67
- * @observable
68
- */
69
27
  this.set('isSelected', false);
70
- /**
71
- * Flag that indicates whether resizer is rendered (visible on the screen).
72
- *
73
- * @readonly
74
- * @observable
75
- */
76
28
  this.bind('isVisible').to(this, 'isEnabled', this, 'isSelected', (isEnabled, isSelected) => isEnabled && isSelected);
77
29
  this.decorate('begin');
78
30
  this.decorate('cancel');
@@ -87,6 +39,11 @@ export default class Resizer extends ObservableMixin() {
87
39
  }
88
40
  }, { priority: 'high' });
89
41
  }
42
+ /**
43
+ * Stores the state of the resizable host geometry, such as the original width, the currently proposed height, etc.
44
+ *
45
+ * Note that a new state is created for each resize transaction.
46
+ */
90
47
  get state() {
91
48
  return this._state;
92
49
  }
@@ -149,7 +106,7 @@ export default class Resizer extends ObservableMixin() {
149
106
  * Creates a new {@link #state} for the current process.
150
107
  *
151
108
  * @fires begin
152
- * @param {HTMLElement} domResizeHandle Clicked handle.
109
+ * @param domResizeHandle Clicked handle.
153
110
  */
154
111
  begin(domResizeHandle) {
155
112
  this._state = new ResizeState(this._options);
@@ -161,7 +118,6 @@ export default class Resizer extends ObservableMixin() {
161
118
  * Updates the proposed size based on `domEventData`.
162
119
  *
163
120
  * @fires updateSize
164
- * @param {Event} domEventData
165
121
  */
166
122
  updateSize(domEventData) {
167
123
  const newSize = this._proposeNewSize(domEventData);
@@ -220,7 +176,7 @@ export default class Resizer extends ObservableMixin() {
220
176
  /**
221
177
  * Redraws the resizer.
222
178
  *
223
- * @param {module:utils/dom/rect~Rect} [handleHostRect] Handle host rectangle might be given to improve performance.
179
+ * @param handleHostRect Handle host rectangle might be given to improve performance.
224
180
  */
225
181
  redraw(handleHostRect) {
226
182
  const domWrapper = this._domResizerWrapper;
@@ -283,8 +239,6 @@ export default class Resizer extends ObservableMixin() {
283
239
  }
284
240
  /**
285
241
  * Cleans up the context state.
286
- *
287
- * @protected
288
242
  */
289
243
  _cleanup() {
290
244
  this._sizeView._dismiss();
@@ -296,11 +250,7 @@ export default class Resizer extends ObservableMixin() {
296
250
  /**
297
251
  * Calculates the proposed size as the resize handles are dragged.
298
252
  *
299
- * @private
300
- * @param {Event} domEventData Event data that caused the size update request. It should be used to calculate the proposed size.
301
- * @returns {Object} return
302
- * @returns {Number} return.width Proposed width.
303
- * @returns {Number} return.height Proposed height.
253
+ * @param domEventData Event data that caused the size update request. It should be used to calculate the proposed size.
304
254
  */
305
255
  _proposeNewSize(domEventData) {
306
256
  const state = this.state;
@@ -352,9 +302,6 @@ export default class Resizer extends ObservableMixin() {
352
302
  * Obtains the resize host.
353
303
  *
354
304
  * Resize host is an object that receives dimensions which are the result of resizing.
355
- *
356
- * @protected
357
- * @returns {HTMLElement}
358
305
  */
359
306
  _getResizeHost() {
360
307
  const widgetWrapper = this._domResizerWrapper.parentElement;
@@ -367,9 +314,6 @@ export default class Resizer extends ObservableMixin() {
367
314
  *
368
315
  * Handle host will not always be an entire widget itself. Take an image as an example. The image widget
369
316
  * contains an image and a caption. Only the image should be surrounded with handles.
370
- *
371
- * @protected
372
- * @returns {HTMLElement}
373
317
  */
374
318
  _getHandleHost() {
375
319
  const widgetWrapper = this._domResizerWrapper.parentElement;
@@ -380,9 +324,6 @@ export default class Resizer extends ObservableMixin() {
380
324
  *
381
325
  * Note that this property will have a value only after the element bound with the resizer is rendered
382
326
  * (otherwise `null`).
383
- *
384
- * @private
385
- * @member {HTMLElement|null}
386
327
  */
387
328
  get _domResizerWrapper() {
388
329
  return this._options.editor.editing.view.domConverter.mapViewToDom(this._viewResizerWrapper);
@@ -390,8 +331,7 @@ export default class Resizer extends ObservableMixin() {
390
331
  /**
391
332
  * Renders the resize handles in the DOM.
392
333
  *
393
- * @private
394
- * @param {HTMLElement} domElement The resizer wrapper.
334
+ * @param domElement The resizer wrapper.
395
335
  */
396
336
  _appendHandles(domElement) {
397
337
  const resizerPositions = ['top-left', 'top-right', 'bottom-right', 'bottom-left'];
@@ -406,9 +346,6 @@ export default class Resizer extends ObservableMixin() {
406
346
  }
407
347
  /**
408
348
  * Sets up the {@link #_sizeView} property and adds it to the passed `domElement`.
409
- *
410
- * @private
411
- * @param {HTMLElement} domElement
412
349
  */
413
350
  _appendSizeUI(domElement) {
414
351
  this._sizeView = new SizeView();
@@ -417,9 +354,10 @@ export default class Resizer extends ObservableMixin() {
417
354
  domElement.appendChild(this._sizeView.element);
418
355
  }
419
356
  }
420
- // @private
421
- // @param {String} resizerPosition Expected resizer position like `"top-left"`, `"bottom-right"`.
422
- // @returns {String} A prefixed HTML class name for the resizer element
357
+ /**
358
+ * @param resizerPosition Expected resizer position like `"top-left"`, `"bottom-right"`.
359
+ * @returns A prefixed HTML class name for the resizer element
360
+ */
423
361
  function getResizerClass(resizerPosition) {
424
362
  return `ck-widget__resizer__handle-${resizerPosition}`;
425
363
  }
@@ -0,0 +1,125 @@
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 { ResizerOptions } from '../widgetresize';
6
+ declare const ResizeState_base: {
7
+ new (): import("@ckeditor/ckeditor5-utils").Observable;
8
+ prototype: import("@ckeditor/ckeditor5-utils").Observable;
9
+ };
10
+ /**
11
+ * Stores the internal state of a single resizable object.
12
+ */
13
+ export default class ResizeState extends ResizeState_base {
14
+ /**
15
+ * The position of the handle that initiated the resizing. E.g. `"top-left"`, `"bottom-right"` etc. or `null`
16
+ * if unknown.
17
+ *
18
+ * @readonly
19
+ * @observable
20
+ */
21
+ activeHandlePosition: string | null;
22
+ /**
23
+ * The width (percents) proposed, but not committed yet, in the current resize process.
24
+ *
25
+ * @readonly
26
+ * @observable
27
+ */
28
+ proposedWidthPercents: number | null;
29
+ /**
30
+ * The width (pixels) proposed, but not committed yet, in the current resize process.
31
+ *
32
+ * @readonly
33
+ * @observable
34
+ */
35
+ proposedWidth: number | null;
36
+ /**
37
+ * The height (pixels) proposed, but not committed yet, in the current resize process.
38
+ *
39
+ * @readonly
40
+ * @observable
41
+ */
42
+ proposedHeight: number | null;
43
+ /**
44
+ * @readonly
45
+ * @observable
46
+ */
47
+ proposedHandleHostWidth: number | null;
48
+ /**
49
+ * @readonly
50
+ * @observable
51
+ */
52
+ proposedHandleHostHeight: number | null;
53
+ /**
54
+ * The reference point of the resizer where the dragging started. It is used to measure the distance the user cursor
55
+ * traveled, so how much the image should be enlarged.
56
+ * This information is only known after the DOM was rendered, so it will be updated later.
57
+ *
58
+ * @internal
59
+ */
60
+ _referenceCoordinates: {
61
+ x: number;
62
+ y: number;
63
+ } | null;
64
+ /**
65
+ * Resizer options.
66
+ */
67
+ private readonly _options;
68
+ /**
69
+ * The original width (pixels) of the resized object when the resize process was started.
70
+ *
71
+ * @readonly
72
+ */
73
+ private _originalWidth?;
74
+ /**
75
+ * The original height (pixels) of the resized object when the resize process was started.
76
+ *
77
+ * @readonly
78
+ */
79
+ private _originalHeight?;
80
+ /**
81
+ * The original width (percents) of the resized object when the resize process was started.
82
+ *
83
+ * @readonly
84
+ */
85
+ private _originalWidthPercents?;
86
+ /**
87
+ * A width to height ratio of the resized image.
88
+ *
89
+ * @readonly
90
+ */
91
+ private _aspectRatio?;
92
+ /**
93
+ * @param options Resizer options.
94
+ */
95
+ constructor(options: ResizerOptions);
96
+ /**
97
+ * The original width (pixels) of the resized object when the resize process was started.
98
+ */
99
+ get originalWidth(): number | undefined;
100
+ /**
101
+ * The original height (pixels) of the resized object when the resize process was started.
102
+ */
103
+ get originalHeight(): number | undefined;
104
+ /**
105
+ * The original width (percents) of the resized object when the resize process was started.
106
+ */
107
+ get originalWidthPercents(): number | undefined;
108
+ /**
109
+ * A width to height ratio of the resized image.
110
+ */
111
+ get aspectRatio(): number | undefined;
112
+ /**
113
+ *
114
+ * @param domResizeHandle The handle used to calculate the reference point.
115
+ */
116
+ begin(domResizeHandle: HTMLElement, domHandleHost: HTMLElement, domResizeHost: HTMLElement): void;
117
+ update(newSize: {
118
+ width: number;
119
+ height: number;
120
+ widthPercents: number;
121
+ handleHostWidth: number;
122
+ handleHostHeight: number;
123
+ }): void;
124
+ }
125
+ export {};