imageresize.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import {
  20. focusEditor,
  21. mouseMock,
  22. getWidgetDomParts,
  23. getHandleCenterPoint
  24. } from '@ckeditor/ckeditor5-widget/tests/widgetresize/_utils/utils';
  25. describe( 'ImageResize', () => {
  26. // 100x50 black png image
  27. const IMAGE_SRC_FIXTURE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  28. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  29. let absoluteContainer, widget, editor, view, viewDocument, editorElement;
  30. before( () => {
  31. // This container is required to position editor element in a reliable element.
  32. // @todo ensure whether it's required after migrating the tests.
  33. absoluteContainer = document.createElement( 'div' );
  34. absoluteContainer.style.top = '50px';
  35. absoluteContainer.style.left = '50px';
  36. absoluteContainer.style.height = '1000px';
  37. absoluteContainer.style.width = '500px';
  38. absoluteContainer.style.position = 'absolute';
  39. document.body.appendChild( absoluteContainer );
  40. } );
  41. after( () => {
  42. absoluteContainer.remove();
  43. } );
  44. afterEach( () => {
  45. if ( editorElement ) {
  46. editorElement.remove();
  47. }
  48. if ( editor ) {
  49. return editor.destroy();
  50. }
  51. } );
  52. describe( 'conversion', () => {
  53. beforeEach( () => createEditor() );
  54. it( 'upcasts 100px width correctly', () => {
  55. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  56. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  57. } );
  58. it( 'upcasts 50% width correctly', () => {
  59. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  60. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  61. } );
  62. it( 'downcasts 100px width correctly', () => {
  63. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  64. expect( editor.getData() )
  65. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  66. } );
  67. it( 'downcasts 50% width correctly', () => {
  68. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  69. expect( editor.getData() )
  70. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  71. } );
  72. it( 'doesn\'t downcast consumed tokens', () => {
  73. editor.conversion.for( 'downcast' ).add( dispatcher =>
  74. dispatcher.on( 'attribute:width:image', ( evt, data, conversionApi ) => {
  75. conversionApi.consumable.consume( data.item, 'attribute:width:image' );
  76. }, { priority: 'high' } )
  77. );
  78. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  79. expect( editor.getData() )
  80. .to.equal( `<figure class="image"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  81. } );
  82. } );
  83. describe( 'schema', () => {
  84. beforeEach( () => createEditor() );
  85. it( 'allows the width attribute', () => {
  86. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  87. } );
  88. it( 'defines width as a formatting attribute', () => {
  89. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  90. } );
  91. } );
  92. describe( 'command', () => {
  93. beforeEach( () => createEditor() );
  94. it( 'defines the imageResize command', () => {
  95. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  96. } );
  97. it( 'uses the command on commit', async () => {
  98. const spy = sinon.spy( editor.commands.get( 'imageResize' ), 'execute' );
  99. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  100. widget = viewDocument.getRoot().getChild( 1 );
  101. const domParts = getWidgetDomParts( editor, widget, 'bottom-left' );
  102. const finalPointerPosition = getHandleCenterPoint( domParts.widget, 'bottom-left' ).moveBy( 10, -10 );
  103. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  104. expect( spy.calledOnce ).to.be.true;
  105. expect( spy.args[ 0 ][ 0 ] ).to.deep.equal( { width: '80px' } );
  106. } );
  107. it( 'disables the resizer if the command is disabled', () => {
  108. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  109. const resizer = getSelectedImageResizer( editor );
  110. let isEnabled = false;
  111. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  112. evt.return = isEnabled;
  113. evt.stop();
  114. }, { priority: 'highest' } );
  115. editor.commands.get( 'imageResize' ).refresh();
  116. expect( resizer.isEnabled ).to.be.false;
  117. isEnabled = true;
  118. editor.commands.get( 'imageResize' ).refresh();
  119. expect( resizer.isEnabled ).to.be.true;
  120. } );
  121. it( 'the resizer is disabled from the beginning when the command is disabled when the image is inserted', () => {
  122. setData( editor.model, '<paragraph>foo[]</paragraph>' );
  123. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  124. evt.return = false;
  125. evt.stop();
  126. }, { priority: 'highest' } );
  127. editor.commands.get( 'imageResize' ).refresh();
  128. editor.model.change( writer => {
  129. editor.model.insertContent( writer.createElement( 'image', { src: IMAGE_SRC_FIXTURE } ) );
  130. } );
  131. const resizer = getSelectedImageResizer( editor );
  132. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  133. expect( resizer.isEnabled ).to.be.false;
  134. expect( resizerWrapper.style.display ).to.equal( 'none' );
  135. } );
  136. } );
  137. describe( 'side image resizing', () => {
  138. beforeEach( () => createEditor() );
  139. beforeEach( () => {
  140. setData( editor.model, `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  141. widget = viewDocument.getRoot().getChild( 1 );
  142. } );
  143. it( 'doesn\'t flicker at the beginning of the resize', async () => {
  144. // (#5189)
  145. const resizerPosition = 'bottom-left';
  146. const domParts = getWidgetDomParts( editor, widget, resizerPosition );
  147. const initialPointerPosition = getHandleCenterPoint( domParts.widget, resizerPosition );
  148. const resizeWrapperView = widget.getChild( 1 );
  149. mouseMock.down( editor, domParts.resizeHandle );
  150. await wait( 40 );
  151. mouseMock.move( editor, domParts.resizeHandle, null, initialPointerPosition );
  152. expect( resizeWrapperView.getStyle( 'width' ) ).to.be.equal( '100px' );
  153. mouseMock.up( editor );
  154. } );
  155. it( 'makes no change when clicking the handle without drag', () => {
  156. const resizerPosition = 'bottom-left';
  157. const expectedWidth = 100;
  158. const domParts = getWidgetDomParts( editor, widget, resizerPosition );
  159. mouseMock.down( editor, domParts.resizeHandle );
  160. expect( getDomWidth( domParts.widget ), 'DOM width check' ).to.be.closeTo( expectedWidth, 2 );
  161. mouseMock.up( editor );
  162. const modelItem = editor.model.document.getRoot().getChild( 1 );
  163. expect( modelItem.getAttribute( 'width' ), 'model width attribute' ).to.be.undefined;
  164. } );
  165. } );
  166. describe( 'undo integration', () => {
  167. beforeEach( () => createEditor() );
  168. beforeEach( () => {
  169. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  170. widget = viewDocument.getRoot().getChild( 1 );
  171. } );
  172. it( 'has correct border size after undo', async () => {
  173. const domParts = getWidgetDomParts( editor, widget, 'bottom-left' );
  174. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-left' );
  175. const finalPointerPosition = initialPosition.clone().moveBy( 0, 10 );
  176. mouseMock.dragTo( editor, domParts.resizeHandle, {
  177. from: initialPosition,
  178. to: finalPointerPosition
  179. } );
  180. expect( '120px' ).to.be.equal( domParts.widget.style.width );
  181. editor.commands.get( 'undo' ).execute();
  182. await wait( 180 ); // ui#update event is throttled.
  183. const resizerWrapper = document.querySelector( '.ck-widget__resizer' );
  184. const shadowBoundingRect = resizerWrapper.getBoundingClientRect();
  185. expect( shadowBoundingRect.width ).to.be.equal( 100 );
  186. expect( shadowBoundingRect.height ).to.be.equal( 50 );
  187. } );
  188. } );
  189. describe( 'table integration', () => {
  190. beforeEach( () => createEditor() );
  191. it( 'works when resizing in a table', () => {
  192. setData( editor.model,
  193. '<table>' +
  194. `<tableRow><tableCell>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]</tableCell></tableRow>` +
  195. '</table>'
  196. );
  197. widget = viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  198. const model = editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  199. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  200. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  201. const finalPointerPosition = initialPosition.clone().moveBy( -40, -20 );
  202. mouseMock.dragTo( editor, domParts.resizeHandle, {
  203. from: initialPosition,
  204. to: finalPointerPosition
  205. } );
  206. expect( model.getAttribute( 'width' ) ).to.be.equal( '60px' );
  207. } );
  208. } );
  209. describe( 'srcset integration', () => {
  210. beforeEach( () => createEditor() );
  211. // The image is 96x96 pixels.
  212. const imageBaseUrl = '/assets/sample.png';
  213. let model;
  214. let images = [];
  215. before( () => {
  216. return Promise.all( [
  217. preloadImage( imageBaseUrl ),
  218. preloadImage( imageBaseUrl + '?a' ),
  219. preloadImage( imageBaseUrl + '?b' ),
  220. preloadImage( imageBaseUrl + '?c' )
  221. ] ).then( loadedImages => {
  222. images = loadedImages;
  223. } );
  224. } );
  225. after( () => {
  226. for ( const image of images ) {
  227. image.remove();
  228. }
  229. } );
  230. beforeEach( () => {
  231. editor.setData(
  232. `<figure class="image">
  233. <img src="${ imageBaseUrl }"
  234. srcset="${ imageBaseUrl }?a 110w,
  235. ${ imageBaseUrl }?b 440w,
  236. ${ imageBaseUrl }?c 1025w"
  237. sizes="100vw" width="96">
  238. </figure>`
  239. );
  240. widget = viewDocument.getRoot().getChild( 0 );
  241. model = editor.model.document.getRoot().getChild( 0 );
  242. } );
  243. it( 'works with images containing srcset', () => {
  244. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  245. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  246. const finalPointerPosition = initialPosition.clone().moveBy( -20, -20 );
  247. mouseMock.dragTo( editor, domParts.resizeHandle, {
  248. from: initialPosition,
  249. to: finalPointerPosition
  250. } );
  251. expect( model.getAttribute( 'width' ) ).to.be.equal( '76px' );
  252. } );
  253. it( 'retains width after removing srcset', () => {
  254. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  255. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  256. const finalPointerPosition = initialPosition.clone().moveBy( -16, -16 );
  257. mouseMock.dragTo( editor, domParts.resizeHandle, {
  258. from: initialPosition,
  259. to: finalPointerPosition
  260. } );
  261. editor.model.change( writer => {
  262. writer.removeAttribute( 'srcset', model );
  263. } );
  264. const expectedHtml = '<figure class="image image_resized" style="width:80px;"><img src="/assets/sample.png"></figure>';
  265. expect( editor.getData() ).to.be.equal( expectedHtml );
  266. } );
  267. async function preloadImage( imageUrl ) {
  268. const image = document.createElement( 'img' );
  269. image.src = imageUrl;
  270. return new Promise( ( resolve, reject ) => {
  271. image.addEventListener( 'load', () => resolve( image ) );
  272. image.addEventListener( 'error', () => reject( image ) );
  273. document.body.appendChild( image );
  274. } );
  275. }
  276. } );
  277. describe( 'widget toolbar integration', () => {
  278. let widgetToolbarRepository;
  279. beforeEach( () => createEditor( {
  280. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize, ImageToolbar, ImageTextAlternative ],
  281. image: {
  282. toolbar: [ 'imageTextAlternative' ],
  283. resizeUnit: 'px'
  284. }
  285. } ) );
  286. beforeEach( async () => {
  287. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  288. widget = viewDocument.getRoot().getChild( 1 );
  289. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  290. } );
  291. it( 'default toolbar visibility', async () => {
  292. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  293. } );
  294. it( 'visibility during the resize', async () => {
  295. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  296. mouseMock.down( editor, domResizeHandle );
  297. expect( widgetToolbarRepository.isEnabled ).to.be.false;
  298. mouseMock.up( editor, domResizeHandle );
  299. } );
  300. it( 'visibility after the resize', async () => {
  301. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  302. mouseMock.down( editor, domResizeHandle );
  303. mouseMock.up( editor, domResizeHandle );
  304. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  305. } );
  306. it( 'visibility after the resize was canceled', async () => {
  307. const resizer = getSelectedImageResizer( editor );
  308. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  309. mouseMock.down( editor, domResizeHandle );
  310. resizer.cancel();
  311. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  312. } );
  313. it( 'restores toolbar when clicking the handle without drag', () => {
  314. // (https://github.com/ckeditor/ckeditor5-widget/pull/112#pullrequestreview-337725256).
  315. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  316. mouseMock.down( editor, domResizeHandle );
  317. mouseMock.up( editor, domResizeHandle );
  318. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  319. } );
  320. } );
  321. function wait( delay ) {
  322. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  323. }
  324. function getDomWidth( domElement ) {
  325. return new Rect( domElement ).width;
  326. }
  327. function getSelectedImageResizer( editor ) {
  328. return editor.plugins.get( 'WidgetResize' )._getResizerByViewElement(
  329. editor.editing.view.document.selection.getSelectedElement()
  330. );
  331. }
  332. function createEditor( config ) {
  333. editorElement = document.createElement( 'div' );
  334. absoluteContainer.appendChild( editorElement );
  335. return ClassicEditor
  336. .create( editorElement, config || {
  337. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ],
  338. image: {
  339. resizeUnit: 'px'
  340. }
  341. } )
  342. .then( newEditor => {
  343. editor = newEditor;
  344. view = editor.editing.view;
  345. viewDocument = view.document;
  346. widget = viewDocument.getRoot().getChild( 1 );
  347. focusEditor( editor );
  348. } );
  349. }
  350. } );