resizerstate.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module widget/widgetresize/resizerstate
  7. */
  8. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  9. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. /**
  12. * Stores the internal state of a single resizable object.
  13. *
  14. */
  15. export default class ResizeState {
  16. /**
  17. * @param {module:widget/widgetresize~ResizerOptions} options Resizer options.
  18. */
  19. constructor( options ) {
  20. /**
  21. * TODO
  22. *
  23. * @readonly
  24. * @member {Number} #originalWidth
  25. */
  26. /**
  27. * TODO
  28. *
  29. * @readonly
  30. * @member {Number} #originalHeight
  31. */
  32. /**
  33. * TODO
  34. *
  35. * @readonly
  36. * @member {Number} #originalWidthPercents
  37. */
  38. /**
  39. * Position of the handle that has 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. * TODO
  49. *
  50. * @readonly
  51. * @observable
  52. * @member {Number|null} #proposedWidthPercents
  53. */
  54. this.set( 'proposedWidthPercents', null );
  55. /**
  56. * TODO
  57. *
  58. * @readonly
  59. * @observable
  60. * @member {Number|null} #proposedWidthPixels
  61. */
  62. this.set( 'proposedWidth', null );
  63. /**
  64. * TODO
  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. * TODO
  75. *
  76. * @readonly
  77. * @member {Number} #aspectRatio
  78. */
  79. /**
  80. * @private
  81. * @type {module:widget/widgetresize~ResizerOptions}
  82. */
  83. this._options = options;
  84. /**
  85. * Reference point of resizer where the dragging started. It is used to measure the distance to user cursor
  86. * traveled, thus how much the image should be enlarged.
  87. * This information is only known after DOM was rendered, so it will be updated later.
  88. *
  89. * @private
  90. * @type {Object}
  91. */
  92. this._referenceCoordinates = null;
  93. }
  94. /**
  95. *
  96. * @param {HTMLElement} domResizeHandle The handle used to calculate the reference point.
  97. * @param {HTMLElement} domHandleHost
  98. * @param {HTMLElement} domResizeHost
  99. */
  100. begin( domResizeHandle, domHandleHost, domResizeHost ) {
  101. const clientRect = new Rect( domHandleHost );
  102. this.activeHandlePosition = getHandlePosition( domResizeHandle );
  103. this._referenceCoordinates = getAbsoluteBoundaryPoint( domHandleHost, getOppositePosition( this.activeHandlePosition ) );
  104. this.originalWidth = clientRect.width;
  105. this.originalHeight = clientRect.height;
  106. this.aspectRatio = clientRect.width / clientRect.height;
  107. const widthStyle = domResizeHost.style.width;
  108. if ( widthStyle ) {
  109. if ( widthStyle.match( /^\d+\.?\d*%$/ ) ) {
  110. this.originalWidthPercents = parseFloat( widthStyle );
  111. } else {
  112. // TODO we need to check it via comparing to the parent's width.
  113. this.originalWidthPercents = 50;
  114. }
  115. } else {
  116. this.originalWidthPercents = 100;
  117. }
  118. }
  119. update( newSize ) {
  120. this.proposedWidth = newSize.width;
  121. this.proposedHeight = newSize.height;
  122. this.proposedWidthPercents = newSize.widthPercents;
  123. this.proposedHandleHostWidth = newSize.handleHostWidth;
  124. this.proposedHandleHostHeight = newSize.handleHostHeight;
  125. }
  126. }
  127. mix( ResizeState, ObservableMixin );
  128. /**
  129. * Returns coordinates of top-left corner of a element, relative to the document's top-left corner.
  130. *
  131. * @private
  132. * @param {HTMLElement} element
  133. * @param {String} resizerPosition Position of the resize handler, e.g. `"top-left"`, `"bottom-right"`.
  134. * @returns {Object} return
  135. * @returns {Number} return.x
  136. * @returns {Number} return.y
  137. */
  138. function getAbsoluteBoundaryPoint( element, resizerPosition ) {
  139. const elementRect = new Rect( element );
  140. const positionParts = resizerPosition.split( '-' );
  141. const ret = {
  142. x: positionParts[ 1 ] == 'right' ? elementRect.right : elementRect.left,
  143. y: positionParts[ 0 ] == 'bottom' ? elementRect.bottom : elementRect.top
  144. };
  145. ret.x += element.ownerDocument.defaultView.scrollX;
  146. ret.y += element.ownerDocument.defaultView.scrollY;
  147. return ret;
  148. }
  149. /**
  150. * @private
  151. * @param {String} resizerPosition Expected resizer position like `"top-left"`, `"bottom-right"`.
  152. * @returns {String} A prefixed HTML class name for the resizer element
  153. */
  154. function getResizerHandleClass( resizerPosition ) {
  155. return `ck-widget__resizer__handle-${ resizerPosition }`;
  156. }
  157. /**
  158. * Determines the position of a given resize handle.
  159. *
  160. * @private
  161. * @param {HTMLElement} domHandle Handler used to calculate reference point.
  162. * @returns {String|undefined} Returns a string like `"top-left"` or `undefined` if not matched.
  163. */
  164. function getHandlePosition( domHandle ) {
  165. const resizerPositions = [ 'top-left', 'top-right', 'bottom-right', 'bottom-left' ];
  166. for ( const position of resizerPositions ) {
  167. if ( domHandle.classList.contains( getResizerHandleClass( position ) ) ) {
  168. return position;
  169. }
  170. }
  171. }
  172. /**
  173. * @param {String} position Like `"top-left"`.
  174. * @returns {String} Inverted `position`, e.g. returns `"bottom-right"` if `"top-left"` was given as `position`.
  175. */
  176. function getOppositePosition( position ) {
  177. const parts = position.split( '-' );
  178. const replacements = {
  179. top: 'bottom',
  180. bottom: 'top',
  181. left: 'right',
  182. right: 'left'
  183. };
  184. return `${ replacements[ parts[ 0 ] ] }-${ replacements[ parts[ 1 ] ] }`;
  185. }