heading.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. 'Paragraph': 'Akapit',
  14. 'Heading': 'Nagłówek',
  15. 'Heading 1': 'Nagłówek 1',
  16. 'Heading 2': 'Nagłówek 2',
  17. } );
  18. testUtils.createSinonSandbox();
  19. describe( 'Heading', () => {
  20. let editor, editorElement, dropdown;
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. document.body.appendChild( editorElement );
  24. return ClassicTestEditor.create( editorElement, {
  25. plugins: [ Heading ],
  26. toolbar: [ 'heading' ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. dropdown = editor.ui.componentFactory.create( 'headings' );
  31. } );
  32. } );
  33. afterEach( () => {
  34. editorElement.remove();
  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( '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( 'Heading' );
  88. commands.heading2.value = true;
  89. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  90. } );
  91. } );
  92. describe( 'localization', () => {
  93. let commands, editor, dropdown;
  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( 'Nagłówek' );
  111. commands.paragraph.value = true;
  112. expect( buttonView.label ).to.equal( 'Akapit' );
  113. commands.paragraph.value = false;
  114. commands.heading1.value = true;
  115. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  116. } );
  117. it( 'works for the listView#items in the panel', () => {
  118. const listView = dropdown.listView;
  119. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  120. 'Akapit',
  121. 'Nagłówek 1',
  122. 'Nagłówek 2'
  123. ] );
  124. } );
  125. it( 'allows custom titles', () => {
  126. return localizedEditor( [
  127. { modelElement: 'paragraph', title: 'Custom paragraph title' },
  128. { modelElement: 'heading1', title: 'Custom heading1 title' }
  129. ] ).then( () => {
  130. const listView = dropdown.listView;
  131. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  132. 'Custom paragraph title',
  133. 'Custom heading1 title',
  134. ] );
  135. } );
  136. } );
  137. it( 'translates default using the the locale', () => {
  138. return localizedEditor( [
  139. { modelElement: 'paragraph', title: 'Paragraph' }
  140. ] ).then( () => {
  141. const listView = dropdown.listView;
  142. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  143. 'Akapit'
  144. ] );
  145. } );
  146. } );
  147. function localizedEditor( options ) {
  148. const editorElement = document.createElement( 'div' );
  149. document.body.appendChild( editorElement );
  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. editorElement.remove();
  166. return editor.destroy();
  167. } );
  168. }
  169. } );
  170. describe( 'class', () => {
  171. it( 'is set for the listView#items in the panel', () => {
  172. const listView = dropdown.listView;
  173. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  174. 'ck-heading_paragraph',
  175. 'ck-heading_heading1',
  176. 'ck-heading_heading2',
  177. 'ck-heading_heading3'
  178. ] );
  179. } );
  180. it( 'reflects the #value of the commands', () => {
  181. const listView = dropdown.listView;
  182. editor.commands.get( 'heading2' ).value = true;
  183. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  184. false,
  185. false,
  186. true,
  187. false
  188. ] );
  189. } );
  190. } );
  191. } );
  192. } );