8
0

widgetresize.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 { resizerMouseSimulator, focusEditor, getHandleCenterPoint, getWidgetDomParts } from './widgetresize/_utils/utils';
  13. describe( 'WidgetResize', () => {
  14. let editor, editorElement, widget, mouseListenerSpies, commitStub;
  15. beforeEach( async () => {
  16. editorElement = createEditorElement();
  17. editor = await createEditor( editorElement );
  18. setModelData( editor.model, '[<widget></widget>]' );
  19. focusEditor( editor );
  20. widget = editor.editing.view.document.getRoot().getChild( 0 );
  21. // It's crucial to have a precisely defined editor size for this test suite.
  22. editor.editing.view.change( writer => {
  23. const viewEditableRoot = editor.editing.view.document.getRoot();
  24. writer.setAttribute( 'style', 'width: 400px; padding: 0px; overflow: hidden', viewEditableRoot );
  25. } );
  26. commitStub = sinon.stub();
  27. mouseListenerSpies = {
  28. down: sinon.spy( WidgetResize.prototype, '_mouseDownListener' ),
  29. move: sinon.spy( WidgetResize.prototype, '_mouseMoveListener' ),
  30. up: sinon.spy( WidgetResize.prototype, '_mouseUpListener' )
  31. };
  32. } );
  33. afterEach( () => {
  34. editorElement.remove();
  35. for ( const stub of Object.values( mouseListenerSpies ) ) {
  36. stub.restore();
  37. }
  38. if ( editor ) {
  39. return editor.destroy();
  40. }
  41. } );
  42. describe( 'plugin', () => {
  43. it( 'is loaded', () => {
  44. expect( editor.plugins.get( WidgetResize ) ).to.be.instanceOf( WidgetResize );
  45. } );
  46. } );
  47. describe( 'mouse listeners', () => {
  48. beforeEach( () => {
  49. createResizer();
  50. } );
  51. it( 'don\'t break when called with unexpected element', () => {
  52. const unrelatedElement = document.createElement( 'div' );
  53. editor.plugins.get( WidgetResize )._mouseDownListener( {}, {
  54. target: unrelatedElement
  55. } );
  56. } );
  57. it( 'passes new width to the options.onCommit()', () => {
  58. const usedResizer = 'top-right';
  59. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  60. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  61. const finalPointerPosition = initialPointerPosition.clone().moveBy( 20, 0 );
  62. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  63. expect( commitStub.callCount ).to.be.equal( 1 );
  64. sinon.assert.calledWithExactly( commitStub, '120px' );
  65. } );
  66. it( 'are detached when plugin is destroyed', async () => {
  67. await editor.destroy();
  68. const plugin = editor.plugins.get( WidgetResize );
  69. editor = null;
  70. const event = new Event( 'mousedown', { bubbles: true } );
  71. document.body.dispatchEvent( event );
  72. // Ensure nothing got called.
  73. expect( plugin._mouseDownListener.callCount ).to.be.equal( 0 );
  74. } );
  75. it( 'nothing bad happens if activeResizer got unset', () => {
  76. createResizer( {
  77. isCentered: () => true
  78. } );
  79. const usedResizer = 'top-right';
  80. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  81. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  82. editor.plugins.get( WidgetResize )._getResizerByHandle = sinon.stub().returns( null );
  83. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, initialPointerPosition );
  84. // No exception should be thrown.
  85. } );
  86. } );
  87. describe( 'visibility', () => {
  88. beforeEach( () => {
  89. createResizer();
  90. } );
  91. it( 'it\'s hidden when no widget is focused', () => {
  92. // This particular test needs a paragraph, so that widget is no longer focused.
  93. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  94. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  95. for ( const resizer of allResizers ) {
  96. expect( isVisible( resizer ) ).to.be.false;
  97. }
  98. } );
  99. it( 'it\'s visible once the widget is focused', () => {
  100. // Widget is focused by default.
  101. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  102. for ( const resizer of allResizers ) {
  103. expect( isVisible( resizer ) ).to.be.true;
  104. }
  105. } );
  106. function isVisible( element ) {
  107. // Checks if the DOM element is visible to the end user.
  108. return element.offsetParent !== null && !element.classList.contains( 'ck-hidden' );
  109. }
  110. } );
  111. describe( 'integration (pixels)', () => {
  112. describe( 'aligned widget', () => {
  113. beforeEach( () => {
  114. createResizer();
  115. } );
  116. it( 'properly sets the state for subsequent resizes', () => {
  117. const usedResizer = 'top-right';
  118. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  119. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  120. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 50, 0 );
  121. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  122. sinon.assert.calledWithExactly( commitStub.firstCall, '150px' );
  123. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 50, 0 );
  124. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  125. sinon.assert.calledWithExactly( commitStub.secondCall, '200px' );
  126. expect( commitStub.callCount ).to.be.equal( 2 );
  127. } );
  128. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  129. usedHandle: 'bottom-left',
  130. movePointerBy: { x: 20, y: -10 },
  131. expectedWidth: '80px'
  132. } ) );
  133. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  134. usedHandle: 'bottom-right',
  135. movePointerBy: { x: -20, y: -10 },
  136. expectedWidth: '80px'
  137. } ) );
  138. it( 'shrinks correctly with left-top handler', generateResizeTest( {
  139. usedHandle: 'top-left',
  140. movePointerBy: { x: 20, y: 10 },
  141. expectedWidth: '80px'
  142. } ) );
  143. it( 'shrinks correctly with right-top handler', generateResizeTest( {
  144. usedHandle: 'top-right',
  145. movePointerBy: { x: -20, y: 10 },
  146. expectedWidth: '80px'
  147. } ) );
  148. it( 'enlarges correctly with left-bottom handler', generateResizeTest( {
  149. usedHandle: 'bottom-left',
  150. movePointerBy: { x: -10, y: 10 },
  151. expectedWidth: '120px'
  152. } ) );
  153. it( 'enlarges correctly with right-bottom handler', generateResizeTest( {
  154. usedHandle: 'bottom-right',
  155. movePointerBy: { x: 10, y: 10 },
  156. expectedWidth: '120px'
  157. } ) );
  158. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  159. usedHandle: 'bottom-right',
  160. movePointerBy: { x: 0, y: 20 },
  161. expectedWidth: '140px'
  162. } ) );
  163. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  164. usedHandle: 'bottom-right',
  165. movePointerBy: { x: 40, y: 0 },
  166. expectedWidth: '140px'
  167. } ) );
  168. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  169. usedHandle: 'top-left',
  170. movePointerBy: { x: -20, y: -10 },
  171. expectedWidth: '120px'
  172. } ) );
  173. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  174. usedHandle: 'top-right',
  175. movePointerBy: { x: 20, y: 10 },
  176. expectedWidth: '120px'
  177. } ) );
  178. } );
  179. describe( 'centered widget', () => {
  180. beforeEach( () => {
  181. createResizer( {
  182. isCentered: () => true
  183. } );
  184. } );
  185. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  186. usedHandle: 'bottom-left',
  187. movePointerBy: { x: 10, y: -10 },
  188. expectedWidth: '80px'
  189. } ) );
  190. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  191. usedHandle: 'bottom-right',
  192. movePointerBy: { x: -10, y: -10 },
  193. expectedWidth: '80px'
  194. } ) );
  195. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  196. usedHandle: 'bottom-right',
  197. movePointerBy: { x: 10, y: 0 },
  198. expectedWidth: '120px'
  199. } ) );
  200. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  201. usedHandle: 'bottom-right',
  202. movePointerBy: { x: 0, y: 10 },
  203. expectedWidth: '120px'
  204. } ) );
  205. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  206. usedHandle: 'bottom-left',
  207. movePointerBy: { x: -10, y: 0 },
  208. expectedWidth: '120px'
  209. } ) );
  210. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  211. usedHandle: 'bottom-left',
  212. movePointerBy: { x: 0, y: 10 },
  213. expectedWidth: '120px'
  214. } ) );
  215. // --- top handlers ---
  216. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  217. usedHandle: 'top-left',
  218. movePointerBy: { x: -10, y: -10 },
  219. expectedWidth: '120px'
  220. } ) );
  221. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  222. usedHandle: 'top-left',
  223. movePointerBy: { x: 0, y: -10 },
  224. expectedWidth: '120px'
  225. } ) );
  226. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  227. usedHandle: 'top-right',
  228. movePointerBy: { x: 10, y: -10 },
  229. expectedWidth: '120px'
  230. } ) );
  231. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  232. usedHandle: 'top-right',
  233. movePointerBy: { x: 0, y: -10 },
  234. expectedWidth: '120px'
  235. } ) );
  236. } );
  237. } );
  238. describe( 'integration (percents)', () => {
  239. describe( 'side aligned widget', () => {
  240. beforeEach( () => {
  241. createResizer( {
  242. unit: undefined
  243. } );
  244. } );
  245. it( 'properly sets the state for subsequent resizes', () => {
  246. const usedResizer = 'top-right';
  247. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  248. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  249. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  250. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  251. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  252. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  253. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  254. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  255. expect( commitStub.callCount ).to.be.equal( 2 );
  256. } );
  257. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  258. usedHandle: 'bottom-left',
  259. movePointerBy: { x: 10, y: -10 },
  260. expectedWidth: '22.5%'
  261. } ) );
  262. } );
  263. describe( 'centered widget', () => {
  264. beforeEach( () => {
  265. createResizer( {
  266. isCentered: () => true,
  267. unit: undefined
  268. } );
  269. } );
  270. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  271. usedHandle: 'bottom-left',
  272. movePointerBy: { x: 10, y: -10 },
  273. expectedWidth: '20%'
  274. } ) );
  275. it( 'enlarges correctly with bottom-right handler', generateResizeTest( {
  276. usedHandle: 'bottom-right',
  277. movePointerBy: { x: 0, y: 5 },
  278. expectedWidth: '27.5%'
  279. } ) );
  280. it( 'enlarges correctly an image with unsupported width unit', () => {
  281. editor.editing.view.change( writer => {
  282. writer.setStyle( 'width', '100pt', widget );
  283. } );
  284. generateResizeTest( {
  285. usedHandle: 'bottom-right',
  286. movePointerBy: { x: 0, y: 5 },
  287. expectedWidth: '36.67%'
  288. } )();
  289. } );
  290. } );
  291. it( 'doesn\'t call options.onCommit() in case of no change', () => {
  292. const commitStub = sinon.stub();
  293. createResizer( {
  294. onCommit: commitStub
  295. } );
  296. const domParts = getWidgetDomParts( editor, widget, 'top-left' );
  297. resizerMouseSimulator.down( editor, domParts.resizeHandle );
  298. resizerMouseSimulator.up( editor );
  299. expect( commitStub.callCount, 'call count' ).to.be.eql( 0 );
  300. } );
  301. it( 'restores the original view width after resize is done', () => {
  302. // Note that ultimately width should be changed, but through a model converter, not with direct view changes (#6060).
  303. createResizer();
  304. const usedHandle = 'bottom-right';
  305. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  306. const finalPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 40, 40 );
  307. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  308. expect( widget.getStyle( 'width' ) ).to.be.equal( '25%' );
  309. } );
  310. it( 'returns proper value when resize host is different from widget wrapper', () => {
  311. createResizer( {
  312. unit: undefined,
  313. getHandleHost( domWidgetElement ) {
  314. return domWidgetElement.querySelector( '.sub-div' );
  315. }
  316. } );
  317. const usedHandle = 'bottom-right';
  318. const pointerDifference = { x: -10, y: -10 };
  319. const expectedWidth = '20%';
  320. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  321. const resizeHost = domParts.widget.querySelector( '.sub-div' );
  322. const initialPointerPosition = getHandleCenterPoint( resizeHost, usedHandle );
  323. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  324. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  325. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  326. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.be.equal( expectedWidth );
  327. sinon.assert.calledOnce( commitStub );
  328. } );
  329. it( 'doesn\'t break if the widget wrapper was removed from DOM', () => {
  330. const resizer = createResizer( {
  331. unit: undefined
  332. } );
  333. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  334. domParts.resizeWrapper.remove();
  335. resizer.redraw();
  336. } );
  337. } );
  338. describe( 'attachTo()', () => {
  339. it( 'works without WidgetToolbarRepository plugin', async () => {
  340. const localEditorElement = createEditorElement();
  341. const localEditor = await ClassicEditor.create( localEditorElement, {
  342. plugins: [
  343. WidgetResize, simpleWidgetPlugin
  344. ]
  345. } );
  346. setModelData( localEditor.model, '[<widget></widget>]' );
  347. const resizerOptions = {
  348. modelElement: localEditor.model.document.getRoot().getChild( 0 ),
  349. viewElement: localEditor.editing.view.document.getRoot().getChild( 0 ),
  350. editor: localEditor,
  351. isCentered: () => false,
  352. getHandleHost( domWidgetElement ) {
  353. return domWidgetElement;
  354. },
  355. getResizeHost( domWidgetElement ) {
  356. return domWidgetElement;
  357. },
  358. onCommit: commitStub
  359. };
  360. localEditor.plugins.get( WidgetResize ).attachTo( resizerOptions );
  361. // Nothing should be thrown.
  362. // And clean up.
  363. localEditorElement.remove();
  364. return localEditor.destroy();
  365. } );
  366. } );
  367. describe( 'init()', () => {
  368. it( 'adds listener to redraw resizer on visible resizer change', async () => {
  369. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  370. widget = editor.editing.view.document.getRoot().getChild( 0 );
  371. const resizer = createResizer();
  372. const redrawSpy = sinon.spy( resizer, 'redraw' );
  373. focusEditor( editor );
  374. editor.model.change( writer => {
  375. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  376. writer.setSelection( widgetModel, 'on' );
  377. } );
  378. expect( redrawSpy.callCount ).to.be.equal( 1 );
  379. } );
  380. } );
  381. describe( 'cancel()', () => {
  382. it( 'restores original view width', () => {
  383. const resizer = createResizer();
  384. const usedHandle = 'bottom-right';
  385. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  386. const startingPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 100, 0 );
  387. const domTarget = domParts.resizeHandle;
  388. resizerMouseSimulator.down( editor, domTarget );
  389. resizerMouseSimulator.move( editor, domTarget, {
  390. pageX: startingPosition.x,
  391. pageY: startingPosition.y
  392. } );
  393. resizer.cancel();
  394. // Value should be restored to the initial value (#6060).
  395. expect( widget.getStyle( 'width' ) ).to.be.equal( '25%' );
  396. } );
  397. } );
  398. describe( '_getResizerByHandle()', () => {
  399. it( 'returns properly in case of invalid handle element', () => {
  400. const randomElement = document.createElement( 'span' );
  401. const plugin = editor.plugins.get( WidgetResize );
  402. createResizer();
  403. expect( plugin._getResizerByHandle( randomElement ) ).to.be.undefined;
  404. } );
  405. } );
  406. /**
  407. * @param {Object} options
  408. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  409. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  410. * @param {String} options.expectedWidth
  411. */
  412. function generateResizeTest( options ) {
  413. return () => {
  414. options = options || {};
  415. const usedHandle = options.usedHandle;
  416. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  417. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  418. const pointerDifference = options.movePointerBy;
  419. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  420. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  421. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  422. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.be.equal( options.expectedWidth );
  423. sinon.assert.calledOnce( commitStub );
  424. };
  425. }
  426. function createEditor( element ) {
  427. return ClassicEditor
  428. .create( element, {
  429. plugins: [
  430. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  431. ]
  432. } );
  433. }
  434. function simpleWidgetPlugin( editor ) {
  435. editor.model.schema.register( 'widget', {
  436. inheritAllFrom: '$block',
  437. isObject: true
  438. } );
  439. editor.conversion.for( 'downcast' )
  440. .elementToElement( {
  441. model: 'widget',
  442. view: ( modelItem, viewWriter ) => {
  443. const parentDiv = viewWriter.createContainerElement( 'div' );
  444. viewWriter.setStyle( 'height', '50px', parentDiv );
  445. viewWriter.setStyle( 'width', '25%', parentDiv ); // It evaluates to 100px.
  446. const subDiv = viewWriter.createContainerElement( 'div' );
  447. viewWriter.insert( viewWriter.createPositionAt( subDiv, 'start' ), viewWriter.createText( 'foo' ) );
  448. viewWriter.addClass( 'sub-div', subDiv );
  449. viewWriter.setStyle( 'height', '20px', subDiv );
  450. viewWriter.setStyle( 'width', '50px', subDiv );
  451. viewWriter.insert( viewWriter.createPositionAt( parentDiv, 'start' ), subDiv );
  452. return toWidget( parentDiv, viewWriter, {
  453. label: 'element label'
  454. } );
  455. }
  456. } );
  457. }
  458. function createEditorElement() {
  459. const element = document.createElement( 'div' );
  460. document.body.appendChild( element );
  461. return element;
  462. }
  463. function createResizer( resizerOptions ) {
  464. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  465. const defaultOptions = {
  466. unit: 'px',
  467. modelElement: widgetModel,
  468. viewElement: widget,
  469. editor,
  470. isCentered: () => false,
  471. getHandleHost( domWidgetElement ) {
  472. return domWidgetElement;
  473. },
  474. getResizeHost( domWidgetElement ) {
  475. return domWidgetElement;
  476. },
  477. onCommit: commitStub
  478. };
  479. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  480. }
  481. } );