heading.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 as addTranslations, _clear as clearTranslations } 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. testUtils.createSinonSandbox();
  14. describe( 'Heading', () => {
  15. let editor, editorElement, dropdown;
  16. before( () => {
  17. addTranslations( 'en', {
  18. 'Choose heading': 'Choose heading',
  19. 'Paragraph': 'Paragraph',
  20. 'Heading': 'Heading',
  21. 'Heading 1': 'Heading 1',
  22. 'Heading 2': 'Heading 2',
  23. } );
  24. addTranslations( 'pl', {
  25. 'Choose heading': 'Wybierz nagłówek',
  26. 'Paragraph': 'Akapit',
  27. 'Heading': 'Nagłówek',
  28. 'Heading 1': 'Nagłówek 1',
  29. 'Heading 2': 'Nagłówek 2',
  30. } );
  31. } );
  32. after( () => {
  33. clearTranslations();
  34. } );
  35. beforeEach( () => {
  36. editorElement = document.createElement( 'div' );
  37. document.body.appendChild( editorElement );
  38. return ClassicTestEditor
  39. .create( editorElement, {
  40. plugins: [ Heading ],
  41. toolbar: [ 'heading' ]
  42. } )
  43. .then( newEditor => {
  44. editor = newEditor;
  45. dropdown = editor.ui.componentFactory.create( 'headings' );
  46. // Set data so the commands will be enabled.
  47. setData( editor.document, '<paragraph>f{}oo</paragraph>' );
  48. } );
  49. } );
  50. afterEach( () => {
  51. editorElement.remove();
  52. return editor.destroy();
  53. } );
  54. it( 'should be loaded', () => {
  55. expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
  56. } );
  57. it( 'should load HeadingEngine', () => {
  58. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  59. } );
  60. describe( 'init()', () => {
  61. it( 'should register options feature component', () => {
  62. const dropdown = editor.ui.componentFactory.create( 'headings' );
  63. expect( dropdown ).to.be.instanceOf( DropdownView );
  64. expect( dropdown.buttonView.isEnabled ).to.be.true;
  65. expect( dropdown.buttonView.isOn ).to.be.undefined;
  66. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  67. expect( dropdown.buttonView.tooltip ).to.equal( 'Heading' );
  68. } );
  69. it( 'should execute format command on model execute event', () => {
  70. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  71. const dropdown = editor.ui.componentFactory.create( 'headings' );
  72. dropdown.commandName = 'paragraph';
  73. dropdown.fire( 'execute' );
  74. sinon.assert.calledOnce( executeSpy );
  75. sinon.assert.calledWithExactly( executeSpy, 'paragraph' );
  76. } );
  77. it( 'should focus view after command execution', () => {
  78. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  79. const dropdown = editor.ui.componentFactory.create( 'headings' );
  80. dropdown.commandName = 'paragraph';
  81. dropdown.fire( 'execute' );
  82. sinon.assert.calledOnce( focusSpy );
  83. } );
  84. it( 'should add custom CSS class to dropdown', () => {
  85. const dropdown = editor.ui.componentFactory.create( 'headings' );
  86. dropdown.render();
  87. expect( dropdown.element.classList.contains( 'ck-heading-dropdown' ) ).to.be.true;
  88. } );
  89. describe( 'model to command binding', () => {
  90. let commands;
  91. beforeEach( () => {
  92. commands = {};
  93. editor.config.get( 'heading.options' ).forEach( ( { model } ) => {
  94. commands[ model ] = editor.commands.get( model );
  95. } );
  96. } );
  97. it( 'isEnabled', () => {
  98. for ( const name in commands ) {
  99. commands[ name ].isEnabled = false;
  100. }
  101. expect( dropdown.buttonView.isEnabled ).to.be.false;
  102. commands.heading2.isEnabled = true;
  103. expect( dropdown.buttonView.isEnabled ).to.be.true;
  104. } );
  105. it( 'label', () => {
  106. for ( const name in commands ) {
  107. commands[ name ].value = false;
  108. }
  109. expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
  110. commands.heading2.value = true;
  111. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  112. } );
  113. } );
  114. describe( 'localization', () => {
  115. let commands, editor, dropdown;
  116. beforeEach( () => {
  117. return localizedEditor( [
  118. { model: 'paragraph', title: 'Paragraph' },
  119. { model: 'heading1', view: { name: 'h2' }, title: 'Heading 1' },
  120. { model: 'heading2', view: { name: 'h3' }, title: 'Heading 2' }
  121. ] );
  122. } );
  123. it( 'does not alter the original config', () => {
  124. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  125. { model: 'paragraph', title: 'Paragraph' },
  126. { model: 'heading1', view: { name: 'h2' }, title: 'Heading 1' },
  127. { model: 'heading2', view: { name: 'h3' }, title: 'Heading 2' }
  128. ] );
  129. } );
  130. it( 'works for the #buttonView', () => {
  131. const buttonView = dropdown.buttonView;
  132. // Setting manually paragraph.value to `false` because there might be some content in editor
  133. // after initialisation (for example empty <p></p> inserted when editor is empty).
  134. commands.paragraph.value = false;
  135. expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
  136. expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
  137. commands.paragraph.value = true;
  138. expect( buttonView.label ).to.equal( 'Akapit' );
  139. commands.paragraph.value = false;
  140. commands.heading1.value = true;
  141. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  142. } );
  143. it( 'works for the listView#items in the panel', () => {
  144. const listView = dropdown.listView;
  145. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  146. 'Akapit',
  147. 'Nagłówek 1',
  148. 'Nagłówek 2'
  149. ] );
  150. } );
  151. it( 'allows custom titles', () => {
  152. return localizedEditor( [
  153. { model: 'paragraph', title: 'Custom paragraph title' },
  154. { model: 'heading1', view: { name: 'h1' }, title: 'Custom heading1 title' }
  155. ] ).then( () => {
  156. const listView = dropdown.listView;
  157. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  158. 'Custom paragraph title',
  159. 'Custom heading1 title',
  160. ] );
  161. } );
  162. } );
  163. it( 'translates default using the the locale', () => {
  164. return localizedEditor( [
  165. { model: 'paragraph', title: 'Paragraph' }
  166. ] ).then( () => {
  167. const listView = dropdown.listView;
  168. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  169. 'Akapit'
  170. ] );
  171. } );
  172. } );
  173. function localizedEditor( options ) {
  174. const editorElement = document.createElement( 'div' );
  175. document.body.appendChild( editorElement );
  176. return ClassicTestEditor
  177. .create( editorElement, {
  178. plugins: [ Heading ],
  179. toolbar: [ 'heading' ],
  180. language: 'pl',
  181. heading: {
  182. options
  183. }
  184. } )
  185. .then( newEditor => {
  186. editor = newEditor;
  187. dropdown = editor.ui.componentFactory.create( 'headings' );
  188. commands = {};
  189. editor.config.get( 'heading.options' ).forEach( ( { model } ) => {
  190. commands[ model ] = editor.commands.get( model );
  191. } );
  192. editorElement.remove();
  193. return editor.destroy();
  194. } );
  195. }
  196. } );
  197. describe( 'class', () => {
  198. it( 'is set for the listView#items in the panel', () => {
  199. const listView = dropdown.listView;
  200. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  201. 'ck-heading_paragraph',
  202. 'ck-heading_heading1',
  203. 'ck-heading_heading2',
  204. 'ck-heading_heading3'
  205. ] );
  206. } );
  207. it( 'reflects the #value of the commands', () => {
  208. const listView = dropdown.listView;
  209. setData( editor.document, '<heading2>f{}oo</heading2>' );
  210. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  211. false,
  212. false,
  213. true,
  214. false
  215. ] );
  216. } );
  217. } );
  218. } );
  219. } );