highlightui.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. expect( button ).to.have.property( 'isToggleable', true );
  67. } );
  68. it( 'should have proper icons in dropdown', () => {
  69. const toolbar = dropdown.toolbarView;
  70. // Not in a selection with highlight.
  71. command.value = undefined;
  72. expect( toolbar.items.map( item => item.icon ) )
  73. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  74. } );
  75. it( 'should have proper colors in dropdown', () => {
  76. const toolbar = dropdown.toolbarView;
  77. expect( toolbar.items.map( item => item.iconView && item.iconView.fillColor ) ).to.deep.equal( [
  78. 'var(--ck-highlight-marker-yellow)',
  79. 'var(--ck-highlight-marker-green)',
  80. 'var(--ck-highlight-marker-pink)',
  81. 'var(--ck-highlight-marker-blue)',
  82. 'var(--ck-highlight-pen-red)',
  83. 'var(--ck-highlight-pen-green)',
  84. undefined,
  85. '',
  86. ] );
  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, 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 ) )
  98. .to.deep.equal( [ false, true, false, false, false, false, undefined, false ] );
  99. } );
  100. it( 'should mark as toggleable all markers and pens', () => {
  101. const toolbar = dropdown.toolbarView;
  102. expect( toolbar.items.map( item => item.isToggleable ) )
  103. .to.deep.equal( [ true, true, true, true, true, true, undefined, false ] );
  104. } );
  105. describe( 'toolbar button behavior', () => {
  106. let button, buttons, options;
  107. beforeEach( () => {
  108. button = dropdown.buttonView;
  109. buttons = dropdown.toolbarView.items.map( b => b );
  110. options = editor.config.get( 'highlight.options' );
  111. } );
  112. function validateButton( which ) {
  113. expect( button.icon ).to.equal( buttons[ which ].icon );
  114. expect( button.actionView.iconView.fillColor ).to.equal( options[ which ].color );
  115. }
  116. it( 'should have properties of first defined highlighter', () => {
  117. validateButton( 0 );
  118. } );
  119. it( 'should change button on selection', () => {
  120. command.value = 'redPen';
  121. validateButton( 4 );
  122. command.value = undefined;
  123. validateButton( 0 );
  124. } );
  125. it( 'should change button on execute option', () => {
  126. command.value = 'yellowMarker';
  127. validateButton( 0 );
  128. buttons[ 5 ].fire( 'execute' );
  129. command.value = 'greenPen';
  130. // Simulate selection moved to not highlighted text.
  131. command.value = undefined;
  132. validateButton( 5 );
  133. } );
  134. it( 'should focus view after command execution', () => {
  135. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  136. dropdown.buttonView.commandName = 'highlight';
  137. dropdown.buttonView.fire( 'execute' );
  138. sinon.assert.calledOnce( focusSpy );
  139. } );
  140. } );
  141. describe( 'model to command binding', () => {
  142. it( 'isEnabled', () => {
  143. command.isEnabled = false;
  144. expect( dropdown.buttonView.isEnabled ).to.be.false;
  145. command.isEnabled = true;
  146. expect( dropdown.buttonView.isEnabled ).to.be.true;
  147. } );
  148. } );
  149. describe( 'localization', () => {
  150. beforeEach( () => {
  151. return localizedEditor();
  152. } );
  153. it( 'works for the #buttonView', () => {
  154. const buttonView = dropdown.buttonView;
  155. expect( buttonView.tooltip ).to.equal( 'Zakreślacz' );
  156. } );
  157. it( 'works for the listView#items in the panel', () => {
  158. const listView = dropdown.toolbarView;
  159. expect( listView.items.map( item => item.label ).filter( label => !!label ) ).to.deep.equal( [
  160. 'Żółty marker',
  161. 'Zielony marker',
  162. 'Różowy marker',
  163. 'Niebieski marker',
  164. 'Czerwony długopis',
  165. 'Zielony długopis',
  166. 'Usuń zaznaczenie'
  167. ] );
  168. } );
  169. function localizedEditor() {
  170. const editorElement = document.createElement( 'div' );
  171. document.body.appendChild( editorElement );
  172. return ClassicTestEditor
  173. .create( editorElement, {
  174. plugins: [ HighlightEditing, HighlightUI ],
  175. toolbar: [ 'highlight' ],
  176. language: 'pl'
  177. } )
  178. .then( newEditor => {
  179. editor = newEditor;
  180. dropdown = editor.ui.componentFactory.create( 'highlight' );
  181. command = editor.commands.get( 'highlight' );
  182. editorElement.remove();
  183. return editor.destroy();
  184. } );
  185. }
  186. } );
  187. } );
  188. } );