8
0

imageresize.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. /* 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 ImageToolbar from '../src/imagetoolbar';
  14. import ImageTextAlternative from '../src/imagetextalternative';
  15. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  16. import Table from '@ckeditor/ckeditor5-table/src/table';
  17. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  18. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. describe( 'ImageResize', () => {
  20. // Id of the left mouse button.
  21. const MOUSE_BUTTON_MAIN = 0;
  22. // 60x50 black png image
  23. const IMAGE_SRC_FIXTURE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  24. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  25. let absoluteContainer, widget, editor, view, viewDocument, editorElement;
  26. before( () => {
  27. // This container is required to position editor element in a reliable element.
  28. // See fireMouseEvent method for more information regarding imprecise mouse position.
  29. absoluteContainer = document.createElement( 'div' );
  30. absoluteContainer.style.top = '50px';
  31. absoluteContainer.style.left = '50px';
  32. absoluteContainer.style.height = '1000px';
  33. absoluteContainer.style.width = '500px';
  34. absoluteContainer.style.position = 'absolute';
  35. document.body.appendChild( absoluteContainer );
  36. } );
  37. after( () => {
  38. absoluteContainer.remove();
  39. } );
  40. afterEach( () => {
  41. if ( editorElement ) {
  42. editorElement.remove();
  43. }
  44. if ( editor ) {
  45. return editor.destroy();
  46. }
  47. } );
  48. describe( 'conversion', () => {
  49. beforeEach( () => createEditor() );
  50. it( 'upcasts 100px width correctly', () => {
  51. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  52. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  53. } );
  54. it( 'upcasts 50% width correctly', () => {
  55. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  56. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  57. } );
  58. it( 'downcasts 100px width correctly', () => {
  59. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  60. expect( editor.getData() )
  61. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  62. } );
  63. it( 'downcasts 50% width correctly', () => {
  64. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  65. expect( editor.getData() )
  66. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  67. } );
  68. it( 'doesn\'t downcast consumed tokens', () => {
  69. editor.conversion.for( 'downcast' ).add( dispatcher =>
  70. dispatcher.on( 'attribute:width:image', ( evt, data, conversionApi ) => {
  71. conversionApi.consumable.consume( data.item, 'attribute:width:image' );
  72. }, { priority: 'high' } )
  73. );
  74. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  75. expect( editor.getData() )
  76. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  77. } );
  78. } );
  79. describe( 'schema', () => {
  80. beforeEach( () => createEditor() );
  81. it( 'allows the width attribute', () => {
  82. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  83. } );
  84. it( 'defines width as a formatting attribute', () => {
  85. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  86. } );
  87. } );
  88. describe( 'command', () => {
  89. beforeEach( () => createEditor() );
  90. it( 'defines the imageResize command', () => {
  91. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  92. } );
  93. it( 'uses the command on commit', async () => {
  94. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  95. widget = viewDocument.getRoot().getChild( 1 );
  96. const spy = sinon.spy( editor.commands.get( 'imageResize' ), 'execute' );
  97. await generateResizeTest( {
  98. expectedWidth: 80,
  99. pointerOffset: {
  100. x: 10,
  101. y: -10
  102. },
  103. resizerPosition: 'bottom-left'
  104. } )();
  105. // It's either 80px or 81px depending on the device, so we need to make the test a bit more loose.
  106. const realWidth = editor.model.document.getRoot().getChild( 1 ).getAttribute( 'width' );
  107. expect( realWidth ).to.match( /^\d\dpx$/ );
  108. expect( spy.calledOnce ).to.be.true;
  109. expect( spy.args[ 0 ][ 0 ] ).to.deep.equal( { width: realWidth } );
  110. } );
  111. it( 'disables the resizer if the command is disabled', () => {
  112. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  113. const resizer = getSelectedImageResizer( editor );
  114. let isEnabled = false;
  115. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  116. evt.return = isEnabled;
  117. evt.stop();
  118. }, { priority: 'highest' } );
  119. editor.commands.get( 'imageResize' ).refresh();
  120. expect( resizer.isEnabled ).to.be.false;
  121. isEnabled = true;
  122. editor.commands.get( 'imageResize' ).refresh();
  123. expect( resizer.isEnabled ).to.be.true;
  124. } );
  125. it( 'the resizer is disabled from the beginning when the command is disabled when the image is inserted', () => {
  126. setData( editor.model, '<paragraph>foo[]</paragraph>' );
  127. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  128. evt.return = false;
  129. evt.stop();
  130. }, { priority: 'highest' } );
  131. editor.commands.get( 'imageResize' ).refresh();
  132. editor.model.change( writer => {
  133. editor.model.insertContent( writer.createElement( 'image', { src: IMAGE_SRC_FIXTURE } ) );
  134. } );
  135. const resizer = getSelectedImageResizer( editor );
  136. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  137. expect( resizer.isEnabled ).to.be.false;
  138. expect( resizerWrapper.style.display ).to.equal( 'none' );
  139. } );
  140. } );
  141. describe( 'side image resizing', () => {
  142. beforeEach( () => createEditor() );
  143. beforeEach( () => {
  144. setData( editor.model, `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  145. widget = viewDocument.getRoot().getChild( 1 );
  146. } );
  147. it( 'doesn\'t flicker at the beginning of the resize', async () => {
  148. // (#5189)
  149. const resizerPosition = 'bottom-left';
  150. const domParts = getWidgetDomParts( widget, resizerPosition );
  151. const initialPointerPosition = getResizerCoordinates( domParts.figure, resizerPosition );
  152. const resizeWrapperView = widget.getChild( 1 );
  153. focusEditor( editor );
  154. fireMouseEvent( domParts.resizeHandle, 'mousedown', initialPointerPosition );
  155. await wait( 40 );
  156. fireMouseEvent( domParts.resizeHandle, 'mousemove', initialPointerPosition );
  157. expect( resizeWrapperView.getStyle( 'width' ) ).to.be.equal( '100px' );
  158. fireMouseEvent( domParts.resizeHandle, 'mouseup', initialPointerPosition );
  159. } );
  160. it( 'makes no change when clicking the handle without drag', () => {
  161. const resizerPosition = 'bottom-left';
  162. const expectedWidth = 100;
  163. const domParts = getWidgetDomParts( widget, resizerPosition );
  164. const initialPointerPosition = getResizerCoordinates( domParts.figure, resizerPosition );
  165. focusEditor( editor );
  166. fireMouseEvent( domParts.resizeHandle, 'mousedown', initialPointerPosition );
  167. expect( getDomWidth( domParts.figure ), 'DOM width check' ).to.be.closeTo( expectedWidth, 2 );
  168. fireMouseEvent( domParts.resizeHandle, 'mouseup', initialPointerPosition );
  169. const modelItem = editor.model.document.getRoot().getChild( 1 );
  170. expect( modelItem.getAttribute( 'width' ), 'model width attribute' ).to.be.undefined;
  171. } );
  172. } );
  173. describe( 'percent resizing', () => {
  174. beforeEach( () => createEditor( {
  175. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ]
  176. } ) );
  177. describe( 'standard image', () => {
  178. beforeEach( () => {
  179. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  180. widget = viewDocument.getRoot().getChild( 1 );
  181. } );
  182. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  183. expectedWidth: 16,
  184. modelRegExp: /<paragraph>foo<\/paragraph><image src=".+?" width="([\d]{2}(?:\.[\d]{1,2}))%"><\/image>/,
  185. pointerOffset: {
  186. x: 10,
  187. y: -10
  188. },
  189. resizerPosition: 'bottom-left'
  190. } ) );
  191. it( 'enlarges correctly with right-bottom handler', generateResizeTest( {
  192. expectedWidth: 22,
  193. modelRegExp: /<paragraph>foo<\/paragraph><image src=".+?" width="([\d]{2}(?:\.[\d]{1,2}))%"><\/image>/,
  194. pointerOffset: {
  195. x: 0,
  196. y: 5
  197. },
  198. resizerPosition: 'bottom-right'
  199. } ) );
  200. it( 'enlarges correctly an image with unsupported width unit', async () => {
  201. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }" width="50pt"></image>]` );
  202. widget = viewDocument.getRoot().getChild( 1 );
  203. await generateResizeTest( {
  204. expectedWidth: 15,
  205. modelRegExp: /<paragraph>foo<\/paragraph><image src=".+?" width="([\d]{2}(?:\.[\d]{1,2}))%"><\/image>/,
  206. pointerOffset: {
  207. x: 0,
  208. y: 5
  209. },
  210. resizerPosition: 'bottom-right'
  211. } )();
  212. } );
  213. } );
  214. describe( 'side image', () => {
  215. beforeEach( () => {
  216. setData( editor.model, `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  217. view = editor.editing.view;
  218. viewDocument = view.document;
  219. widget = viewDocument.getRoot().getChild( 1 );
  220. } );
  221. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  222. expectedWidth: 18,
  223. modelRegExp: /<paragraph>foo<\/paragraph><image imageStyle="side" src=".+?" width="([\d]{2}(?:\.[\d]{1,2}))%"><\/image>/,
  224. pointerOffset: {
  225. x: 10,
  226. y: -10
  227. },
  228. resizerPosition: 'bottom-left'
  229. } ) );
  230. } );
  231. } );
  232. describe( 'undo integration', () => {
  233. beforeEach( () => createEditor() );
  234. beforeEach( () => {
  235. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  236. widget = viewDocument.getRoot().getChild( 1 );
  237. } );
  238. it( 'has correct border size after undo', async () => {
  239. await generateResizeTest( {
  240. expectedWidth: 120,
  241. pointerOffset: {
  242. x: 0,
  243. y: 10
  244. },
  245. resizerPosition: 'bottom-left'
  246. } )();
  247. editor.commands.get( 'undo' ).execute();
  248. await wait( 160 ); // ui#update event is throttled.
  249. const resizerWrapper = document.querySelector( '.ck-widget__resizer' );
  250. const shadowBoundingRect = resizerWrapper.getBoundingClientRect();
  251. expect( shadowBoundingRect.width ).to.be.equal( 100 );
  252. expect( shadowBoundingRect.height ).to.be.equal( 50 );
  253. } );
  254. } );
  255. describe( 'table integration', () => {
  256. beforeEach( () => createEditor() );
  257. beforeEach( () => {
  258. setData( editor.model,
  259. '<table>' +
  260. `<tableRow><tableCell>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]</tableCell></tableRow>` +
  261. '</table>'
  262. );
  263. widget = viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  264. } );
  265. it( 'works when resizing in a table', generateResizeTest( {
  266. getModel: () => editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 ),
  267. expectedWidth: 60,
  268. modelRegExp: /.+/,
  269. pointerOffset: {
  270. x: -40,
  271. y: -20
  272. },
  273. resizerPosition: 'bottom-right'
  274. } ) );
  275. } );
  276. describe( 'srcset integration', () => {
  277. beforeEach( () => createEditor() );
  278. // The image is 96x96 pixels.
  279. const imageBaseUrl = '/assets/sample.png';
  280. const getModel = () => editor.model.document.getRoot().getChild( 0 );
  281. let images = [];
  282. before( () => {
  283. return Promise.all( [
  284. preloadImage( imageBaseUrl ),
  285. preloadImage( imageBaseUrl + '?a' ),
  286. preloadImage( imageBaseUrl + '?b' ),
  287. preloadImage( imageBaseUrl + '?c' )
  288. ] ).then( loadedImages => {
  289. images = loadedImages;
  290. } );
  291. } );
  292. after( () => {
  293. for ( const image of images ) {
  294. image.remove();
  295. }
  296. } );
  297. beforeEach( () => {
  298. editor.setData(
  299. `<figure class="image">
  300. <img src="${ imageBaseUrl }"
  301. srcset="${ imageBaseUrl }?a 110w,
  302. ${ imageBaseUrl }?b 440w,
  303. ${ imageBaseUrl }?c 1025w"
  304. sizes="100vw" width="96">
  305. </figure>`
  306. );
  307. widget = viewDocument.getRoot().getChild( 0 );
  308. } );
  309. it( 'works with images containing srcset', generateResizeTest( {
  310. getModel,
  311. expectedWidth: 76,
  312. modelRegExp: /.+/,
  313. pointerOffset: {
  314. x: -20,
  315. y: -20
  316. },
  317. resizerPosition: 'bottom-right'
  318. } ) );
  319. it( 'retains width after removing srcset', async () => {
  320. await generateResizeTest( {
  321. getModel,
  322. expectedWidth: 80,
  323. modelRegExp: /.+/,
  324. pointerOffset: {
  325. x: -16,
  326. y: -16
  327. },
  328. resizerPosition: 'bottom-right'
  329. } )();
  330. editor.model.change( writer => {
  331. writer.removeAttribute( 'srcset', getModel() );
  332. } );
  333. expect( editor.getData() )
  334. .to.match( /<figure class="image image_resized" style="width:[\d.]{2,}px;"><img src="\/assets\/sample.png"><\/figure>/ );
  335. } );
  336. async function preloadImage( imageUrl ) {
  337. const image = document.createElement( 'img' );
  338. image.src = imageUrl;
  339. return new Promise( ( resolve, reject ) => {
  340. image.addEventListener( 'load', () => resolve( image ) );
  341. image.addEventListener( 'error', () => reject( image ) );
  342. document.body.appendChild( image );
  343. } );
  344. }
  345. } );
  346. describe( 'widget toolbar integration', () => {
  347. let widgetToolbarRepository;
  348. beforeEach( () => createEditor( {
  349. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize, ImageToolbar, ImageTextAlternative ],
  350. image: {
  351. toolbar: [ 'imageTextAlternative' ],
  352. resizeUnit: 'px'
  353. }
  354. } ) );
  355. beforeEach( async () => {
  356. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  357. widget = viewDocument.getRoot().getChild( 1 );
  358. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  359. } );
  360. it( 'default toolbar visibility', async () => {
  361. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  362. } );
  363. it( 'visibility during the resize', async () => {
  364. await generateResizeTest( {
  365. expectedWidth: 100,
  366. modelRegExp: /.+/,
  367. pointerOffset: {
  368. x: 0,
  369. y: 0
  370. },
  371. resizerPosition: 'bottom-right',
  372. checkBeforeMouseUp: () => {
  373. expect( widgetToolbarRepository.isEnabled ).to.be.false;
  374. }
  375. } )();
  376. } );
  377. it( 'visibility after the resize', async () => {
  378. await generateResizeTest( {
  379. expectedWidth: 100,
  380. modelRegExp: /.+/,
  381. pointerOffset: {
  382. x: 0,
  383. y: 0
  384. },
  385. resizerPosition: 'bottom-right'
  386. } )();
  387. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  388. } );
  389. it( 'visibility after the resize was canceled', async () => {
  390. const resizer = getSelectedImageResizer( editor );
  391. await generateResizeTest( {
  392. expectedWidth: 100,
  393. modelRegExp: /.+/,
  394. pointerOffset: {
  395. x: 0,
  396. y: 0
  397. },
  398. resizerPosition: 'bottom-right',
  399. checkBeforeMouseUp: () => {
  400. resizer.cancel();
  401. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  402. }
  403. } )();
  404. } );
  405. it( 'restores toolbar when clicking the handle without drag', () => {
  406. // (https://github.com/ckeditor/ckeditor5-widget/pull/112#pullrequestreview-337725256).
  407. const resizerPosition = 'bottom-left';
  408. const domParts = getWidgetDomParts( widget, resizerPosition );
  409. const initialPointerPosition = getResizerCoordinates( domParts.figure, resizerPosition );
  410. focusEditor( editor );
  411. fireMouseEvent( domParts.resizeHandle, 'mousedown', initialPointerPosition );
  412. fireMouseEvent( domParts.resizeHandle, 'mouseup', initialPointerPosition );
  413. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  414. } );
  415. } );
  416. function fireMouseEvent( target, eventType, eventData ) {
  417. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  418. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  419. // However there's still a problem, that events created with `initMouseEvent` have **floored** pageX, pageY numbers.
  420. const event = document.createEvent( 'MouseEvent' );
  421. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  422. MOUSE_BUTTON_MAIN, null );
  423. target.dispatchEvent( event );
  424. }
  425. function wait( delay ) {
  426. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  427. }
  428. function generateResizeTest( options ) {
  429. // options.resizerPosition - top-left / top-right / bottom-right / bottom-left
  430. // options.pointerOffset - object - pointer offset relative to the dragged corner. Negative values are perfectly fine.
  431. // e.g. { x: 10, y: -5 }
  432. // options.expectedWidth
  433. // Returns a test case that puts
  434. return async function() {
  435. const domParts = getWidgetDomParts( widget, options.resizerPosition );
  436. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  437. const modelRegExp = options.modelRegExp ? options.modelRegExp :
  438. /<paragraph>foo<\/paragraph><image src=".+?" width="([\d]+)px"><\/image>/;
  439. focusEditor( editor );
  440. const initialPointerPosition = getResizerCoordinates( domParts.figure, options.resizerPosition );
  441. fireMouseEvent( domParts.resizeHandle, 'mousedown', initialPointerPosition );
  442. fireMouseEvent( domParts.resizeHandle, 'mousemove', initialPointerPosition );
  443. // We need to wait as mousemove events are throttled.
  444. await wait( 40 );
  445. const finishPointerPosition = Object.assign( {}, initialPointerPosition );
  446. finishPointerPosition.pageX += options.pointerOffset.x || 0;
  447. finishPointerPosition.pageY += options.pointerOffset.y || 0;
  448. fireMouseEvent( domParts.resizeHandle, 'mousemove', finishPointerPosition );
  449. expect( parseInt( domParts.figure.style.width ) ).to.be.closeTo( options.expectedWidth, 2, 'DOM width check' );
  450. if ( options.checkBeforeMouseUp ) {
  451. options.checkBeforeMouseUp( domParts.figure, domResizeWrapper );
  452. }
  453. fireMouseEvent( domParts.resizeHandle, 'mouseup', finishPointerPosition );
  454. expect( getData( editor.model, {
  455. withoutSelection: true
  456. } ) ).to.match( modelRegExp );
  457. const modelItem = options.getModel ? options.getModel() : editor.model.document.getRoot().getChild( 1 );
  458. const modelWidth = modelItem.getAttribute( 'width' );
  459. expect( parseFloat( modelWidth, 0 ) )
  460. .to.be.closeTo( options.expectedWidth, 2, 'Model width check' );
  461. };
  462. }
  463. function focusEditor( editor ) {
  464. editor.editing.view.focus();
  465. editor.ui.focusTracker.isFocused = true;
  466. }
  467. function getResizerCoordinates( domFigure, resizerPosition ) {
  468. const domImage = domFigure.querySelector( 'img' );
  469. const imageRect = new Rect( domImage );
  470. const initialPointerPosition = {
  471. pageX: imageRect.left,
  472. pageY: imageRect.top
  473. };
  474. const resizerPositionParts = resizerPosition.split( '-' );
  475. if ( resizerPositionParts.includes( 'right' ) ) {
  476. initialPointerPosition.pageX = imageRect.right;
  477. }
  478. if ( resizerPositionParts.includes( 'bottom' ) ) {
  479. initialPointerPosition.pageY = imageRect.bottom;
  480. }
  481. return initialPointerPosition;
  482. }
  483. function getWidgetDomParts( widget, resizerPosition ) {
  484. const resizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  485. const resizeHandle = resizeWrapper.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` );
  486. const figure = view.domConverter.mapViewToDom( widget );
  487. return {
  488. resizeWrapper,
  489. resizeHandle,
  490. figure
  491. };
  492. }
  493. function getDomWidth( domElement ) {
  494. return new Rect( domElement ).width;
  495. }
  496. function getSelectedImageResizer( editor ) {
  497. return editor.plugins.get( 'WidgetResize' )._getResizerByViewElement(
  498. editor.editing.view.document.selection.getSelectedElement()
  499. );
  500. }
  501. function createEditor( config ) {
  502. editorElement = document.createElement( 'div' );
  503. absoluteContainer.appendChild( editorElement );
  504. return ClassicEditor
  505. .create( editorElement, config || {
  506. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ],
  507. image: {
  508. resizeUnit: 'px'
  509. }
  510. } )
  511. .then( newEditor => {
  512. editor = newEditor;
  513. view = editor.editing.view;
  514. viewDocument = view.document;
  515. widget = viewDocument.getRoot().getChild( 1 );
  516. } );
  517. }
  518. } );