resize.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = 100;
  14. const FIXTURE_HEIGHT = 50;
  15. const MOUSE_BUTTON_MAIN = 0; // Id of left mouse button.
  16. // 60x30 black png image
  17. const imageFixture = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  18. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  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. let widget;
  66. beforeEach( async () => {
  67. await editor.setData( `<p>foo</p><figure class="image image-style-side"><img src="${ imageFixture }"></figure>` );
  68. widget = viewDocument.getRoot().getChild( 1 );
  69. const domEventDataMock = {
  70. target: widget,
  71. preventDefault: sinon.spy()
  72. };
  73. viewDocument.fire( 'mousedown', domEventDataMock );
  74. } );
  75. } );
  76. describe( 'side image', () => {
  77. let widget;
  78. beforeEach( async () => {
  79. await editor.setData( `<p>foo</p><figure class="image image-style-side"><img src="${ imageFixture }"></figure>` );
  80. widget = viewDocument.getRoot().getChild( 1 );
  81. const domEventDataMock = {
  82. target: widget,
  83. preventDefault: sinon.spy()
  84. };
  85. viewDocument.fire( 'mousedown', domEventDataMock );
  86. } );
  87. it( 'shrinks correctly with left-bottom handler', () => {
  88. const expectedWidth = 80;
  89. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  90. const domBottomLeftResizer = domResizeWrapper.querySelector( '.ck-widget__resizer-bottom-left' );
  91. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  92. const imageTopLeftPosition = getElementPosition( domImage );
  93. const initialPointerPosition = {
  94. pageX: imageTopLeftPosition.x,
  95. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT
  96. };
  97. const finishPointerPosition = {
  98. pageX: imageTopLeftPosition.x + 20,
  99. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT - 10
  100. };
  101. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  102. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  103. // We need to wait as mousemove events are throttled.
  104. return wait( 30 )
  105. .then( () => {
  106. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  107. expect( domImage.width ).to.be.closeTo( 80, 1, 'View width check' );
  108. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  109. expect( getData( editor.model, {
  110. withoutSelection: true
  111. } ) ).to.match( /<paragraph>foo<\/paragraph><image src=".+?" width="(\d+)"><\/image>/ );
  112. const modelItem = editor.model.document.getRoot().getChild( 1 );
  113. expect( modelItem.getAttribute( 'width' ) ).to.be.closeTo( expectedWidth, 1, 'Model check' );
  114. } );
  115. } );
  116. it( 'shrinks correctly with right-bottom handler', () => {
  117. const expectedWidth = 80;
  118. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  119. const domBottomLeftResizer = domResizeWrapper.querySelector( '.ck-widget__resizer-bottom-left' );
  120. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  121. const imageTopLeftPosition = getElementPosition( domImage );
  122. const initialPointerPosition = {
  123. pageX: imageTopLeftPosition.x + FIXTURE_WIDTH,
  124. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT
  125. };
  126. const finishPointerPosition = {
  127. pageX: imageTopLeftPosition.x + FIXTURE_WIDTH - 20,
  128. pageY: imageTopLeftPosition.y + FIXTURE_HEIGHT - 10
  129. };
  130. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  131. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  132. // We need to wait as mousemove events are throttled.
  133. return wait( 30 )
  134. .then( () => {
  135. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  136. expect( domImage.width ).to.be.closeTo( expectedWidth, 1 );
  137. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  138. expect( getData( editor.model, {
  139. withoutSelection: true
  140. } ) ).to.equal( `<paragraph>foo</paragraph><image src="${ imageFixture }" width="${ expectedWidth }"></image>` );
  141. } );
  142. } );
  143. } );
  144. function isVisible( element ) {
  145. // Checks if the DOM element is visible to the end user.
  146. return element.offsetParent !== null;
  147. }
  148. function fireMouseEvent( target, eventType, eventData ) {
  149. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  150. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  151. // However there's still a problem, that events created with `initMouseEvent` have **floored** pageX, pageY numbers.
  152. const event = document.createEvent( 'MouseEvent' );
  153. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  154. MOUSE_BUTTON_MAIN, null );
  155. target.dispatchEvent( event );
  156. }
  157. function getElementPosition( element ) {
  158. // Returns top left corner point.
  159. const viewportPosition = element.getBoundingClientRect();
  160. return {
  161. x: viewportPosition.left + window.scrollX,
  162. y: viewportPosition.top + window.scrollY
  163. };
  164. }
  165. function wait( delay ) {
  166. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  167. }
  168. } );