fontsizeui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 'Normal': 'Normal',
  20. 'Tiny': 'Tiny',
  21. 'Small': 'Small',
  22. 'Big': 'Big',
  23. 'Huge': 'Huge'
  24. } );
  25. addTranslations( 'pl', {
  26. 'Font Size': 'Rozmiar czcionki',
  27. 'Normal': 'Normalny',
  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( 'tooltip', 'Font Size' );
  61. expect( button ).to.have.property( 'icon', fontSizeIcon );
  62. expect( button ).to.have.property( 'withText', false );
  63. } );
  64. it( 'should add custom CSS class to dropdown', () => {
  65. const dropdown = editor.ui.componentFactory.create( 'fontSize' );
  66. dropdown.render();
  67. expect( dropdown.element.classList.contains( 'ck-font-size-dropdown' ) ).to.be.true;
  68. } );
  69. it( 'should focus view after command execution', () => {
  70. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  71. const dropdown = editor.ui.componentFactory.create( 'fontSize' );
  72. dropdown.commandName = 'fontSize';
  73. dropdown.fire( 'execute' );
  74. sinon.assert.calledOnce( focusSpy );
  75. } );
  76. describe( 'model to command binding', () => {
  77. it( 'isEnabled', () => {
  78. command.isEnabled = false;
  79. expect( dropdown.buttonView.isEnabled ).to.be.false;
  80. command.isEnabled = true;
  81. expect( dropdown.buttonView.isEnabled ).to.be.true;
  82. } );
  83. } );
  84. describe( 'localization', () => {
  85. beforeEach( () => {
  86. return localizedEditor( [ 'tiny', 'small', 'normal', 'big', 'huge' ] );
  87. } );
  88. it( 'does not alter normalizeOptions() internals', () => {
  89. const options = normalizeOptions( [ 'tiny', 'small', 'normal', 'big', 'huge' ] );
  90. expect( options ).to.deep.equal( [
  91. { title: 'Tiny', model: 'text-tiny', view: { name: 'span', class: 'text-tiny' } },
  92. { title: 'Small', model: 'text-small', view: { name: 'span', class: 'text-small' } },
  93. { title: 'Normal', model: undefined },
  94. { title: 'Big', model: 'text-big', view: { name: 'span', class: 'text-big' } },
  95. { title: 'Huge', model: 'text-huge', view: { name: 'span', class: 'text-huge' } }
  96. ] );
  97. } );
  98. it( 'works for the #buttonView', () => {
  99. const buttonView = dropdown.buttonView;
  100. expect( buttonView.tooltip ).to.equal( 'Rozmiar czcionki' );
  101. } );
  102. it( 'works for the listView#items in the panel', () => {
  103. const listView = dropdown.listView;
  104. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  105. 'Tyci',
  106. 'Mały',
  107. 'Normalny',
  108. 'Duży',
  109. 'Ogromny'
  110. ] );
  111. } );
  112. function localizedEditor( options ) {
  113. const editorElement = document.createElement( 'div' );
  114. document.body.appendChild( editorElement );
  115. return ClassicTestEditor
  116. .create( editorElement, {
  117. plugins: [ FontSizeEditing, FontSizeUI ],
  118. toolbar: [ 'fontSize' ],
  119. language: 'pl',
  120. fontSize: {
  121. options
  122. }
  123. } )
  124. .then( newEditor => {
  125. editor = newEditor;
  126. dropdown = editor.ui.componentFactory.create( 'fontSize' );
  127. command = editor.commands.get( 'fontSize' );
  128. editorElement.remove();
  129. return editor.destroy();
  130. } );
  131. }
  132. } );
  133. } );
  134. } );