8
0

widgetresize.js 13 KB

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