8
0

heading.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // Setting manually paragraph.value to `false` because there might be some content in editor
  119. // after initialisation (for example empty <p></p> inserted when editor is empty).
  120. commands.paragraph.value = false;
  121. expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
  122. expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
  123. commands.paragraph.value = true;
  124. expect( buttonView.label ).to.equal( 'Akapit' );
  125. commands.paragraph.value = false;
  126. commands.heading1.value = true;
  127. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  128. } );
  129. it( 'works for the listView#items in the panel', () => {
  130. const listView = dropdown.listView;
  131. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  132. 'Akapit',
  133. 'Nagłówek 1',
  134. 'Nagłówek 2'
  135. ] );
  136. } );
  137. it( 'allows custom titles', () => {
  138. return localizedEditor( [
  139. { modelElement: 'paragraph', title: 'Custom paragraph title' },
  140. { modelElement: 'heading1', title: 'Custom heading1 title' }
  141. ] ).then( () => {
  142. const listView = dropdown.listView;
  143. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  144. 'Custom paragraph title',
  145. 'Custom heading1 title',
  146. ] );
  147. } );
  148. } );
  149. it( 'translates default using the the locale', () => {
  150. return localizedEditor( [
  151. { modelElement: 'paragraph', title: 'Paragraph' }
  152. ] ).then( () => {
  153. const listView = dropdown.listView;
  154. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  155. 'Akapit'
  156. ] );
  157. } );
  158. } );
  159. function localizedEditor( options ) {
  160. const editorElement = document.createElement( 'div' );
  161. document.body.appendChild( editorElement );
  162. return ClassicTestEditor.create( editorElement, {
  163. plugins: [ Heading ],
  164. toolbar: [ 'heading' ],
  165. lang: 'pl',
  166. heading: {
  167. options: options
  168. }
  169. } )
  170. .then( newEditor => {
  171. editor = newEditor;
  172. dropdown = editor.ui.componentFactory.create( 'headings' );
  173. commands = {};
  174. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  175. commands[ modelElement ] = editor.commands.get( modelElement );
  176. } );
  177. editorElement.remove();
  178. return editor.destroy();
  179. } );
  180. }
  181. } );
  182. describe( 'class', () => {
  183. it( 'is set for the listView#items in the panel', () => {
  184. const listView = dropdown.listView;
  185. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  186. 'ck-heading_paragraph',
  187. 'ck-heading_heading1',
  188. 'ck-heading_heading2',
  189. 'ck-heading_heading3'
  190. ] );
  191. } );
  192. it( 'reflects the #value of the commands', () => {
  193. const listView = dropdown.listView;
  194. setData( editor.document, '<heading2>f{}oo</heading2>' );
  195. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  196. false,
  197. false,
  198. true,
  199. false
  200. ] );
  201. } );
  202. } );
  203. } );
  204. } );