highlightui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 'Yellow marker': 'Yellow 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. 'Yellow marker': 'Żółty marker',
  30. 'Green marker': 'Zielony marker',
  31. 'Pink marker': 'Różowy marker',
  32. 'Blue marker': 'Niebieski marker',
  33. 'Red pen': 'Czerwony długopis',
  34. 'Green pen': 'Zielony długopis',
  35. 'Remove highlighting': 'Usuń zaznaczenie'
  36. } );
  37. } );
  38. after( () => {
  39. clearTranslations();
  40. } );
  41. beforeEach( () => {
  42. element = document.createElement( 'div' );
  43. document.body.appendChild( element );
  44. return ClassicTestEditor
  45. .create( element, {
  46. plugins: [ HighlightEditing, HighlightUI ]
  47. } )
  48. .then( newEditor => {
  49. editor = newEditor;
  50. } );
  51. } );
  52. afterEach( () => {
  53. element.remove();
  54. return editor.destroy();
  55. } );
  56. describe( 'highlight Dropdown', () => {
  57. let dropdown;
  58. beforeEach( () => {
  59. command = editor.commands.get( 'highlight' );
  60. dropdown = editor.ui.componentFactory.create( 'highlightDropdown' );
  61. } );
  62. it( 'button has the base properties', () => {
  63. const button = dropdown.buttonView;
  64. expect( button ).to.have.property( 'tooltip', 'Highlight' );
  65. expect( button ).to.have.property( 'icon', markerIcon );
  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, true, false, false ] );
  74. } );
  75. it( 'should have proper icons in dropdown', () => {
  76. const toolbar = dropdown.toolbarView;
  77. // Not in a selection with highlight.
  78. command.value = undefined;
  79. expect( toolbar.items.map( item => item.icon ) )
  80. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  81. } );
  82. it( 'should activate current option in dropdown', () => {
  83. const toolbar = dropdown.toolbarView;
  84. // Not in a selection with highlight.
  85. command.value = undefined;
  86. expect( toolbar.items.map( item => item.isOn ) )
  87. .to.deep.equal( [ false, false, false, false, false, false, undefined, false ] );
  88. // Inside a selection with highlight.
  89. command.value = 'greenMarker';
  90. // The second item is 'greenMarker' highlighter.
  91. expect( toolbar.items.map( item => item.isOn ) ).to.deep.equal( [ false, true, false, false, false, false, undefined, false ] );
  92. } );
  93. describe( 'toolbar button behavior', () => {
  94. let button, buttons, options;
  95. beforeEach( () => {
  96. button = dropdown.buttonView;
  97. buttons = dropdown.toolbarView.items.map( b => b );
  98. options = editor.config.get( 'highlight.options' );
  99. } );
  100. function validateButton( which ) {
  101. expect( button.icon ).to.equal( buttons[ which ].icon );
  102. expect( button.actionView.color ).to.equal( options[ which ].color );
  103. }
  104. it( 'should have properties of first defined highlighter', () => {
  105. validateButton( 0 );
  106. } );
  107. it( 'should change button on selection', () => {
  108. command.value = 'redPen';
  109. validateButton( 4 );
  110. command.value = undefined;
  111. validateButton( 0 );
  112. } );
  113. it( 'should change button on execute option', () => {
  114. command.value = 'yellowMarker';
  115. validateButton( 0 );
  116. buttons[ 5 ].fire( 'execute' );
  117. command.value = 'greenPen';
  118. // Simulate selection moved to not highlighted text.
  119. command.value = undefined;
  120. validateButton( 5 );
  121. } );
  122. it( 'should focus view after command execution', () => {
  123. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  124. dropdown.buttonView.commandName = 'highlight';
  125. dropdown.buttonView.fire( 'execute' );
  126. sinon.assert.calledOnce( focusSpy );
  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. 'Żółty marker',
  149. 'Zielony marker',
  150. 'Różowy marker',
  151. 'Niebieski marker',
  152. 'Czerwony długopis',
  153. 'Zielony długopis',
  154. 'Usuń zaznaczenie'
  155. ] );
  156. } );
  157. function localizedEditor() {
  158. const editorElement = document.createElement( 'div' );
  159. document.body.appendChild( editorElement );
  160. return ClassicTestEditor
  161. .create( editorElement, {
  162. plugins: [ HighlightEditing, HighlightUI ],
  163. toolbar: [ 'highlight' ],
  164. language: 'pl'
  165. } )
  166. .then( newEditor => {
  167. editor = newEditor;
  168. dropdown = editor.ui.componentFactory.create( 'highlightDropdown' );
  169. command = editor.commands.get( 'highlight' );
  170. editorElement.remove();
  171. return editor.destroy();
  172. } );
  173. }
  174. } );
  175. } );
  176. } );