widgetresize.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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( 'returns proper value when resize host is different from widget wrapper', () => {
  292. createResizer( {
  293. unit: undefined,
  294. getHandleHost( domWidgetElement ) {
  295. return domWidgetElement.querySelector( '.sub-div' );
  296. }
  297. } );
  298. const usedHandle = 'bottom-right';
  299. const pointerDifference = { x: -10, y: -10 };
  300. const expectedWidth = '20%';
  301. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  302. const resizeHost = domParts.widget.querySelector( '.sub-div' );
  303. const initialPointerPosition = getHandleCenterPoint( resizeHost, usedHandle );
  304. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  305. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  306. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  307. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.be.equal( expectedWidth );
  308. sinon.assert.calledOnce( commitStub );
  309. } );
  310. it( 'doesn\'t break if the widget wrapper was removed from DOM', () => {
  311. const resizer = createResizer( {
  312. unit: undefined
  313. } );
  314. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  315. domParts.resizeWrapper.remove();
  316. resizer.redraw();
  317. } );
  318. } );
  319. describe( 'attachTo()', () => {
  320. it( 'works without WidgetToolbarRepository plugin', async () => {
  321. const localEditorElement = createEditorElement();
  322. const localEditor = await ClassicEditor.create( localEditorElement, {
  323. plugins: [
  324. WidgetResize, simpleWidgetPlugin
  325. ]
  326. } );
  327. setModelData( localEditor.model, '[<widget></widget>]' );
  328. const resizerOptions = {
  329. modelElement: localEditor.model.document.getRoot().getChild( 0 ),
  330. viewElement: localEditor.editing.view.document.getRoot().getChild( 0 ),
  331. editor: localEditor,
  332. isCentered: () => false,
  333. getHandleHost( domWidgetElement ) {
  334. return domWidgetElement;
  335. },
  336. getResizeHost( domWidgetElement ) {
  337. return domWidgetElement;
  338. },
  339. onCommit: commitStub
  340. };
  341. localEditor.plugins.get( WidgetResize ).attachTo( resizerOptions );
  342. // Nothing should be thrown.
  343. // And clean up.
  344. localEditorElement.remove();
  345. return localEditor.destroy();
  346. } );
  347. } );
  348. describe( 'init()', () => {
  349. it( 'adds listener to redraw resizer on visible resizer change', async () => {
  350. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  351. widget = editor.editing.view.document.getRoot().getChild( 0 );
  352. const resizer = createResizer();
  353. const redrawSpy = sinon.spy( resizer, 'redraw' );
  354. focusEditor( editor );
  355. editor.model.change( writer => {
  356. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  357. writer.setSelection( widgetModel, 'on' );
  358. } );
  359. expect( redrawSpy.callCount ).to.be.equal( 1 );
  360. } );
  361. } );
  362. describe( '_getResizerByHandle()', () => {
  363. it( 'returns properly in case of invalid handle element', () => {
  364. const randomElement = document.createElement( 'span' );
  365. const plugin = editor.plugins.get( WidgetResize );
  366. createResizer();
  367. expect( plugin._getResizerByHandle( randomElement ) ).to.be.undefined;
  368. } );
  369. } );
  370. /**
  371. * @param {Object} options
  372. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  373. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  374. * @param {String} options.expectedWidth
  375. */
  376. function generateResizeTest( options ) {
  377. return () => {
  378. options = options || {};
  379. const usedHandle = options.usedHandle;
  380. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  381. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  382. const pointerDifference = options.movePointerBy;
  383. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  384. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  385. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  386. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.be.equal( options.expectedWidth );
  387. sinon.assert.calledOnce( commitStub );
  388. };
  389. }
  390. function createEditor( element ) {
  391. return ClassicEditor
  392. .create( element, {
  393. plugins: [
  394. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  395. ]
  396. } );
  397. }
  398. function simpleWidgetPlugin( editor ) {
  399. editor.model.schema.register( 'widget', {
  400. inheritAllFrom: '$block',
  401. isObject: true
  402. } );
  403. editor.conversion.for( 'downcast' )
  404. .elementToElement( {
  405. model: 'widget',
  406. view: ( modelItem, viewWriter ) => {
  407. const parentDiv = viewWriter.createContainerElement( 'div' );
  408. viewWriter.setStyle( 'height', '50px', parentDiv );
  409. viewWriter.setStyle( 'width', '25%', parentDiv ); // It evaluates to 100px.
  410. const subDiv = viewWriter.createContainerElement( 'div' );
  411. viewWriter.insert( viewWriter.createPositionAt( subDiv, 'start' ), viewWriter.createText( 'foo' ) );
  412. viewWriter.addClass( 'sub-div', subDiv );
  413. viewWriter.setStyle( 'height', '20px', subDiv );
  414. viewWriter.setStyle( 'width', '50px', subDiv );
  415. viewWriter.insert( viewWriter.createPositionAt( parentDiv, 'start' ), subDiv );
  416. return toWidget( parentDiv, viewWriter, {
  417. label: 'element label'
  418. } );
  419. }
  420. } );
  421. }
  422. function createEditorElement() {
  423. const element = document.createElement( 'div' );
  424. document.body.appendChild( element );
  425. return element;
  426. }
  427. function createResizer( resizerOptions ) {
  428. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  429. const defaultOptions = {
  430. unit: 'px',
  431. modelElement: widgetModel,
  432. viewElement: widget,
  433. editor,
  434. isCentered: () => false,
  435. getHandleHost( domWidgetElement ) {
  436. return domWidgetElement;
  437. },
  438. getResizeHost( domWidgetElement ) {
  439. return domWidgetElement;
  440. },
  441. onCommit: commitStub
  442. };
  443. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  444. }
  445. } );