heading.js 6.8 KB

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