headingui.js 8.3 KB

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