resize.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, window */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import ImagePlugin from '../../src/image';
  9. import {
  10. getData
  11. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'Image resizer', () => {
  13. const FIXTURE_WIDTH = 60;
  14. const FIXTURE_HEIGHT = 30;
  15. const MOUSE_BUTTON_MAIN = 0; // Id of left mouse button.
  16. // 60x30 black png image
  17. const imageFixture = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAAeCAQAAADakbXEAAAAKElEQVR42u3NMQEAAAgDoK1/' +
  18. 'aI2hBxSgmZyoWCwWi8VisVgsFov/xguoPx4B+tNoGwAAAABJRU5ErkJggg==';
  19. let editor, view, viewDocument, editorElement;
  20. beforeEach( () => {
  21. editorElement = document.createElement( 'div' );
  22. editorElement.innerHTML = `<p>foo</p><figure><img src="${ imageFixture }"></figure>`;
  23. document.body.appendChild( editorElement );
  24. return ClassicTestEditor
  25. .create( editorElement, {
  26. plugins: [ ImagePlugin, ParagraphPlugin ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. view = editor.editing.view;
  31. viewDocument = view.document;
  32. } );
  33. } );
  34. afterEach( () => {
  35. editorElement.remove();
  36. return editor.destroy();
  37. } );
  38. describe( 'visual resizers', () => {
  39. it( 'correct amount is added by default', () => {
  40. const resizers = document.querySelectorAll( '.ck-widget__resizer' );
  41. expect( resizers.length ).to.be.equal( 4 );
  42. } );
  43. describe( 'visibility', () => {
  44. it( 'is hidden by default', () => {
  45. const allResizers = document.querySelectorAll( '.ck-widget__resizer' );
  46. for ( const resizer of allResizers ) {
  47. expect( isVisible( resizer ) ).to.be.false;
  48. }
  49. } );
  50. it( 'is shown when image is focused', () => {
  51. const widget = viewDocument.getRoot().getChild( 1 );
  52. const allResizers = document.querySelectorAll( '.ck-widget__resizer' );
  53. const domEventDataMock = {
  54. target: widget,
  55. preventDefault: sinon.spy()
  56. };
  57. viewDocument.fire( 'mousedown', domEventDataMock );
  58. for ( const resizer of allResizers ) {
  59. expect( isVisible( resizer ) ).to.be.true;
  60. }
  61. } );
  62. } );
  63. } );
  64. describe( 'standard image', () => {
  65. it( 'works', () => {} );
  66. } );
  67. describe( 'side image', () => {
  68. let widget;
  69. beforeEach( async () => {
  70. await editor.setData( `<p>foo</p><figure class="image image-style-side"><img src="${ imageFixture }"></figure>` );
  71. widget = viewDocument.getRoot().getChild( 1 );
  72. const domEventDataMock = {
  73. target: widget,
  74. preventDefault: sinon.spy()
  75. };
  76. viewDocument.fire( 'mousedown', domEventDataMock );
  77. } );
  78. it.only( 'shrinks correctly with left-bottom handler', () => {
  79. const expectedWidth = 50;
  80. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  81. const domBottomLeftResizer = domResizeWrapper.querySelector( '.ck-widget__resizer-bottom-left' );
  82. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  83. const imageTopLeftPosition = getElementPosition( domImage );
  84. const initialPointerPosition = {
  85. pageX: imageTopLeftPosition.x,
  86. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT
  87. };
  88. const finishPointerPosition = {
  89. pageX: imageTopLeftPosition.x + 20,
  90. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT - 10
  91. };
  92. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  93. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  94. // We need to wait as mousemove events are throttled.
  95. return wait( 30 )
  96. .then( () => {
  97. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  98. expect( domImage.width ).to.be.equal( expectedWidth );
  99. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  100. expect( getData( editor.model, {
  101. withoutSelection: true
  102. } ) ).to.equal( `<paragraph>foo</paragraph><image src="${ imageFixture }" width="${ expectedWidth }"></image>` );
  103. } );
  104. } );
  105. it.only( 'shrinks correctly with right-bottom handler', () => {
  106. const expectedWidth = 50;
  107. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  108. const domBottomLeftResizer = domResizeWrapper.querySelector( '.ck-widget__resizer-bottom-left' );
  109. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  110. const imageTopLeftPosition = getElementPosition( domImage );
  111. const initialPointerPosition = {
  112. pageX: imageTopLeftPosition.x + FIXTURE_WIDTH,
  113. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT
  114. };
  115. const finishPointerPosition = {
  116. pageX: imageTopLeftPosition.x + FIXTURE_WIDTH - 20,
  117. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT - 10
  118. };
  119. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  120. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  121. // We need to wait as mousemove events are throttled.
  122. return wait( 30 )
  123. .then( () => {
  124. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  125. expect( domImage.width ).to.be.equal( expectedWidth );
  126. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  127. expect( getData( editor.model, {
  128. withoutSelection: true
  129. } ) ).to.equal( `<paragraph>foo</paragraph><image src="${ imageFixture }" width="${ expectedWidth }"></image>` );
  130. } );
  131. } );
  132. } );
  133. function isVisible( element ) {
  134. // Checks if the DOM element is visible to the end user.
  135. return element.offsetParent !== null;
  136. }
  137. function fireMouseEvent( target, eventType, eventData ) {
  138. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  139. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  140. const event = document.createEvent( 'MouseEvent' );
  141. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  142. MOUSE_BUTTON_MAIN, null );
  143. target.dispatchEvent( event );
  144. }
  145. function getElementPosition( element ) {
  146. // Returns top left corner point.
  147. const viewportPosition = element.getBoundingClientRect();
  148. return {
  149. x: viewportPosition.left + window.scrollX,
  150. y: viewportPosition.top + window.scrollY
  151. };
  152. }
  153. function wait( delay ) {
  154. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  155. }
  156. } );