8
0

headingbuttonsui.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 HeadingEditing from '../src/headingediting';
  8. import HeadingButtonsUI from '../src/headingbuttonsui';
  9. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import { getLocalizedOptions } from '../src/utils';
  12. import iconHeading2 from '../theme/icons/heading2.svg';
  13. describe( 'HeadingButtonUI', () => {
  14. let editorElement, editor;
  15. describe( 'default config', () => {
  16. beforeEach( () => {
  17. editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ HeadingButtonsUI, HeadingEditing ],
  22. toolbar: [ 'heading1', 'heading2', 'heading3', 'heading4', 'heading5', 'heading6' ],
  23. heading: {
  24. options: [
  25. { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  26. { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  27. { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  28. { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' },
  29. { model: 'heading4', view: 'h5', title: 'Heading 4', class: 'ck-heading_heading4' },
  30. { model: 'heading5', view: 'h6', title: 'Heading 5', class: 'ck-heading_heading5' },
  31. { model: 'heading6', view: 'p', title: 'Heading 6', class: 'ck-heading_heading6' }
  32. ]
  33. }
  34. } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. // Set data so the commands will be enabled.
  38. setData( editor.model, '<heading1>f{}oo</heading1>' );
  39. } );
  40. } );
  41. afterEach( () => {
  42. editorElement.remove();
  43. return editor.destroy();
  44. } );
  45. it( 'should define default buttons', () => {
  46. const factory = editor.ui.componentFactory;
  47. expect( factory.create( 'heading1' ) ).to.be.instanceOf( ButtonView );
  48. expect( factory.create( 'heading2' ) ).to.be.instanceOf( ButtonView );
  49. expect( factory.create( 'heading3' ) ).to.be.instanceOf( ButtonView );
  50. expect( factory.create( 'heading4' ) ).to.be.instanceOf( ButtonView );
  51. expect( factory.create( 'heading5' ) ).to.be.instanceOf( ButtonView );
  52. expect( factory.create( 'heading6' ) ).to.be.instanceOf( ButtonView );
  53. } );
  54. it( 'should initialize buttons with correct localized data', () => {
  55. const localizedOptions = getLocalizedOptions( editor ).filter( option => option.model == 'heading2' )[ 0 ];
  56. const heading2Button = editor.ui.componentFactory.create( 'heading2' );
  57. expect( heading2Button.label ).to.equal( localizedOptions.title );
  58. expect( heading2Button.icon ).to.equal( iconHeading2 );
  59. expect( heading2Button.tooltip ).to.equal( true );
  60. } );
  61. it( 'should bind buttons to correct commands', () => {
  62. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  63. const headingCommand = editor.commands.get( 'heading' );
  64. expect( headingCommand.isEnabled ).to.be.true;
  65. expect( headingButton.isEnabled ).to.be.true;
  66. headingCommand.isEnabled = false;
  67. expect( headingButton.isEnabled ).to.be.false;
  68. expect( headingCommand.value ).to.equal( 'heading1' );
  69. expect( headingButton.isOn ).to.be.true;
  70. setData( editor.model, '<heading2>f{}oo</heading2>' );
  71. expect( headingCommand.value ).to.equal( 'heading2' );
  72. expect( headingButton.isOn ).to.be.false;
  73. } );
  74. it( 'should bind button execute to command execute', () => {
  75. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  76. const executeCommandSpy = sinon.spy( editor, 'execute' );
  77. headingButton.fire( 'execute' );
  78. sinon.assert.calledOnce( executeCommandSpy );
  79. sinon.assert.calledWithExactly( executeCommandSpy, 'heading', { value: 'heading1' } );
  80. } );
  81. it( 'should be initialized as toggleable button', () => {
  82. const factory = editor.ui.componentFactory;
  83. expect( factory.create( 'heading1' ).isToggleable ).to.be.true;
  84. expect( factory.create( 'heading2' ).isToggleable ).to.be.true;
  85. expect( factory.create( 'heading3' ).isToggleable ).to.be.true;
  86. expect( factory.create( 'heading4' ).isToggleable ).to.be.true;
  87. expect( factory.create( 'heading5' ).isToggleable ).to.be.true;
  88. expect( factory.create( 'heading6' ).isToggleable ).to.be.true;
  89. } );
  90. } );
  91. describe( 'custom config', () => {
  92. const customIcon = '<svg></svg>';
  93. beforeEach( () => {
  94. editorElement = document.createElement( 'div' );
  95. document.body.appendChild( editorElement );
  96. return ClassicTestEditor
  97. .create( editorElement, {
  98. heading: {
  99. options: [
  100. { model: 'paragraph' },
  101. { model: 'heading1', view: 'h2', icon: customIcon }
  102. ]
  103. },
  104. plugins: [ HeadingButtonsUI, HeadingEditing ],
  105. toolbar: [ 'heading1', 'heading2', 'heading3' ]
  106. } )
  107. .then( newEditor => {
  108. editor = newEditor;
  109. // Set data so the commands will be enabled.
  110. setData( editor.model, '<heading1>f{}oo</heading1>' );
  111. } );
  112. } );
  113. afterEach( () => {
  114. editorElement.remove();
  115. return editor.destroy();
  116. } );
  117. it( 'should allow to pass custom image to the configuration', () => {
  118. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  119. expect( headingButton.icon ).to.equal( customIcon );
  120. } );
  121. } );
  122. } );