8
0

heading.js 6.6 KB

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