fontsizeui.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import FontSizeEditing from '../../src/fontsize/fontsizeediting';
  7. import FontSizeUI from '../../src/fontsize/fontsizeui';
  8. import fontSizeIcon from '../../theme/icons/font-size.svg';
  9. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  12. import { normalizeOptions } from '../../src/fontsize/utils';
  13. testUtils.createSinonSandbox();
  14. describe( 'FontSizeUI', () => {
  15. let editor, command, element;
  16. before( () => {
  17. addTranslations( 'en', {
  18. 'Font Size': 'Font Size',
  19. 'Default': 'Default',
  20. 'Tiny': 'Tiny',
  21. 'Small': 'Small',
  22. 'Big': 'Big',
  23. 'Huge': 'Huge'
  24. } );
  25. addTranslations( 'pl', {
  26. 'Font Size': 'Rozmiar czcionki',
  27. 'Default': 'Domyślny',
  28. 'Tiny': 'Tyci',
  29. 'Small': 'Mały',
  30. 'Big': 'Duży',
  31. 'Huge': 'Ogromny'
  32. } );
  33. } );
  34. after( () => {
  35. clearTranslations();
  36. } );
  37. beforeEach( () => {
  38. element = document.createElement( 'div' );
  39. document.body.appendChild( element );
  40. return ClassicTestEditor
  41. .create( element, {
  42. plugins: [ FontSizeEditing, FontSizeUI ]
  43. } )
  44. .then( newEditor => {
  45. editor = newEditor;
  46. } );
  47. } );
  48. afterEach( () => {
  49. element.remove();
  50. return editor.destroy();
  51. } );
  52. describe( 'fontSize Dropdown', () => {
  53. let dropdown;
  54. beforeEach( () => {
  55. command = editor.commands.get( 'fontSize' );
  56. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  57. } );
  58. it( 'button has the base properties', () => {
  59. const button = dropdown.buttonView;
  60. expect( button ).to.have.property( 'label', 'Font Size' );
  61. expect( button ).to.have.property( 'tooltip', true );
  62. expect( button ).to.have.property( 'icon', fontSizeIcon );
  63. } );
  64. it( 'should add custom CSS class to dropdown', () => {
  65. dropdown.render();
  66. expect( dropdown.element.classList.contains( 'ck-font-size-dropdown' ) ).to.be.true;
  67. } );
  68. it( 'should focus view after command execution', () => {
  69. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  70. dropdown.commandName = 'fontSize';
  71. dropdown.fire( 'execute' );
  72. sinon.assert.calledOnce( focusSpy );
  73. } );
  74. it( 'should activate current option in dropdown', () => {
  75. const listView = dropdown.listView;
  76. command.value = undefined;
  77. // The third item is 'default' font size.
  78. expect( listView.items.map( item => item.children.first.isOn ) ).to.deep.equal( [ false, false, true, false, false ] );
  79. command.value = 'tiny';
  80. // The first item is 'tiny' font size.
  81. expect( listView.items.map( item => item.children.first.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  82. } );
  83. describe( 'model to command binding', () => {
  84. it( 'isEnabled', () => {
  85. command.isEnabled = false;
  86. expect( dropdown.buttonView.isEnabled ).to.be.false;
  87. command.isEnabled = true;
  88. expect( dropdown.buttonView.isEnabled ).to.be.true;
  89. } );
  90. } );
  91. describe( 'config', () => {
  92. describe( 'using presets', () => {
  93. beforeEach( () => {
  94. element = document.createElement( 'div' );
  95. document.body.appendChild( element );
  96. return ClassicTestEditor
  97. .create( element, {
  98. plugins: [ FontSizeEditing, FontSizeUI ],
  99. fontSize: {
  100. options: [ 'tiny', 'small', 'default', 'big', 'huge' ]
  101. }
  102. } )
  103. .then( newEditor => {
  104. editor = newEditor;
  105. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  106. } );
  107. } );
  108. it( 'adds css class to listView#items in the panel', () => {
  109. const listView = dropdown.listView;
  110. expect( listView.items.map( item => item.children.first.class ) ).to.deep.equal( [
  111. 'ck-fontsize-option text-tiny',
  112. 'ck-fontsize-option text-small',
  113. 'ck-fontsize-option',
  114. 'ck-fontsize-option text-big',
  115. 'ck-fontsize-option text-huge'
  116. ] );
  117. } );
  118. } );
  119. describe( 'using numerical values', () => {
  120. beforeEach( () => {
  121. element = document.createElement( 'div' );
  122. document.body.appendChild( element );
  123. return ClassicTestEditor
  124. .create( element, {
  125. plugins: [ FontSizeEditing, FontSizeUI ],
  126. fontSize: {
  127. options: [ 10, 12, 'default', 16, 18 ]
  128. }
  129. } )
  130. .then( newEditor => {
  131. editor = newEditor;
  132. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  133. } );
  134. } );
  135. it( 'adds css class to listView#items in the panel', () => {
  136. const listView = dropdown.listView;
  137. expect( listView.items.map( item => item.children.first.class ) ).to.deep.equal( [
  138. 'ck-fontsize-option',
  139. 'ck-fontsize-option',
  140. 'ck-fontsize-option',
  141. 'ck-fontsize-option',
  142. 'ck-fontsize-option'
  143. ] );
  144. } );
  145. it( 'adds font-size style to listView#items in the panel', () => {
  146. const listView = dropdown.listView;
  147. expect( listView.items.map( item => item.children.first.labelStyle ) ).to.deep.equal( [
  148. 'font-size:10px',
  149. 'font-size:12px',
  150. undefined,
  151. 'font-size:16px',
  152. 'font-size:18px'
  153. ] );
  154. } );
  155. } );
  156. } );
  157. describe( 'localization', () => {
  158. beforeEach( () => {
  159. return localizedEditor( [ 'tiny', 'small', 'default', 'big', 'huge' ] );
  160. } );
  161. it( 'does not alter normalizeOptions() internals', () => {
  162. const options = normalizeOptions( [ 'tiny', 'small', 'default', 'big', 'huge' ] );
  163. expect( options ).to.deep.equal( [
  164. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 5 } },
  165. { title: 'Small', model: 'small', view: { name: 'span', classes: 'text-small', priority: 5 } },
  166. { title: 'Default', model: undefined },
  167. { title: 'Big', model: 'big', view: { name: 'span', classes: 'text-big', priority: 5 } },
  168. { title: 'Huge', model: 'huge', view: { name: 'span', classes: 'text-huge', priority: 5 } }
  169. ] );
  170. } );
  171. it( 'works for the #buttonView', () => {
  172. const buttonView = dropdown.buttonView;
  173. expect( buttonView.label ).to.equal( 'Rozmiar czcionki' );
  174. } );
  175. it( 'works for the listView#items in the panel', () => {
  176. const listView = dropdown.listView;
  177. expect( listView.items.map( item => item.children.first.label ) ).to.deep.equal( [
  178. 'Tyci',
  179. 'Mały',
  180. 'Domyślny',
  181. 'Duży',
  182. 'Ogromny'
  183. ] );
  184. } );
  185. function localizedEditor( options ) {
  186. const editorElement = document.createElement( 'div' );
  187. document.body.appendChild( editorElement );
  188. return ClassicTestEditor
  189. .create( editorElement, {
  190. plugins: [ FontSizeEditing, FontSizeUI ],
  191. toolbar: [ 'fontSize' ],
  192. language: 'pl',
  193. fontSize: {
  194. options
  195. }
  196. } )
  197. .then( newEditor => {
  198. editor = newEditor;
  199. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  200. command = editor.commands.get( 'fontSize' );
  201. editorElement.remove();
  202. return editor.destroy();
  203. } );
  204. }
  205. } );
  206. } );
  207. } );