imageresize.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. // ClassicTestEditor can't be used, as it doesn't handle the focus, which is needed to test resizer visual cues.
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Image from '../src/image';
  10. import ImageResize from '../src/imageresize';
  11. import ImageResizeCommand from '../src/imageresize/imageresizecommand';
  12. import ImageStyle from '../src/imagestyle';
  13. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  14. import Table from '@ckeditor/ckeditor5-table/src/table';
  15. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'ImageResize', () => {
  17. const FIXTURE_WIDTH = 100;
  18. const FIXTURE_HEIGHT = 50;
  19. // Id of the left mouse button.
  20. const MOUSE_BUTTON_MAIN = 0;
  21. // 60x50 black png image
  22. const IMAGE_SRC_FIXTURE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  23. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  24. let absoluteContainer, widget, editor, view, viewDocument, editorElement;
  25. before( () => {
  26. // This container is required to position editor element in a reliable element.
  27. // See fireMouseEvent method for more information regarding imprecise mouse position.
  28. absoluteContainer = document.createElement( 'div' );
  29. absoluteContainer.style.top = '50px';
  30. absoluteContainer.style.left = '50px';
  31. absoluteContainer.style.height = '1000px';
  32. absoluteContainer.style.width = '500px';
  33. absoluteContainer.style.position = 'absolute';
  34. document.body.appendChild( absoluteContainer );
  35. } );
  36. after( () => {
  37. absoluteContainer.remove();
  38. } );
  39. beforeEach( () => {
  40. editorElement = document.createElement( 'div' );
  41. editorElement.innerHTML = `<p>foo</p><figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>`;
  42. absoluteContainer.appendChild( editorElement );
  43. return ClassicEditor
  44. .create( editorElement, {
  45. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ]
  46. } )
  47. .then( newEditor => {
  48. editor = newEditor;
  49. view = editor.editing.view;
  50. viewDocument = view.document;
  51. } );
  52. } );
  53. afterEach( () => {
  54. editorElement.remove();
  55. return editor.destroy();
  56. } );
  57. describe( 'conversion', () => {
  58. it( 'upcasts 100px width correctly', () => {
  59. editor.setData( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }" style="width:100px;"></figure>` );
  60. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  61. } );
  62. it( 'upcasts 50% width correctly', () => {
  63. editor.setData( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }" style="width:50%;"></figure>` );
  64. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  65. } );
  66. it( 'downcasts 100px width correctly', () => {
  67. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  68. expect( editor.getData() )
  69. .to.equal( `<figure class="image image_resized"><img style="width:100px;" src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  70. } );
  71. it( 'downcasts 50% width correctly', () => {
  72. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  73. expect( editor.getData() )
  74. .to.equal( `<figure class="image image_resized"><img style="width:50%;" src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  75. } );
  76. } );
  77. describe( 'schema', () => {
  78. it( 'allows the width attribute', () => {
  79. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  80. } );
  81. it( 'defines width as a formatting attribute', () => {
  82. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  83. } );
  84. } );
  85. describe( 'command', () => {
  86. it( 'defines the imageResize command', () => {
  87. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  88. } );
  89. } );
  90. describe( 'visual resizers', () => {
  91. it( 'correct amount is added by default', () => {
  92. const resizers = document.querySelectorAll( '.ck-widget__resizer__handle' );
  93. expect( resizers.length ).to.be.equal( 4 );
  94. } );
  95. describe( 'visibility', () => {
  96. it( 'is hidden by default', () => {
  97. const allResizers = document.querySelectorAll( '.ck-widget__resizer__handle' );
  98. for ( const resizer of allResizers ) {
  99. expect( isVisible( resizer ) ).to.be.false;
  100. }
  101. } );
  102. it( 'is shown when image is focused', () => {
  103. const widget = viewDocument.getRoot().getChild( 1 );
  104. const allResizers = document.querySelectorAll( '.ck-widget__resizer__handle' );
  105. const domEventDataMock = {
  106. target: widget,
  107. preventDefault: sinon.spy()
  108. };
  109. focusEditor( editor );
  110. viewDocument.fire( 'mousedown', domEventDataMock );
  111. for ( const resizer of allResizers ) {
  112. expect( isVisible( resizer ) ).to.be.true;
  113. }
  114. } );
  115. } );
  116. } );
  117. it( 'uses the command on commit', async () => {
  118. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  119. widget = viewDocument.getRoot().getChild( 1 );
  120. const spy = sinon.spy( editor.commands.get( 'imageResize' ), 'execute' );
  121. await generateResizeTest( {
  122. expectedWidth: 80,
  123. pointerOffset: {
  124. x: 10,
  125. y: -10
  126. },
  127. resizerPosition: 'bottom-left'
  128. } )();
  129. expect( spy.calledOnce ).to.be.true;
  130. expect( spy.args[ 0 ][ 0 ] ).to.deep.equal( { width: '80px' } );
  131. } );
  132. describe( 'standard image resizing', () => {
  133. beforeEach( () => {
  134. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  135. widget = viewDocument.getRoot().getChild( 1 );
  136. } );
  137. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  138. expectedWidth: 80,
  139. pointerOffset: {
  140. x: 10,
  141. y: -10
  142. },
  143. resizerPosition: 'bottom-left'
  144. } ) );
  145. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  146. expectedWidth: 80,
  147. pointerOffset: {
  148. x: -10,
  149. y: -10
  150. },
  151. resizerPosition: 'bottom-right'
  152. } ) );
  153. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  154. expectedWidth: 120,
  155. pointerOffset: {
  156. x: 10,
  157. y: 0
  158. },
  159. resizerPosition: 'bottom-right'
  160. } ) );
  161. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  162. expectedWidth: 120,
  163. pointerOffset: {
  164. x: 0,
  165. y: 10
  166. },
  167. resizerPosition: 'bottom-right'
  168. } ) );
  169. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  170. expectedWidth: 120,
  171. pointerOffset: {
  172. x: -10,
  173. y: 0
  174. },
  175. resizerPosition: 'bottom-left'
  176. } ) );
  177. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  178. expectedWidth: 120,
  179. pointerOffset: {
  180. x: 0,
  181. y: 10
  182. },
  183. resizerPosition: 'bottom-left'
  184. } ) );
  185. // --- top handlers ---
  186. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  187. expectedWidth: 120,
  188. pointerOffset: {
  189. x: -10,
  190. y: -10
  191. },
  192. resizerPosition: 'top-left'
  193. } ) );
  194. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  195. expectedWidth: 120,
  196. pointerOffset: {
  197. x: 0,
  198. y: -10
  199. },
  200. resizerPosition: 'top-left'
  201. } ) );
  202. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  203. expectedWidth: 120,
  204. pointerOffset: {
  205. x: 10,
  206. y: -10
  207. },
  208. resizerPosition: 'top-right'
  209. } ) );
  210. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  211. expectedWidth: 120,
  212. pointerOffset: {
  213. x: 0,
  214. y: -10
  215. },
  216. resizerPosition: 'top-right'
  217. } ) );
  218. } );
  219. describe( 'side image resizing', () => {
  220. beforeEach( () => {
  221. setData( editor.model, `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  222. widget = viewDocument.getRoot().getChild( 1 );
  223. } );
  224. it( 'shrinks correctly with left-bottom handler', generateSideResizeTest( {
  225. isSideImage: true,
  226. expectedWidth: 80,
  227. pointerOffset: {
  228. x: 20,
  229. y: -10
  230. },
  231. resizerPosition: 'bottom-left'
  232. } ) );
  233. it( 'shrinks correctly with right-bottom handler', generateSideResizeTest( {
  234. isSideImage: true,
  235. expectedWidth: 80,
  236. pointerOffset: {
  237. x: -20,
  238. y: -10
  239. },
  240. resizerPosition: 'bottom-right'
  241. } ) );
  242. it( 'shrinks correctly with left-top handler', generateSideResizeTest( {
  243. isSideImage: true,
  244. expectedWidth: 80,
  245. pointerOffset: {
  246. x: 20,
  247. y: 10
  248. },
  249. resizerPosition: 'top-left'
  250. } ) );
  251. it( 'shrinks correctly with right-top handler', generateSideResizeTest( {
  252. isSideImage: true,
  253. expectedWidth: 80,
  254. pointerOffset: {
  255. x: -20,
  256. y: 10
  257. },
  258. resizerPosition: 'top-right'
  259. } ) );
  260. it( 'enlarges correctly with left-bottom handler', generateSideResizeTest( {
  261. isSideImage: true,
  262. expectedWidth: 120,
  263. pointerOffset: {
  264. x: -10,
  265. y: 10
  266. },
  267. resizerPosition: 'bottom-left'
  268. } ) );
  269. it( 'enlarges correctly with right-bottom handler', generateSideResizeTest( {
  270. isSideImage: true,
  271. expectedWidth: 120,
  272. pointerOffset: {
  273. x: 10,
  274. y: 10
  275. },
  276. resizerPosition: 'bottom-right'
  277. } ) );
  278. it( 'enlarges correctly with right-bottom handler, y axis only', generateSideResizeTest( {
  279. isSideImage: true,
  280. expectedWidth: 140,
  281. pointerOffset: {
  282. x: 0,
  283. y: 20
  284. },
  285. resizerPosition: 'bottom-right'
  286. } ) );
  287. it( 'enlarges correctly with right-bottom handler, x axis only', generateSideResizeTest( {
  288. isSideImage: true,
  289. expectedWidth: 140,
  290. pointerOffset: {
  291. x: 40,
  292. y: 0
  293. },
  294. resizerPosition: 'bottom-right'
  295. } ) );
  296. it( 'enlarges correctly with left-top handler', generateSideResizeTest( {
  297. isSideImage: true,
  298. expectedWidth: 120,
  299. pointerOffset: {
  300. x: -20,
  301. y: -10
  302. },
  303. resizerPosition: 'top-left'
  304. } ) );
  305. it( 'enlarges correctly with right-top handler', generateSideResizeTest( {
  306. isSideImage: true,
  307. expectedWidth: 120,
  308. pointerOffset: {
  309. x: 20,
  310. y: 10
  311. },
  312. resizerPosition: 'top-right'
  313. } ) );
  314. function generateSideResizeTest( options ) {
  315. return generateResizeTest( Object.assign( {
  316. isSideImage: true,
  317. modelRegExp: /<paragraph>foo<\/paragraph><image imageStyle="side" src=".+?" width="([\d.]+)px"><\/image>/
  318. }, options ) );
  319. }
  320. } );
  321. describe( 'undo integration', () => {
  322. beforeEach( () => {
  323. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  324. widget = viewDocument.getRoot().getChild( 1 );
  325. } );
  326. it( 'has correct border size after undo', async () => {
  327. await generateResizeTest( {
  328. expectedWidth: 120,
  329. pointerOffset: {
  330. x: 0,
  331. y: 10
  332. },
  333. resizerPosition: 'bottom-left'
  334. } )();
  335. editor.commands.get( 'undo' ).execute();
  336. await wait( 40 );
  337. const resizerWrapper = document.querySelector( '.ck-widget__resizer' );
  338. const shadowBoundingRect = resizerWrapper.getBoundingClientRect();
  339. expect( shadowBoundingRect.width ).to.be.equal( 100 );
  340. expect( shadowBoundingRect.height ).to.be.equal( 50 );
  341. } );
  342. } );
  343. describe( 'table integration', () => {
  344. beforeEach( () => {
  345. setData( editor.model,
  346. '<table>' +
  347. `<tableRow><tableCell>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]</tableCell></tableRow>` +
  348. '</table>'
  349. );
  350. widget = viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  351. } );
  352. it( 'works when resizing in a table', generateResizeTest( {
  353. getModel: () => editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 ),
  354. expectedWidth: 60,
  355. modelRegExp: /.+/,
  356. pointerOffset: {
  357. x: -40,
  358. y: -20
  359. },
  360. resizerPosition: 'bottom-right'
  361. } ) );
  362. } );
  363. function isVisible( element ) {
  364. // Checks if the DOM element is visible to the end user.
  365. return element.offsetParent !== null;
  366. }
  367. function fireMouseEvent( target, eventType, eventData ) {
  368. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  369. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  370. // However there's still a problem, that events created with `initMouseEvent` have **floored** pageX, pageY numbers.
  371. const event = document.createEvent( 'MouseEvent' );
  372. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  373. MOUSE_BUTTON_MAIN, null );
  374. target.dispatchEvent( event );
  375. }
  376. function getElementPosition( element ) {
  377. // Returns top left corner point.
  378. const viewportPosition = element.getBoundingClientRect();
  379. return {
  380. x: viewportPosition.left,
  381. y: viewportPosition.top
  382. };
  383. }
  384. function wait( delay ) {
  385. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  386. }
  387. function generateResizeTest( options ) {
  388. // options.resizerPosition - top-left / top-right / bottom-right / bottom-left
  389. // options.pointerOffset - object - pointer offset relative to the dragged corner. Negative values are perfectly fine.
  390. // e.g. { x: 10, y: -5 }
  391. // options.expectedWidth
  392. // [options.isSideImage=false]
  393. // Returns a test case that puts
  394. return function() {
  395. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  396. const domBottomLeftResizer = domResizeWrapper.querySelector( `.ck-widget__resizer__handle-${ options.resizerPosition }` );
  397. const domImage = view.domConverter.mapViewToDom( widget ).querySelector( 'img' );
  398. const imageTopLeftPosition = getElementPosition( domImage );
  399. const resizerPositionParts = options.resizerPosition.split( '-' );
  400. const modelRegExp = options.modelRegExp ? options.modelRegExp :
  401. /<paragraph>foo<\/paragraph><image src=".+?" width="([\d.]+)px"><\/image>/;
  402. focusEditor( editor );
  403. const initialPointerPosition = {
  404. pageX: imageTopLeftPosition.x,
  405. pageY: imageTopLeftPosition.y
  406. };
  407. if ( resizerPositionParts.includes( 'right' ) ) {
  408. initialPointerPosition.pageX += FIXTURE_WIDTH;
  409. }
  410. if ( resizerPositionParts.includes( 'bottom' ) ) {
  411. initialPointerPosition.pageY += FIXTURE_HEIGHT;
  412. }
  413. const finishPointerPosition = Object.assign( {}, initialPointerPosition );
  414. finishPointerPosition.pageX += options.pointerOffset.x || 0;
  415. finishPointerPosition.pageY += options.pointerOffset.y || 0;
  416. fireMouseEvent( domBottomLeftResizer, 'mousedown', initialPointerPosition );
  417. fireMouseEvent( domBottomLeftResizer, 'mousemove', initialPointerPosition );
  418. // We need to wait as mousemove events are throttled.
  419. return wait( 40 )
  420. .then( () => {
  421. fireMouseEvent( domBottomLeftResizer, 'mousemove', finishPointerPosition );
  422. expect( domImage.width ).to.be.closeTo( options.expectedWidth, 2, 'DOM width check' );
  423. fireMouseEvent( domBottomLeftResizer, 'mouseup', finishPointerPosition );
  424. expect( getData( editor.model, {
  425. withoutSelection: true
  426. } ) ).to.match( modelRegExp );
  427. const modelItem = options.getModel ? options.getModel() : editor.model.document.getRoot().getChild( 1 );
  428. expect( modelItem.getAttribute( 'width' ) ).to.match( /^([\d.]+)px$/, 'Model width is properly formatted' );
  429. expect( parseInt( modelItem.getAttribute( 'width' ), 0 ) )
  430. .to.be.closeTo( options.expectedWidth, 2, 'Model width check' );
  431. } );
  432. };
  433. }
  434. function focusEditor( editor ) {
  435. editor.editing.view.focus();
  436. editor.ui.focusTracker.isFocused = true;
  437. }
  438. } );