imageresize.js 17 KB

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