@ckeditor/ckeditor5-widget 35.2.0 → 35.3.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.
@@ -2,163 +2,148 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module widget/widgetresize/resizerstate
8
7
  */
9
-
10
8
  import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
11
-
12
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
13
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
14
-
9
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
15
10
  /**
16
11
  * Stores the internal state of a single resizable object.
17
12
  *
18
13
  */
19
- export default class ResizeState {
20
- /**
21
- * @param {module:widget/widgetresize~ResizerOptions} options Resizer options.
22
- */
23
- constructor( options ) {
24
- /**
25
- * The original width (pixels) of the resized object when the resize process was started.
26
- *
27
- * @readonly
28
- * @member {Number} #originalWidth
29
- */
30
-
31
- /**
32
- * The original height (pixels) of the resized object when the resize process was started.
33
- *
34
- * @readonly
35
- * @member {Number} #originalHeight
36
- */
37
-
38
- /**
39
- * The original width (percents) of the resized object when the resize process was started.
40
- *
41
- * @readonly
42
- * @member {Number} #originalWidthPercents
43
- */
44
-
45
- /**
46
- * The position of the handle that initiated the resizing. E.g. `"top-left"`, `"bottom-right"` etc. or `null`
47
- * if unknown.
48
- *
49
- * @readonly
50
- * @observable
51
- * @member {String|null} #activeHandlePosition
52
- */
53
- this.set( 'activeHandlePosition', null );
54
-
55
- /**
56
- * The width (percents) proposed, but not committed yet, in the current resize process.
57
- *
58
- * @readonly
59
- * @observable
60
- * @member {Number|null} #proposedWidthPercents
61
- */
62
- this.set( 'proposedWidthPercents', null );
63
-
64
- /**
65
- * The width (pixels) proposed, but not committed yet, in the current resize process.
66
- *
67
- * @readonly
68
- * @observable
69
- * @member {Number|null} #proposedWidthPixels
70
- */
71
- this.set( 'proposedWidth', null );
72
-
73
- /**
74
- * The height (pixels) proposed, but not committed yet, in the current resize process.
75
- *
76
- * @readonly
77
- * @observable
78
- * @member {Number|null} #proposedHeightPixels
79
- */
80
- this.set( 'proposedHeight', null );
81
-
82
- this.set( 'proposedHandleHostWidth', null );
83
- this.set( 'proposedHandleHostHeight', null );
84
-
85
- /**
86
- * A width to height ratio of the resized image.
87
- *
88
- * @readonly
89
- * @member {Number} #aspectRatio
90
- */
91
-
92
- /**
93
- * @private
94
- * @type {module:widget/widgetresize~ResizerOptions}
95
- */
96
- this._options = options;
97
-
98
- /**
99
- * The reference point of the resizer where the dragging started. It is used to measure the distance the user cursor
100
- * traveled, so how much the image should be enlarged.
101
- * This information is only known after the DOM was rendered, so it will be updated later.
102
- *
103
- * @private
104
- * @type {Object}
105
- */
106
- this._referenceCoordinates = null;
107
- }
108
-
109
- /**
110
- *
111
- * @param {HTMLElement} domResizeHandle The handle used to calculate the reference point.
112
- * @param {HTMLElement} domHandleHost
113
- * @param {HTMLElement} domResizeHost
114
- */
115
- begin( domResizeHandle, domHandleHost, domResizeHost ) {
116
- const clientRect = new Rect( domHandleHost );
117
-
118
- this.activeHandlePosition = getHandlePosition( domResizeHandle );
119
-
120
- this._referenceCoordinates = getAbsoluteBoundaryPoint( domHandleHost, getOppositePosition( this.activeHandlePosition ) );
121
-
122
- this.originalWidth = clientRect.width;
123
- this.originalHeight = clientRect.height;
124
-
125
- this.aspectRatio = clientRect.width / clientRect.height;
126
-
127
- const widthStyle = domResizeHost.style.width;
128
-
129
- if ( widthStyle && widthStyle.match( /^\d+(\.\d*)?%$/ ) ) {
130
- this.originalWidthPercents = parseFloat( widthStyle );
131
- } else {
132
- this.originalWidthPercents = calculateHostPercentageWidth( domResizeHost, clientRect );
133
- }
134
- }
135
-
136
- update( newSize ) {
137
- this.proposedWidth = newSize.width;
138
- this.proposedHeight = newSize.height;
139
- this.proposedWidthPercents = newSize.widthPercents;
140
-
141
- this.proposedHandleHostWidth = newSize.handleHostWidth;
142
- this.proposedHandleHostHeight = newSize.handleHostHeight;
143
- }
14
+ export default class ResizeState extends Observable {
15
+ /**
16
+ * @param {module:widget/widgetresize~ResizerOptions} options Resizer options.
17
+ */
18
+ constructor(options) {
19
+ super();
20
+ /**
21
+ * The original width (pixels) of the resized object when the resize process was started.
22
+ *
23
+ * @readonly
24
+ * @member {Number} #originalWidth
25
+ */
26
+ /**
27
+ * The original height (pixels) of the resized object when the resize process was started.
28
+ *
29
+ * @readonly
30
+ * @member {Number} #originalHeight
31
+ */
32
+ /**
33
+ * The original width (percents) of the resized object when the resize process was started.
34
+ *
35
+ * @readonly
36
+ * @member {Number} #originalWidthPercents
37
+ */
38
+ /**
39
+ * The position of the handle that initiated the resizing. E.g. `"top-left"`, `"bottom-right"` etc. or `null`
40
+ * if unknown.
41
+ *
42
+ * @readonly
43
+ * @observable
44
+ * @member {String|null} #activeHandlePosition
45
+ */
46
+ this.set('activeHandlePosition', null);
47
+ /**
48
+ * The width (percents) proposed, but not committed yet, in the current resize process.
49
+ *
50
+ * @readonly
51
+ * @observable
52
+ * @member {Number|null} #proposedWidthPercents
53
+ */
54
+ this.set('proposedWidthPercents', null);
55
+ /**
56
+ * The width (pixels) proposed, but not committed yet, in the current resize process.
57
+ *
58
+ * @readonly
59
+ * @observable
60
+ * @member {Number|null} #proposedWidthPixels
61
+ */
62
+ this.set('proposedWidth', null);
63
+ /**
64
+ * The height (pixels) proposed, but not committed yet, in the current resize process.
65
+ *
66
+ * @readonly
67
+ * @observable
68
+ * @member {Number|null} #proposedHeightPixels
69
+ */
70
+ this.set('proposedHeight', null);
71
+ this.set('proposedHandleHostWidth', null);
72
+ this.set('proposedHandleHostHeight', null);
73
+ /**
74
+ * A width to height ratio of the resized image.
75
+ *
76
+ * @readonly
77
+ * @member {Number} #aspectRatio
78
+ */
79
+ /**
80
+ * @private
81
+ * @type {module:widget/widgetresize~ResizerOptions}
82
+ */
83
+ this._options = options;
84
+ /**
85
+ * The reference point of the resizer where the dragging started. It is used to measure the distance the user cursor
86
+ * traveled, so how much the image should be enlarged.
87
+ * This information is only known after the DOM was rendered, so it will be updated later.
88
+ *
89
+ * @private
90
+ * @type {Object}
91
+ */
92
+ this._referenceCoordinates = null;
93
+ }
94
+ get originalWidth() {
95
+ return this._originalWidth;
96
+ }
97
+ get originalHeight() {
98
+ return this._originalHeight;
99
+ }
100
+ get originalWidthPercents() {
101
+ return this._originalWidthPercents;
102
+ }
103
+ get aspectRatio() {
104
+ return this._aspectRatio;
105
+ }
106
+ /**
107
+ *
108
+ * @param {HTMLElement} domResizeHandle The handle used to calculate the reference point.
109
+ * @param {HTMLElement} domHandleHost
110
+ * @param {HTMLElement} domResizeHost
111
+ */
112
+ begin(domResizeHandle, domHandleHost, domResizeHost) {
113
+ const clientRect = new Rect(domHandleHost);
114
+ this.activeHandlePosition = getHandlePosition(domResizeHandle);
115
+ this._referenceCoordinates = getAbsoluteBoundaryPoint(domHandleHost, getOppositePosition(this.activeHandlePosition));
116
+ this._originalWidth = clientRect.width;
117
+ this._originalHeight = clientRect.height;
118
+ this._aspectRatio = clientRect.width / clientRect.height;
119
+ const widthStyle = domResizeHost.style.width;
120
+ if (widthStyle && widthStyle.match(/^\d+(\.\d*)?%$/)) {
121
+ this._originalWidthPercents = parseFloat(widthStyle);
122
+ }
123
+ else {
124
+ this._originalWidthPercents = calculateHostPercentageWidth(domResizeHost, clientRect);
125
+ }
126
+ }
127
+ update(newSize) {
128
+ this.proposedWidth = newSize.width;
129
+ this.proposedHeight = newSize.height;
130
+ this.proposedWidthPercents = newSize.widthPercents;
131
+ this.proposedHandleHostWidth = newSize.handleHostWidth;
132
+ this.proposedHandleHostHeight = newSize.handleHostHeight;
133
+ }
144
134
  }
145
-
146
- mix( ResizeState, ObservableMixin );
147
-
148
135
  // Calculates a relative width of a `domResizeHost` compared to it's parent in percents.
149
136
  //
150
137
  // @private
151
138
  // @param {HTMLElement} domResizeHost
152
139
  // @param {module:utils/dom/rect~Rect} resizeHostRect
153
140
  // @returns {Number}
154
- function calculateHostPercentageWidth( domResizeHost, resizeHostRect ) {
155
- const domResizeHostParent = domResizeHost.parentElement;
156
- // Need to use computed style as it properly excludes parent's paddings from the returned value.
157
- const parentWidth = parseFloat( domResizeHostParent.ownerDocument.defaultView.getComputedStyle( domResizeHostParent ).width );
158
-
159
- return resizeHostRect.width / parentWidth * 100;
141
+ function calculateHostPercentageWidth(domResizeHost, resizeHostRect) {
142
+ const domResizeHostParent = domResizeHost.parentElement;
143
+ // Need to use computed style as it properly excludes parent's paddings from the returned value.
144
+ const parentWidth = parseFloat(domResizeHostParent.ownerDocument.defaultView.getComputedStyle(domResizeHostParent).width);
145
+ return resizeHostRect.width / parentWidth * 100;
160
146
  }
161
-
162
147
  // Returns coordinates of the top-left corner of an element, relative to the document's top-left corner.
163
148
  //
164
149
  // @private
@@ -167,53 +152,46 @@ function calculateHostPercentageWidth( domResizeHost, resizeHostRect ) {
167
152
  // @returns {Object} return
168
153
  // @returns {Number} return.x
169
154
  // @returns {Number} return.y
170
- function getAbsoluteBoundaryPoint( element, resizerPosition ) {
171
- const elementRect = new Rect( element );
172
- const positionParts = resizerPosition.split( '-' );
173
- const ret = {
174
- x: positionParts[ 1 ] == 'right' ? elementRect.right : elementRect.left,
175
- y: positionParts[ 0 ] == 'bottom' ? elementRect.bottom : elementRect.top
176
- };
177
-
178
- ret.x += element.ownerDocument.defaultView.scrollX;
179
- ret.y += element.ownerDocument.defaultView.scrollY;
180
-
181
- return ret;
155
+ function getAbsoluteBoundaryPoint(element, resizerPosition) {
156
+ const elementRect = new Rect(element);
157
+ const positionParts = resizerPosition.split('-');
158
+ const ret = {
159
+ x: positionParts[1] == 'right' ? elementRect.right : elementRect.left,
160
+ y: positionParts[0] == 'bottom' ? elementRect.bottom : elementRect.top
161
+ };
162
+ ret.x += element.ownerDocument.defaultView.scrollX;
163
+ ret.y += element.ownerDocument.defaultView.scrollY;
164
+ return ret;
182
165
  }
183
-
184
166
  // @private
185
167
  // @param {String} resizerPosition The expected resizer position, like `"top-left"`, `"bottom-right"`.
186
168
  // @returns {String} A prefixed HTML class name for the resizer element.
187
- function getResizerHandleClass( resizerPosition ) {
188
- return `ck-widget__resizer__handle-${ resizerPosition }`;
169
+ function getResizerHandleClass(resizerPosition) {
170
+ return `ck-widget__resizer__handle-${resizerPosition}`;
189
171
  }
190
-
191
172
  // Determines the position of a given resize handle.
192
173
  //
193
174
  // @private
194
175
  // @param {HTMLElement} domHandle Handle used to calculate the reference point.
195
176
  // @returns {String|undefined} Returns a string like `"top-left"` or `undefined` if not matched.
196
- function getHandlePosition( domHandle ) {
197
- const resizerPositions = [ 'top-left', 'top-right', 'bottom-right', 'bottom-left' ];
198
-
199
- for ( const position of resizerPositions ) {
200
- if ( domHandle.classList.contains( getResizerHandleClass( position ) ) ) {
201
- return position;
202
- }
203
- }
177
+ function getHandlePosition(domHandle) {
178
+ const resizerPositions = ['top-left', 'top-right', 'bottom-right', 'bottom-left'];
179
+ for (const position of resizerPositions) {
180
+ if (domHandle.classList.contains(getResizerHandleClass(position))) {
181
+ return position;
182
+ }
183
+ }
204
184
  }
205
-
206
185
  // @private
207
186
  // @param {String} position Like `"top-left"`.
208
187
  // @returns {String} Inverted `position`, e.g. it returns `"bottom-right"` if `"top-left"` was given as `position`.
209
- function getOppositePosition( position ) {
210
- const parts = position.split( '-' );
211
- const replacements = {
212
- top: 'bottom',
213
- bottom: 'top',
214
- left: 'right',
215
- right: 'left'
216
- };
217
-
218
- return `${ replacements[ parts[ 0 ] ] }-${ replacements[ parts[ 1 ] ] }`;
188
+ function getOppositePosition(position) {
189
+ const parts = position.split('-');
190
+ const replacements = {
191
+ top: 'bottom',
192
+ bottom: 'top',
193
+ left: 'right',
194
+ right: 'left'
195
+ };
196
+ return `${replacements[parts[0]]}-${replacements[parts[1]]}`;
219
197
  }
@@ -2,13 +2,10 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module widget/widgetresize/sizeview
8
7
  */
9
-
10
8
  import View from '@ckeditor/ckeditor5-ui/src/view';
11
-
12
9
  /**
13
10
  * A view displaying the proposed new element size during the resizing.
14
11
  *
@@ -16,99 +13,83 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
16
13
  * @extends {module:ui/view~View}
17
14
  */
18
15
  export default class SizeView extends View {
19
- constructor() {
20
- super();
21
-
22
- /**
23
- * The visibility of the view defined based on the existence of the host proposed dimensions.
24
- *
25
- * @private
26
- * @observable
27
- * @readonly
28
- * @member {Boolean} #_isVisible
29
- */
30
-
31
- /**
32
- * The text that will be displayed in the `SizeView` child.
33
- * It can be formatted as the pixel values (e.g. 10x20) or the percentage value (e.g. 10%).
34
- *
35
- * @private
36
- * @observable
37
- * @readonly
38
- * @member {Boolean} #_label
39
- */
40
-
41
- /**
42
- * The position of the view defined based on the host size and active handle position.
43
- *
44
- * @private
45
- * @observable
46
- * @readonly
47
- * @member {String} #_viewPosition
48
- */
49
-
50
- const bind = this.bindTemplate;
51
-
52
- this.setTemplate( {
53
- tag: 'div',
54
- attributes: {
55
- class: [
56
- 'ck',
57
- 'ck-size-view',
58
- bind.to( '_viewPosition', value => value ? `ck-orientation-${ value }` : '' )
59
- ],
60
- style: {
61
- display: bind.if( '_isVisible', 'none', visible => !visible )
62
- }
63
- },
64
- children: [ {
65
- text: bind.to( '_label' )
66
- } ]
67
- } );
68
- }
69
-
70
- /**
71
- * A method used for binding the `SizeView` instance properties to the `ResizeState` instance observable properties.
72
- *
73
- * @protected
74
- * @param {module:widget/widgetresize~ResizerOptions} options
75
- * An object defining the resizer options, used for setting the proper size label.
76
- * @param {module:widget/widgetresize/resizerstate~ResizeState} resizeState
77
- * The `ResizeState` class instance, used for keeping the `SizeView` state up to date.
78
- */
79
- _bindToState( options, resizeState ) {
80
- this.bind( '_isVisible' ).to( resizeState, 'proposedWidth', resizeState, 'proposedHeight', ( width, height ) =>
81
- width !== null && height !== null );
82
-
83
- this.bind( '_label' ).to(
84
- resizeState, 'proposedHandleHostWidth',
85
- resizeState, 'proposedHandleHostHeight',
86
- resizeState, 'proposedWidthPercents',
87
- ( width, height, widthPercents ) => {
88
- if ( options.unit === 'px' ) {
89
- return `${ width }×${ height }`;
90
- } else {
91
- return `${ widthPercents }%`;
92
- }
93
- }
94
- );
95
-
96
- this.bind( '_viewPosition' ).to(
97
- resizeState, 'activeHandlePosition',
98
- resizeState, 'proposedHandleHostWidth',
99
- resizeState, 'proposedHandleHostHeight',
100
- // If the widget is too small to contain the size label, display the label above.
101
- ( position, width, height ) => width < 50 || height < 50 ? 'above-center' : position
102
- );
103
- }
104
-
105
- /**
106
- * A method used for cleaning up. It removes the bindings and hides the view.
107
- *
108
- * @protected
109
- */
110
- _dismiss() {
111
- this.unbind();
112
- this._isVisible = false;
113
- }
16
+ constructor() {
17
+ super();
18
+ /**
19
+ * The visibility of the view defined based on the existence of the host proposed dimensions.
20
+ *
21
+ * @private
22
+ * @observable
23
+ * @readonly
24
+ * @member {Boolean} #_isVisible
25
+ */
26
+ /**
27
+ * The text that will be displayed in the `SizeView` child.
28
+ * It can be formatted as the pixel values (e.g. 10x20) or the percentage value (e.g. 10%).
29
+ *
30
+ * @private
31
+ * @observable
32
+ * @readonly
33
+ * @member {Boolean} #_label
34
+ */
35
+ /**
36
+ * The position of the view defined based on the host size and active handle position.
37
+ *
38
+ * @private
39
+ * @observable
40
+ * @readonly
41
+ * @member {String} #_viewPosition
42
+ */
43
+ const bind = this.bindTemplate;
44
+ this.setTemplate({
45
+ tag: 'div',
46
+ attributes: {
47
+ class: [
48
+ 'ck',
49
+ 'ck-size-view',
50
+ bind.to('_viewPosition', value => value ? `ck-orientation-${value}` : '')
51
+ ],
52
+ style: {
53
+ display: bind.if('_isVisible', 'none', visible => !visible)
54
+ }
55
+ },
56
+ children: [{
57
+ text: bind.to('_label')
58
+ }]
59
+ });
60
+ }
61
+ /**
62
+ * A method used for binding the `SizeView` instance properties to the `ResizeState` instance observable properties.
63
+ *
64
+ * @protected
65
+ * @internal
66
+ * @param {module:widget/widgetresize~ResizerOptions} options
67
+ * An object defining the resizer options, used for setting the proper size label.
68
+ * @param {module:widget/widgetresize/resizerstate~ResizeState} resizeState
69
+ * The `ResizeState` class instance, used for keeping the `SizeView` state up to date.
70
+ */
71
+ _bindToState(options, resizeState) {
72
+ this.bind('_isVisible').to(resizeState, 'proposedWidth', resizeState, 'proposedHeight', (width, height) => width !== null && height !== null);
73
+ this.bind('_label').to(resizeState, 'proposedHandleHostWidth', resizeState, 'proposedHandleHostHeight', resizeState, 'proposedWidthPercents', (width, height, widthPercents) => {
74
+ if (options.unit === 'px') {
75
+ return `${width}×${height}`;
76
+ }
77
+ else {
78
+ return `${widthPercents}%`;
79
+ }
80
+ });
81
+ this.bind('_viewPosition').to(resizeState, 'activeHandlePosition', resizeState, 'proposedHandleHostWidth', resizeState, 'proposedHandleHostHeight',
82
+ // If the widget is too small to contain the size label, display the label above.
83
+ (position, width, height) => width < 50 || height < 50 ? 'above-center' : position);
84
+ }
85
+ /**
86
+ * A method used for cleaning up. It removes the bindings and hides the view.
87
+ *
88
+ * @protected
89
+ * @internal
90
+ */
91
+ _dismiss() {
92
+ this.unbind();
93
+ this._isVisible = false;
94
+ }
114
95
  }