widgetresize.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, Event */
  6. import WidgetResize from '../src/widgetresize';
  7. // ClassicTestEditor can't be used, as it doesn't handle the focus, which is needed to test resizer visual cues.
  8. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  9. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  10. import { toWidget } from '../src/utils';
  11. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  13. import { mouseMock, Point } from './widgetresize/_utils/utils';
  14. describe( 'WidgetResize', () => {
  15. let editor, editorElement, widget, mouseListenerSpies;
  16. const commitStub = sinon.stub();
  17. before( () => {
  18. mouseListenerSpies = {
  19. down: sinon.spy( WidgetResize.prototype, '_mouseDownListener' ),
  20. move: sinon.spy( WidgetResize.prototype, '_mouseMoveListener' ),
  21. up: sinon.spy( WidgetResize.prototype, '_mouseUpListener' )
  22. };
  23. } );
  24. after( () => {
  25. for ( const stub of Object.values( mouseListenerSpies ) ) {
  26. stub.restore();
  27. }
  28. } );
  29. beforeEach( async () => {
  30. editorElement = createEditorElement();
  31. editor = await createEditor( editorElement );
  32. setModelData( editor.model, '[<widget></widget>]' );
  33. focusEditor( editor );
  34. widget = editor.editing.view.document.getRoot().getChild( 0 );
  35. for ( const stub of Object.values( mouseListenerSpies ) ) {
  36. stub.resetHistory();
  37. }
  38. commitStub.resetHistory();
  39. // It's crucial to have a precisely defined editor size for this test suite.
  40. editor.editing.view.change( writer => {
  41. const viewEditableRoot = editor.editing.view.document.getRoot();
  42. writer.setAttribute( 'style', 'width: 400px; padding: 0px; overflow: hidden', viewEditableRoot );
  43. } );
  44. } );
  45. afterEach( () => {
  46. editorElement.remove();
  47. if ( editor ) {
  48. return editor.destroy();
  49. }
  50. } );
  51. describe( 'plugin', () => {
  52. it( 'is loaded', () => {
  53. expect( editor.plugins.get( WidgetResize ) ).to.be.instanceOf( WidgetResize );
  54. } );
  55. } );
  56. describe( 'mouse listeners', () => {
  57. beforeEach( () => {
  58. createResizer();
  59. } );
  60. it( 'don\'t break when called with unexpected element', () => {
  61. const unrelatedElement = document.createElement( 'div' );
  62. editor.plugins.get( WidgetResize )._mouseDownListener( {}, {
  63. target: unrelatedElement
  64. } );
  65. } );
  66. it( 'passes new width to the options.onCommit()', () => {
  67. const usedResizer = 'top-right';
  68. const domParts = getWidgetDomParts( widget, usedResizer );
  69. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  70. const finalPointerPosition = initialPointerPosition.clone().moveBy( 20, 0 );
  71. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  72. expect( commitStub.callCount ).to.be.equal( 1 );
  73. sinon.assert.calledWithExactly( commitStub, '120px' );
  74. } );
  75. } );
  76. it( 'are detached when plugin is destroyed', async () => {
  77. await editor.destroy();
  78. const plugin = editor.plugins.get( WidgetResize );
  79. editor = null;
  80. const event = new Event( 'mousedown', { bubbles: true } );
  81. document.body.dispatchEvent( event );
  82. // Ensure nothing got called.
  83. expect( plugin._mouseDownListener.callCount ).to.be.equal( 0 );
  84. } );
  85. it( 'nothing bad happens if activeResizer got unset', () => {
  86. createResizer( {
  87. isCentered: () => true
  88. } );
  89. const usedResizer = 'top-right';
  90. const domParts = getWidgetDomParts( widget, usedResizer );
  91. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  92. editor.plugins.get( WidgetResize )._getResizerByHandle = sinon.stub().returns( null );
  93. mouseMock.dragTo( editor, domParts.resizeHandle, initialPointerPosition );
  94. // No exception should be thrown.
  95. } );
  96. describe( 'Integration (pixels)', () => {
  97. let resizer;
  98. beforeEach( () => {
  99. resizer = createResizer();
  100. } );
  101. it( 'properly sets the state for subsequent resizes', () => {
  102. const usedResizer = 'top-right';
  103. const domParts = getWidgetDomParts( widget, usedResizer );
  104. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  105. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 50, 0 );
  106. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  107. sinon.assert.calledWithExactly( commitStub.firstCall, '150px' );
  108. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 50, 0 );
  109. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  110. sinon.assert.calledWithExactly( commitStub.secondCall, '200px' );
  111. expect( commitStub.callCount ).to.be.equal( 2 );
  112. } );
  113. it( 'shrinks correctly with bottom-left handler', () => {
  114. const usedResizer = 'bottom-left';
  115. const domParts = getWidgetDomParts( widget, usedResizer );
  116. const finalPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer ).moveBy( 10, -10 );
  117. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  118. sinon.assert.calledWithExactly( commitStub.firstCall, '90px' );
  119. sinon.assert.calledOnce( commitStub );
  120. } );
  121. it( 'shrinks correctly with bottom-right handler', () => {
  122. const usedResizer = 'bottom-right';
  123. const domParts = getWidgetDomParts( widget, usedResizer );
  124. const finalPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer ).moveBy( -10, -10 );
  125. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  126. sinon.assert.calledWithExactly( commitStub.firstCall, '90px' );
  127. sinon.assert.calledOnce( commitStub );
  128. } );
  129. generateResizeTest( 'enlarges correctly with bottom-right handler, x axis only', {
  130. usedResizer: 'bottom-right',
  131. movePointerBy: { x: 10, y: 0 },
  132. expectedWidth: '110px'
  133. } );
  134. it( 'hides the resize wrapper when resizer gets disabled', () => {
  135. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  136. expect( resizerWrapper.style.display ).to.equal( '' );
  137. resizer.isEnabled = false;
  138. expect( resizerWrapper.style.display ).to.equal( 'none' );
  139. } );
  140. /**
  141. *
  142. * @param {String} caseTitle
  143. * @param {Object} options
  144. * @param {String} options.caseTitle
  145. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  146. */
  147. function generateResizeTest( caseTitle, options ) {
  148. options = options || {};
  149. const usedResizer = options.usedResizer;
  150. const pointerDifference = options.movePointerBy;
  151. it( caseTitle, () => {
  152. const domParts = getWidgetDomParts( widget, usedResizer );
  153. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  154. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  155. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  156. expect( commitStub.args[ 0 ][ 0 ] ).to.be.equal( options.expectedWidth );
  157. sinon.assert.calledOnce( commitStub );
  158. } );
  159. }
  160. } );
  161. describe( 'Integration (percents)', () => {
  162. beforeEach( () => {
  163. createResizer( {
  164. unit: undefined
  165. } );
  166. } );
  167. it( 'properly sets the state for subsequent resizes', () => {
  168. const usedResizer = 'top-right';
  169. const domParts = getWidgetDomParts( widget, usedResizer );
  170. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  171. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  172. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  173. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  174. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  175. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  176. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  177. expect( commitStub.callCount ).to.be.equal( 2 );
  178. } );
  179. } );
  180. function createEditor( element ) {
  181. return ClassicEditor
  182. .create( element, {
  183. plugins: [
  184. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  185. ]
  186. } );
  187. function simpleWidgetPlugin( editor ) {
  188. editor.model.schema.register( 'widget', {
  189. inheritAllFrom: '$block',
  190. isObject: true
  191. } );
  192. editor.conversion.for( 'downcast' )
  193. .elementToElement( {
  194. model: 'widget',
  195. view: ( modelItem, viewWriter ) => {
  196. const div = viewWriter.createContainerElement( 'div' );
  197. viewWriter.setStyle( 'height', '100px', div );
  198. viewWriter.setStyle( 'width', '25%', div ); // It evaluates to 100px.
  199. return toWidget( div, viewWriter, {
  200. label: 'element label'
  201. } );
  202. }
  203. } );
  204. }
  205. }
  206. function createEditorElement() {
  207. const element = document.createElement( 'div' );
  208. document.body.appendChild( element );
  209. return element;
  210. }
  211. function createResizer( resizerOptions ) {
  212. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  213. const defaultOptions = {
  214. unit: 'px',
  215. modelElement: widgetModel,
  216. viewElement: widget,
  217. editor,
  218. isCentered: () => false,
  219. getHandleHost( domWidgetElement ) {
  220. return domWidgetElement;
  221. },
  222. getResizeHost( domWidgetElement ) {
  223. return domWidgetElement;
  224. },
  225. onCommit: commitStub
  226. };
  227. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  228. }
  229. function getWidgetDomParts( widget, resizerPosition ) {
  230. const view = editor.editing.view;
  231. const resizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 0 ) );
  232. return {
  233. resizeWrapper,
  234. resizeHandle: resizeWrapper.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` ),
  235. widget: view.domConverter.mapViewToDom( widget )
  236. };
  237. }
  238. /**
  239. * Returns a center point for a given handle.
  240. *
  241. * @param {HTMLElement} domWrapper Wrapper of an element that contains the resizer.
  242. * @param {String} [handlePosition='top-left']
  243. * @returns {Point}
  244. */
  245. function getHandleCenterPoint( domWrapper, handlePosition ) {
  246. const wrapperRect = new Rect( domWrapper );
  247. const returnValue = new Point( wrapperRect.left, wrapperRect.top );
  248. const cornerPositionParts = handlePosition.split( '-' );
  249. if ( cornerPositionParts.includes( 'right' ) ) {
  250. returnValue.x = wrapperRect.right;
  251. }
  252. if ( cornerPositionParts.includes( 'bottom' ) ) {
  253. returnValue.y = wrapperRect.bottom;
  254. }
  255. return returnValue;
  256. }
  257. function focusEditor( editor ) {
  258. editor.editing.view.focus();
  259. editor.ui.focusTracker.isFocused = true;
  260. }
  261. } );