fontfamilyui.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 FontFamilyEditing from '../../src/fontfamily/fontfamilyediting';
  7. import FontFamilyUI from '../../src/fontfamily/fontfamilyui';
  8. import fontFamilyIcon from '../../theme/icons/font-family.svg';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  13. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'FontFamilyUI', () => {
  15. let editor, command, element;
  16. testUtils.createSinonSandbox();
  17. before( () => {
  18. addTranslations( 'en', {
  19. 'Font Family': 'Font Family',
  20. 'Default': 'Default'
  21. } );
  22. addTranslations( 'pl', {
  23. 'Font Family': 'Czcionka',
  24. 'Default': 'Domyślna'
  25. } );
  26. } );
  27. after( () => {
  28. clearTranslations();
  29. } );
  30. beforeEach( () => {
  31. element = document.createElement( 'div' );
  32. document.body.appendChild( element );
  33. return ClassicTestEditor
  34. .create( element, {
  35. plugins: [ FontFamilyEditing, FontFamilyUI ]
  36. } )
  37. .then( newEditor => {
  38. editor = newEditor;
  39. } );
  40. } );
  41. afterEach( () => {
  42. element.remove();
  43. return editor.destroy();
  44. } );
  45. describe( 'fontFamily Dropdown', () => {
  46. let dropdown;
  47. beforeEach( () => {
  48. command = editor.commands.get( 'fontFamily' );
  49. dropdown = editor.ui.componentFactory.create( 'fontFamily' );
  50. } );
  51. it( 'button has the base properties', () => {
  52. const button = dropdown.buttonView;
  53. expect( button ).to.have.property( 'label', 'Font Family' );
  54. expect( button ).to.have.property( 'tooltip', true );
  55. expect( button ).to.have.property( 'icon', fontFamilyIcon );
  56. } );
  57. it( 'should add custom CSS class to dropdown', () => {
  58. const dropdown = editor.ui.componentFactory.create( 'fontFamily' );
  59. dropdown.render();
  60. expect( dropdown.element.classList.contains( 'ck-font-family-dropdown' ) ).to.be.true;
  61. } );
  62. it( 'should focus view after command execution', () => {
  63. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  64. const dropdown = editor.ui.componentFactory.create( 'fontFamily' );
  65. dropdown.commandName = 'fontFamily';
  66. dropdown.fire( 'execute' );
  67. sinon.assert.calledOnce( focusSpy );
  68. } );
  69. it( 'should activate current option in dropdown', () => {
  70. const listView = dropdown.listView;
  71. command.value = undefined;
  72. // The first item is 'default' font family.
  73. expect( listView.items.map( item => item.children.first.isOn ) )
  74. .to.deep.equal( [ true, false, false, false, false, false, false, false, false ] );
  75. command.value = 'Arial, Helvetica, sans-serif';
  76. // The second item is 'Arial' font family.
  77. expect( listView.items.map( item => item.children.first.isOn ) )
  78. .to.deep.equal( [ false, true, false, false, false, false, false, false, false ] );
  79. } );
  80. describe( 'with supportAllValues=true', () => {
  81. let editor, element, command, dropdown;
  82. beforeEach( async () => {
  83. element = document.createElement( 'div' );
  84. document.body.appendChild( element );
  85. editor = await ClassicTestEditor
  86. .create( element, {
  87. plugins: [ Paragraph, FontFamilyEditing, FontFamilyUI ],
  88. fontSize: {
  89. supportAllValues: true
  90. }
  91. } );
  92. command = editor.commands.get( 'fontFamily' );
  93. dropdown = editor.ui.componentFactory.create( 'fontFamily' );
  94. } );
  95. afterEach( async () => {
  96. await editor.destroy();
  97. element.remove();
  98. } );
  99. it( 'should activate the current option in the dropdown for full font family definitions', () => {
  100. const listView = dropdown.listView;
  101. command.value = undefined;
  102. // The first item is 'default' font family.
  103. expect( listView.items.map( item => item.children.first.isOn ) )
  104. .to.deep.equal( [ true, false, false, false, false, false, false, false, false ] );
  105. command.value = '\'Courier New\', Courier, monospace';
  106. // The third item is 'Courier New' font family.
  107. expect( listView.items.map( item => item.children.first.isOn ) )
  108. .to.deep.equal( [ false, false, true, false, false, false, false, false, false ] );
  109. } );
  110. it( 'should apply the complete font-family value (list of font-families)', () => {
  111. const listView = dropdown.listView;
  112. const fontFamilyArialButton = listView.items.get( 1 ).children.first;
  113. setModelData( editor.model, '<paragraph>f[oo]</paragraph>' );
  114. fontFamilyArialButton.fire( 'execute' );
  115. expect( getModelData( editor.model ) ).to.equal(
  116. '<paragraph>f[<$text fontFamily="Arial, Helvetica, sans-serif">oo</$text>]</paragraph>'
  117. );
  118. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Arial, Helvetica, sans-serif;">oo</span></p>' );
  119. } );
  120. } );
  121. describe( 'model to command binding', () => {
  122. it( 'isEnabled', () => {
  123. command.isEnabled = false;
  124. expect( dropdown.buttonView.isEnabled ).to.be.false;
  125. command.isEnabled = true;
  126. expect( dropdown.buttonView.isEnabled ).to.be.true;
  127. } );
  128. } );
  129. describe( 'localization', () => {
  130. let editorElement;
  131. beforeEach( async () => {
  132. await editor.destroy();
  133. return localizedEditor( [ 'default', 'Arial' ] );
  134. } );
  135. afterEach( () => {
  136. editorElement.remove();
  137. } );
  138. it( 'works for the #buttonView', () => {
  139. const buttonView = dropdown.buttonView;
  140. expect( buttonView.label ).to.equal( 'Czcionka' );
  141. } );
  142. it( 'works for the listView#items in the panel', () => {
  143. const listView = dropdown.listView;
  144. expect( listView.items.map( item => item.children.first.label ) ).to.deep.equal( [
  145. 'Domyślna',
  146. 'Arial'
  147. ] );
  148. } );
  149. function localizedEditor( options ) {
  150. editorElement = document.createElement( 'div' );
  151. document.body.appendChild( editorElement );
  152. return ClassicTestEditor
  153. .create( editorElement, {
  154. plugins: [ FontFamilyEditing, FontFamilyUI ],
  155. toolbar: [ 'fontFamily' ],
  156. language: 'pl',
  157. fontFamily: {
  158. options
  159. }
  160. } )
  161. .then( newEditor => {
  162. editor = newEditor;
  163. dropdown = editor.ui.componentFactory.create( 'fontFamily' );
  164. command = editor.commands.get( 'fontFamily' );
  165. editorElement.remove();
  166. return editor.destroy();
  167. } );
  168. }
  169. } );
  170. } );
  171. } );