fontsizeui.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 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. describe( 'FontSizeUI', () => {
  14. let editor, command, element;
  15. testUtils.createSinonSandbox();
  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. beforeEach( () => {
  93. // Each test case in this group creates its own element, so make sure to delete editor created in
  94. // the main beforeEach in this file, as later element and editor vars are overridden (#6002).
  95. element.remove();
  96. return editor.destroy();
  97. } );
  98. describe( 'using presets', () => {
  99. beforeEach( () => {
  100. element = document.createElement( 'div' );
  101. document.body.appendChild( element );
  102. return ClassicTestEditor
  103. .create( element, {
  104. plugins: [ FontSizeEditing, FontSizeUI ],
  105. fontSize: {
  106. options: [ 'tiny', 'small', 'default', 'big', 'huge' ]
  107. }
  108. } )
  109. .then( newEditor => {
  110. editor = newEditor;
  111. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  112. } );
  113. } );
  114. it( 'adds css class to listView#items in the panel', () => {
  115. const listView = dropdown.listView;
  116. expect( listView.items.map( item => item.children.first.class ) ).to.deep.equal( [
  117. 'ck-fontsize-option text-tiny',
  118. 'ck-fontsize-option text-small',
  119. 'ck-fontsize-option',
  120. 'ck-fontsize-option text-big',
  121. 'ck-fontsize-option text-huge'
  122. ] );
  123. } );
  124. } );
  125. describe( 'using numerical values', () => {
  126. beforeEach( () => {
  127. element = document.createElement( 'div' );
  128. document.body.appendChild( element );
  129. return ClassicTestEditor
  130. .create( element, {
  131. plugins: [ FontSizeEditing, FontSizeUI ],
  132. fontSize: {
  133. options: [ 10, 12, 'default', 16, 18 ]
  134. }
  135. } )
  136. .then( newEditor => {
  137. editor = newEditor;
  138. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  139. } );
  140. } );
  141. it( 'adds css class to listView#items in the panel', () => {
  142. const listView = dropdown.listView;
  143. expect( listView.items.map( item => item.children.first.class ) ).to.deep.equal( [
  144. 'ck-fontsize-option',
  145. 'ck-fontsize-option',
  146. 'ck-fontsize-option',
  147. 'ck-fontsize-option',
  148. 'ck-fontsize-option'
  149. ] );
  150. } );
  151. it( 'adds font-size style to listView#items in the panel', () => {
  152. const listView = dropdown.listView;
  153. expect( listView.items.map( item => item.children.first.labelStyle ) ).to.deep.equal( [
  154. 'font-size:10px',
  155. 'font-size:12px',
  156. undefined,
  157. 'font-size:16px',
  158. 'font-size:18px'
  159. ] );
  160. } );
  161. } );
  162. } );
  163. describe( 'localization', () => {
  164. beforeEach( async () => {
  165. element.remove();
  166. await editor.destroy();
  167. return localizedEditor( [ 'tiny', 'small', 'default', 'big', 'huge' ] );
  168. } );
  169. it( 'does not alter normalizeOptions() internals', () => {
  170. const options = normalizeOptions( [ 'tiny', 'small', 'default', 'big', 'huge' ] );
  171. expect( options ).to.deep.equal( [
  172. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 7 } },
  173. { title: 'Small', model: 'small', view: { name: 'span', classes: 'text-small', priority: 7 } },
  174. { title: 'Default', model: undefined },
  175. { title: 'Big', model: 'big', view: { name: 'span', classes: 'text-big', priority: 7 } },
  176. { title: 'Huge', model: 'huge', view: { name: 'span', classes: 'text-huge', priority: 7 } }
  177. ] );
  178. } );
  179. it( 'works for the #buttonView', () => {
  180. const buttonView = dropdown.buttonView;
  181. expect( buttonView.label ).to.equal( 'Rozmiar czcionki' );
  182. } );
  183. it( 'works for the listView#items in the panel', () => {
  184. const listView = dropdown.listView;
  185. expect( listView.items.map( item => item.children.first.label ) ).to.deep.equal( [
  186. 'Tyci',
  187. 'Mały',
  188. 'Domyślny',
  189. 'Duży',
  190. 'Ogromny'
  191. ] );
  192. } );
  193. function localizedEditor( options ) {
  194. const editorElement = document.createElement( 'div' );
  195. document.body.appendChild( editorElement );
  196. return ClassicTestEditor
  197. .create( editorElement, {
  198. plugins: [ FontSizeEditing, FontSizeUI ],
  199. toolbar: [ 'fontSize' ],
  200. language: 'pl',
  201. fontSize: {
  202. options
  203. }
  204. } )
  205. .then( newEditor => {
  206. editor = newEditor;
  207. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  208. command = editor.commands.get( 'fontSize' );
  209. editorElement.remove();
  210. return editor.destroy();
  211. } );
  212. }
  213. } );
  214. } );
  215. } );