highlightui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import HighlightEditing from '../src/highlightediting';
  7. import HighlightUI from '../src/highlightui';
  8. import markerIcon from '../theme/icons/marker.svg';
  9. import penIcon from '../theme/icons/pen.svg';
  10. import eraserIcon from '../theme/icons/eraser.svg';
  11. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  14. testUtils.createSinonSandbox();
  15. describe( 'HighlightUI', () => {
  16. let editor, command, element;
  17. before( () => {
  18. addTranslations( 'en', {
  19. 'Highlight': 'Highlight',
  20. 'Marker': 'Marker',
  21. 'Green marker': 'Green marker',
  22. 'Pink marker': 'Pink marker',
  23. 'Red pen': 'Red pen',
  24. 'Blue pen': 'Blue pen',
  25. 'Remove highlighting': 'Remove highlighting'
  26. } );
  27. addTranslations( 'pl', {
  28. 'Highlight': 'Zakreślacz',
  29. 'Marker': 'Marker',
  30. 'Green marker': 'Zielony marker',
  31. 'Pink marker': 'Różowy marker',
  32. 'Red pen': 'Czerwony długopis',
  33. 'Blue pen': 'Niebieski długopis',
  34. 'Remove highlighting': 'Usuń zaznaczenie'
  35. } );
  36. } );
  37. after( () => {
  38. clearTranslations();
  39. } );
  40. beforeEach( () => {
  41. element = document.createElement( 'div' );
  42. document.body.appendChild( element );
  43. return ClassicTestEditor
  44. .create( element, {
  45. plugins: [ HighlightEditing, HighlightUI ]
  46. } )
  47. .then( newEditor => {
  48. editor = newEditor;
  49. } );
  50. } );
  51. afterEach( () => {
  52. element.remove();
  53. return editor.destroy();
  54. } );
  55. describe( 'highlight Dropdown', () => {
  56. let dropdown;
  57. beforeEach( () => {
  58. command = editor.commands.get( 'highlight' );
  59. dropdown = editor.ui.componentFactory.create( 'highlightDropdown' );
  60. } );
  61. it( 'button has the base properties', () => {
  62. const button = dropdown.buttonView;
  63. expect( button ).to.have.property( 'tooltip', 'Highlight' );
  64. expect( button ).to.have.property( 'icon', markerIcon );
  65. expect( button ).to.have.property( 'withText', false );
  66. } );
  67. it( 'should add custom CSS class to dropdown and dropdown buttons', () => {
  68. dropdown.render();
  69. expect( dropdown.element.classList.contains( 'ck-highlight-dropdown' ) ).to.be.true;
  70. expect( dropdown.buttonView.element.classList.contains( 'ck-highlight-button' ) ).to.be.true;
  71. // There should be 5 highlight buttons, one separator and highlight remove button in toolbar.
  72. expect( dropdown.toolbarView.items.map( button => button.element.classList.contains( 'ck-highlight-button' ) ) )
  73. .to.deep.equal( [ true, true, true, true, true, false, false ] );
  74. } );
  75. it( 'should focus view after command execution', () => {
  76. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  77. dropdown.commandName = 'highlight';
  78. dropdown.fire( 'execute' );
  79. sinon.assert.calledOnce( focusSpy );
  80. } );
  81. it( 'should have proper icons in dropdown', () => {
  82. const toolbar = dropdown.toolbarView;
  83. // Not in a selection with highlight.
  84. command.value = undefined;
  85. expect( toolbar.items.map( item => item.icon ) )
  86. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  87. } );
  88. it( 'should activate current option in dropdown', () => {
  89. const toolbar = dropdown.toolbarView;
  90. // Not in a selection with highlight.
  91. command.value = undefined;
  92. expect( toolbar.items.map( item => item.isOn ) )
  93. .to.deep.equal( [ false, false, false, false, false, undefined, false ] );
  94. // Inside a selection with highlight.
  95. command.value = 'greenMarker';
  96. // The second item is 'greenMarker' highlighter.
  97. expect( toolbar.items.map( item => item.isOn ) ).to.deep.equal( [ false, true, false, false, false, undefined, false ] );
  98. } );
  99. describe( 'toolbar button behavior', () => {
  100. let button, buttons, options;
  101. beforeEach( () => {
  102. button = dropdown.buttonView;
  103. buttons = dropdown.toolbarView.items.map( b => b );
  104. options = editor.config.get( 'highlight.options' );
  105. } );
  106. function validateButton( which ) {
  107. expect( button.icon ).to.equal( buttons[ which ].icon );
  108. expect( button.actionView.color ).to.equal( options[ which ].color );
  109. }
  110. it( 'should have properties of first defined highlighter', () => {
  111. validateButton( 0 );
  112. } );
  113. it( 'should change button on selection', () => {
  114. command.value = 'redPen';
  115. validateButton( 3 );
  116. command.value = undefined;
  117. validateButton( 0 );
  118. } );
  119. it( 'should change button on execute option', () => {
  120. command.value = 'marker';
  121. validateButton( 0 );
  122. buttons[ 4 ].fire( 'execute' );
  123. command.value = 'bluePen';
  124. // Simulate selection moved to not highlighted text.
  125. command.value = undefined;
  126. validateButton( 4 );
  127. } );
  128. } );
  129. describe( 'model to command binding', () => {
  130. it( 'isEnabled', () => {
  131. command.isEnabled = false;
  132. expect( dropdown.buttonView.isEnabled ).to.be.false;
  133. command.isEnabled = true;
  134. expect( dropdown.buttonView.isEnabled ).to.be.true;
  135. } );
  136. } );
  137. describe( 'localization', () => {
  138. beforeEach( () => {
  139. return localizedEditor();
  140. } );
  141. it( 'works for the #buttonView', () => {
  142. const buttonView = dropdown.buttonView;
  143. expect( buttonView.tooltip ).to.equal( 'Zakreślacz' );
  144. } );
  145. it( 'works for the listView#items in the panel', () => {
  146. const listView = dropdown.toolbarView;
  147. expect( listView.items.map( item => item.label ).filter( label => !!label ) ).to.deep.equal( [
  148. 'Marker',
  149. 'Zielony marker',
  150. 'Różowy marker',
  151. 'Czerwony długopis',
  152. 'Niebieski długopis',
  153. 'Usuń zaznaczenie'
  154. ] );
  155. } );
  156. function localizedEditor() {
  157. const editorElement = document.createElement( 'div' );
  158. document.body.appendChild( editorElement );
  159. return ClassicTestEditor
  160. .create( editorElement, {
  161. plugins: [ HighlightEditing, HighlightUI ],
  162. toolbar: [ 'highlight' ],
  163. language: 'pl'
  164. } )
  165. .then( newEditor => {
  166. editor = newEditor;
  167. dropdown = editor.ui.componentFactory.create( 'highlightDropdown' );
  168. command = editor.commands.get( 'highlight' );
  169. editorElement.remove();
  170. return editor.destroy();
  171. } );
  172. }
  173. } );
  174. } );
  175. } );