widgetresize.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. it( 'enlarges correctly with bottom-right handler, x axis only', generateResizeTest( {
  130. usedHandle: 'bottom-right',
  131. movePointerBy: { x: 10, y: 0 },
  132. expectedWidth: '110px'
  133. } ) );
  134. it( 'enlarges correctly with bottom-right handler, y axis only', generateResizeTest( {
  135. usedHandle: 'bottom-right',
  136. movePointerBy: { x: 0, y: 10 },
  137. expectedWidth: '110px'
  138. } ) );
  139. it( 'enlarges correctly with bottom-left handler, x axis only', generateResizeTest( {
  140. usedHandle: 'bottom-left',
  141. movePointerBy: { x: -10, y: 0 },
  142. expectedWidth: '110px'
  143. } ) );
  144. it( 'enlarges correctly with bottom-left handler, y axis only', generateResizeTest( {
  145. usedHandle: 'bottom-left',
  146. movePointerBy: { x: 0, y: 10 },
  147. expectedWidth: '110px'
  148. } ) );
  149. // --- top handlers ---
  150. it( 'enlarges correctly with top-left handler', generateResizeTest( {
  151. usedHandle: 'top-left',
  152. movePointerBy: { x: -10, y: -10 },
  153. expectedWidth: '110px'
  154. } ) );
  155. it( 'enlarges correctly with top-left handler, y axis only', generateResizeTest( {
  156. usedHandle: 'top-left',
  157. movePointerBy: { x: 0, y: -10 },
  158. expectedWidth: '110px'
  159. } ) );
  160. it( 'enlarges correctly with top-right handler', generateResizeTest( {
  161. usedHandle: 'top-right',
  162. movePointerBy: { x: 10, y: -10 },
  163. expectedWidth: '110px'
  164. } ) );
  165. it( 'enlarges correctly with top-right handler, y axis only', generateResizeTest( {
  166. usedHandle: 'top-right',
  167. movePointerBy: { x: 0, y: -10 },
  168. expectedWidth: '110px'
  169. } ) );
  170. it( 'hides the resize wrapper when resizer gets disabled', () => {
  171. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  172. expect( resizerWrapper.style.display ).to.equal( '' );
  173. resizer.isEnabled = false;
  174. expect( resizerWrapper.style.display ).to.equal( 'none' );
  175. } );
  176. /**
  177. * @param {Object} options
  178. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  179. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  180. * @param {String} options.expectedWidth
  181. */
  182. function generateResizeTest( options ) {
  183. return () => {
  184. options = options || {};
  185. const usedHandle = options.usedHandle;
  186. const domParts = getWidgetDomParts( widget, usedHandle );
  187. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  188. const pointerDifference = options.movePointerBy;
  189. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  190. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  191. expect( commitStub.args[ 0 ][ 0 ] ).to.be.equal( options.expectedWidth );
  192. sinon.assert.calledOnce( commitStub );
  193. };
  194. }
  195. } );
  196. describe( 'Integration (percents)', () => {
  197. beforeEach( () => {
  198. createResizer( {
  199. unit: undefined
  200. } );
  201. } );
  202. it( 'properly sets the state for subsequent resizes', () => {
  203. const usedResizer = 'top-right';
  204. const domParts = getWidgetDomParts( widget, usedResizer );
  205. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  206. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  207. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  208. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  209. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  210. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  211. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  212. expect( commitStub.callCount ).to.be.equal( 2 );
  213. } );
  214. } );
  215. function createEditor( element ) {
  216. return ClassicEditor
  217. .create( element, {
  218. plugins: [
  219. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  220. ]
  221. } );
  222. function simpleWidgetPlugin( editor ) {
  223. editor.model.schema.register( 'widget', {
  224. inheritAllFrom: '$block',
  225. isObject: true
  226. } );
  227. editor.conversion.for( 'downcast' )
  228. .elementToElement( {
  229. model: 'widget',
  230. view: ( modelItem, viewWriter ) => {
  231. const div = viewWriter.createContainerElement( 'div' );
  232. viewWriter.setStyle( 'height', '100px', div );
  233. viewWriter.setStyle( 'width', '25%', div ); // It evaluates to 100px.
  234. return toWidget( div, viewWriter, {
  235. label: 'element label'
  236. } );
  237. }
  238. } );
  239. }
  240. }
  241. function createEditorElement() {
  242. const element = document.createElement( 'div' );
  243. document.body.appendChild( element );
  244. return element;
  245. }
  246. function createResizer( resizerOptions ) {
  247. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  248. const defaultOptions = {
  249. unit: 'px',
  250. modelElement: widgetModel,
  251. viewElement: widget,
  252. editor,
  253. isCentered: () => false,
  254. getHandleHost( domWidgetElement ) {
  255. return domWidgetElement;
  256. },
  257. getResizeHost( domWidgetElement ) {
  258. return domWidgetElement;
  259. },
  260. onCommit: commitStub
  261. };
  262. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  263. }
  264. function getWidgetDomParts( widget, resizerPosition ) {
  265. const view = editor.editing.view;
  266. const resizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 0 ) );
  267. return {
  268. resizeWrapper,
  269. resizeHandle: resizeWrapper.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` ),
  270. widget: view.domConverter.mapViewToDom( widget )
  271. };
  272. }
  273. /**
  274. * Returns a center point for a given handle.
  275. *
  276. * @param {HTMLElement} domWrapper Wrapper of an element that contains the resizer.
  277. * @param {String} [handlePosition='top-left']
  278. * @returns {Point}
  279. */
  280. function getHandleCenterPoint( domWrapper, handlePosition ) {
  281. const wrapperRect = new Rect( domWrapper );
  282. const returnValue = new Point( wrapperRect.left, wrapperRect.top );
  283. const cornerPositionParts = handlePosition.split( '-' );
  284. if ( cornerPositionParts.includes( 'right' ) ) {
  285. returnValue.x = wrapperRect.right;
  286. }
  287. if ( cornerPositionParts.includes( 'bottom' ) ) {
  288. returnValue.y = wrapperRect.bottom;
  289. }
  290. return returnValue;
  291. }
  292. function focusEditor( editor ) {
  293. editor.editing.view.focus();
  294. editor.ui.focusTracker.isFocused = true;
  295. }
  296. } );