heading.js 7.2 KB

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