imageresizehandles.js 16 KB

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