imageresizeediting.js 17 KB

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