imageresizeui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. },
  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. it( 'should be disabled when command is disabled', () => {
  67. command.isEnabled = true;
  68. expect( plugin.isEnabled ).to.be.true;
  69. command.isEnabled = false;
  70. expect( plugin.isEnabled ).to.be.false;
  71. } );
  72. } );
  73. it( 'should create `_resizeUnit`', () => {
  74. const unit = plugin._resizeUnit;
  75. expect( unit ).to.equal( '%' );
  76. } );
  77. describe( 'init()', () => {
  78. it( 'should have set "%" resize unit', () => {
  79. expect( plugin._resizeUnit ).to.equal( '%' );
  80. } );
  81. it( 'should have set "%" resize unit if not defined', async () => {
  82. const dropdown = editor.ui.componentFactory.create( 'imageResize' );
  83. const button = dropdown.listView.items.get( 1 ).children.get( 0 );
  84. command.isEnabled = true;
  85. dropdown.render();
  86. button.fire( 'execute' );
  87. expect( editor.config.get( 'image.resizeUnit' ) ).to.be.undefined;
  88. expect( command.value.width.includes( '%' ) ).to.be.true;
  89. await editor.destroy();
  90. } );
  91. it( 'should have set "px" resize unit', async () => {
  92. const editor = await ClassicTestEditor
  93. .create( editorElement, {
  94. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  95. image: {
  96. resizeUnit: 'px',
  97. resizeOptions
  98. }
  99. } );
  100. const dropdown = editor.ui.componentFactory.create( 'imageResize' );
  101. const button = dropdown.listView.items.get( 1 ).children.get( 0 );
  102. const command = editor.commands.get( 'imageResize' );
  103. command.isEnabled = true;
  104. button.fire( 'execute' );
  105. expect( command.value.width.includes( 'px' ) ).to.be.true;
  106. await editor.destroy();
  107. } );
  108. it( 'should not register a dropdown or buttons if no resize options passed', async () => {
  109. const editor = await ClassicTestEditor
  110. .create( editorElement, {
  111. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  112. image: {
  113. resizeUnit: 'px'
  114. }
  115. } );
  116. const resizeOptions = editor.config.get( 'image.resizeOptions' );
  117. expect( resizeOptions ).to.be.undefined;
  118. expect( editor.ui.componentFactory.has( 'imageResize' ) ).to.be.false;
  119. await editor.destroy();
  120. } );
  121. } );
  122. describe( 'resize options dropdown', () => {
  123. it( 'should be disabled when plugin is disabled', () => {
  124. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  125. plugin.isEnabled = true;
  126. expect( dropdownView.isEnabled ).to.be.true;
  127. plugin.isEnabled = false;
  128. expect( dropdownView.isEnabled ).to.be.false;
  129. } );
  130. it( 'should be an instance of `DropdownView` if component is created without a value suffix', () => {
  131. expect( editor.ui.componentFactory.create( 'imageResize' ) ).to.be.instanceof( DropdownView );
  132. } );
  133. it( 'should have 4 resize options in the `imageResize` dropdown', () => {
  134. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  135. expect( dropdownView.listView.items.length ).to.equal( 4 );
  136. expect( dropdownView.listView.items.first.element.textContent ).to.equal( 'Original' );
  137. expect( dropdownView.listView.items._items[ 1 ].element.textContent ).to.equal( '25%' );
  138. expect( dropdownView.listView.items.last.element.textContent ).to.equal( '75%' );
  139. } );
  140. it( 'should be created with a proper tooltip', () => {
  141. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  142. expect( dropdownView.buttonView.tooltip ).to.equal( 'Resize image' );
  143. } );
  144. it( 'should execute resize command with a proper value', () => {
  145. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  146. const commandSpy = sinon.spy( command, 'execute' );
  147. const resizeBy50Percent = dropdownView.listView.items._items[ 1 ].children._items[ 0 ];
  148. command.isEnabled = true;
  149. resizeBy50Percent.fire( 'execute' );
  150. sinon.assert.calledOnce( commandSpy );
  151. expect( command.value.width ).to.equal( '25%' );
  152. } );
  153. } );
  154. describe( 'resize option button', () => {
  155. let editor, plugin;
  156. beforeEach( async () => {
  157. editor = await ClassicTestEditor
  158. .create( editorElement, {
  159. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  160. image: {
  161. resizeUnit: '%',
  162. resizeOptions: [ {
  163. name: 'imageResize:original',
  164. value: null,
  165. icon: 'original'
  166. },
  167. {
  168. name: 'imageResize:25',
  169. value: '25',
  170. icon: 'small'
  171. },
  172. {
  173. name: 'imageResize:50',
  174. value: '50',
  175. icon: 'medium'
  176. },
  177. {
  178. name: 'imageResize:75',
  179. value: '75',
  180. icon: 'large'
  181. } ],
  182. toolbar: [ 'imageResize:original', 'imageResize:25', 'imageResize:50', 'imageResize:75' ]
  183. }
  184. } );
  185. plugin = editor.plugins.get( 'ImageResizeUI' );
  186. } );
  187. afterEach( async () => {
  188. if ( editorElement ) {
  189. editorElement.remove();
  190. }
  191. if ( editor ) {
  192. await editor.destroy();
  193. }
  194. } );
  195. it( 'should be bound to `#isEnabled`', () => {
  196. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  197. plugin.isEnabled = true;
  198. expect( buttonView.isEnabled ).to.be.true;
  199. plugin.isEnabled = false;
  200. expect( buttonView.isEnabled ).to.be.false;
  201. } );
  202. it( 'should be an instance of `ButtonView` if component is created with a value suffix', () => {
  203. expect( editor.ui.componentFactory.create( 'imageResize:50' ) ).to.be.instanceof( ButtonView );
  204. } );
  205. it( 'should be created with invisible "Resize image: 30%" label when is provided', async () => {
  206. const editor = await ClassicTestEditor
  207. .create( editorElement, {
  208. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  209. image: {
  210. resizeUnit: '%',
  211. resizeOptions: [ {
  212. name: 'imageResize:30',
  213. value: '30',
  214. label: 'Resize image: 30%',
  215. icon: 'small'
  216. } ],
  217. toolbar: [ 'imageResize:30' ]
  218. }
  219. } );
  220. const buttonView = editor.ui.componentFactory.create( 'imageResize:30' );
  221. buttonView.render();
  222. expect( buttonView.withText ).to.be.false;
  223. expect( buttonView.label ).to.equal( 'Resize image: 30%' );
  224. expect( buttonView.labelView ).to.be.instanceOf( View );
  225. editor.destroy();
  226. } );
  227. it( 'should be created with invisible "Resize image to 50%" label when is not provided', async () => {
  228. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  229. buttonView.render();
  230. expect( buttonView.withText ).to.be.false;
  231. expect( buttonView.label ).to.equal( 'Resize image to 50%' );
  232. expect( buttonView.labelView ).to.be.instanceOf( View );
  233. editor.destroy();
  234. } );
  235. it( 'should be created with a proper tooltip, depends on the set value', () => {
  236. const buttonViewOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  237. const buttonView50 = editor.ui.componentFactory.create( 'imageResize:50' );
  238. buttonViewOriginal.render();
  239. buttonView50.render();
  240. expect( buttonViewOriginal.tooltip ).to.equal( 'Resize image to the original size' );
  241. expect( buttonView50.tooltip ).to.equal( 'Resize image to 50%' );
  242. } );
  243. it( 'should have `commandValue` equal "50%"', () => {
  244. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  245. expect( buttonView.commandValue ).to.equal( '50%' );
  246. } );
  247. it( 'should execute `imageResize` command with "50%" value', () => {
  248. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  249. const command = editor.commands.get( 'imageResize' );
  250. const commandSpy = sinon.spy( command, 'execute' );
  251. command.isEnabled = true;
  252. buttonView.fire( 'execute' );
  253. sinon.assert.calledOnce( commandSpy );
  254. expect( command.value.width ).to.equal( '50%' );
  255. } );
  256. it( 'should have set a proper icon', () => {
  257. const buttonOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  258. const button25 = editor.ui.componentFactory.create( 'imageResize:25' );
  259. const button50 = editor.ui.componentFactory.create( 'imageResize:50' );
  260. const button75 = editor.ui.componentFactory.create( 'imageResize:75' );
  261. expect( buttonOriginal.icon ).to.deep.equal( iconFull );
  262. expect( button25.icon ).to.deep.equal( iconSmall );
  263. expect( button50.icon ).to.deep.equal( iconMedium );
  264. expect( button75.icon ).to.deep.equal( iconLarge );
  265. } );
  266. it( 'should throw the CKEditorError if no `icon` is provided', async () => {
  267. const editor = await ClassicTestEditor
  268. .create( editorElement, {
  269. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  270. image: {
  271. resizeUnit: '%',
  272. resizeOptions: [ {
  273. name: 'imageResize:noicon',
  274. value: '100'
  275. } ],
  276. toolbar: [ 'imageResize:noicon' ]
  277. }
  278. } );
  279. const errMsg = 'The resize option "imageResize:noicon" misses an `icon` property ' +
  280. 'or its value doesn\'t match the available options.';
  281. expectToThrowCKEditorError( () => {
  282. editor.ui.componentFactory.create( 'imageResize:noicon' );
  283. }, errMsg, editor );
  284. editor.destroy();
  285. } );
  286. } );
  287. } );