8
0

heading.js 7.5 KB

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