imageresizeui.js 9.7 KB

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