widgetresize.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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( 'visibility', () => {
  97. beforeEach( () => {
  98. createResizer();
  99. } );
  100. it( 'it\'s hidden by default', () => {
  101. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  102. // Since widget is already focused, move the selection out of it.
  103. editor.model.change( writer => {
  104. const modelWidget = editor.editing.mapper.toModelElement( widget );
  105. writer.setSelection( modelWidget, 'before' );
  106. } );
  107. for ( const resizer of allResizers ) {
  108. expect( isVisible( resizer ) ).to.be.false;
  109. }
  110. } );
  111. it( 'it\'s visible once widget is focused', () => {
  112. // Widget is focused by default.
  113. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  114. for ( const resizer of allResizers ) {
  115. expect( isVisible( resizer ) ).to.be.true;
  116. }
  117. } );
  118. function isVisible( element ) {
  119. // Checks if the DOM element is visible to the end user.
  120. return element.offsetParent !== null && !element.classList.contains( 'ck-hidden' );
  121. }
  122. } );
  123. describe( 'integration (pixels)', () => {
  124. describe( 'aligned widget', () => {
  125. beforeEach( () => {
  126. createResizer();
  127. } );
  128. it( 'properly sets the state for subsequent resizes', () => {
  129. const usedResizer = 'top-right';
  130. const domParts = getWidgetDomParts( widget, usedResizer );
  131. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  132. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 50, 0 );
  133. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  134. sinon.assert.calledWithExactly( commitStub.firstCall, '150px' );
  135. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 50, 0 );
  136. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  137. sinon.assert.calledWithExactly( commitStub.secondCall, '200px' );
  138. expect( commitStub.callCount ).to.be.equal( 2 );
  139. } );
  140. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  141. usedHandle: 'bottom-left',
  142. movePointerBy: { x: 20, y: -10 },
  143. expectedWidth: '80px'
  144. } ) );
  145. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  146. usedHandle: 'bottom-right',
  147. movePointerBy: { x: -20, y: -10 },
  148. expectedWidth: '80px'
  149. } ) );
  150. it( 'shrinks correctly with left-top handler', generateResizeTest( {
  151. usedHandle: 'top-left',
  152. movePointerBy: { x: 20, y: 10 },
  153. expectedWidth: '80px'
  154. } ) );
  155. it( 'shrinks correctly with right-top handler', generateResizeTest( {
  156. usedHandle: 'top-right',
  157. movePointerBy: { x: -20, y: 10 },
  158. expectedWidth: '80px'
  159. } ) );
  160. it( 'enlarges correctly with left-bottom handler', generateResizeTest( {
  161. usedHandle: 'bottom-left',
  162. movePointerBy: { x: -10, y: 10 },
  163. expectedWidth: '120px'
  164. } ) );
  165. it( 'enlarges correctly with right-bottom handler', generateResizeTest( {
  166. usedHandle: 'bottom-right',
  167. movePointerBy: { x: 10, y: 10 },
  168. expectedWidth: '120px'
  169. } ) );
  170. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  171. usedHandle: 'bottom-right',
  172. movePointerBy: { x: 0, y: 20 },
  173. expectedWidth: '140px'
  174. } ) );
  175. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  176. usedHandle: 'bottom-right',
  177. movePointerBy: { x: 40, y: 0 },
  178. expectedWidth: '140px'
  179. } ) );
  180. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  181. usedHandle: 'top-left',
  182. movePointerBy: { x: -20, y: -10 },
  183. expectedWidth: '120px'
  184. } ) );
  185. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  186. usedHandle: 'top-right',
  187. movePointerBy: { x: 20, y: 10 },
  188. expectedWidth: '120px'
  189. } ) );
  190. } );
  191. describe( 'centered widget', () => {
  192. beforeEach( () => {
  193. createResizer( {
  194. isCentered: () => true
  195. } );
  196. } );
  197. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  198. usedHandle: 'bottom-left',
  199. movePointerBy: { x: 10, y: -10 },
  200. expectedWidth: '80px'
  201. } ) );
  202. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  203. usedHandle: 'bottom-right',
  204. movePointerBy: { x: -10, y: -10 },
  205. expectedWidth: '80px'
  206. } ) );
  207. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  208. usedHandle: 'bottom-right',
  209. movePointerBy: { x: 10, y: 0 },
  210. expectedWidth: '120px'
  211. } ) );
  212. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  213. usedHandle: 'bottom-right',
  214. movePointerBy: { x: 0, y: 10 },
  215. expectedWidth: '120px'
  216. } ) );
  217. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  218. usedHandle: 'bottom-left',
  219. movePointerBy: { x: -10, y: 0 },
  220. expectedWidth: '120px'
  221. } ) );
  222. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  223. usedHandle: 'bottom-left',
  224. movePointerBy: { x: 0, y: 10 },
  225. expectedWidth: '120px'
  226. } ) );
  227. // --- top handlers ---
  228. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  229. usedHandle: 'top-left',
  230. movePointerBy: { x: -10, y: -10 },
  231. expectedWidth: '120px'
  232. } ) );
  233. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  234. usedHandle: 'top-left',
  235. movePointerBy: { x: 0, y: -10 },
  236. expectedWidth: '120px'
  237. } ) );
  238. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  239. usedHandle: 'top-right',
  240. movePointerBy: { x: 10, y: -10 },
  241. expectedWidth: '120px'
  242. } ) );
  243. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  244. usedHandle: 'top-right',
  245. movePointerBy: { x: 0, y: -10 },
  246. expectedWidth: '120px'
  247. } ) );
  248. } );
  249. } );
  250. describe( 'integration (percents)', () => {
  251. describe( 'side aligned widget', () => {
  252. beforeEach( () => {
  253. createResizer( {
  254. unit: undefined
  255. } );
  256. } );
  257. it( 'properly sets the state for subsequent resizes', () => {
  258. const usedResizer = 'top-right';
  259. const domParts = getWidgetDomParts( widget, usedResizer );
  260. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  261. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  262. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  263. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  264. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  265. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  266. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  267. expect( commitStub.callCount ).to.be.equal( 2 );
  268. } );
  269. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  270. usedHandle: 'bottom-left',
  271. movePointerBy: { x: 10, y: -10 },
  272. expectedWidth: '22.5%'
  273. } ) );
  274. } );
  275. describe( 'centered widget', () => {
  276. beforeEach( () => {
  277. createResizer( {
  278. isCentered: () => true,
  279. unit: undefined
  280. } );
  281. } );
  282. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  283. usedHandle: 'bottom-left',
  284. movePointerBy: { x: 10, y: -10 },
  285. expectedWidth: '20%'
  286. } ) );
  287. it( 'enlarges correctly with bottom-right handler', generateResizeTest( {
  288. usedHandle: 'bottom-right',
  289. movePointerBy: { x: 0, y: 5 },
  290. expectedWidth: '27.5%'
  291. } ) );
  292. it( 'enlarges correctly an image with unsupported width unit', () => {
  293. editor.editing.view.change( writer => {
  294. writer.setStyle( 'width', '100pt', widget );
  295. } );
  296. generateResizeTest( {
  297. usedHandle: 'bottom-right',
  298. movePointerBy: { x: 0, y: 5 },
  299. expectedWidth: '36.67%'
  300. } )();
  301. } );
  302. } );
  303. } );
  304. /**
  305. * @param {Object} options
  306. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  307. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  308. * @param {String} options.expectedWidth
  309. */
  310. function generateResizeTest( options ) {
  311. return () => {
  312. options = options || {};
  313. const usedHandle = options.usedHandle;
  314. const domParts = getWidgetDomParts( widget, usedHandle );
  315. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  316. const pointerDifference = options.movePointerBy;
  317. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  318. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  319. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  320. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.be.equal( options.expectedWidth );
  321. sinon.assert.calledOnce( commitStub );
  322. };
  323. }
  324. function createEditor( element ) {
  325. return ClassicEditor
  326. .create( element, {
  327. plugins: [
  328. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  329. ]
  330. } );
  331. function simpleWidgetPlugin( editor ) {
  332. editor.model.schema.register( 'widget', {
  333. inheritAllFrom: '$block',
  334. isObject: true
  335. } );
  336. editor.conversion.for( 'downcast' )
  337. .elementToElement( {
  338. model: 'widget',
  339. view: ( modelItem, viewWriter ) => {
  340. const div = viewWriter.createContainerElement( 'div' );
  341. viewWriter.setStyle( 'height', '50px', div );
  342. viewWriter.setStyle( 'width', '25%', div ); // It evaluates to 100px.
  343. return toWidget( div, viewWriter, {
  344. label: 'element label'
  345. } );
  346. }
  347. } );
  348. }
  349. }
  350. function createEditorElement() {
  351. const element = document.createElement( 'div' );
  352. document.body.appendChild( element );
  353. return element;
  354. }
  355. function createResizer( resizerOptions ) {
  356. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  357. const defaultOptions = {
  358. unit: 'px',
  359. modelElement: widgetModel,
  360. viewElement: widget,
  361. editor,
  362. isCentered: () => false,
  363. getHandleHost( domWidgetElement ) {
  364. return domWidgetElement;
  365. },
  366. getResizeHost( domWidgetElement ) {
  367. return domWidgetElement;
  368. },
  369. onCommit: commitStub
  370. };
  371. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  372. }
  373. function getWidgetDomParts( widget, resizerPosition ) {
  374. const view = editor.editing.view;
  375. const resizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 0 ) );
  376. return {
  377. resizeWrapper,
  378. resizeHandle: resizeWrapper.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` ),
  379. widget: view.domConverter.mapViewToDom( widget )
  380. };
  381. }
  382. /**
  383. * Returns a center point for a given handle.
  384. *
  385. * @param {HTMLElement} domWrapper Wrapper of an element that contains the resizer.
  386. * @param {String} [handlePosition='top-left']
  387. * @returns {Point}
  388. */
  389. function getHandleCenterPoint( domWrapper, handlePosition ) {
  390. const wrapperRect = new Rect( domWrapper );
  391. const returnValue = new Point( wrapperRect.left, wrapperRect.top );
  392. const cornerPositionParts = handlePosition.split( '-' );
  393. if ( cornerPositionParts.includes( 'right' ) ) {
  394. returnValue.x = wrapperRect.right;
  395. }
  396. if ( cornerPositionParts.includes( 'bottom' ) ) {
  397. returnValue.y = wrapperRect.bottom;
  398. }
  399. return returnValue;
  400. }
  401. function focusEditor( editor ) {
  402. editor.editing.view.focus();
  403. editor.ui.focusTracker.isFocused = true;
  404. }
  405. } );