highlightui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 '@ckeditor/ckeditor5-core/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. describe( 'HighlightUI', () => {
  15. let editor, command, element;
  16. testUtils.createSinonSandbox();
  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 highlight': 'Remove highlight'
  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 highlight': '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( 'highlight' );
  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 have proper icons in dropdown', () => {
  68. const toolbar = dropdown.toolbarView;
  69. // Not in a selection with highlight.
  70. command.value = undefined;
  71. expect( toolbar.items.map( item => item.icon ) )
  72. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  73. } );
  74. it( 'should have proper colors in dropdown', () => {
  75. const toolbar = dropdown.toolbarView;
  76. expect( toolbar.items.map( item => item.iconView && item.iconView.fillColor ) ).to.deep.equal( [
  77. 'var(--ck-highlight-marker-yellow)',
  78. 'var(--ck-highlight-marker-green)',
  79. 'var(--ck-highlight-marker-pink)',
  80. 'var(--ck-highlight-marker-blue)',
  81. 'var(--ck-highlight-pen-red)',
  82. 'var(--ck-highlight-pen-green)',
  83. undefined,
  84. '',
  85. ] );
  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, 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 ) )
  97. .to.deep.equal( [ false, true, false, 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.iconView.fillColor ).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( 4 );
  116. command.value = undefined;
  117. validateButton( 0 );
  118. } );
  119. it( 'should change button on execute option', () => {
  120. command.value = 'yellowMarker';
  121. validateButton( 0 );
  122. buttons[ 5 ].fire( 'execute' );
  123. command.value = 'greenPen';
  124. // Simulate selection moved to not highlighted text.
  125. command.value = undefined;
  126. validateButton( 5 );
  127. } );
  128. it( 'should focus view after command execution', () => {
  129. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  130. dropdown.buttonView.commandName = 'highlight';
  131. dropdown.buttonView.fire( 'execute' );
  132. sinon.assert.calledOnce( focusSpy );
  133. } );
  134. } );
  135. describe( 'model to command binding', () => {
  136. it( 'isEnabled', () => {
  137. command.isEnabled = false;
  138. expect( dropdown.buttonView.isEnabled ).to.be.false;
  139. command.isEnabled = true;
  140. expect( dropdown.buttonView.isEnabled ).to.be.true;
  141. } );
  142. } );
  143. describe( 'localization', () => {
  144. beforeEach( () => {
  145. return localizedEditor();
  146. } );
  147. it( 'works for the #buttonView', () => {
  148. const buttonView = dropdown.buttonView;
  149. expect( buttonView.tooltip ).to.equal( 'Zakreślacz' );
  150. } );
  151. it( 'works for the listView#items in the panel', () => {
  152. const listView = dropdown.toolbarView;
  153. expect( listView.items.map( item => item.label ).filter( label => !!label ) ).to.deep.equal( [
  154. 'Żółty marker',
  155. 'Zielony marker',
  156. 'Różowy marker',
  157. 'Niebieski marker',
  158. 'Czerwony długopis',
  159. 'Zielony długopis',
  160. 'Usuń zaznaczenie'
  161. ] );
  162. } );
  163. function localizedEditor() {
  164. const editorElement = document.createElement( 'div' );
  165. document.body.appendChild( editorElement );
  166. return ClassicTestEditor
  167. .create( editorElement, {
  168. plugins: [ HighlightEditing, HighlightUI ],
  169. toolbar: [ 'highlight' ],
  170. language: 'pl'
  171. } )
  172. .then( newEditor => {
  173. editor = newEditor;
  174. dropdown = editor.ui.componentFactory.create( 'highlight' );
  175. command = editor.commands.get( 'highlight' );
  176. editorElement.remove();
  177. return editor.destroy();
  178. } );
  179. }
  180. } );
  181. } );
  182. } );