imageresize.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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( 'table integration', () => {
  167. beforeEach( () => createEditor() );
  168. it( 'works when resizing in a table', () => {
  169. setData( editor.model,
  170. '<table>' +
  171. `<tableRow><tableCell>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]</tableCell></tableRow>` +
  172. '</table>'
  173. );
  174. widget = viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  175. const model = editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  176. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  177. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  178. const finalPointerPosition = initialPosition.clone().moveBy( -40, -20 );
  179. mouseMock.dragTo( editor, domParts.resizeHandle, {
  180. from: initialPosition,
  181. to: finalPointerPosition
  182. } );
  183. expect( model.getAttribute( 'width' ) ).to.be.equal( '60px' );
  184. } );
  185. } );
  186. describe( 'srcset integration', () => {
  187. beforeEach( () => createEditor() );
  188. // The image is 96x96 pixels.
  189. const imageBaseUrl = '/assets/sample.png';
  190. let model;
  191. let images = [];
  192. before( () => {
  193. return Promise.all( [
  194. preloadImage( imageBaseUrl ),
  195. preloadImage( imageBaseUrl + '?a' ),
  196. preloadImage( imageBaseUrl + '?b' ),
  197. preloadImage( imageBaseUrl + '?c' )
  198. ] ).then( loadedImages => {
  199. images = loadedImages;
  200. } );
  201. } );
  202. after( () => {
  203. for ( const image of images ) {
  204. image.remove();
  205. }
  206. } );
  207. beforeEach( () => {
  208. editor.setData(
  209. `<figure class="image">
  210. <img src="${ imageBaseUrl }"
  211. srcset="${ imageBaseUrl }?a 110w,
  212. ${ imageBaseUrl }?b 440w,
  213. ${ imageBaseUrl }?c 1025w"
  214. sizes="100vw" width="96">
  215. </figure>`
  216. );
  217. widget = viewDocument.getRoot().getChild( 0 );
  218. model = editor.model.document.getRoot().getChild( 0 );
  219. } );
  220. it( 'works with images containing srcset', () => {
  221. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  222. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  223. const finalPointerPosition = initialPosition.clone().moveBy( -20, -20 );
  224. mouseMock.dragTo( editor, domParts.resizeHandle, {
  225. from: initialPosition,
  226. to: finalPointerPosition
  227. } );
  228. expect( model.getAttribute( 'width' ) ).to.be.equal( '76px' );
  229. } );
  230. it( 'retains width after removing srcset', () => {
  231. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  232. const initialPosition = getHandleCenterPoint( domParts.widget, 'bottom-right' );
  233. const finalPointerPosition = initialPosition.clone().moveBy( -16, -16 );
  234. mouseMock.dragTo( editor, domParts.resizeHandle, {
  235. from: initialPosition,
  236. to: finalPointerPosition
  237. } );
  238. editor.model.change( writer => {
  239. writer.removeAttribute( 'srcset', model );
  240. } );
  241. const expectedHtml = '<figure class="image image_resized" style="width:80px;"><img src="/assets/sample.png"></figure>';
  242. expect( editor.getData() ).to.be.equal( expectedHtml );
  243. } );
  244. async function preloadImage( imageUrl ) {
  245. const image = document.createElement( 'img' );
  246. image.src = imageUrl;
  247. return new Promise( ( resolve, reject ) => {
  248. image.addEventListener( 'load', () => resolve( image ) );
  249. image.addEventListener( 'error', () => reject( image ) );
  250. document.body.appendChild( image );
  251. } );
  252. }
  253. } );
  254. describe( 'widget toolbar integration', () => {
  255. let widgetToolbarRepository;
  256. beforeEach( () => createEditor( {
  257. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize, ImageToolbar, ImageTextAlternative ],
  258. image: {
  259. toolbar: [ 'imageTextAlternative' ],
  260. resizeUnit: 'px'
  261. }
  262. } ) );
  263. beforeEach( async () => {
  264. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  265. widget = viewDocument.getRoot().getChild( 1 );
  266. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  267. } );
  268. it( 'default toolbar visibility', async () => {
  269. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  270. } );
  271. it( 'visibility during the resize', async () => {
  272. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  273. mouseMock.down( editor, domResizeHandle );
  274. expect( widgetToolbarRepository.isEnabled ).to.be.false;
  275. mouseMock.up( editor, domResizeHandle );
  276. } );
  277. it( 'visibility after the resize', async () => {
  278. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  279. mouseMock.down( editor, domResizeHandle );
  280. mouseMock.up( editor, domResizeHandle );
  281. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  282. } );
  283. it( 'visibility after the resize was canceled', async () => {
  284. const resizer = getSelectedImageResizer( editor );
  285. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  286. mouseMock.down( editor, domResizeHandle );
  287. resizer.cancel();
  288. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  289. } );
  290. it( 'restores toolbar when clicking the handle without drag', () => {
  291. // (https://github.com/ckeditor/ckeditor5-widget/pull/112#pullrequestreview-337725256).
  292. const domResizeHandle = getWidgetDomParts( editor, widget, 'bottom-left' ).resizeHandle;
  293. mouseMock.down( editor, domResizeHandle );
  294. mouseMock.up( editor, domResizeHandle );
  295. expect( widgetToolbarRepository.isEnabled ).to.be.true;
  296. } );
  297. } );
  298. function wait( delay ) {
  299. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  300. }
  301. function getDomWidth( domElement ) {
  302. return new Rect( domElement ).width;
  303. }
  304. function getSelectedImageResizer( editor ) {
  305. return editor.plugins.get( 'WidgetResize' )._getResizerByViewElement(
  306. editor.editing.view.document.selection.getSelectedElement()
  307. );
  308. }
  309. function createEditor( config ) {
  310. editorElement = document.createElement( 'div' );
  311. absoluteContainer.appendChild( editorElement );
  312. return ClassicEditor
  313. .create( editorElement, config || {
  314. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ],
  315. image: {
  316. resizeUnit: 'px'
  317. }
  318. } )
  319. .then( newEditor => {
  320. editor = newEditor;
  321. view = editor.editing.view;
  322. viewDocument = view.document;
  323. widget = viewDocument.getRoot().getChild( 1 );
  324. focusEditor( editor );
  325. } );
  326. }
  327. } );