heading.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  27. .create( editorElement, {
  28. plugins: [ Heading ],
  29. toolbar: [ 'heading' ]
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. dropdown = editor.ui.componentFactory.create( 'headings' );
  34. // Set data so the commands will be enabled.
  35. setData( editor.document, '<paragraph>f{}oo</paragraph>' );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return editor.destroy();
  41. } );
  42. it( 'should be loaded', () => {
  43. expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
  44. } );
  45. it( 'should load HeadingEngine', () => {
  46. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  47. } );
  48. describe( 'init()', () => {
  49. it( 'should register options feature component', () => {
  50. const dropdown = editor.ui.componentFactory.create( 'headings' );
  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. dropdown.render();
  75. expect( dropdown.element.classList.contains( 'ck-heading-dropdown' ) ).to.be.true;
  76. } );
  77. describe( 'model to command binding', () => {
  78. let commands;
  79. beforeEach( () => {
  80. commands = {};
  81. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  82. commands[ modelElement ] = editor.commands.get( modelElement );
  83. } );
  84. } );
  85. it( 'isEnabled', () => {
  86. for ( const name in commands ) {
  87. commands[ name ].isEnabled = false;
  88. }
  89. expect( dropdown.buttonView.isEnabled ).to.be.false;
  90. commands.heading2.isEnabled = true;
  91. expect( dropdown.buttonView.isEnabled ).to.be.true;
  92. } );
  93. it( 'label', () => {
  94. for ( const name in commands ) {
  95. commands[ name ].value = false;
  96. }
  97. expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
  98. commands.heading2.value = true;
  99. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  100. } );
  101. } );
  102. describe( 'localization', () => {
  103. let commands, editor, dropdown;
  104. beforeEach( () => {
  105. return localizedEditor( [
  106. { modelElement: 'paragraph', title: 'Paragraph' },
  107. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  108. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  109. ] );
  110. } );
  111. it( 'does not alter the original config', () => {
  112. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  113. { modelElement: 'paragraph', title: 'Paragraph' },
  114. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  115. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  116. ] );
  117. } );
  118. it( 'works for the #buttonView', () => {
  119. const buttonView = dropdown.buttonView;
  120. // Setting manually paragraph.value to `false` because there might be some content in editor
  121. // after initialisation (for example empty <p></p> inserted when editor is empty).
  122. commands.paragraph.value = false;
  123. expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
  124. expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
  125. commands.paragraph.value = true;
  126. expect( buttonView.label ).to.equal( 'Akapit' );
  127. commands.paragraph.value = false;
  128. commands.heading1.value = true;
  129. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  130. } );
  131. it( 'works for the listView#items in the panel', () => {
  132. const listView = dropdown.listView;
  133. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  134. 'Akapit',
  135. 'Nagłówek 1',
  136. 'Nagłówek 2'
  137. ] );
  138. } );
  139. it( 'allows custom titles', () => {
  140. return localizedEditor( [
  141. { modelElement: 'paragraph', title: 'Custom paragraph title' },
  142. { modelElement: 'heading1', title: 'Custom heading1 title' }
  143. ] ).then( () => {
  144. const listView = dropdown.listView;
  145. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  146. 'Custom paragraph title',
  147. 'Custom heading1 title',
  148. ] );
  149. } );
  150. } );
  151. it( 'translates default using the the locale', () => {
  152. return localizedEditor( [
  153. { modelElement: 'paragraph', title: 'Paragraph' }
  154. ] ).then( () => {
  155. const listView = dropdown.listView;
  156. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  157. 'Akapit'
  158. ] );
  159. } );
  160. } );
  161. function localizedEditor( options ) {
  162. const editorElement = document.createElement( 'div' );
  163. document.body.appendChild( editorElement );
  164. return ClassicTestEditor
  165. .create( editorElement, {
  166. plugins: [ Heading ],
  167. toolbar: [ 'heading' ],
  168. lang: 'pl',
  169. heading: {
  170. options
  171. }
  172. } )
  173. .then( newEditor => {
  174. editor = newEditor;
  175. dropdown = editor.ui.componentFactory.create( 'headings' );
  176. commands = {};
  177. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  178. commands[ modelElement ] = editor.commands.get( modelElement );
  179. } );
  180. editorElement.remove();
  181. return editor.destroy();
  182. } );
  183. }
  184. } );
  185. describe( 'class', () => {
  186. it( 'is set for the listView#items in the panel', () => {
  187. const listView = dropdown.listView;
  188. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  189. 'ck-heading_paragraph',
  190. 'ck-heading_heading1',
  191. 'ck-heading_heading2',
  192. 'ck-heading_heading3'
  193. ] );
  194. } );
  195. it( 'reflects the #value of the commands', () => {
  196. const listView = dropdown.listView;
  197. setData( editor.document, '<heading2>f{}oo</heading2>' );
  198. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  199. false,
  200. false,
  201. true,
  202. false
  203. ] );
  204. } );
  205. } );
  206. } );
  207. } );