resize.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 ImageStyle from '../../src/imagestyle';
  10. import {
  11. getData
  12. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe.only( 'Image resizer', () => {
  14. const FIXTURE_WIDTH = 100;
  15. const FIXTURE_HEIGHT = 50;
  16. const MOUSE_BUTTON_MAIN = 0; // Id of left mouse button.
  17. // 60x30 black png image
  18. const imageFixture = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  19. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  20. let editor, view, viewDocument, editorElement;
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. editorElement.innerHTML = `<p>foo</p><figure><img src="${ imageFixture }"></figure>`;
  24. document.body.appendChild( editorElement );
  25. return ClassicTestEditor
  26. .create( editorElement, {
  27. plugins: [ ImagePlugin, ImageStyle, ParagraphPlugin ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. view = editor.editing.view;
  32. viewDocument = view.document;
  33. } );
  34. } );
  35. afterEach( () => {
  36. editorElement.remove();
  37. return editor.destroy();
  38. } );
  39. describe( 'visual resizers', () => {
  40. it( 'correct amount is added by default', () => {
  41. const resizers = document.querySelectorAll( '.ck-widget__resizer' );
  42. expect( resizers.length ).to.be.equal( 4 );
  43. } );
  44. describe( 'visibility', () => {
  45. it( 'is hidden by default', () => {
  46. const allResizers = document.querySelectorAll( '.ck-widget__resizer' );
  47. for ( const resizer of allResizers ) {
  48. expect( isVisible( resizer ) ).to.be.false;
  49. }
  50. } );
  51. it( 'is shown when image is focused', () => {
  52. const widget = viewDocument.getRoot().getChild( 1 );
  53. const allResizers = document.querySelectorAll( '.ck-widget__resizer' );
  54. const domEventDataMock = {
  55. target: widget,
  56. preventDefault: sinon.spy()
  57. };
  58. viewDocument.fire( 'mousedown', domEventDataMock );
  59. for ( const resizer of allResizers ) {
  60. expect( isVisible( resizer ) ).to.be.true;
  61. }
  62. } );
  63. } );
  64. } );
  65. describe( 'standard image', () => {
  66. let widget;
  67. beforeEach( async function() {
  68. await editor.setData( `<p>foo</p><figure><img src="${ imageFixture }"></figure>` );
  69. widget = viewDocument.getRoot().getChild( 1 );
  70. const domEventDataMock = {
  71. target: widget,
  72. preventDefault: sinon.spy()
  73. };
  74. this.widget = widget;
  75. viewDocument.fire( 'mousedown', domEventDataMock );
  76. } );
  77. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  78. expectedWidth: 80,
  79. pointerOffset: {
  80. x: 10,
  81. y: -10
  82. },
  83. resizerPosition: 'bottom-left'
  84. } ) );
  85. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  86. expectedWidth: 80,
  87. pointerOffset: {
  88. x: -10,
  89. y: -10
  90. },
  91. resizerPosition: 'bottom-right'
  92. } ) );
  93. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  94. expectedWidth: 110,
  95. pointerOffset: {
  96. x: 10,
  97. y: 0
  98. },
  99. resizerPosition: 'bottom-right'
  100. } ) );
  101. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  102. expectedWidth: 120,
  103. pointerOffset: {
  104. x: 0,
  105. y: 10
  106. },
  107. resizerPosition: 'bottom-right'
  108. } ) );
  109. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  110. expectedWidth: 110,
  111. pointerOffset: {
  112. x: -10,
  113. y: 0
  114. },
  115. resizerPosition: 'bottom-left'
  116. } ) );
  117. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  118. expectedWidth: 120,
  119. pointerOffset: {
  120. x: 0,
  121. y: 10
  122. },
  123. resizerPosition: 'bottom-left'
  124. } ) );
  125. // --- top handlers ---
  126. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  127. expectedWidth: 120,
  128. pointerOffset: {
  129. x: -10,
  130. y: -10
  131. },
  132. resizerPosition: 'top-left'
  133. } ) );
  134. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  135. expectedWidth: 120,
  136. pointerOffset: {
  137. x: 0,
  138. y: -10
  139. },
  140. resizerPosition: 'top-left'
  141. } ) );
  142. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  143. expectedWidth: 120,
  144. pointerOffset: {
  145. x: 10,
  146. y: -10
  147. },
  148. resizerPosition: 'top-right'
  149. } ) );
  150. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  151. expectedWidth: 120,
  152. pointerOffset: {
  153. x: 0,
  154. y: -10
  155. },
  156. resizerPosition: 'top-right'
  157. } ) );
  158. } );
  159. describe( 'side image', () => {
  160. let widget;
  161. beforeEach( async function() {
  162. await editor.setData( `<p>foo</p><figure class="image image-style-side"><img src="${ imageFixture }"></figure>` );
  163. widget = viewDocument.getRoot().getChild( 1 );
  164. const domEventDataMock = {
  165. target: widget,
  166. preventDefault: sinon.spy()
  167. };
  168. this.widget = widget;
  169. viewDocument.fire( 'mousedown', domEventDataMock );
  170. } );
  171. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  172. isSideImage: true,
  173. expectedWidth: 80,
  174. pointerOffset: {
  175. x: 20,
  176. y: -10
  177. },
  178. resizerPosition: 'bottom-left'
  179. } ) );
  180. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  181. isSideImage: true,
  182. expectedWidth: 80,
  183. pointerOffset: {
  184. x: -20,
  185. y: -10
  186. },
  187. resizerPosition: 'bottom-right'
  188. } ) );
  189. it( 'enlarges correctly with left-bottom handler', generateResizeTest( {
  190. isSideImage: true,
  191. expectedWidth: 120,
  192. pointerOffset: {
  193. x: -10,
  194. y: 10
  195. },
  196. resizerPosition: 'bottom-left'
  197. } ) );
  198. it( 'enlarges correctly with right-bottom handler', generateResizeTest( {
  199. isSideImage: true,
  200. expectedWidth: 120,
  201. pointerOffset: {
  202. x: 10,
  203. y: 10
  204. },
  205. resizerPosition: 'bottom-right'
  206. } ) );
  207. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  208. isSideImage: true,
  209. expectedWidth: 140,
  210. pointerOffset: {
  211. x: 0,
  212. y: 20
  213. },
  214. resizerPosition: 'bottom-right'
  215. } ) );
  216. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  217. isSideImage: true,
  218. expectedWidth: 140,
  219. pointerOffset: {
  220. x: 40,
  221. y: 0
  222. },
  223. resizerPosition: 'bottom-right'
  224. } ) );
  225. } );
  226. function isVisible( element ) {
  227. // Checks if the DOM element is visible to the end user.
  228. return element.offsetParent !== null;
  229. }
  230. function fireMouseEvent( target, eventType, eventData ) {
  231. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  232. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  233. // However there's still a problem, that events created with `initMouseEvent` have **floored** pageX, pageY numbers.
  234. const event = document.createEvent( 'MouseEvent' );
  235. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  236. MOUSE_BUTTON_MAIN, null );
  237. target.dispatchEvent( event );
  238. }
  239. function getElementPosition( element ) {
  240. // Returns top left corner point.
  241. const viewportPosition = element.getBoundingClientRect();
  242. return {
  243. x: viewportPosition.left + window.scrollX,
  244. y: viewportPosition.top + window.scrollY
  245. };
  246. }
  247. function wait( delay ) {
  248. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  249. }
  250. function generateResizeTest( options ) {
  251. // options.resizerPosition - top-left / top-right / bottom-right / bottom-left
  252. // options.pointerOffset - object - pointer offset relative to the dragged corner. Negative values are perfectly fine.
  253. // e.g. { x: 10, y: -5 }
  254. // options.expectedWidth
  255. // [options.isSideImage=false]
  256. // Returns a test case that puts
  257. return function() {
  258. const widget = this.widget;
  259. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  260. const domBottomLeftResizer = domResizeWrapper.querySelector( `.ck-widget__resizer-${ options.resizerPosition }` );
  261. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  262. const imageTopLeftPosition = getElementPosition( domImage );
  263. const resizerPositionParts = options.resizerPosition.split( '-' );
  264. const initialPointerPosition = {
  265. pageX: imageTopLeftPosition.x,
  266. pageY: imageTopLeftPosition.y
  267. };
  268. if ( resizerPositionParts.includes( 'right' ) ) {
  269. initialPointerPosition.pageX += FIXTURE_WIDTH;
  270. }
  271. if ( resizerPositionParts.includes( 'bottom' ) ) {
  272. initialPointerPosition.pageY += FIXTURE_HEIGHT;
  273. }
  274. const finishPointerPosition = Object.assign( {}, initialPointerPosition );
  275. finishPointerPosition.pageX += options.pointerOffset.x || 0;
  276. finishPointerPosition.pageY += options.pointerOffset.y || 0;
  277. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  278. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  279. // We need to wait as mousemove events are throttled.
  280. return wait( 30 )
  281. .then( () => {
  282. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  283. expect( domImage.width ).to.be.closeTo( options.expectedWidth, 1 );
  284. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  285. const modelDataRegExp = !options.isSideImage ? /<paragraph>foo<\/paragraph><image src=".+?" width="(\d+)"><\/image>/ :
  286. /<paragraph>foo<\/paragraph><image imageStyle="side" src=".+?" width="(\d+)"><\/image>/;
  287. expect( getData( editor.model, {
  288. withoutSelection: true
  289. } ) ).to.match( modelDataRegExp );
  290. const modelItem = editor.model.document.getRoot().getChild( 1 );
  291. expect( modelItem.getAttribute( 'width' ) ).to.be.closeTo( options.expectedWidth, 1, 'Model check' );
  292. } );
  293. };
  294. }
  295. } );