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