heading.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 HeadingEngine from '../src/headingengine';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import { add } from '@ckeditor/ckeditor5-utils/src/translation-service';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. add( 'pl', {
  13. 'Choose heading': 'Wybierz nagłówek',
  14. 'Paragraph': 'Akapit',
  15. 'Heading': 'Nagłówek',
  16. 'Heading 1': 'Nagłówek 1',
  17. 'Heading 2': 'Nagłówek 2',
  18. } );
  19. testUtils.createSinonSandbox();
  20. describe( 'Heading', () => {
  21. let editor, dropdown;
  22. beforeEach( () => {
  23. const editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. return ClassicTestEditor.create( editorElement, {
  26. plugins: [ Heading ],
  27. toolbar: [ 'heading' ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. dropdown = editor.ui.componentFactory.create( 'headings' );
  32. } );
  33. } );
  34. afterEach( () => {
  35. return editor.destroy();
  36. } );
  37. it( 'should be loaded', () => {
  38. expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
  39. } );
  40. it( 'should load HeadingEngine', () => {
  41. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  42. } );
  43. describe( 'init()', () => {
  44. it( 'should register options feature component', () => {
  45. const dropdown = editor.ui.componentFactory.create( 'headings' );
  46. expect( dropdown ).to.be.instanceOf( DropdownView );
  47. expect( dropdown.buttonView.isEnabled ).to.be.true;
  48. expect( dropdown.buttonView.isOn ).to.be.undefined;
  49. expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
  50. expect( dropdown.buttonView.tooltip ).to.equal( 'Heading' );
  51. } );
  52. it( 'should execute format command on model execute event', () => {
  53. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  54. const dropdown = editor.ui.componentFactory.create( 'headings' );
  55. dropdown.commandName = 'paragraph';
  56. dropdown.fire( 'execute' );
  57. sinon.assert.calledOnce( executeSpy );
  58. sinon.assert.calledWithExactly( executeSpy, 'paragraph' );
  59. } );
  60. it( 'should focus view after command execution', () => {
  61. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  62. const dropdown = editor.ui.componentFactory.create( 'headings' );
  63. dropdown.commandName = 'paragraph';
  64. dropdown.fire( 'execute' );
  65. sinon.assert.calledOnce( focusSpy );
  66. } );
  67. describe( 'model to command binding', () => {
  68. let commands;
  69. beforeEach( () => {
  70. commands = {};
  71. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  72. commands[ modelElement ] = editor.commands.get( modelElement );
  73. } );
  74. } );
  75. it( 'isEnabled', () => {
  76. for ( let name in commands ) {
  77. commands[ name ].isEnabled = false;
  78. }
  79. expect( dropdown.buttonView.isEnabled ).to.be.false;
  80. commands.heading2.isEnabled = true;
  81. expect( dropdown.buttonView.isEnabled ).to.be.true;
  82. } );
  83. it( 'label', () => {
  84. for ( let name in commands ) {
  85. commands[ name ].value = false;
  86. }
  87. expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
  88. commands.heading2.value = true;
  89. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  90. } );
  91. } );
  92. describe( 'localization', () => {
  93. let commands;
  94. beforeEach( () => {
  95. return localizedEditor( [
  96. { modelElement: 'paragraph', title: 'Paragraph' },
  97. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  98. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  99. ] );
  100. } );
  101. it( 'does not alter the original config', () => {
  102. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  103. { modelElement: 'paragraph', title: 'Paragraph' },
  104. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  105. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  106. ] );
  107. } );
  108. it( 'works for the #buttonView', () => {
  109. const buttonView = dropdown.buttonView;
  110. expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
  111. expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
  112. commands.paragraph.value = true;
  113. expect( buttonView.label ).to.equal( 'Akapit' );
  114. commands.paragraph.value = false;
  115. commands.heading1.value = true;
  116. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  117. } );
  118. it( 'works for the listView#items in the panel', () => {
  119. const listView = dropdown.listView;
  120. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  121. 'Akapit',
  122. 'Nagłówek 1',
  123. 'Nagłówek 2'
  124. ] );
  125. } );
  126. it( 'allows custom titles', () => {
  127. return localizedEditor( [
  128. { modelElement: 'paragraph', title: 'Custom paragraph title' },
  129. { modelElement: 'heading1', title: 'Custom heading1 title' }
  130. ] ).then( () => {
  131. const listView = dropdown.listView;
  132. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  133. 'Custom paragraph title',
  134. 'Custom heading1 title',
  135. ] );
  136. } );
  137. } );
  138. it( 'translates default using the the locale', () => {
  139. return localizedEditor( [
  140. { modelElement: 'paragraph', title: 'Paragraph' }
  141. ] ).then( () => {
  142. const listView = dropdown.listView;
  143. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  144. 'Akapit'
  145. ] );
  146. } );
  147. } );
  148. function localizedEditor( options ) {
  149. const editorElement = document.createElement( 'div' );
  150. return ClassicTestEditor.create( editorElement, {
  151. plugins: [ Heading ],
  152. toolbar: [ 'heading' ],
  153. lang: 'pl',
  154. heading: {
  155. options: options
  156. }
  157. } )
  158. .then( newEditor => {
  159. editor = newEditor;
  160. dropdown = editor.ui.componentFactory.create( 'headings' );
  161. commands = {};
  162. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  163. commands[ modelElement ] = editor.commands.get( modelElement );
  164. } );
  165. } );
  166. }
  167. } );
  168. describe( 'class', () => {
  169. it( 'is set for the listView#items in the panel', () => {
  170. const listView = dropdown.listView;
  171. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  172. 'ck-heading_paragraph',
  173. 'ck-heading_heading1',
  174. 'ck-heading_heading2',
  175. 'ck-heading_heading3'
  176. ] );
  177. } );
  178. it( 'reflects the #value of the commands', () => {
  179. const listView = dropdown.listView;
  180. editor.commands.get( 'heading2' ).value = true;
  181. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  182. false,
  183. false,
  184. true,
  185. false
  186. ] );
  187. } );
  188. } );
  189. } );
  190. } );