8
0

highlightui.js 5.9 KB

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