highlightui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. } );
  66. it( 'should add custom CSS class to dropdown and dropdown buttons', () => {
  67. dropdown.render();
  68. expect( dropdown.element.classList.contains( 'ck-highlight-dropdown' ) ).to.be.true;
  69. expect( dropdown.buttonView.element.classList.contains( 'ck-highlight-button' ) ).to.be.true;
  70. // There should be 5 highlight buttons, one separator and highlight remove button in toolbar.
  71. expect( dropdown.toolbarView.items.map( button => button.element.classList.contains( 'ck-highlight-button' ) ) )
  72. .to.deep.equal( [ true, true, true, true, true, false, false ] );
  73. } );
  74. it( 'should focus view after command execution', () => {
  75. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  76. dropdown.commandName = 'highlight';
  77. dropdown.fire( 'execute' );
  78. sinon.assert.calledOnce( focusSpy );
  79. } );
  80. it( 'should have proper icons in dropdown', () => {
  81. const toolbar = dropdown.toolbarView;
  82. // Not in a selection with highlight.
  83. command.value = undefined;
  84. expect( toolbar.items.map( item => item.icon ) )
  85. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  86. } );
  87. it( 'should activate current option in dropdown', () => {
  88. const toolbar = dropdown.toolbarView;
  89. // Not in a selection with highlight.
  90. command.value = undefined;
  91. expect( toolbar.items.map( item => item.isOn ) )
  92. .to.deep.equal( [ false, false, false, false, false, undefined, false ] );
  93. // Inside a selection with highlight.
  94. command.value = 'greenMarker';
  95. // The second item is 'greenMarker' highlighter.
  96. expect( toolbar.items.map( item => item.isOn ) ).to.deep.equal( [ false, true, false, false, false, undefined, false ] );
  97. } );
  98. describe( 'toolbar button behavior', () => {
  99. let button, buttons, options;
  100. beforeEach( () => {
  101. button = dropdown.buttonView;
  102. buttons = dropdown.toolbarView.items.map( b => b );
  103. options = editor.config.get( 'highlight.options' );
  104. } );
  105. function validateButton( which ) {
  106. expect( button.icon ).to.equal( buttons[ which ].icon );
  107. expect( button.actionView.color ).to.equal( options[ which ].color );
  108. }
  109. it( 'should have properties of first defined highlighter', () => {
  110. validateButton( 0 );
  111. } );
  112. it( 'should change button on selection', () => {
  113. command.value = 'redPen';
  114. validateButton( 3 );
  115. command.value = undefined;
  116. validateButton( 0 );
  117. } );
  118. it( 'should change button on execute option', () => {
  119. command.value = 'marker';
  120. validateButton( 0 );
  121. buttons[ 4 ].fire( 'execute' );
  122. command.value = 'bluePen';
  123. // Simulate selection moved to not highlighted text.
  124. command.value = undefined;
  125. validateButton( 4 );
  126. } );
  127. } );
  128. describe( 'model to command binding', () => {
  129. it( 'isEnabled', () => {
  130. command.isEnabled = false;
  131. expect( dropdown.buttonView.isEnabled ).to.be.false;
  132. command.isEnabled = true;
  133. expect( dropdown.buttonView.isEnabled ).to.be.true;
  134. } );
  135. } );
  136. describe( 'localization', () => {
  137. beforeEach( () => {
  138. return localizedEditor();
  139. } );
  140. it( 'works for the #buttonView', () => {
  141. const buttonView = dropdown.buttonView;
  142. expect( buttonView.tooltip ).to.equal( 'Zakreślacz' );
  143. } );
  144. it( 'works for the listView#items in the panel', () => {
  145. const listView = dropdown.toolbarView;
  146. expect( listView.items.map( item => item.label ).filter( label => !!label ) ).to.deep.equal( [
  147. 'Marker',
  148. 'Zielony marker',
  149. 'Różowy marker',
  150. 'Czerwony długopis',
  151. 'Niebieski długopis',
  152. 'Usuń zaznaczenie'
  153. ] );
  154. } );
  155. function localizedEditor() {
  156. const editorElement = document.createElement( 'div' );
  157. document.body.appendChild( editorElement );
  158. return ClassicTestEditor
  159. .create( editorElement, {
  160. plugins: [ HighlightEditing, HighlightUI ],
  161. toolbar: [ 'highlight' ],
  162. language: 'pl'
  163. } )
  164. .then( newEditor => {
  165. editor = newEditor;
  166. dropdown = editor.ui.componentFactory.create( 'highlightDropdown' );
  167. command = editor.commands.get( 'highlight' );
  168. editorElement.remove();
  169. return editor.destroy();
  170. } );
  171. }
  172. } );
  173. } );
  174. } );