headingui.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Heading from '../src/heading';
  8. import HeadingEditing from '../src/headingediting';
  9. import HeadingUI from '../src/headingui';
  10. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  11. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. testUtils.createSinonSandbox();
  15. describe( 'HeadingUI', () => {
  16. let editor, editorElement, dropdown;
  17. before( () => {
  18. addTranslations( 'en', {
  19. 'Choose heading': 'Choose heading',
  20. 'Paragraph': 'Paragraph',
  21. 'Heading': 'Heading',
  22. 'Heading 1': 'Heading 1',
  23. 'Heading 2': 'Heading 2',
  24. } );
  25. addTranslations( 'pl', {
  26. 'Choose heading': 'Wybierz nagłówek',
  27. 'Paragraph': 'Akapit',
  28. 'Heading': 'Nagłówek',
  29. 'Heading 1': 'Nagłówek 1',
  30. 'Heading 2': 'Nagłówek 2',
  31. } );
  32. } );
  33. after( () => {
  34. clearTranslations();
  35. } );
  36. beforeEach( () => {
  37. editorElement = document.createElement( 'div' );
  38. document.body.appendChild( editorElement );
  39. return ClassicTestEditor
  40. .create( editorElement, {
  41. plugins: [ HeadingUI, HeadingEditing ],
  42. toolbar: [ 'heading' ]
  43. } )
  44. .then( newEditor => {
  45. editor = newEditor;
  46. dropdown = editor.ui.componentFactory.create( 'headings' );
  47. // Set data so the commands will be enabled.
  48. setData( editor.model, '<paragraph>f{}oo</paragraph>' );
  49. } );
  50. } );
  51. afterEach( () => {
  52. editorElement.remove();
  53. return editor.destroy();
  54. } );
  55. describe( 'init()', () => {
  56. it( 'should register options feature component', () => {
  57. const dropdown = editor.ui.componentFactory.create( 'headings' );
  58. expect( dropdown ).to.be.instanceOf( DropdownView );
  59. expect( dropdown.buttonView.isEnabled ).to.be.true;
  60. expect( dropdown.buttonView.isOn ).to.be.false;
  61. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  62. expect( dropdown.buttonView.tooltip ).to.equal( 'Heading' );
  63. } );
  64. it( 'should execute format command on model execute event', () => {
  65. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  66. const dropdown = editor.ui.componentFactory.create( 'headings' );
  67. dropdown.commandName = 'paragraph';
  68. dropdown.fire( 'execute' );
  69. sinon.assert.calledOnce( executeSpy );
  70. sinon.assert.calledWithExactly( executeSpy, 'paragraph' );
  71. } );
  72. it( 'should focus view after command execution', () => {
  73. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  74. const dropdown = editor.ui.componentFactory.create( 'headings' );
  75. dropdown.commandName = 'paragraph';
  76. dropdown.fire( 'execute' );
  77. sinon.assert.calledOnce( focusSpy );
  78. } );
  79. it( 'should add custom CSS class to dropdown', () => {
  80. const dropdown = editor.ui.componentFactory.create( 'headings' );
  81. dropdown.render();
  82. expect( dropdown.element.classList.contains( 'ck-heading-dropdown' ) ).to.be.true;
  83. } );
  84. describe( 'model to command binding', () => {
  85. let commands;
  86. beforeEach( () => {
  87. commands = {};
  88. editor.config.get( 'heading.options' ).forEach( ( { model } ) => {
  89. commands[ model ] = editor.commands.get( model );
  90. } );
  91. } );
  92. it( 'isEnabled', () => {
  93. for ( const name in commands ) {
  94. commands[ name ].isEnabled = false;
  95. }
  96. expect( dropdown.buttonView.isEnabled ).to.be.false;
  97. commands.heading2.isEnabled = true;
  98. expect( dropdown.buttonView.isEnabled ).to.be.true;
  99. } );
  100. it( 'label', () => {
  101. for ( const name in commands ) {
  102. commands[ name ].value = false;
  103. }
  104. expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
  105. commands.heading2.value = true;
  106. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  107. } );
  108. } );
  109. describe( 'localization', () => {
  110. let commands, editor, dropdown;
  111. beforeEach( () => {
  112. return localizedEditor( [
  113. { model: 'paragraph', title: 'Paragraph' },
  114. { model: 'heading1', view: { name: 'h2' }, title: 'Heading 1' },
  115. { model: 'heading2', view: { name: 'h3' }, title: 'Heading 2' }
  116. ] );
  117. } );
  118. it( 'does not alter the original config', () => {
  119. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  120. { model: 'paragraph', title: 'Paragraph' },
  121. { model: 'heading1', view: { name: 'h2' }, title: 'Heading 1' },
  122. { model: 'heading2', view: { name: 'h3' }, title: 'Heading 2' }
  123. ] );
  124. } );
  125. it( 'works for the #buttonView', () => {
  126. const buttonView = dropdown.buttonView;
  127. // Setting manually paragraph.value to `false` because there might be some content in editor
  128. // after initialisation (for example empty <p></p> inserted when editor is empty).
  129. commands.paragraph.value = false;
  130. expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
  131. expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
  132. commands.paragraph.value = true;
  133. expect( buttonView.label ).to.equal( 'Akapit' );
  134. commands.paragraph.value = false;
  135. commands.heading1.value = true;
  136. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  137. } );
  138. it( 'works for the listView#items in the panel', () => {
  139. const listView = dropdown.listView;
  140. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  141. 'Akapit',
  142. 'Nagłówek 1',
  143. 'Nagłówek 2'
  144. ] );
  145. } );
  146. it( 'allows custom titles', () => {
  147. return localizedEditor( [
  148. { model: 'paragraph', title: 'Custom paragraph title' },
  149. { model: 'heading1', view: { name: 'h1' }, title: 'Custom heading1 title' }
  150. ] ).then( () => {
  151. const listView = dropdown.listView;
  152. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  153. 'Custom paragraph title',
  154. 'Custom heading1 title',
  155. ] );
  156. } );
  157. } );
  158. it( 'translates default using the the locale', () => {
  159. return localizedEditor( [
  160. { model: 'paragraph', title: 'Paragraph' }
  161. ] ).then( () => {
  162. const listView = dropdown.listView;
  163. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  164. 'Akapit'
  165. ] );
  166. } );
  167. } );
  168. function localizedEditor( options ) {
  169. const editorElement = document.createElement( 'div' );
  170. document.body.appendChild( editorElement );
  171. return ClassicTestEditor
  172. .create( editorElement, {
  173. plugins: [ Heading ],
  174. toolbar: [ 'heading' ],
  175. language: 'pl',
  176. heading: {
  177. options
  178. }
  179. } )
  180. .then( newEditor => {
  181. editor = newEditor;
  182. dropdown = editor.ui.componentFactory.create( 'headings' );
  183. commands = {};
  184. editor.config.get( 'heading.options' ).forEach( ( { model } ) => {
  185. commands[ model ] = editor.commands.get( model );
  186. } );
  187. editorElement.remove();
  188. return editor.destroy();
  189. } );
  190. }
  191. } );
  192. describe( 'class', () => {
  193. it( 'is set for the listView#items in the panel', () => {
  194. const listView = dropdown.listView;
  195. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  196. 'ck-heading_paragraph',
  197. 'ck-heading_heading1',
  198. 'ck-heading_heading2',
  199. 'ck-heading_heading3'
  200. ] );
  201. } );
  202. it( 'reflects the #value of the commands', () => {
  203. const listView = dropdown.listView;
  204. setData( editor.model, '<heading2>f{}oo</heading2>' );
  205. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  206. false,
  207. false,
  208. true,
  209. false
  210. ] );
  211. } );
  212. } );
  213. } );
  214. } );