8
0

imageresizeui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Image from '../../src/image';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import ImageResizeUI from '../../src/imageresize/imageresizeui';
  13. import ImageStyle from '../../src/imagestyle';
  14. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  15. import Table from '@ckeditor/ckeditor5-table/src/table';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  18. import iconSmall from '../../theme/icons/image-resize-small.svg';
  19. import iconMedium from '../../theme/icons/image-resize-medium.svg';
  20. import iconLarge from '../../theme/icons/image-resize-large.svg';
  21. import iconFull from '../../theme/icons/image-resize-full.svg';
  22. describe( 'ImageResizeUI', () => {
  23. let plugin, command, editor, editorElement;
  24. const resizeOptions = [ {
  25. name: 'imageResize:original',
  26. value: null,
  27. icon: 'original'
  28. },
  29. {
  30. name: 'imageResize:25',
  31. label: '25%',
  32. value: '25',
  33. icon: 'small'
  34. },
  35. {
  36. name: 'imageResize:50',
  37. label: '50%',
  38. value: '50',
  39. icon: 'medium'
  40. },
  41. {
  42. name: 'imageResize:75',
  43. label: '75%',
  44. value: '75',
  45. icon: 'large'
  46. } ];
  47. testUtils.createSinonSandbox();
  48. beforeEach( async () => {
  49. editorElement = document.createElement( 'div' );
  50. document.body.appendChild( editorElement );
  51. editor = await ClassicTestEditor
  52. .create( editorElement, {
  53. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  54. image: {
  55. resizeUnit: '%',
  56. resizeOptions
  57. }
  58. } );
  59. command = editor.commands.get( 'imageResize' );
  60. plugin = editor.plugins.get( 'ImageResizeUI' );
  61. } );
  62. afterEach( async () => {
  63. if ( editorElement ) {
  64. editorElement.remove();
  65. }
  66. if ( editor ) {
  67. await editor.destroy();
  68. }
  69. } );
  70. describe( 'plugin', () => {
  71. it( 'should be named', () => {
  72. expect( ImageResizeUI.pluginName ).to.equal( 'ImageResizeUI' );
  73. } );
  74. it( 'should be disabled when command is disabled', () => {
  75. command.isEnabled = true;
  76. expect( plugin.isEnabled ).to.be.true;
  77. command.isEnabled = false;
  78. expect( plugin.isEnabled ).to.be.false;
  79. } );
  80. } );
  81. it( 'should create `_resizeUnit`', () => {
  82. const unit = plugin._resizeUnit;
  83. expect( unit ).to.equal( '%' );
  84. } );
  85. describe( 'init()', () => {
  86. it( 'should have set "%" resize unit', () => {
  87. const unit = editor.config.get( 'image.resizeUnit' );
  88. expect( unit ).to.equal( '%' );
  89. } );
  90. it( 'should have set "%" resize unit if not defined', async () => {
  91. const editor = await ClassicTestEditor
  92. .create( editorElement, {
  93. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  94. image: {
  95. resizeOptions
  96. }
  97. } );
  98. const button = editor.ui.componentFactory.create( 'imageResize:50' );
  99. const command = editor.commands.get( 'imageResize' );
  100. command.isEnabled = true;
  101. button.fire( 'execute' );
  102. expect( command.value.width.includes( '%' ) ).to.be.true;
  103. await editor.destroy();
  104. } );
  105. it( 'should have set "px" resize unit', async () => {
  106. const editor = await ClassicTestEditor
  107. .create( editorElement, {
  108. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  109. image: {
  110. resizeUnit: 'px',
  111. resizeOptions
  112. }
  113. } );
  114. const button = editor.ui.componentFactory.create( 'imageResize:50' );
  115. const command = editor.commands.get( 'imageResize' );
  116. command.isEnabled = true;
  117. button.fire( 'execute' );
  118. expect( command.value.width.includes( 'px' ) ).to.be.true;
  119. await editor.destroy();
  120. } );
  121. it( 'should not register a dropdown or buttons if no resize options passed', async () => {
  122. const editor = await ClassicTestEditor
  123. .create( editorElement, {
  124. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  125. image: {
  126. resizeUnit: 'px'
  127. }
  128. } );
  129. const resizeOptions = editor.config.get( 'image.resizeOptions' );
  130. expect( resizeOptions ).to.be.undefined;
  131. expect( editor.ui.componentFactory.has( 'imageResize' ) ).to.be.false;
  132. await editor.destroy();
  133. } );
  134. } );
  135. describe( 'resize options dropdown', () => {
  136. it( 'should be disabled when plugin is disabled', () => {
  137. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  138. plugin.isEnabled = true;
  139. expect( dropdownView.isEnabled ).to.be.true;
  140. plugin.isEnabled = false;
  141. expect( dropdownView.isEnabled ).to.be.false;
  142. } );
  143. it( 'should be an instance of `DropdownView` if component is created without a value suffix', () => {
  144. expect( editor.ui.componentFactory.create( 'imageResize' ) ).to.be.instanceof( DropdownView );
  145. } );
  146. it( 'should have 4 resize options in the `imageResize` dropdown', () => {
  147. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  148. expect( dropdownView.listView.items.length ).to.equal( 4 );
  149. expect( dropdownView.listView.items.first.element.textContent ).to.equal( 'Original' );
  150. expect( dropdownView.listView.items._items[ 1 ].element.textContent ).to.equal( '25%' );
  151. expect( dropdownView.listView.items.last.element.textContent ).to.equal( '75%' );
  152. } );
  153. it( 'should be created with a proper tooltip', () => {
  154. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  155. expect( dropdownView.buttonView.tooltip ).to.equal( 'Resize image' );
  156. } );
  157. it( 'should execute resize command with a proper value', () => {
  158. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  159. const commandSpy = sinon.spy( command, 'execute' );
  160. const resizeBy50Percent = dropdownView.listView.items._items[ 1 ].children._items[ 0 ];
  161. command.isEnabled = true;
  162. resizeBy50Percent.fire( 'execute' );
  163. sinon.assert.calledOnce( commandSpy );
  164. expect( command.value.width ).to.equal( '25%' );
  165. } );
  166. } );
  167. describe( 'resize option button', () => {
  168. it( 'should be bound to `#isEnabled`', () => {
  169. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  170. plugin.isEnabled = true;
  171. expect( buttonView.isEnabled ).to.be.true;
  172. plugin.isEnabled = false;
  173. expect( buttonView.isEnabled ).to.be.false;
  174. } );
  175. it( 'should be an instance of `ButtonView` if component is created with a value suffix', () => {
  176. expect( editor.ui.componentFactory.create( 'imageResize:50' ) ).to.be.instanceof( ButtonView );
  177. } );
  178. it( 'should be created with invisible "50%" label when is provided', () => {
  179. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  180. buttonView.render();
  181. expect( buttonView.withText ).to.be.false;
  182. expect( buttonView.label ).to.equal( '50%' );
  183. expect( buttonView.labelView ).to.be.instanceOf( View );
  184. } );
  185. it( 'should be created with invisible "50%" label when is not provided', async () => {
  186. const editor = await ClassicTestEditor
  187. .create( editorElement, {
  188. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  189. image: {
  190. resizeUnit: '%',
  191. resizeOptions: [ {
  192. name: 'imageResize:50',
  193. value: '50',
  194. icon: 'medium'
  195. } ]
  196. }
  197. } );
  198. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  199. buttonView.render();
  200. expect( buttonView.withText ).to.be.false;
  201. expect( buttonView.label ).to.equal( '50%' );
  202. expect( buttonView.labelView ).to.be.instanceOf( View );
  203. editor.destroy();
  204. } );
  205. it( 'should be created with a proper tooltip, depends on the set value', () => {
  206. const buttonViewOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  207. const buttonView50 = editor.ui.componentFactory.create( 'imageResize:50' );
  208. buttonViewOriginal.render();
  209. buttonView50.render();
  210. expect( buttonViewOriginal.tooltip ).to.equal( 'Resize image to the original size' );
  211. expect( buttonView50.tooltip ).to.equal( 'Resize image to 50%' );
  212. } );
  213. it( 'should have `commandValue` equal "50%"', () => {
  214. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  215. expect( buttonView.commandValue ).to.equal( '50%' );
  216. } );
  217. it( 'should execute `imageResize` command with "50%" value', () => {
  218. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  219. const command = editor.commands.get( 'imageResize' );
  220. const commandSpy = sinon.spy( command, 'execute' );
  221. command.isEnabled = true;
  222. buttonView.fire( 'execute' );
  223. sinon.assert.calledOnce( commandSpy );
  224. expect( command.value.width ).to.equal( '50%' );
  225. } );
  226. it( 'should have set a proper icon', () => {
  227. const buttonOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  228. const button25 = editor.ui.componentFactory.create( 'imageResize:25' );
  229. const button50 = editor.ui.componentFactory.create( 'imageResize:50' );
  230. const button75 = editor.ui.componentFactory.create( 'imageResize:75' );
  231. expect( buttonOriginal.icon ).to.deep.equal( iconFull );
  232. expect( button25.icon ).to.deep.equal( iconSmall );
  233. expect( button50.icon ).to.deep.equal( iconMedium );
  234. expect( button75.icon ).to.deep.equal( iconLarge );
  235. } );
  236. it( 'should throw the CKEditorError if no `icon` is provided', async () => {
  237. const editor = await ClassicTestEditor
  238. .create( editorElement, {
  239. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  240. image: {
  241. resizeUnit: '%',
  242. resizeOptions: [ {
  243. name: 'imageResize:noicon',
  244. value: '100'
  245. } ],
  246. toolbar: [ 'imageResize:noicon' ]
  247. }
  248. } );
  249. const errMsg = 'The resize option "imageResize:noicon" misses an `icon` property ' +
  250. 'or its value doesn\'t match the available options.';
  251. expectToThrowCKEditorError( () => {
  252. editor.ui.componentFactory.create( 'imageResize:noicon' );
  253. }, errMsg );
  254. editor.destroy();
  255. } );
  256. } );
  257. } );